1 /* 2 * Copyright (c) 2022 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 SOFTBUS_SOCKET_H 17 #define SOFTBUS_SOCKET_H 18 19 #include <sys/types.h> 20 #include <sys/uio.h> 21 22 #include "softbus_adapter_socket.h" 23 #include "softbus_conn_interface.h" 24 #include "softbus_protocol_def.h" 25 26 #ifdef __cplusplus 27 #if __cplusplus 28 extern "C" { 29 #endif 30 #endif 31 32 #ifndef SOFTBUS_TEMP_FAILURE_RETRY 33 #define SOFTBUS_TEMP_FAILURE_RETRY(expression) \ 34 (__extension__({ \ 35 long int __result; \ 36 do { \ 37 __result = (long int)(expression); \ 38 } while (__result == SOFTBUS_ADAPTER_SOCKET_EINTR); \ 39 __result; \ 40 })) 41 #endif 42 43 #define ADDR_FEATURE_IPV6 ':' 44 #define ADDR_SPLIT_IPV6 "%" 45 46 enum SocketEvent { 47 SOFTBUS_SOCKET_OUT, // writable 48 SOFTBUS_SOCKET_IN, // readable 49 SOFTBUS_SOCKET_EXCEPTION, // exception 50 }; 51 52 typedef struct { 53 char addr[MAX_SOCKET_ADDR_LEN]; 54 int32_t port; 55 } SocketAddr; 56 57 typedef struct SocketInterface { 58 const char *name; 59 const ProtocolType type; 60 int32_t (*GetSockPort)(int32_t fd); 61 int32_t (*OpenServerSocket)(const LocalListenerInfo *option); 62 int32_t (*OpenClientSocket)(const ConnectOption *option, const char *bindAddr, bool isNonBlock); 63 int32_t (*AcceptClient)(int32_t fd, ConnectOption *clientAddr, int32_t *cfd); 64 } SocketInterface; 65 66 int32_t ConnInitSockets(void); 67 void ConnDeinitSockets(void); 68 69 const SocketInterface *GetSocketInterface(ProtocolType protocolType); 70 71 int32_t RegistSocketProtocol(const SocketInterface *interface); 72 73 int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock); 74 75 ssize_t ConnSendSocketData(int32_t fd, const char *buf, size_t len, int32_t timeout); 76 ssize_t ConnRecvSocketData(int32_t fd, char *buf, size_t len, int32_t timeout); 77 void ConnCloseSocket(int32_t fd); 78 void ConnShutdownSocket(int32_t fd); 79 int32_t ConnSetTcpKeepalive(int32_t fd, int32_t seconds, int32_t keepAliveIntvl, int32_t keepAliveCount); 80 int32_t ConnSetTcpUserTimeOut(int32_t fd, uint32_t millSec); 81 82 int32_t ConnToggleNonBlockMode(int32_t fd, bool isNonBlock); 83 int32_t ConnGetSocketError(int32_t fd); 84 int32_t ConnGetLocalSocketPort(int32_t fd); 85 int32_t ConnGetPeerSocketAddr(int32_t fd, SocketAddr *socketAddr); 86 87 int32_t ConnPreAssignPort(int32_t domain); 88 int32_t GetDomainByAddr(const char *addr); 89 int32_t Ipv6AddrInToAddr(SoftBusSockAddrIn6 *addrIn6, char *addr, int32_t addrLen); 90 int32_t Ipv6AddrToAddrIn(SoftBusSockAddrIn6 *addrIn6, const char *ip, uint16_t port); 91 int32_t Ipv4AddrToAddrIn(SoftBusSockAddrIn *addrIn, const char *ip, uint16_t port); 92 93 void BindToInterface(const char *myIp, int32_t domain, int fd, char *ifName, int32_t ifNameMaxLen); 94 95 bool IsHmlIpAddr(const char *ip); 96 97 #ifdef __cplusplus 98 #if __cplusplus 99 } 100 #endif /* __cplusplus */ 101 #endif /* __cplusplus */ 102 103 #endif 104