Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #include "someip/TcpConfig.h"
12 :
13 : #include "someip/logger.h"
14 :
15 : // Logger API uses printf-style varargs for fixed diagnostic messages in this module.
16 : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
17 :
18 : namespace someip
19 : {
20 : using ::ip::IPEndpoint;
21 : using ::util::logger::SOMEIP;
22 :
23 35 : TcpServerConfig::TcpServerConfig(
24 : ::etl::span<TcpServer> servers,
25 : ::etl::span<uint16_t> const ports,
26 35 : ::etl::span<ClientBuffers> const clientBuffers)
27 35 : : _servers(servers), _ports(ports), _clientBuffers(clientBuffers)
28 35 : {}
29 :
30 6 : size_t TcpServerConfig::getSize() const { return _servers.size(); }
31 :
32 13 : TcpServer* TcpServerConfig::getServer(size_t const pos) const { return &_servers[pos]; }
33 :
34 3 : TcpServer* TcpServerConfig::nextServer() const
35 : {
36 4 : for (auto& server : _servers)
37 : {
38 3 : if (!server.isOpen())
39 : {
40 2 : return &server;
41 : }
42 : }
43 :
44 1 : return nullptr;
45 : }
46 :
47 3 : TcpServer* TcpServerConfig::getOpenServer(uint16_t const localPort) const
48 : {
49 5 : for (auto& server : _servers)
50 : {
51 3 : auto const portResult = server.getLocalPort();
52 3 : if (server.isOpen() && portResult.has_value() && (portResult.value() == localPort))
53 : {
54 1 : return &server;
55 : }
56 : }
57 :
58 2 : return nullptr;
59 : }
60 :
61 4 : bool TcpServerConfig::initPort(uint16_t const port) const
62 : {
63 5 : for (uint16_t& _port : _ports)
64 : {
65 4 : if ((_port == port) || (_port == port::INVALID))
66 : {
67 3 : _port = port;
68 3 : return true;
69 : }
70 : }
71 1 : return false;
72 : }
73 :
74 4 : bool TcpServerConfig::setBufferPool(uint16_t const port, ::etl::ivector<::etl::span<uint8_t>>& pool)
75 : {
76 5 : for (ClientBuffers& clientBuffers : _clientBuffers)
77 : {
78 3 : if ((clientBuffers.serverPort == port) || (clientBuffers.serverPort == port::INVALID))
79 : {
80 2 : clientBuffers.serverPort = port;
81 2 : clientBuffers.clientBufferPool = &pool;
82 2 : return true;
83 : }
84 : }
85 :
86 2 : return false;
87 : }
88 :
89 3 : TcpServer::ClientBufferPool* TcpServerConfig::getBufferPool(uint16_t const port) const
90 : {
91 4 : for (ClientBuffers& clientBuffers : _clientBuffers)
92 : {
93 3 : if (clientBuffers.serverPort == port)
94 : {
95 2 : return clientBuffers.clientBufferPool;
96 : }
97 : }
98 :
99 1 : return nullptr;
100 : }
101 :
102 8 : ::etl::expected<uint16_t, PortError> TcpServerConfig::getPort(size_t const pos) const
103 : {
104 8 : if (pos < _ports.size())
105 : {
106 8 : uint16_t const port = _ports.at(pos);
107 8 : if (port == port::INVALID)
108 : {
109 8 : return ::etl::unexpected<PortError>(PortError::NOT_INITIALIZED);
110 : }
111 4 : return port;
112 : }
113 0 : return ::etl::unexpected<PortError>(PortError::OUT_OF_RANGE);
114 : }
115 :
116 2 : void TcpServerConfig::close()
117 : {
118 4 : for (size_t i = 0U; i < _servers.size(); ++i)
119 : {
120 2 : TcpServer& server = _servers.at(i);
121 2 : if (server.isOpen())
122 : {
123 0 : INFO_LOG(SOMEIP, "Network: close tcp-server[%d]", i);
124 0 : server.close();
125 : }
126 : }
127 2 : }
128 :
129 60 : TcpProxyConfig::TcpProxyConfig(::etl::span<TcpProxy> proxies, BufferType const bufferType)
130 60 : : _proxies(proxies), _bufferType(bufferType)
131 60 : {}
132 :
133 7 : void TcpProxyConfig::setListener(INetworkListener& listener) const
134 : {
135 14 : for (auto& proxy : _proxies)
136 : {
137 7 : proxy.setListener(listener);
138 : }
139 7 : }
140 :
141 50 : size_t TcpProxyConfig::getSize() const { return _proxies.size(); }
142 :
143 35 : TcpProxy* TcpProxyConfig::getProxy(size_t const pos) const { return &_proxies[pos]; }
144 :
145 5 : TcpProxy* TcpProxyConfig::getProxy(::tcp::AbstractSocket const& socket) const
146 : {
147 9 : for (TcpProxy& tcpProxy : _proxies)
148 : {
149 7 : if (&(tcpProxy.getSocket()) == (&socket))
150 : {
151 3 : return &tcpProxy;
152 : }
153 : }
154 :
155 2 : return nullptr;
156 : }
157 :
158 14 : TcpProxy* TcpProxyConfig::nextProxy() const
159 : {
160 25 : for (auto& proxy : _proxies)
161 : {
162 20 : if (proxy.isIdle())
163 : {
164 9 : return &proxy;
165 : }
166 : }
167 :
168 5 : return nullptr;
169 : }
170 :
171 : TcpProxy*
172 0 : TcpProxyConfig::getOpenProxy(::ip::IPAddress const& remoteIp, uint16_t const localPort) const
173 : {
174 0 : for (auto& proxy : _proxies)
175 : {
176 0 : auto const portResult = proxy.getLocalPort();
177 0 : if (proxy.isInitialized() && (proxy.isOpen()) && portResult.has_value()
178 0 : && (portResult.value() == localPort)
179 0 : && (proxy.getRemoteEndpoint().getAddress() == remoteIp))
180 : {
181 0 : return &proxy;
182 : }
183 : }
184 :
185 0 : return nullptr;
186 : }
187 :
188 : TcpProxy*
189 17 : TcpProxyConfig::getOpenProxy(uint16_t const localPort, IPEndpoint const& remoteEndpoint) const
190 : {
191 43 : for (auto& proxy : _proxies)
192 : {
193 29 : auto const portResult = proxy.getLocalPort();
194 44 : if (proxy.isOpen() && portResult.has_value() && (portResult.value() == localPort)
195 44 : && (proxy.getRemoteEndpoint() == remoteEndpoint))
196 : {
197 3 : return &proxy;
198 : }
199 : }
200 :
201 14 : return nullptr;
202 : }
203 :
204 1 : void TcpProxyConfig::close()
205 : {
206 2 : for (size_t i = 0U; i < _proxies.size(); ++i)
207 : {
208 1 : TcpProxy& proxy = _proxies[i];
209 1 : if (proxy.isOpen())
210 : {
211 1 : INFO_LOG(SOMEIP, "Network: close tcp-proxy[%d]", i);
212 1 : proxy.close();
213 : }
214 : }
215 1 : }
216 :
217 35 : TcpConfig::TcpConfig(TcpServerConfig& tcpServers, TcpProxyConfig& tcpProxies)
218 35 : : _tcpServers(tcpServers), _tcpProxies(tcpProxies)
219 35 : {}
220 :
221 : } // namespace someip
222 :
223 : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
|