• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_ADAPTER_SOCKET_H
17 #define SOFTBUS_ADAPTER_SOCKET_H
18 
19 #include <stdint.h>
20 #include "softbus_adapter_define.h"
21 #include "softbus_adapter_timer.h"
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #define SA_DATA_SIZE (26)
29 #define ADDR_IN6_8_SIZE (16)
30 #define ADDR_IN6_16_SIZE (8)
31 #define ADDR_IN6_32_SIZE (4)
32 #define IF_NAME_SIZE (16)
33 
34 #if defined(__aarch64__) || defined(__x86_64__) || (defined(__riscv) && (__riscv_xlen == 64))
35 #define ADDR_IN_RESER_SIZE (4)
36 #else
37 #define ADDR_IN_RESER_SIZE (8)
38 #endif
39 
40 /* sys/socket.h */
41 #define SOFTBUS_PF_UNSPEC SOFTBUS_PF_UNSPEC_
42 #define SOFTBUS_AF_UNSPEC SOFTBUS_AF_UNSPEC_
43 
44 #define SOFTBUS_PF_INET SOFTBUS_PF_INET_
45 #define SOFTBUS_AF_INET SOFTBUS_AF_INET_
46 
47 #define SOFTBUS_PF_INET6 SOFTBUS_PF_INET6_
48 #define SOFTBUS_AF_INET6 SOFTBUS_AF_INET6_
49 
50 #define SOFTBUS_PF_NETLINK SOFTBUS_PF_NETLINK_
51 #define SOFTBUS_AF_NETLINK SOFTBUS_AF_NETLINK_
52 
53 #define SOFTBUS_SOCK_STREAM SOFTBUS_SOCK_STREAM_
54 #define SOFTBUS_SOCK_DGRAM SOFTBUS_SOCK_DGRAM_
55 #define SOFTBUS_SOCK_RAW SOFTBUS_SOCK_RAW_
56 
57 #define SOFTBUS_SOCK_CLOEXEC SOFTBUS_SOCK_CLOEXEC_
58 #define SOFTBUS_SOCK_NONBLOCK SOFTBUS_SOCK_NONBLOCK_
59 
60 #define SOFTBUS_SOL_SOCKET SOFTBUS_SOL_SOCKET_
61 
62 #define SOFTBUS_SO_REUSEADDR SOFTBUS_SO_REUSEADDR_
63 #define SOFTBUS_SO_RCVBUF SOFTBUS_SO_RCVBUF_
64 #define SOFTBUS_SO_SNDBUF SOFTBUS_SO_SNDBUF_
65 #define SOFTBUS_SO_KEEPALIVE SOFTBUS_SO_KEEPALIVE_
66 #define SOFTBUS_SO_REUSEPORT SOFTBUS_SO_REUSEPORT_
67 #define SOFTBUS_SO_RCVBUFFORCE SOFTBUS_SO_RCVBUFFORCE_
68 #define SOFTBUS_SO_BINDTODEVICE SOFTBUS_SO_BINDTODEVICE_
69 
70 #define SOFTBUS_TCP_KEEPIDLE SOFTBUS_TCP_KEEPIDLE_
71 #define SOFTBUS_TCP_KEEPINTVL SOFTBUS_TCP_KEEPINTVL_
72 #define SOFTBUS_TCP_KEEPCNT SOFTBUS_TCP_KEEPCNT_
73 #define SOFTBUS_TCP_USER_TIMEOUT SOFTBUS_TCP_USER_TIMEOUT_
74 
75 #define SOFTBUS_SHUT_RD SOFTBUS_SHUT_RD_
76 #define SOFTBUS_SHUT_WR SOFTBUS_SHUT_WR_
77 #define SOFTBUS_SHUT_RDWR SOFTBUS_SHUT_RDWR_
78 
79 /* netinet/in.h */
80 #define SOFTBUS_IPPROTO_IP SOFTBUS_IPPROTO_IP_
81 #define SOFTBUS_IPPROTO_TCP SOFTBUS_IPPROTO_TCP_
82 
83 #define SOFTBUS_IPPROTO_IPV6 SOFTBUS_IPPROTO_IPV6_
84 #define SOFTBUS_IPV6_TCLASS SOFTBUS_IPV6_TCLASS_
85 
86 #define SOFTBUS_IP_TOS SOFTBUS_IP_TOS_
87 
88 /* netinet/tcp.h */
89 #define SOFTBUS_TCP_NODELAY SOFTBUS_TCP_NODELAY_
90 
91 /* fcntl.h */
92 #define SOFTBUS_F_GETFL SOFTBUS_F_GETFL_
93 #define SOFTBUS_F_SETFL SOFTBUS_F_SETFL_
94 
95 #define SOFTBUS_O_NONBLOCK SOFTBUS_O_NONBLOCK_
96 
97 /* select.h */
98 /* linux support 1024, liteos support 640 */
99 #define SOFTBUS_FD_SETSIZE SOFTBUS_FD_SETSIZE_
100 
101 #define IPV6_MESSAGE_TOS 0xA8
102 typedef SoftBusSysTime SoftBusSockTimeOut;
103 /* netinet/in.h */
104 typedef struct {
105     uint16_t saFamily; /* address family */
106     char saData[SA_DATA_SIZE];
107 } SoftBusSockAddr;
108 
109 #pragma pack (1)
110 typedef struct {
111     unsigned long sAddr;
112 } SoftBusInAddr;
113 
114 typedef struct {
115     uint16_t sinFamily; /* address family */
116     uint16_t sinPort; /* Port number */
117     SoftBusInAddr sinAddr; /* Internet address */
118     unsigned char sinZero[ADDR_IN_RESER_SIZE]; /* Same size as struct sockaddr */
119 } SoftBusSockAddrIn;
120 
121 typedef struct {
122     union {
123         uint8_t sA6ddr8[ADDR_IN6_8_SIZE];
124         uint8_t sA6ddr16[ADDR_IN6_16_SIZE];
125         uint8_t sA6ddr32[ADDR_IN6_32_SIZE];
126     } sA6ddr;
127 } SoftBusIn6Addr;
128 
129 typedef struct {
130     uint16_t sin6Family; /* Ipv6 address family */
131     uint16_t sin6Port; /* Ipv6 Port number */
132     uint32_t sin6FlowInfo; /* Ipv6 flow info */
133     SoftBusIn6Addr sin6Addr; /* Ipv6 address */
134     uint32_t sin6ScopeId; /* Ipv6 scope id */
135 } SoftBusSockAddrIn6;
136 #pragma pack ()
137 
138 typedef struct {
139     uint32_t fdsCount;
140     unsigned long fdsBits[SOFTBUS_FD_SETSIZE / 8 / sizeof(long)];
141 } SoftBusFdSet;
142 
143 int32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd);
144 int32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName,  const void *optVal, int32_t optLen);
145 int32_t SoftBusSocketGetOpt(int32_t socketFd, int32_t level, int32_t optName,  void *optVal, int32_t *optLen);
146 int32_t SoftBusSocketGetLocalName(int32_t socketFd, SoftBusSockAddr *addr);
147 int32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr);
148 
149 int32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen);
150 int32_t SoftBusSocketListen(int32_t socketFd, int32_t backLog);
151 int32_t SoftBusSocketAccept(int32_t socketFd, SoftBusSockAddr *addr, int32_t *acceptFd);
152 int32_t SoftBusSocketConnect(int32_t socketFd, const SoftBusSockAddr *addr, int32_t addrLen);
153 
154 void SoftBusSocketFdZero(SoftBusFdSet *set);
155 void SoftBusSocketFdSet(int32_t socketFd, SoftBusFdSet *set);
156 void SoftBusSocketFdClr(int32_t socketFd, SoftBusFdSet *set);
157 int32_t SoftBusSocketFdIsset(int32_t socketFd, SoftBusFdSet *set);
158 
159 int32_t SoftBusSocketSelect(int32_t nfds, SoftBusFdSet *readFds, SoftBusFdSet *writeFds, SoftBusFdSet *exceptFds,
160     SoftBusSockTimeOut *timeOut);
161 int32_t SoftBusSocketIoctl(int32_t socketFd, long cmd, void *argp);
162 int32_t SoftBusSocketFcntl(int32_t socketFd, long cmd, long flag);
163 
164 int32_t SoftBusSocketSend(int32_t socketFd, const void *buf, uint32_t len, uint32_t flags);
165 int32_t SoftBusSocketSendTo(int32_t socketFd, const void *buf, uint32_t len, int32_t flags,
166     const SoftBusSockAddr *toAddr, int32_t toAddrLen);
167 
168 int32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags);
169 int32_t SoftBusSocketRecvFrom(int32_t socketFd, void *buf, uint32_t len, int32_t flags, SoftBusSockAddr *fromAddr,
170     int32_t *fromAddrLen);
171 
172 
173 int32_t SoftBusSocketShutDown(int32_t socketFd, int32_t how);
174 int32_t SoftBusSocketClose(int32_t socketFd);
175 
176 int32_t SoftBusInetPtoN(int32_t af, const char *src, void *dst);
177 const char *SoftBusInetNtoP(int32_t af, const void *src, char *dst, int32_t size);
178 
179 uint32_t SoftBusHtoNl(uint32_t hostlong);
180 uint16_t SoftBusHtoNs(uint16_t hostshort);
181 uint32_t SoftBusNtoHl(uint32_t netlong);
182 uint16_t SoftBusNtoHs(uint16_t netshort);
183 
184 /* host to little-endian */
185 uint16_t SoftBusHtoLs(uint16_t value);
186 uint32_t SoftBusHtoLl(uint32_t value);
187 uint64_t SoftBusHtoLll(uint64_t value);
188 /* little-endian to host */
189 uint16_t SoftBusLtoHs(uint16_t value);
190 uint32_t SoftBusLtoHl(uint32_t value);
191 uint64_t SoftBusLtoHll(uint64_t value);
192 uint16_t SoftBusLEtoBEs(uint16_t value);
193 uint16_t SoftBusBEtoLEs(uint16_t value);
194 
195 uint32_t SoftBusInetAddr(const char *cp);
196 uint32_t SoftBusIfNameToIndex(const char *name);
197 int32_t SoftBusIndexToIfName(int32_t index, char *ifname, uint32_t nameLen);
198 
199 int32_t SoftBusSocketGetError(int32_t socketFd);
200 int32_t GetErrCodeBySocketErr(int32_t transErrCode);
201 
202 #ifdef __cplusplus
203 #if __cplusplus
204 }
205 #endif /* __cplusplus */
206 #endif /* __cplusplus */
207 
208 #endif // SOFTBUS_ADAPTER_SOCKET_H
209