1 /* 2 * Copyright (c) 2021-2023 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 37 #include <set> 38 39 namespace OHOS::NetStack::Socket::SocketExec { 40 int MakeTcpSocket(sa_family_t family, bool needNonblock = true); 41 42 int MakeUdpSocket(sa_family_t family); 43 44 void NotifyRegisterEvent(); 45 46 /* async work execute */ 47 bool ExecUdpBind(BindContext *context); 48 49 bool ExecUdpSend(UdpSendContext *context); 50 51 bool ExecUdpAddMembership(MulticastMembershipContext *context); 52 53 bool ExecUdpDropMembership(MulticastMembershipContext *context); 54 55 bool ExecSetMulticastTTL(MulticastSetTTLContext *context); 56 57 bool ExecGetMulticastTTL(MulticastGetTTLContext *context); 58 59 bool ExecSetLoopbackMode(MulticastSetLoopbackContext *context); 60 61 bool ExecGetLoopbackMode(MulticastGetLoopbackContext *context); 62 63 bool ExecTcpBind(BindContext *context); 64 65 bool ExecConnect(ConnectContext *context); 66 67 bool ExecTcpSend(TcpSendContext *context); 68 69 bool ExecClose(CloseContext *context); 70 71 bool ExecGetState(GetStateContext *context); 72 73 bool ExecGetRemoteAddress(GetRemoteAddressContext *context); 74 75 bool ExecTcpSetExtraOptions(TcpSetExtraOptionsContext *context); 76 77 bool ExecUdpSetExtraOptions(UdpSetExtraOptionsContext *context); 78 79 bool ExecTcpGetSocketFd(GetSocketFdContext *context); 80 81 bool ExecUdpGetSocketFd(GetSocketFdContext *context); 82 83 bool ExecTcpConnectionSend(TcpServerSendContext *context); 84 85 bool ExecTcpConnectionGetRemoteAddress(TcpServerGetRemoteAddressContext *context); 86 87 bool ExecTcpConnectionClose(TcpServerCloseContext *context); 88 89 bool ExecTcpServerListen(TcpServerListenContext *context); 90 91 bool ExecTcpServerSetExtraOptions(TcpServerSetExtraOptionsContext *context); 92 93 bool ExecTcpServerGetState(TcpServerGetStateContext *context); 94 95 /* async work callback */ 96 napi_value BindCallback(BindContext *context); 97 98 napi_value UdpSendCallback(UdpSendContext *context); 99 100 napi_value UdpAddMembershipCallback(MulticastMembershipContext *context); 101 102 napi_value UdpDropMembershipCallback(MulticastMembershipContext *context); 103 104 napi_value UdpSetMulticastTTLCallback(MulticastSetTTLContext *context); 105 106 napi_value UdpGetMulticastTTLCallback(MulticastGetTTLContext *context); 107 108 napi_value UdpSetLoopbackModeCallback(MulticastSetLoopbackContext *context); 109 110 napi_value UdpGetLoopbackModeCallback(MulticastGetLoopbackContext *context); 111 112 napi_value ConnectCallback(ConnectContext *context); 113 114 napi_value TcpSendCallback(TcpSendContext *context); 115 116 napi_value CloseCallback(CloseContext *context); 117 118 napi_value GetStateCallback(GetStateContext *context); 119 120 napi_value GetRemoteAddressCallback(GetRemoteAddressContext *context); 121 122 napi_value TcpSetExtraOptionsCallback(TcpSetExtraOptionsContext *context); 123 124 napi_value UdpSetExtraOptionsCallback(UdpSetExtraOptionsContext *context); 125 126 napi_value TcpGetSocketFdCallback(GetSocketFdContext *context); 127 128 napi_value TcpConnectionSendCallback(TcpServerSendContext *context); 129 130 napi_value TcpConnectionCloseCallback(TcpServerCloseContext *context); 131 132 napi_value TcpConnectionGetRemoteAddressCallback(TcpServerGetRemoteAddressContext *context); 133 134 napi_value ListenCallback(TcpServerListenContext *context); 135 136 napi_value TcpServerSetExtraOptionsCallback(TcpServerSetExtraOptionsContext *context); 137 138 napi_value TcpServerGetStateCallback(TcpServerGetStateContext *context); 139 140 napi_value UdpGetSocketFdCallback(GetSocketFdContext *context); 141 142 class SingletonSocketConfig { 143 public: GetInstance()144 static SingletonSocketConfig& GetInstance() 145 { 146 static SingletonSocketConfig instance; 147 return instance; 148 } 149 SetTcpExtraOptions(int listenFd,const TCPExtraOptions & option)150 void SetTcpExtraOptions(int listenFd, const TCPExtraOptions& option) 151 { 152 tcpExtraOptions_.EnsureInsert(listenFd, option); 153 } 154 GetTcpExtraOptions(int listenFd,TCPExtraOptions & option)155 bool GetTcpExtraOptions(int listenFd, TCPExtraOptions& option) 156 { 157 return tcpExtraOptions_.Find(listenFd, option); 158 } 159 AddNewListenSocket(int listenFd)160 void AddNewListenSocket(int listenFd) 161 { 162 tcpClients_.Insert(listenFd, {}); 163 } 164 AddNewAcceptSocket(int listenFd,int acceptFd)165 void AddNewAcceptSocket(int listenFd, int acceptFd) 166 { 167 std::set<int> fdSet; 168 auto fn = [&](std::set<int> &value) -> void { 169 value.emplace(acceptFd); 170 }; 171 if (tcpClients_.Find(listenFd, fdSet)) { 172 tcpClients_.ChangeValueByLambda(listenFd, fn); 173 } 174 } 175 RemoveAcceptSocket(int acceptFd)176 void RemoveAcceptSocket(int acceptFd) 177 { 178 tcpClients_.Iterate([acceptFd](int listenFd, std::set<int> fdSet) { 179 if (auto ite = fdSet.find(acceptFd); ite != fdSet.end()) { 180 fdSet.erase(ite); 181 } 182 }); 183 } 184 GetClients(int listenFd)185 std::set<int> GetClients(int listenFd) 186 { 187 std::set<int> fdSet; 188 tcpClients_.Find(listenFd, fdSet); 189 return fdSet; 190 } 191 RemoveServerSocket(int listenFd)192 void RemoveServerSocket(int listenFd) 193 { 194 tcpExtraOptions_.Erase(listenFd); 195 tcpClients_.Erase(listenFd); 196 } 197 198 private: SingletonSocketConfig()199 SingletonSocketConfig() {} ~SingletonSocketConfig()200 ~SingletonSocketConfig() {} 201 202 SingletonSocketConfig(const SingletonSocketConfig& singleton) = delete; 203 SingletonSocketConfig& operator=(const SingletonSocketConfig& singleton) = delete; 204 205 SafeMap<int, TCPExtraOptions> tcpExtraOptions_; 206 SafeMap<int, std::set<int>> tcpClients_; 207 }; 208 } // namespace OHOS::NetStack::Socket::SocketExec 209 #endif /* COMMUNICATIONNETSTACK_SOCKET_EXEC_H */ 210