1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci 16060ff233Sopenharmony_ci#include "softbus_adapter_socket.h" 17060ff233Sopenharmony_ci 18060ff233Sopenharmony_ci#include <arpa/inet.h> 19060ff233Sopenharmony_ci#include <errno.h> 20060ff233Sopenharmony_ci#include <fcntl.h> 21060ff233Sopenharmony_ci#include <netinet/in.h> 22060ff233Sopenharmony_ci#include <net/if.h> 23060ff233Sopenharmony_ci#include <securec.h> 24060ff233Sopenharmony_ci#include <string.h> 25060ff233Sopenharmony_ci#include <sys/ioctl.h> 26060ff233Sopenharmony_ci#include <sys/select.h> 27060ff233Sopenharmony_ci#include <sys/socket.h> 28060ff233Sopenharmony_ci#include <unistd.h> 29060ff233Sopenharmony_ci 30060ff233Sopenharmony_ci#include "comm_log.h" 31060ff233Sopenharmony_ci#include "conn_event.h" 32060ff233Sopenharmony_ci#include "endian.h" /* liteos_m htons */ 33060ff233Sopenharmony_ci#include "softbus_adapter_errcode.h" 34060ff233Sopenharmony_ci#include "softbus_error_code.h" 35060ff233Sopenharmony_ci#include "softbus_def.h" 36060ff233Sopenharmony_ci 37060ff233Sopenharmony_cistatic void ShiftByte(uint8_t *in, int8_t inSize) 38060ff233Sopenharmony_ci{ 39060ff233Sopenharmony_ci int8_t left = 0; 40060ff233Sopenharmony_ci int8_t right = inSize - 1; 41060ff233Sopenharmony_ci while (left < right) { 42060ff233Sopenharmony_ci in[left] ^= in[right]; 43060ff233Sopenharmony_ci in[right] ^= in[left]; 44060ff233Sopenharmony_ci in[left] ^= in[right]; 45060ff233Sopenharmony_ci ++left; 46060ff233Sopenharmony_ci --right; 47060ff233Sopenharmony_ci } 48060ff233Sopenharmony_ci} 49060ff233Sopenharmony_ci 50060ff233Sopenharmony_cistatic int32_t GetErrorCode(void) 51060ff233Sopenharmony_ci{ 52060ff233Sopenharmony_ci int32_t errCode; 53060ff233Sopenharmony_ci switch (errno) { 54060ff233Sopenharmony_ci case EINTR: 55060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_SOCKET_EINTR; 56060ff233Sopenharmony_ci break; 57060ff233Sopenharmony_ci case EINPROGRESS: 58060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_SOCKET_EINPROGRESS; 59060ff233Sopenharmony_ci break; 60060ff233Sopenharmony_ci case EAGAIN: 61060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_SOCKET_EAGAIN; 62060ff233Sopenharmony_ci break; 63060ff233Sopenharmony_ci case EBADF: 64060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_SOCKET_EBADF; 65060ff233Sopenharmony_ci break; 66060ff233Sopenharmony_ci case EINVAL: 67060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_SOCKET_EINVAL; 68060ff233Sopenharmony_ci break; 69060ff233Sopenharmony_ci case ENETUNREACH: 70060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_SOCKET_ENETUNREACH; 71060ff233Sopenharmony_ci break; 72060ff233Sopenharmony_ci default: 73060ff233Sopenharmony_ci errCode = SOFTBUS_ADAPTER_ERR; 74060ff233Sopenharmony_ci break; 75060ff233Sopenharmony_ci } 76060ff233Sopenharmony_ci return errCode; 77060ff233Sopenharmony_ci} 78060ff233Sopenharmony_ci 79060ff233Sopenharmony_cistatic void DfxReportAdapterSocket(ConnEventScene scene, int32_t res, int32_t fd, int32_t cfd) 80060ff233Sopenharmony_ci{ 81060ff233Sopenharmony_ci ConnEventExtra extra = { 82060ff233Sopenharmony_ci .fd = fd, 83060ff233Sopenharmony_ci .cfd = cfd, 84060ff233Sopenharmony_ci .errcode = res, 85060ff233Sopenharmony_ci .result = res == SOFTBUS_OK ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED 86060ff233Sopenharmony_ci }; 87060ff233Sopenharmony_ci CONN_EVENT(scene, EVENT_STAGE_TCP_COMMON_ONE, extra); 88060ff233Sopenharmony_ci} 89060ff233Sopenharmony_ci 90060ff233Sopenharmony_cistatic int32_t SockOptErrorToSoftBusError(int32_t errorCode) 91060ff233Sopenharmony_ci{ 92060ff233Sopenharmony_ci return SOFTBUS_ERRNO(KERNELS_SUB_MODULE_CODE) + abs(errorCode); 93060ff233Sopenharmony_ci} 94060ff233Sopenharmony_ci 95060ff233Sopenharmony_ciint32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd) 96060ff233Sopenharmony_ci{ 97060ff233Sopenharmony_ci if (socketFd == NULL) { 98060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socketFd is null"); 99060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_INVALID_PARAM; 100060ff233Sopenharmony_ci } 101060ff233Sopenharmony_ci int32_t ret = socket(domain, type, protocol); 102060ff233Sopenharmony_ci if (ret < 0) { 103060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socket errno=%{public}s, ret=%{public}d", strerror(errno), ret); 104060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 105060ff233Sopenharmony_ci } else { 106060ff233Sopenharmony_ci *socketFd = ret; 107060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 108060ff233Sopenharmony_ci } 109060ff233Sopenharmony_ci} 110060ff233Sopenharmony_ci 111060ff233Sopenharmony_ciint32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName, const void *optVal, int32_t optLen) 112060ff233Sopenharmony_ci{ 113060ff233Sopenharmony_ci int32_t ret = setsockopt(socketFd, level, optName, optVal, (socklen_t)optLen); 114060ff233Sopenharmony_ci if (ret != 0) { 115060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "setsockopt errno=%{public}s, ret=%{public}d", strerror(errno), ret); 116060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 117060ff233Sopenharmony_ci } 118060ff233Sopenharmony_ci 119060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 120060ff233Sopenharmony_ci} 121060ff233Sopenharmony_ci 122060ff233Sopenharmony_ciint32_t SoftBusSocketGetOpt(int32_t socketFd, int32_t level, int32_t optName, void *optVal, int32_t *optLen) 123060ff233Sopenharmony_ci{ 124060ff233Sopenharmony_ci int32_t ret = getsockopt(socketFd, level, optName, optVal, (socklen_t *)optLen); 125060ff233Sopenharmony_ci if (ret != 0) { 126060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "getsockopt errno=%{public}s, ret=%{public}d", strerror(errno), ret); 127060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 128060ff233Sopenharmony_ci } 129060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 130060ff233Sopenharmony_ci} 131060ff233Sopenharmony_ci 132060ff233Sopenharmony_ciint32_t SoftBusSocketGetError(int32_t socketFd) 133060ff233Sopenharmony_ci{ 134060ff233Sopenharmony_ci int32_t err = 0; 135060ff233Sopenharmony_ci socklen_t errSize = sizeof(err); 136060ff233Sopenharmony_ci int32_t ret = getsockopt(socketFd, SOL_SOCKET, SO_ERROR, &err, &errSize); 137060ff233Sopenharmony_ci if (ret < 0) { 138060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "getsockopt fd=%{public}d, errno=%{public}s, ret=%{public}d", 139060ff233Sopenharmony_ci socketFd, strerror(errno), ret); 140060ff233Sopenharmony_ci return SockOptErrorToSoftBusError(errno); 141060ff233Sopenharmony_ci } 142060ff233Sopenharmony_ci if (err != 0) { 143060ff233Sopenharmony_ci COMM_LOGD(COMM_ADAPTER, "getsockopt fd=%{public}d, err=%{public}d", socketFd, err); 144060ff233Sopenharmony_ci return SockOptErrorToSoftBusError(err); 145060ff233Sopenharmony_ci } 146060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 147060ff233Sopenharmony_ci} 148060ff233Sopenharmony_ci 149060ff233Sopenharmony_ciint32_t SoftBusSocketGetLocalName(int32_t socketFd, SoftBusSockAddr *addr) 150060ff233Sopenharmony_ci{ 151060ff233Sopenharmony_ci if (addr == NULL) { 152060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "get local name invalid input"); 153060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 154060ff233Sopenharmony_ci } 155060ff233Sopenharmony_ci uint32_t len = sizeof(*addr); 156060ff233Sopenharmony_ci int32_t ret = getsockname(socketFd, (struct sockaddr *)addr, (socklen_t *)&len); 157060ff233Sopenharmony_ci if (ret != 0) { 158060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "getsockname errno=%{public}s, ret=%{public}d", strerror(errno), ret); 159060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 160060ff233Sopenharmony_ci } 161060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 162060ff233Sopenharmony_ci} 163060ff233Sopenharmony_ci 164060ff233Sopenharmony_ciint32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr) 165060ff233Sopenharmony_ci{ 166060ff233Sopenharmony_ci if (addr == NULL) { 167060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "get peer name invalid input"); 168060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 169060ff233Sopenharmony_ci } 170060ff233Sopenharmony_ci 171060ff233Sopenharmony_ci uint32_t len = sizeof(*addr); 172060ff233Sopenharmony_ci int32_t ret = getpeername(socketFd, (struct sockaddr *)addr, (socklen_t *)&len); 173060ff233Sopenharmony_ci if (ret != 0) { 174060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "getpeername errno=%{public}s, ret=%{public}d", strerror(errno), ret); 175060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 176060ff233Sopenharmony_ci } 177060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 178060ff233Sopenharmony_ci} 179060ff233Sopenharmony_ci 180060ff233Sopenharmony_ciint32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen) 181060ff233Sopenharmony_ci{ 182060ff233Sopenharmony_ci if (addr == NULL || addrLen < 0) { 183060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socket bind invalid input"); 184060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 185060ff233Sopenharmony_ci } 186060ff233Sopenharmony_ci 187060ff233Sopenharmony_ci int32_t ret = bind(socketFd, (struct sockaddr *)addr, (socklen_t)addrLen); 188060ff233Sopenharmony_ci if (ret != 0) { 189060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "bind strerror=%{public}s, errno=%{public}d, ret=%{public}d", 190060ff233Sopenharmony_ci strerror(errno), errno, ret); 191060ff233Sopenharmony_ci return GetErrorCode(); 192060ff233Sopenharmony_ci } 193060ff233Sopenharmony_ci 194060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 195060ff233Sopenharmony_ci} 196060ff233Sopenharmony_ci 197060ff233Sopenharmony_ciint32_t SoftBusSocketListen(int32_t socketFd, int32_t backLog) 198060ff233Sopenharmony_ci{ 199060ff233Sopenharmony_ci int32_t ret = listen(socketFd, backLog); 200060ff233Sopenharmony_ci if (ret != 0) { 201060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "listen strerror=%{public}s, errno=%{public}d, ret=%{public}d", 202060ff233Sopenharmony_ci strerror(errno), errno, ret); 203060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_LISTEN, SOFTBUS_TCPCONNECTION_SOCKET_ERR, socketFd, 0); 204060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 205060ff233Sopenharmony_ci } 206060ff233Sopenharmony_ci 207060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_LISTEN, SOFTBUS_OK, socketFd, 0); 208060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 209060ff233Sopenharmony_ci} 210060ff233Sopenharmony_ci 211060ff233Sopenharmony_ciint32_t SoftBusSocketAccept(int32_t socketFd, SoftBusSockAddr *addr, int32_t *acceptFd) 212060ff233Sopenharmony_ci{ 213060ff233Sopenharmony_ci if (addr == NULL || acceptFd == NULL) { 214060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socket accept invalid input"); 215060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_INVALID_PARAM; 216060ff233Sopenharmony_ci } 217060ff233Sopenharmony_ci 218060ff233Sopenharmony_ci uint32_t len = sizeof(*addr); 219060ff233Sopenharmony_ci int32_t ret = accept(socketFd, (struct sockaddr *)addr, (socklen_t *)&len); 220060ff233Sopenharmony_ci if (ret < 0) { 221060ff233Sopenharmony_ci COMM_LOGD(COMM_ADAPTER, "accept strerror=%{public}s, errno=%{public}d, ret=%{public}d", 222060ff233Sopenharmony_ci strerror(errno), errno, ret); 223060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_ACCEPT, SOFTBUS_TCPCONNECTION_SOCKET_ERR, socketFd, 0); 224060ff233Sopenharmony_ci return GetErrorCode(); 225060ff233Sopenharmony_ci } 226060ff233Sopenharmony_ci *acceptFd = ret; 227060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_ACCEPT, SOFTBUS_OK, socketFd, *acceptFd); 228060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 229060ff233Sopenharmony_ci} 230060ff233Sopenharmony_ci 231060ff233Sopenharmony_ciint32_t SoftBusSocketConnect(int32_t socketFd, const SoftBusSockAddr *addr, int32_t addrLen) 232060ff233Sopenharmony_ci{ 233060ff233Sopenharmony_ci if (addr == NULL || addrLen < 0) { 234060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socket connect invalid input"); 235060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 236060ff233Sopenharmony_ci } 237060ff233Sopenharmony_ci int32_t ret = connect(socketFd, (struct sockaddr *)addr, (socklen_t)addrLen); 238060ff233Sopenharmony_ci if (ret < 0) { 239060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "connect=%{public}s, ret=%{public}d", strerror(errno), ret); 240060ff233Sopenharmony_ci int32_t result = GetErrorCode(); 241060ff233Sopenharmony_ci if (result == SOFTBUS_ADAPTER_SOCKET_EINPROGRESS || result == SOFTBUS_ADAPTER_SOCKET_EAGAIN) { 242060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_CONNECT, SOFTBUS_OK, socketFd, 0); 243060ff233Sopenharmony_ci } else { 244060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_CONNECT, SOFTBUS_TCPCONNECTION_SOCKET_ERR, socketFd, 0); 245060ff233Sopenharmony_ci } 246060ff233Sopenharmony_ci return result; 247060ff233Sopenharmony_ci } 248060ff233Sopenharmony_ci DfxReportAdapterSocket(EVENT_SCENE_SOCKET_CONNECT, SOFTBUS_OK, socketFd, 0); 249060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 250060ff233Sopenharmony_ci} 251060ff233Sopenharmony_ci 252060ff233Sopenharmony_civoid SoftBusSocketFdZero(SoftBusFdSet *set) 253060ff233Sopenharmony_ci{ 254060ff233Sopenharmony_ci if (set == NULL) { 255060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set is null"); 256060ff233Sopenharmony_ci return; 257060ff233Sopenharmony_ci } 258060ff233Sopenharmony_ci 259060ff233Sopenharmony_ci FD_ZERO((fd_set *)set->fdsBits); 260060ff233Sopenharmony_ci} 261060ff233Sopenharmony_ci 262060ff233Sopenharmony_civoid SoftBusSocketFdSet(int32_t socketFd, SoftBusFdSet *set) 263060ff233Sopenharmony_ci{ 264060ff233Sopenharmony_ci if (set == NULL) { 265060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set is null"); 266060ff233Sopenharmony_ci return; 267060ff233Sopenharmony_ci } 268060ff233Sopenharmony_ci if (socketFd >= SOFTBUS_FD_SETSIZE) { 269060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socketFd is too big. socketFd=%{public}d", socketFd); 270060ff233Sopenharmony_ci return; 271060ff233Sopenharmony_ci } 272060ff233Sopenharmony_ci 273060ff233Sopenharmony_ci FD_SET(socketFd, (fd_set *)set->fdsBits); 274060ff233Sopenharmony_ci} 275060ff233Sopenharmony_ci 276060ff233Sopenharmony_civoid SoftBusSocketFdClr(int32_t socketFd, SoftBusFdSet *set) 277060ff233Sopenharmony_ci{ 278060ff233Sopenharmony_ci if (set == NULL) { 279060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set is null"); 280060ff233Sopenharmony_ci return; 281060ff233Sopenharmony_ci } 282060ff233Sopenharmony_ci 283060ff233Sopenharmony_ci FD_CLR(socketFd, (fd_set *)set->fdsBits); 284060ff233Sopenharmony_ci} 285060ff233Sopenharmony_ci 286060ff233Sopenharmony_ciint32_t SoftBusSocketFdIsset(int32_t socketFd, SoftBusFdSet *set) 287060ff233Sopenharmony_ci{ 288060ff233Sopenharmony_ci if (set == NULL) { 289060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set is null"); 290060ff233Sopenharmony_ci return 0; 291060ff233Sopenharmony_ci } 292060ff233Sopenharmony_ci if (socketFd >= SOFTBUS_FD_SETSIZE) { 293060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "socketFd is too big. socketFd=%{public}d", socketFd); 294060ff233Sopenharmony_ci return 0; 295060ff233Sopenharmony_ci } 296060ff233Sopenharmony_ci 297060ff233Sopenharmony_ci if (FD_ISSET(socketFd, (fd_set *)set->fdsBits) == true) { 298060ff233Sopenharmony_ci return 1; 299060ff233Sopenharmony_ci } else { 300060ff233Sopenharmony_ci return 0; 301060ff233Sopenharmony_ci } 302060ff233Sopenharmony_ci} 303060ff233Sopenharmony_ci 304060ff233Sopenharmony_ciint32_t SoftBusSocketSelect( 305060ff233Sopenharmony_ci int32_t nfds, SoftBusFdSet *readFds, SoftBusFdSet *writeFds, SoftBusFdSet *exceptFds, SoftBusSockTimeOut *timeout) 306060ff233Sopenharmony_ci{ 307060ff233Sopenharmony_ci fd_set *tempReadSet = NULL; 308060ff233Sopenharmony_ci fd_set *tempWriteSet = NULL; 309060ff233Sopenharmony_ci fd_set *tempExceptSet = NULL; 310060ff233Sopenharmony_ci 311060ff233Sopenharmony_ci if (readFds != NULL) { 312060ff233Sopenharmony_ci tempReadSet = (fd_set *)readFds->fdsBits; 313060ff233Sopenharmony_ci } 314060ff233Sopenharmony_ci if (writeFds != NULL) { 315060ff233Sopenharmony_ci tempWriteSet = (fd_set *)writeFds->fdsBits; 316060ff233Sopenharmony_ci } 317060ff233Sopenharmony_ci if (exceptFds != NULL) { 318060ff233Sopenharmony_ci tempExceptSet = (fd_set *)exceptFds->fdsBits; 319060ff233Sopenharmony_ci } 320060ff233Sopenharmony_ci 321060ff233Sopenharmony_ci struct timeval *timeoutPtr = NULL; 322060ff233Sopenharmony_ci struct timeval tv = { 0 }; 323060ff233Sopenharmony_ci if (timeout != NULL) { 324060ff233Sopenharmony_ci tv.tv_sec = timeout->sec; 325060ff233Sopenharmony_ci tv.tv_usec = timeout->usec; 326060ff233Sopenharmony_ci timeoutPtr = &tv; 327060ff233Sopenharmony_ci } 328060ff233Sopenharmony_ci#define SELECT_INTERVAL_US (100 * 1000) 329060ff233Sopenharmony_ci#ifdef __LITEOS__ 330060ff233Sopenharmony_ci tv.tv_sec = 0; 331060ff233Sopenharmony_ci tv.tv_usec = SELECT_INTERVAL_US; 332060ff233Sopenharmony_ci timeoutPtr = &tv; 333060ff233Sopenharmony_ci#endif 334060ff233Sopenharmony_ci int32_t ret = select(nfds, tempReadSet, tempWriteSet, tempExceptSet, timeoutPtr); 335060ff233Sopenharmony_ci if (ret < 0) { 336060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "select errno=%{public}s, ret=%{public}d", strerror(errno), ret); 337060ff233Sopenharmony_ci return GetErrorCode(); 338060ff233Sopenharmony_ci } 339060ff233Sopenharmony_ci 340060ff233Sopenharmony_ci return ret; 341060ff233Sopenharmony_ci} 342060ff233Sopenharmony_ci 343060ff233Sopenharmony_ciint32_t SoftBusSocketIoctl(int32_t socketFd, long cmd, void *argp) 344060ff233Sopenharmony_ci{ 345060ff233Sopenharmony_ci int32_t ret = ioctl(socketFd, cmd, argp); 346060ff233Sopenharmony_ci if (ret < 0) { 347060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "ioctl errno=%{public}s, ret=%{public}d", strerror(errno), ret); 348060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 349060ff233Sopenharmony_ci } 350060ff233Sopenharmony_ci 351060ff233Sopenharmony_ci return ret; 352060ff233Sopenharmony_ci} 353060ff233Sopenharmony_ci 354060ff233Sopenharmony_ciint32_t SoftBusSocketFcntl(int32_t socketFd, long cmd, long flag) 355060ff233Sopenharmony_ci{ 356060ff233Sopenharmony_ci int32_t ret = fcntl(socketFd, cmd, flag); 357060ff233Sopenharmony_ci if (ret < 0) { 358060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "fcntl errno=%{public}s, ret=%{public}d", strerror(errno), ret); 359060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 360060ff233Sopenharmony_ci } 361060ff233Sopenharmony_ci 362060ff233Sopenharmony_ci return ret; 363060ff233Sopenharmony_ci} 364060ff233Sopenharmony_ci 365060ff233Sopenharmony_ciint32_t SoftBusSocketSend(int32_t socketFd, const void *buf, uint32_t len, uint32_t flags) 366060ff233Sopenharmony_ci{ 367060ff233Sopenharmony_ci int32_t wrapperFlag = flags | MSG_NOSIGNAL; 368060ff233Sopenharmony_ci int32_t ret = send(socketFd, buf, len, wrapperFlag); 369060ff233Sopenharmony_ci if (ret < 0) { 370060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "send errno=%{public}s, ret=%{public}d", strerror(errno), ret); 371060ff233Sopenharmony_ci return GetErrorCode(); 372060ff233Sopenharmony_ci } 373060ff233Sopenharmony_ci 374060ff233Sopenharmony_ci return ret; 375060ff233Sopenharmony_ci} 376060ff233Sopenharmony_ci 377060ff233Sopenharmony_ciint32_t SoftBusSocketSendTo(int32_t socketFd, const void *buf, uint32_t len, int32_t flags, 378060ff233Sopenharmony_ci const SoftBusSockAddr *toAddr, int32_t toAddrLen) 379060ff233Sopenharmony_ci{ 380060ff233Sopenharmony_ci if ((toAddr == NULL) || (toAddrLen <= 0)) { 381060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "toAddr is null or toAddrLen <= 0"); 382060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 383060ff233Sopenharmony_ci } 384060ff233Sopenharmony_ci int32_t ret = sendto(socketFd, buf, len, flags, (struct sockaddr *)toAddr, toAddrLen); 385060ff233Sopenharmony_ci if (ret < 0) { 386060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "sendto errno=%{public}s, ret=%{public}d", strerror(errno), ret); 387060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 388060ff233Sopenharmony_ci } 389060ff233Sopenharmony_ci 390060ff233Sopenharmony_ci return ret; 391060ff233Sopenharmony_ci} 392060ff233Sopenharmony_ci 393060ff233Sopenharmony_ciint32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags) 394060ff233Sopenharmony_ci{ 395060ff233Sopenharmony_ci int32_t ret = recv(socketFd, buf, len, flags); 396060ff233Sopenharmony_ci if (ret < 0) { 397060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "recv socketFd=%{public}d, errno=%{public}s, ret=%{public}d", 398060ff233Sopenharmony_ci socketFd, strerror(errno), ret); 399060ff233Sopenharmony_ci return GetErrorCode(); 400060ff233Sopenharmony_ci } 401060ff233Sopenharmony_ci 402060ff233Sopenharmony_ci return ret; 403060ff233Sopenharmony_ci} 404060ff233Sopenharmony_ci 405060ff233Sopenharmony_ciint32_t SoftBusSocketRecvFrom(int32_t socketFd, void *buf, uint32_t len, int32_t flags, SoftBusSockAddr *fromAddr, 406060ff233Sopenharmony_ci int32_t *fromAddrLen) 407060ff233Sopenharmony_ci{ 408060ff233Sopenharmony_ci if ((fromAddr == NULL) || (fromAddrLen == NULL)) { 409060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "fromAddr or fromAddrLen is null"); 410060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 411060ff233Sopenharmony_ci } 412060ff233Sopenharmony_ci 413060ff233Sopenharmony_ci int32_t ret = recvfrom(socketFd, buf, len, flags, (struct sockaddr *)fromAddr, (socklen_t *)fromAddrLen); 414060ff233Sopenharmony_ci if (ret < 0) { 415060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "recvfrom errno=%{public}s, ret=%{public}d", strerror(errno), ret); 416060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 417060ff233Sopenharmony_ci } 418060ff233Sopenharmony_ci 419060ff233Sopenharmony_ci return ret; 420060ff233Sopenharmony_ci} 421060ff233Sopenharmony_ci 422060ff233Sopenharmony_ciint32_t SoftBusSocketShutDown(int32_t socketFd, int32_t how) 423060ff233Sopenharmony_ci{ 424060ff233Sopenharmony_ci int32_t ret = shutdown(socketFd, how); 425060ff233Sopenharmony_ci if (ret != 0) { 426060ff233Sopenharmony_ci COMM_LOGD(COMM_ADAPTER, "shutdown=%{public}s, ret=%{public}d", strerror(errno), ret); 427060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 428060ff233Sopenharmony_ci } 429060ff233Sopenharmony_ci 430060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 431060ff233Sopenharmony_ci} 432060ff233Sopenharmony_ci 433060ff233Sopenharmony_ciint32_t SoftBusSocketClose(int32_t socketFd) 434060ff233Sopenharmony_ci{ 435060ff233Sopenharmony_ci int32_t ret = close(socketFd); 436060ff233Sopenharmony_ci if (ret != 0) { 437060ff233Sopenharmony_ci COMM_LOGD(COMM_ADAPTER, "close errno=%{public}s, ret=%{public}d", strerror(errno), ret); 438060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 439060ff233Sopenharmony_ci } 440060ff233Sopenharmony_ci 441060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 442060ff233Sopenharmony_ci} 443060ff233Sopenharmony_ci 444060ff233Sopenharmony_ciint32_t SoftBusInetPtoN(int32_t af, const char *src, void *dst) 445060ff233Sopenharmony_ci{ 446060ff233Sopenharmony_ci if (src == NULL || dst == NULL) { 447060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "src or dst is null"); 448060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 449060ff233Sopenharmony_ci } 450060ff233Sopenharmony_ci int32_t ret = inet_pton(af, src, dst); 451060ff233Sopenharmony_ci if (ret == 1) { 452060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 453060ff233Sopenharmony_ci } else if (ret == 0) { 454060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "invalid str input fromat, ret=%{public}d", ret); 455060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_INVALID_PARAM; 456060ff233Sopenharmony_ci } else { 457060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "inet_pton failed, ret=%{public}d", ret); 458060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 459060ff233Sopenharmony_ci } 460060ff233Sopenharmony_ci} 461060ff233Sopenharmony_ci 462060ff233Sopenharmony_ciconst char *SoftBusInetNtoP(int32_t af, const void *src, char *dst, int32_t size) 463060ff233Sopenharmony_ci{ 464060ff233Sopenharmony_ci return (inet_ntop(af, src, dst, size)); 465060ff233Sopenharmony_ci} 466060ff233Sopenharmony_ci 467060ff233Sopenharmony_ciuint32_t SoftBusHtoNl(uint32_t hostlong) 468060ff233Sopenharmony_ci{ 469060ff233Sopenharmony_ci return htonl(hostlong); 470060ff233Sopenharmony_ci} 471060ff233Sopenharmony_ci 472060ff233Sopenharmony_ciuint16_t SoftBusHtoNs(uint16_t hostshort) 473060ff233Sopenharmony_ci{ 474060ff233Sopenharmony_ci return htons(hostshort); 475060ff233Sopenharmony_ci} 476060ff233Sopenharmony_ci 477060ff233Sopenharmony_ciuint32_t SoftBusNtoHl(uint32_t netlong) 478060ff233Sopenharmony_ci{ 479060ff233Sopenharmony_ci return ntohl(netlong); 480060ff233Sopenharmony_ci} 481060ff233Sopenharmony_ci 482060ff233Sopenharmony_ciuint16_t SoftBusNtoHs(uint16_t netshort) 483060ff233Sopenharmony_ci{ 484060ff233Sopenharmony_ci return ntohs(netshort); 485060ff233Sopenharmony_ci} 486060ff233Sopenharmony_ci 487060ff233Sopenharmony_ciuint32_t SoftBusInetAddr(const char *cp) 488060ff233Sopenharmony_ci{ 489060ff233Sopenharmony_ci return inet_addr(cp); 490060ff233Sopenharmony_ci} 491060ff233Sopenharmony_ci 492060ff233Sopenharmony_ciuint32_t SoftBusIfNameToIndex(const char *name) 493060ff233Sopenharmony_ci{ 494060ff233Sopenharmony_ci return if_nametoindex(name); 495060ff233Sopenharmony_ci} 496060ff233Sopenharmony_ci 497060ff233Sopenharmony_ciint32_t SoftBusIndexToIfName(int32_t index, char *ifname, uint32_t nameLen) 498060ff233Sopenharmony_ci{ 499060ff233Sopenharmony_ci if (index < 0 || ifname == NULL || nameLen < IF_NAME_SIZE) { 500060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "Invalid parm nameLen=%{public}d", nameLen); 501060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_ERR; 502060ff233Sopenharmony_ci } 503060ff233Sopenharmony_ci if (if_indextoname(index, ifname) == NULL) { 504060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "get ifname faild! errno=%{public}s", strerror(errno)); 505060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 506060ff233Sopenharmony_ci } 507060ff233Sopenharmony_ci return SOFTBUS_ADAPTER_OK; 508060ff233Sopenharmony_ci} 509060ff233Sopenharmony_ci 510060ff233Sopenharmony_cistatic bool IsLittleEndian(void) 511060ff233Sopenharmony_ci{ 512060ff233Sopenharmony_ci uint32_t data = 0x1; 513060ff233Sopenharmony_ci if (data == ntohl(data)) { 514060ff233Sopenharmony_ci return false; 515060ff233Sopenharmony_ci } else { 516060ff233Sopenharmony_ci return true; 517060ff233Sopenharmony_ci } 518060ff233Sopenharmony_ci} 519060ff233Sopenharmony_ci 520060ff233Sopenharmony_cistatic void ProcByteOrder(uint8_t *value, int8_t size) 521060ff233Sopenharmony_ci{ 522060ff233Sopenharmony_ci if (IsLittleEndian()) { 523060ff233Sopenharmony_ci return; 524060ff233Sopenharmony_ci } 525060ff233Sopenharmony_ci ShiftByte(value, size); 526060ff233Sopenharmony_ci} 527060ff233Sopenharmony_ci 528060ff233Sopenharmony_ciuint16_t SoftBusHtoLs(uint16_t value) 529060ff233Sopenharmony_ci{ 530060ff233Sopenharmony_ci uint16_t res = value; 531060ff233Sopenharmony_ci ProcByteOrder((uint8_t *)&res, (int8_t)sizeof(res)); 532060ff233Sopenharmony_ci return res; 533060ff233Sopenharmony_ci} 534060ff233Sopenharmony_ci 535060ff233Sopenharmony_ciuint32_t SoftBusHtoLl(uint32_t value) 536060ff233Sopenharmony_ci{ 537060ff233Sopenharmony_ci uint32_t res = value; 538060ff233Sopenharmony_ci ProcByteOrder((uint8_t *)&res, (int8_t)sizeof(res)); 539060ff233Sopenharmony_ci return res; 540060ff233Sopenharmony_ci} 541060ff233Sopenharmony_ci 542060ff233Sopenharmony_ciuint64_t SoftBusHtoLll(uint64_t value) 543060ff233Sopenharmony_ci{ 544060ff233Sopenharmony_ci uint64_t res = value; 545060ff233Sopenharmony_ci ProcByteOrder((uint8_t *)&res, (int8_t)sizeof(res)); 546060ff233Sopenharmony_ci return res; 547060ff233Sopenharmony_ci} 548060ff233Sopenharmony_ci 549060ff233Sopenharmony_ciuint16_t SoftBusLtoHs(uint16_t value) 550060ff233Sopenharmony_ci{ 551060ff233Sopenharmony_ci uint16_t res = value; 552060ff233Sopenharmony_ci ProcByteOrder((uint8_t *)&res, (int8_t)sizeof(res)); 553060ff233Sopenharmony_ci return res; 554060ff233Sopenharmony_ci} 555060ff233Sopenharmony_ci 556060ff233Sopenharmony_ciuint32_t SoftBusLtoHl(uint32_t value) 557060ff233Sopenharmony_ci{ 558060ff233Sopenharmony_ci uint32_t res = value; 559060ff233Sopenharmony_ci ProcByteOrder((uint8_t *)&res, (int8_t)sizeof(res)); 560060ff233Sopenharmony_ci return res; 561060ff233Sopenharmony_ci} 562060ff233Sopenharmony_ci 563060ff233Sopenharmony_ciuint64_t SoftBusLtoHll(uint64_t value) 564060ff233Sopenharmony_ci{ 565060ff233Sopenharmony_ci uint64_t res = value; 566060ff233Sopenharmony_ci ProcByteOrder((uint8_t *)&res, (int8_t)sizeof(res)); 567060ff233Sopenharmony_ci return res; 568060ff233Sopenharmony_ci} 569060ff233Sopenharmony_ci 570060ff233Sopenharmony_ciuint16_t SoftBusLEtoBEs(uint16_t value) 571060ff233Sopenharmony_ci{ 572060ff233Sopenharmony_ci if (!IsLittleEndian()) { 573060ff233Sopenharmony_ci return value; 574060ff233Sopenharmony_ci } 575060ff233Sopenharmony_ci uint16_t res = value; 576060ff233Sopenharmony_ci ShiftByte((uint8_t *)&res, (int8_t)sizeof(res)); 577060ff233Sopenharmony_ci return res; 578060ff233Sopenharmony_ci} 579060ff233Sopenharmony_ci 580060ff233Sopenharmony_ciuint16_t SoftBusBEtoLEs(uint16_t value) 581060ff233Sopenharmony_ci{ 582060ff233Sopenharmony_ci if (!IsLittleEndian()) { 583060ff233Sopenharmony_ci return value; 584060ff233Sopenharmony_ci } 585060ff233Sopenharmony_ci uint16_t res = value; 586060ff233Sopenharmony_ci ShiftByte((uint8_t *)&res, (int8_t)sizeof(res)); 587060ff233Sopenharmony_ci return res; 588060ff233Sopenharmony_ci}