• 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 <sys/select.h>
21 #include <unistd.h>
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 #define SA_DATA_SIZE (14)
30 #define ADDR_IN_RESER_SIZE (8)
31 
32 /* sys/socket.h */
33 #define SOFTBUS_PF_INET (2)
34 #define SOFTBUS_AF_INET SOFTBUS_PF_INET
35 
36 #define SOFTBUS_SOCK_STREAM (1)
37 #define SOFTBUS_SOCK_DGRAM (2)
38 
39 #define SOFTBUS_SOCK_CLOEXEC (02000000)
40 #define SOFTBUS_SOCK_NONBLOCK (04000)
41 
42 #define SOFTBUS_SOL_SOCKET (1)
43 
44 #define SOFTBUS_SO_REUSEADDR (2)
45 #define SOFTBUS_SO_KEEPALIVE (9)
46 #define SOFTBUS_SO_REUSEPORT (15)
47 
48 #define SOFTBUS_TCP_KEEPIDLE (4)
49 #define SOFTBUS_TCP_KEEPINTVL (5)
50 #define SOFTBUS_TCP_KEEPCNT (6)
51 
52 #define SOFTBUS_SHUT_RD (0)
53 #define SOFTBUS_SHUT_WR (1)
54 #define SOFTBUS_SHUT_RDWR (2)
55 
56 /* netinet/in.h */
57 #define SOFTBUS_IPPROTO_IP (0)
58 #define SOFTBUS_IPPROTO_TCP (6)
59 
60 #define SOFTBUS_IP_TOS (1)
61 
62 /* netinet/tcp.h */
63 #define SOFTBUS_TCP_NODELAY (1)
64 
65 /* fcntl.h */
66 #define SOFTBUS_F_GETFL (3)
67 #define SOFTBUS_F_SETFL (4)
68 
69 #define SOFTBUS_O_NONBLOCK (04000)
70 
71 /* select.h */
72 /* linux support 1024, liteos support 640 */
73 #define SOFTBUS_FD_SETSIZE (1024)
74 
75 /* netinet/in.h */
76 typedef struct {
77     unsigned short saFamily; /* address family */
78     char saData[SA_DATA_SIZE];
79 } SoftBusSockAddr;
80 
81 typedef struct {
82     unsigned long sAddr;
83 } SoftBusInAddr;
84 
85 typedef struct {
86     unsigned short sinFamily; /* address family */
87     unsigned short sinPort; /* Port number */
88     SoftBusInAddr sinAddr; /* Internet address */
89     unsigned char sinZero[ADDR_IN_RESER_SIZE]; /* Same size as struct sockaddr */
90 } SoftBusSockAddrIn;
91 
92 typedef struct {
93     uint32_t fdsCount;
94     unsigned long fdsBits[SOFTBUS_FD_SETSIZE / 8 / sizeof(long)];
95 } SoftBusFdSet;
96 
97 int32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd);
98 int32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName,  const void *optVal, int32_t optLen);
99 int32_t SoftBusSocketGetOpt(int32_t socketFd, int32_t level, int32_t optName,  void *optVal, int32_t *optLen);
100 int32_t SoftBusSocketGetLocalName(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen);
101 int32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen);
102 
103 int32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen);
104 int32_t SoftBusSocketListen(int32_t socketFd, int32_t backLog);
105 int32_t SoftBusSocketAccept(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen, int32_t *acceptFd);
106 int32_t SoftBusSocketConnect(int32_t socketFd, const SoftBusSockAddr *addr, int32_t addrLen);
107 
108 void SoftBusSocketFdZero(SoftBusFdSet *set);
109 void SoftBusSocketFdSet(int32_t socketFd, SoftBusFdSet *set);
110 void SoftBusSocketFdClr(int32_t socketFd, SoftBusFdSet *set);
111 int32_t SoftBusSocketFdIsset(int32_t socketFd, SoftBusFdSet *set);
112 
113 int32_t SoftBusSocketSelect(int32_t nfds, SoftBusFdSet *readFds, SoftBusFdSet *writeFds, SoftBusFdSet *exceptFds,
114     struct timeval *timeOut);
115 int32_t SoftBusSocketIoctl(int32_t socketFd, long cmd, void *argp);
116 int32_t SoftBusSocketFcntl(int32_t socketFd, long cmd, long flag);
117 
118 int32_t SoftBusSocketSend(int32_t socketFd, const void *buf, uint32_t len, int32_t flags);
119 int32_t SoftBusSocketSendTo(int32_t socketFd, const void *buf, uint32_t len, int32_t flags,
120     const SoftBusSockAddr *toAddr, int32_t toAddrLen);
121 
122 int32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags);
123 int32_t SoftBusSocketRecvFrom(int32_t socketFd, void *buf, uint32_t len, int32_t flags, SoftBusSockAddr *fromAddr,
124     int32_t *fromAddrLen);
125 
126 
127 int32_t SoftBusSocketShutDown(int32_t socketFd, int32_t how);
128 int32_t SoftBusSocketClose(int32_t socketFd);
129 
130 int32_t SoftBusInetPtoN(int32_t af, const char *src, void *dst);
131 const char *SoftBusInetNtoP(int32_t af, const void* src, char *dst, int32_t size);
132 
133 uint32_t SoftBusHtoNl(uint32_t hostlong);
134 uint16_t SoftBusHtoNs(uint16_t hostshort);
135 uint32_t SoftBusNtoHl(uint32_t netlong);
136 uint16_t SoftBusNtoHs(uint16_t netshort);
137 
138 uint32_t SoftBusInetAddr(const char *cp);
139 
140 int32_t SoftBusSocketGetError(int32_t socketFd);
141 
142 #ifdef __cplusplus
143 #if __cplusplus
144 }
145 #endif /* __cplusplus */
146 #endif /* __cplusplus */
147 
148 #endif
149