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 #include "softbus_adapter_socket.h"
17
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <netinet/in.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <sys/select.h>
25 #include <sys/socket.h>
26
27 #include "endian.h" /* liteos_m htons */
28 #include "softbus_adapter_errcode.h"
29 #include "softbus_adapter_log.h"
30 #include "softbus_def.h"
31
32
GetErrorCode(void)33 static int32_t GetErrorCode(void)
34 {
35 int32_t errCode;
36 switch (errno) {
37 case EINTR:
38 errCode = SOFTBUS_ADAPTER_SOCKET_EINTR;
39 break;
40 case EINPROGRESS:
41 errCode = SOFTBUS_ADAPTER_SOCKET_EINPROGRESS;
42 break;
43 case EAGAIN:
44 errCode = SOFTBUS_ADAPTER_SOCKET_EAGAIN;
45 break;
46 default:
47 errCode = SOFTBUS_ADAPTER_ERR;
48 break;
49 }
50 return errCode;
51 }
52
SoftBusSocketCreate(int32_t domain,int32_t type,int32_t protocol,int32_t * socketFd)53 int32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd)
54 {
55 if (socketFd == NULL) {
56 HILOG_ERROR(SOFTBUS_HILOG_ID, "socketFd is null");
57 return SOFTBUS_ADAPTER_INVALID_PARAM;
58 }
59 int32_t ret;
60 ret = socket(domain, type, protocol);
61 if (ret < 0) {
62 HILOG_ERROR(SOFTBUS_HILOG_ID, "socket %{public}s", strerror(errno));
63 return SOFTBUS_ADAPTER_ERR;
64 } else {
65 *socketFd = ret;
66 return SOFTBUS_ADAPTER_OK;
67 }
68 }
69
SoftBusSocketSetOpt(int32_t socketFd,int32_t level,int32_t optName,const void * optVal,int32_t optLen)70 int32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName, const void *optVal, int32_t optLen)
71 {
72 int32_t ret = setsockopt(socketFd, level, optName, optVal, (socklen_t)optLen);
73 if (ret != 0) {
74 HILOG_ERROR(SOFTBUS_HILOG_ID, "setsockopt : %{public}s", strerror(errno));
75 return SOFTBUS_ADAPTER_ERR;
76 }
77
78 return SOFTBUS_ADAPTER_OK;
79 }
80
SoftBusSocketGetOpt(int32_t socketFd,int32_t level,int32_t optName,void * optVal,int32_t * optLen)81 int32_t SoftBusSocketGetOpt(int32_t socketFd, int32_t level, int32_t optName, void *optVal, int32_t *optLen)
82 {
83 int32_t ret = getsockopt(socketFd, level, optName, optVal, (socklen_t *)optLen);
84 if (ret != 0) {
85 HILOG_ERROR(SOFTBUS_HILOG_ID, "getsockopt : %{public}s", strerror(errno));
86 return SOFTBUS_ADAPTER_ERR;
87 }
88 return SOFTBUS_ADAPTER_OK;
89 }
90
SoftBusSocketGetError(int32_t socketFd)91 int32_t SoftBusSocketGetError(int32_t socketFd)
92 {
93 int32_t err = 0;
94 int32_t errSize = sizeof(err);
95
96 int32_t ret = getsockopt(socketFd, SOL_SOCKET, SO_ERROR, &err, (socklen_t *)&errSize);
97 if (ret < 0) {
98 HILOG_ERROR(SOFTBUS_HILOG_ID, "getsockopt fd=%{public}d, ret=%{public}d", socketFd, ret);
99 return ret;
100 }
101 if (err != 0) {
102 HILOG_ERROR(SOFTBUS_HILOG_ID, "getsockopt fd=%{public}d, err=%{public}d", socketFd, err);
103 return err;
104 }
105 return err;
106 }
107
SoftBusSocketGetLocalName(int32_t socketFd,SoftBusSockAddr * addr,int32_t * addrLen)108 int32_t SoftBusSocketGetLocalName(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen)
109 {
110 int32_t ret = getsockname(socketFd, (struct sockaddr *)addr, (socklen_t *)addrLen);
111 if (ret != 0) {
112 HILOG_ERROR(SOFTBUS_HILOG_ID, "getsockname : %{public}s", strerror(errno));
113 return SOFTBUS_ADAPTER_ERR;
114 }
115 return SOFTBUS_ADAPTER_OK;
116 }
117
SoftBusSocketGetPeerName(int32_t socketFd,SoftBusSockAddr * addr,int32_t * addrLen)118 int32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen)
119 {
120 int32_t ret = getpeername(socketFd, (struct sockaddr *)addr, (socklen_t *)addrLen);
121 if (ret != 0) {
122 HILOG_ERROR(SOFTBUS_HILOG_ID, "getpeername : %{public}s", strerror(errno));
123 return SOFTBUS_ADAPTER_ERR;
124 }
125 return SOFTBUS_ADAPTER_OK;
126 }
127
SoftBusSocketBind(int32_t socketFd,SoftBusSockAddr * addr,int32_t addrLen)128 int32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen)
129 {
130 int32_t ret = bind(socketFd, (struct sockaddr *)addr, (socklen_t)addrLen);
131 if (ret != 0) {
132 HILOG_ERROR(SOFTBUS_HILOG_ID, "bind : %{public}s", strerror(errno));
133 return GetErrorCode();
134 }
135
136 return SOFTBUS_ADAPTER_OK;
137 }
138
SoftBusSocketListen(int32_t socketFd,int32_t backLog)139 int32_t SoftBusSocketListen(int32_t socketFd, int32_t backLog)
140 {
141 int32_t ret = listen(socketFd, backLog);
142 if (ret != 0) {
143 HILOG_ERROR(SOFTBUS_HILOG_ID, "listen : %{public}s", strerror(errno));
144 return SOFTBUS_ADAPTER_ERR;
145 }
146
147 return SOFTBUS_ADAPTER_OK;
148 }
149
SoftBusSocketAccept(int32_t socketFd,SoftBusSockAddr * addr,int32_t * addrLen,int32_t * acceptFd)150 int32_t SoftBusSocketAccept(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen, int32_t *acceptFd)
151 {
152 if (acceptFd == NULL) {
153 HILOG_ERROR(SOFTBUS_HILOG_ID, "acceptFd is null");
154 return SOFTBUS_ADAPTER_INVALID_PARAM;
155 }
156 int32_t ret = accept(socketFd, (struct sockaddr *)addr, (socklen_t *)addrLen);
157 if (ret < 0) {
158 HILOG_ERROR(SOFTBUS_HILOG_ID, "accept : %{public}s", strerror(errno));
159 return GetErrorCode();
160 } else {
161 *acceptFd = ret;
162 return SOFTBUS_ADAPTER_OK;
163 }
164 return SOFTBUS_ADAPTER_OK;
165 }
166
SoftBusSocketConnect(int32_t socketFd,const SoftBusSockAddr * addr,int32_t addrLen)167 int32_t SoftBusSocketConnect(int32_t socketFd, const SoftBusSockAddr *addr, int32_t addrLen)
168 {
169 int32_t ret = connect(socketFd, (struct sockaddr *)addr, (socklen_t)addrLen);
170 if (ret < 0) {
171 HILOG_ERROR(SOFTBUS_HILOG_ID, "connect :%{public}s", strerror(errno));
172 return GetErrorCode();
173 }
174 return SOFTBUS_ADAPTER_OK;
175 }
176
SoftBusSocketFdZero(SoftBusFdSet * set)177 void SoftBusSocketFdZero(SoftBusFdSet *set)
178 {
179 if (set == NULL) {
180 HILOG_ERROR(SOFTBUS_HILOG_ID, "set is null");
181 return;
182 }
183
184 FD_ZERO((fd_set *)set->fdsBits);
185 }
186
SoftBusSocketFdSet(int32_t socketFd,SoftBusFdSet * set)187 void SoftBusSocketFdSet(int32_t socketFd, SoftBusFdSet *set)
188 {
189 if (set == NULL) {
190 HILOG_ERROR(SOFTBUS_HILOG_ID, "set is null");
191 return;
192 }
193
194 FD_SET(socketFd, (fd_set *)set->fdsBits);
195 }
196
SoftBusSocketFdClr(int32_t socketFd,SoftBusFdSet * set)197 void SoftBusSocketFdClr(int32_t socketFd, SoftBusFdSet *set)
198 {
199 if (set == NULL) {
200 HILOG_ERROR(SOFTBUS_HILOG_ID, "set is null");
201 return;
202 }
203
204 FD_CLR(socketFd, (fd_set *)set->fdsBits);
205 }
206
SoftBusSocketFdIsset(int32_t socketFd,SoftBusFdSet * set)207 int32_t SoftBusSocketFdIsset(int32_t socketFd, SoftBusFdSet *set)
208 {
209 if (set == NULL) {
210 HILOG_ERROR(SOFTBUS_HILOG_ID, "set is null");
211 return 0;
212 }
213
214 if (FD_ISSET(socketFd, (fd_set *)set->fdsBits) == true) {
215 return 1;
216 } else {
217 return 0;
218 }
219 }
220
SoftBusSocketSelect(int32_t nfds,SoftBusFdSet * readFds,SoftBusFdSet * writeFds,SoftBusFdSet * exceptFds,struct timeval * timeOut)221 int32_t SoftBusSocketSelect(int32_t nfds, SoftBusFdSet *readFds, SoftBusFdSet *writeFds, SoftBusFdSet *exceptFds,
222 struct timeval *timeOut)
223 {
224 if (timeOut == NULL) {
225 HILOG_ERROR(SOFTBUS_HILOG_ID, "timeOut is null");
226 return SOFTBUS_ADAPTER_ERR;
227 }
228 fd_set *tempReadSet = NULL;
229 fd_set *tempWriteSet = NULL;
230 fd_set *tempExceptSet = NULL;
231
232 if (readFds != NULL) {
233 tempReadSet = (fd_set *)readFds->fdsBits;
234 }
235 if (writeFds != NULL) {
236 tempWriteSet = (fd_set *)writeFds->fdsBits;
237 }
238 if (exceptFds != NULL) {
239 tempExceptSet = (fd_set *)exceptFds->fdsBits;
240 }
241 int32_t ret = select(nfds, tempReadSet, tempWriteSet, tempExceptSet, timeOut);
242 if (ret < 0) {
243 HILOG_ERROR(SOFTBUS_HILOG_ID, "select : %{public}s", strerror(errno));
244 return GetErrorCode();
245 }
246
247 return ret;
248 }
249
SoftBusSocketIoctl(int32_t socketFd,long cmd,void * argp)250 int32_t SoftBusSocketIoctl(int32_t socketFd, long cmd, void *argp)
251 {
252 int32_t ret = ioctl(socketFd, cmd, argp);
253 if (ret < 0) {
254 HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl : %{public}s", strerror(errno));
255 return SOFTBUS_ADAPTER_ERR;
256 }
257
258 return ret;
259 }
260
SoftBusSocketFcntl(int32_t socketFd,long cmd,long flag)261 int32_t SoftBusSocketFcntl(int32_t socketFd, long cmd, long flag)
262 {
263 int32_t ret = fcntl(socketFd, cmd, flag);
264 if (ret < 0) {
265 HILOG_ERROR(SOFTBUS_HILOG_ID, "fcntl : %{public}s", strerror(errno));
266 return SOFTBUS_ADAPTER_ERR;
267 }
268
269 return ret;
270 }
271
SoftBusSocketSend(int32_t socketFd,const void * buf,uint32_t len,int32_t flags)272 int32_t SoftBusSocketSend(int32_t socketFd, const void *buf, uint32_t len, int32_t flags)
273 {
274 int32_t ret = send(socketFd, buf, len, flags);
275 if (ret < 0) {
276 HILOG_ERROR(SOFTBUS_HILOG_ID, "send : %{public}s", strerror(errno));
277 return GetErrorCode();
278 }
279
280 return ret;
281 }
282
SoftBusSocketSendTo(int32_t socketFd,const void * buf,uint32_t len,int32_t flags,const SoftBusSockAddr * toAddr,int32_t toAddrLen)283 int32_t SoftBusSocketSendTo(int32_t socketFd, const void *buf, uint32_t len, int32_t flags,
284 const SoftBusSockAddr *toAddr, int32_t toAddrLen)
285 {
286 if ((toAddr == NULL) || (toAddrLen <= 0)) {
287 HILOG_ERROR(SOFTBUS_HILOG_ID, "toAddr is null or toAddrLen <= 0");
288 return SOFTBUS_ADAPTER_ERR;
289 }
290
291 int32_t ret = sendto(socketFd, buf, len, flags, (struct sockaddr *)toAddr, toAddrLen);
292 if (ret < 0) {
293 HILOG_ERROR(SOFTBUS_HILOG_ID, "sendto : %{public}s", strerror(errno));
294 return SOFTBUS_ADAPTER_ERR;
295 }
296
297 return ret;
298 }
299
SoftBusSocketRecv(int32_t socketFd,void * buf,uint32_t len,int32_t flags)300 int32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags)
301 {
302 int32_t ret = recv(socketFd, buf, len, flags);
303 if (ret < 0) {
304 HILOG_ERROR(SOFTBUS_HILOG_ID, "recv : %{public}s", strerror(errno));
305 return GetErrorCode();
306 }
307
308 return ret;
309 }
310
SoftBusSocketRecvFrom(int32_t socketFd,void * buf,uint32_t len,int32_t flags,SoftBusSockAddr * fromAddr,int32_t * fromAddrLen)311 int32_t SoftBusSocketRecvFrom(int32_t socketFd, void *buf, uint32_t len, int32_t flags, SoftBusSockAddr *fromAddr,
312 int32_t *fromAddrLen)
313 {
314 if ((fromAddr == NULL) || (fromAddrLen == NULL)) {
315 HILOG_ERROR(SOFTBUS_HILOG_ID, "fromAddr or fromAddrLen is null");
316 return SOFTBUS_ADAPTER_ERR;
317 }
318
319 int32_t ret = recvfrom(socketFd, buf, len, flags, (struct sockaddr *)fromAddr, (socklen_t *)fromAddrLen);
320 if (ret < 0) {
321 HILOG_ERROR(SOFTBUS_HILOG_ID, "recvfrom : %{public}s", strerror(errno));
322 return SOFTBUS_ADAPTER_ERR;
323 }
324
325 return ret;
326 }
327
SoftBusSocketShutDown(int32_t socketFd,int32_t how)328 int32_t SoftBusSocketShutDown(int32_t socketFd, int32_t how)
329 {
330 int32_t ret = shutdown(socketFd, how);
331 if (ret != 0) {
332 HILOG_ERROR(SOFTBUS_HILOG_ID, "shutdown :%{public}s", strerror(errno));
333 return SOFTBUS_ADAPTER_ERR;
334 }
335
336 return SOFTBUS_ADAPTER_OK;
337 }
338
SoftBusSocketClose(int32_t socketFd)339 int32_t SoftBusSocketClose(int32_t socketFd)
340 {
341 int32_t ret = close(socketFd);
342 if (ret != 0) {
343 HILOG_ERROR(SOFTBUS_HILOG_ID, "close : %{public}s", strerror(errno));
344 return SOFTBUS_ADAPTER_ERR;
345 }
346
347 return SOFTBUS_ADAPTER_OK;
348 }
349
SoftBusInetPtoN(int32_t af,const char * src,void * dst)350 int32_t SoftBusInetPtoN(int32_t af, const char *src, void *dst)
351 {
352 int32_t ret = inet_pton(af, src, dst);
353 if (ret == 1) {
354 return SOFTBUS_ADAPTER_OK;
355 } else if (ret == 0) {
356 HILOG_ERROR(SOFTBUS_HILOG_ID, "invalid str input fromat");
357 return SOFTBUS_ADAPTER_INVALID_PARAM;
358 } else {
359 HILOG_ERROR(SOFTBUS_HILOG_ID, "inet_pton failed");
360 return SOFTBUS_ADAPTER_ERR;
361 }
362 }
363
SoftBusInetNtoP(int32_t af,const void * src,char * dst,int32_t size)364 const char *SoftBusInetNtoP(int32_t af, const void* src, char *dst, int32_t size)
365 {
366 return (inet_ntop(af, src, dst, size));
367 }
368
SoftBusHtoNl(uint32_t hostlong)369 uint32_t SoftBusHtoNl(uint32_t hostlong)
370 {
371 return htonl(hostlong);
372 }
373
SoftBusHtoNs(uint16_t hostshort)374 uint16_t SoftBusHtoNs(uint16_t hostshort)
375 {
376 return htons(hostshort);
377 }
378
SoftBusNtoHl(uint32_t netlong)379 uint32_t SoftBusNtoHl(uint32_t netlong)
380 {
381 return ntohl(netlong);
382 }
383
SoftBusNtoHs(uint16_t netshort)384 uint16_t SoftBusNtoHs(uint16_t netshort)
385 {
386 return ntohs(netshort);
387 }
388
SoftBusInetAddr(const char * cp)389 uint32_t SoftBusInetAddr(const char *cp)
390 {
391 return inet_addr(cp);
392 }
393