• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMUNICATIONNETSTACK_SOCKET_EXEC_H
17 #define COMMUNICATIONNETSTACK_SOCKET_EXEC_H
18 
19 #include "bind_context.h"
20 #include "common_context.h"
21 #include "connect_context.h"
22 #include "multicast_get_loopback_context.h"
23 #include "multicast_get_ttl_context.h"
24 #include "multicast_membership_context.h"
25 #include "multicast_set_loopback_context.h"
26 #include "multicast_set_ttl_context.h"
27 #include "safe_map.h"
28 #include "tcp_extra_context.h"
29 #include "tcp_send_context.h"
30 #include "tcp_server_common_context.h"
31 #include "tcp_server_extra_context.h"
32 #include "tcp_server_listen_context.h"
33 #include "tcp_server_send_context.h"
34 #include "udp_extra_context.h"
35 #include "udp_send_context.h"
36 #include "napi_utils.h"
37 
38 #include <set>
39 
40 namespace OHOS::NetStack::Socket::SocketExec {
41 void NotifyRegisterEvent();
42 
43 /* async work execute */
44 bool ExecUdpBind(BindContext *context);
45 
46 bool ExecUdpSend(UdpSendContext *context);
47 
48 bool ExecUdpAddMembership(MulticastMembershipContext *context);
49 
50 bool ExecUdpDropMembership(MulticastMembershipContext *context);
51 
52 bool ExecSetMulticastTTL(MulticastSetTTLContext *context);
53 
54 bool ExecGetMulticastTTL(MulticastGetTTLContext *context);
55 
56 bool ExecSetLoopbackMode(MulticastSetLoopbackContext *context);
57 
58 bool ExecGetLoopbackMode(MulticastGetLoopbackContext *context);
59 
60 bool ExecTcpBind(BindContext *context);
61 
62 bool ExecConnect(ConnectContext *context);
63 
64 bool ExecTcpSend(TcpSendContext *context);
65 
66 bool ExecClose(CloseContext *context);
67 
68 bool ExecGetState(GetStateContext *context);
69 
70 bool ExecGetRemoteAddress(GetRemoteAddressContext *context);
71 
72 bool ExecGetLocalAddress(GetLocalAddressContext *context);
73 
74 bool ExecTcpSetExtraOptions(TcpSetExtraOptionsContext *context);
75 
76 bool ExecUdpSetExtraOptions(UdpSetExtraOptionsContext *context);
77 
78 bool ExecTcpGetSocketFd(GetSocketFdContext *context);
79 
80 bool ExecUdpGetSocketFd(GetSocketFdContext *context);
81 
82 bool ExecTcpConnectionSend(TcpServerSendContext *context);
83 
84 bool ExecTcpConnectionGetRemoteAddress(TcpServerGetRemoteAddressContext *context);
85 
86 bool ExecTcpConnectionGetLocalAddress(TcpServerGetLocalAddressContext *context);
87 
88 bool ExecTcpConnectionClose(TcpServerCloseContext *context);
89 
90 bool ExecTcpServerListen(TcpServerListenContext *context);
91 
92 bool ExecTcpServerClose(TcpServerCloseContext *context);
93 
94 bool ExecTcpServerSetExtraOptions(TcpServerSetExtraOptionsContext *context);
95 
96 bool ExecTcpServerGetState(TcpServerGetStateContext *context);
97 
98 bool ExecTcpServerGetLocalAddress(TcpServerGetLocalAddressContext *context);
99 
100 /* async work callback */
101 napi_value BindCallback(BindContext *context);
102 
103 napi_value UdpSendCallback(UdpSendContext *context);
104 
105 napi_value UdpAddMembershipCallback(MulticastMembershipContext *context);
106 
107 napi_value UdpDropMembershipCallback(MulticastMembershipContext *context);
108 
109 napi_value UdpSetMulticastTTLCallback(MulticastSetTTLContext *context);
110 
111 napi_value UdpGetMulticastTTLCallback(MulticastGetTTLContext *context);
112 
113 napi_value UdpSetLoopbackModeCallback(MulticastSetLoopbackContext *context);
114 
115 napi_value UdpGetLoopbackModeCallback(MulticastGetLoopbackContext *context);
116 
117 napi_value ConnectCallback(ConnectContext *context);
118 
119 napi_value TcpSendCallback(TcpSendContext *context);
120 
121 napi_value CloseCallback(CloseContext *context);
122 
123 napi_value GetStateCallback(GetStateContext *context);
124 
125 napi_value GetRemoteAddressCallback(GetRemoteAddressContext *context);
126 
127 napi_value GetLocalAddressCallback(GetLocalAddressContext *context);
128 
129 napi_value TcpSetExtraOptionsCallback(TcpSetExtraOptionsContext *context);
130 
131 napi_value UdpSetExtraOptionsCallback(UdpSetExtraOptionsContext *context);
132 
133 napi_value TcpGetSocketFdCallback(GetSocketFdContext *context);
134 
135 napi_value TcpConnectionSendCallback(TcpServerSendContext *context);
136 
137 napi_value TcpConnectionCloseCallback(TcpServerCloseContext *context);
138 
139 napi_value TcpConnectionGetRemoteAddressCallback(TcpServerGetRemoteAddressContext *context);
140 
141 napi_value TcpConnectionGetLocalAddressCallback(TcpServerGetLocalAddressContext *context);
142 
143 napi_value ListenCallback(TcpServerListenContext *context);
144 
145 napi_value TcpServerCloseCallback(TcpServerCloseContext *context);
146 
147 napi_value TcpServerSetExtraOptionsCallback(TcpServerSetExtraOptionsContext *context);
148 
149 napi_value TcpServerGetStateCallback(TcpServerGetStateContext *context);
150 
151 napi_value TcpServerGetLocalAddressCallback(TcpServerGetLocalAddressContext *context);
152 
153 napi_value UdpGetSocketFdCallback(GetSocketFdContext *context);
154 
155 struct MessageData {
156     MessageData() = delete;
MessageDataMessageData157     MessageData(void *d, size_t l, const SocketRemoteInfo &info) : data(d), len(l), remoteInfo(info) {}
~MessageDataMessageData158     ~MessageData()
159     {
160         if (data) {
161             free(data);
162         }
163     }
164 
165     void *data;
166     size_t len;
167     SocketRemoteInfo remoteInfo;
168 };
169 
170 struct TcpConnection {
171     TcpConnection() = delete;
TcpConnectionTcpConnection172     explicit TcpConnection(int32_t clientid) : clientId(clientid) {}
173     ~TcpConnection() = default;
174 
175     int32_t clientId;
176 };
177 
178 class SingletonSocketConfig {
179 public:
GetInstance()180     static SingletonSocketConfig& GetInstance()
181     {
182         static SingletonSocketConfig instance;
183         return instance;
184     }
185 
SetTcpExtraOptions(int listenFd,const TCPExtraOptions & option)186     void SetTcpExtraOptions(int listenFd, const TCPExtraOptions& option)
187     {
188         tcpExtraOptions_.EnsureInsert(listenFd, option);
189     }
190 
GetTcpExtraOptions(int listenFd,TCPExtraOptions & option)191     bool GetTcpExtraOptions(int listenFd, TCPExtraOptions& option)
192     {
193         return tcpExtraOptions_.Find(listenFd, option);
194     }
195 
AddNewListenSocket(int listenFd)196     void AddNewListenSocket(int listenFd)
197     {
198         tcpClients_.Insert(listenFd, {});
199     }
200 
AddNewAcceptSocket(int listenFd,int acceptFd)201     void AddNewAcceptSocket(int listenFd, int acceptFd)
202     {
203         std::set<int> fdSet;
204         auto fn = [&](std::set<int> &value) -> void {
205             value.emplace(acceptFd);
206         };
207         if (tcpClients_.Find(listenFd, fdSet)) {
208             tcpClients_.ChangeValueByLambda(listenFd, fn);
209         }
210     }
211 
RemoveAcceptSocket(int acceptFd)212     void RemoveAcceptSocket(int acceptFd)
213     {
214         tcpClients_.Iterate([acceptFd](int listenFd, std::set<int> fdSet) {
215             if (auto ite = fdSet.find(acceptFd); ite != fdSet.end()) {
216                 fdSet.erase(ite);
217             }
218         });
219     }
220 
GetClients(int listenFd)221     std::set<int> GetClients(int listenFd)
222     {
223         std::set<int> fdSet;
224         tcpClients_.Find(listenFd, fdSet);
225         return fdSet;
226     }
227 
RemoveServerSocket(int listenFd)228     void RemoveServerSocket(int listenFd)
229     {
230         tcpExtraOptions_.Erase(listenFd);
231         tcpClients_.Erase(listenFd);
232     }
233 
ShutdownAllSockets()234     void ShutdownAllSockets()
235     {
236         tcpClients_.Iterate([](const int key, std::set<int>&) { shutdown(key, SHUT_RDWR); });
237         tcpExtraOptions_.Clear();
238         tcpClients_.Clear();
239     }
240 
241 private:
SingletonSocketConfig()242     SingletonSocketConfig() {}
~SingletonSocketConfig()243     ~SingletonSocketConfig() {}
244 
245     SingletonSocketConfig(const SingletonSocketConfig& singleton) = delete;
246     SingletonSocketConfig& operator=(const SingletonSocketConfig& singleton) = delete;
247 
248     SafeMap<int, TCPExtraOptions> tcpExtraOptions_;
249     SafeMap<int, std::set<int>> tcpClients_;
250 };
251 
252 class MessageCallback {
253 public:
254     MessageCallback() = delete;
255 
256     virtual ~MessageCallback() = default;
257 
MessageCallback(const std::shared_ptr<EventManager> & manager)258     explicit MessageCallback(const std::shared_ptr<EventManager> &manager) : manager_(manager) {}
259 
260     virtual void OnError(int err) const = 0;
261 
262     virtual void OnCloseMessage(const std::shared_ptr<EventManager> &manager) const = 0;
263 
264     virtual bool OnMessage(void *data, size_t dataLen, sockaddr *addr) const = 0;
265 
266     virtual bool OnMessage(int sock, void *data, size_t dataLen, sockaddr *addr,
267         const std::shared_ptr<EventManager> &manager) const = 0;
268 
269     virtual void OnTcpConnectionMessage(int32_t id) const = 0;
270 
271     [[nodiscard]] std::shared_ptr<EventManager> GetEventManager() const;
272 
273 protected:
274     std::shared_ptr<EventManager> manager_ = nullptr;
275 };
276 
277 using SocketRecvCallback = std::function<bool(int socketId, std::pair<std::unique_ptr<char[]> &, int> &bufInfo,
278     std::pair<sockaddr *, socklen_t> &addrInfo, const MessageCallback &callback)>;
279 
280 } // namespace OHOS::NetStack::Socket::SocketExec
281 #endif /* COMMUNICATIONNETSTACK_SOCKET_EXEC_H */
282