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