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 (14) 29 30 #ifdef __aarch64__ 31 #define ADDR_IN_RESER_SIZE (4) 32 #else 33 #define ADDR_IN_RESER_SIZE (8) 34 #endif 35 36 /* sys/socket.h */ 37 #define SOFTBUS_PF_UNSPEC SOFTBUS_PF_UNSPEC_ 38 #define SOFTBUS_AF_UNSPEC SOFTBUS_AF_UNSPEC_ 39 40 #define SOFTBUS_PF_INET SOFTBUS_PF_INET_ 41 #define SOFTBUS_AF_INET SOFTBUS_AF_INET_ 42 43 #define SOFTBUS_PF_NETLINK SOFTBUS_PF_NETLINK_ 44 #define SOFTBUS_AF_NETLINK SOFTBUS_AF_NETLINK_ 45 46 #define SOFTBUS_SOCK_STREAM SOFTBUS_SOCK_STREAM_ 47 #define SOFTBUS_SOCK_DGRAM SOFTBUS_SOCK_DGRAM_ 48 #define SOFTBUS_SOCK_RAW SOFTBUS_SOCK_RAW_ 49 50 #define SOFTBUS_SOCK_CLOEXEC SOFTBUS_SOCK_CLOEXEC_ 51 #define SOFTBUS_SOCK_NONBLOCK SOFTBUS_SOCK_NONBLOCK_ 52 53 #define SOFTBUS_SOL_SOCKET SOFTBUS_SOL_SOCKET_ 54 55 #define SOFTBUS_SO_REUSEADDR SOFTBUS_SO_REUSEADDR_ 56 #define SOFTBUS_SO_RCVBUF SOFTBUS_SO_RCVBUF_ 57 #define SOFTBUS_SO_SNDBUF SOFTBUS_SO_SNDBUF_ 58 #define SOFTBUS_SO_KEEPALIVE SOFTBUS_SO_KEEPALIVE_ 59 #define SOFTBUS_SO_REUSEPORT SOFTBUS_SO_REUSEPORT_ 60 #define SOFTBUS_SO_RCVBUFFORCE SOFTBUS_SO_RCVBUFFORCE_ 61 62 #define SOFTBUS_TCP_KEEPIDLE SOFTBUS_TCP_KEEPIDLE_ 63 #define SOFTBUS_TCP_KEEPINTVL SOFTBUS_TCP_KEEPINTVL_ 64 #define SOFTBUS_TCP_KEEPCNT SOFTBUS_TCP_KEEPCNT_ 65 #define SOFTBUS_TCP_USER_TIMEOUT SOFTBUS_TCP_USER_TIMEOUT_ 66 67 #define SOFTBUS_SHUT_RD SOFTBUS_SHUT_RD_ 68 #define SOFTBUS_SHUT_WR SOFTBUS_SHUT_WR_ 69 #define SOFTBUS_SHUT_RDWR SOFTBUS_SHUT_RDWR_ 70 71 /* netinet/in.h */ 72 #define SOFTBUS_IPPROTO_IP SOFTBUS_IPPROTO_IP_ 73 #define SOFTBUS_IPPROTO_TCP SOFTBUS_IPPROTO_TCP_ 74 75 #define SOFTBUS_IP_TOS SOFTBUS_IP_TOS_ 76 77 /* netinet/tcp.h */ 78 #define SOFTBUS_TCP_NODELAY SOFTBUS_TCP_NODELAY_ 79 80 /* fcntl.h */ 81 #define SOFTBUS_F_GETFL SOFTBUS_F_GETFL_ 82 #define SOFTBUS_F_SETFL SOFTBUS_F_SETFL_ 83 84 #define SOFTBUS_O_NONBLOCK SOFTBUS_O_NONBLOCK_ 85 86 /* select.h */ 87 /* linux support 1024, liteos support 640 */ 88 #define SOFTBUS_FD_SETSIZE SOFTBUS_FD_SETSIZE_ 89 90 typedef SoftBusSysTime SoftBusSockTimeOut; 91 /* netinet/in.h */ 92 typedef struct { 93 uint16_t saFamily; /* address family */ 94 char saData[SA_DATA_SIZE]; 95 } SoftBusSockAddr; 96 97 #pragma pack (1) 98 typedef struct { 99 unsigned long sAddr; 100 } SoftBusInAddr; 101 102 typedef struct { 103 uint16_t sinFamily; /* address family */ 104 uint16_t sinPort; /* Port number */ 105 SoftBusInAddr sinAddr; /* Internet address */ 106 unsigned char sinZero[ADDR_IN_RESER_SIZE]; /* Same size as struct sockaddr */ 107 } SoftBusSockAddrIn; 108 #pragma pack () 109 110 typedef struct { 111 uint32_t fdsCount; 112 unsigned long fdsBits[SOFTBUS_FD_SETSIZE / 8 / sizeof(long)]; 113 } SoftBusFdSet; 114 115 int32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd); 116 int32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName, const void *optVal, int32_t optLen); 117 int32_t SoftBusSocketGetOpt(int32_t socketFd, int32_t level, int32_t optName, void *optVal, int32_t *optLen); 118 int32_t SoftBusSocketGetLocalName(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen); 119 int32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen); 120 121 int32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen); 122 int32_t SoftBusSocketListen(int32_t socketFd, int32_t backLog); 123 int32_t SoftBusSocketAccept(int32_t socketFd, SoftBusSockAddr *addr, int32_t *addrLen, int32_t *acceptFd); 124 int32_t SoftBusSocketConnect(int32_t socketFd, const SoftBusSockAddr *addr, int32_t addrLen); 125 126 void SoftBusSocketFdZero(SoftBusFdSet *set); 127 void SoftBusSocketFdSet(int32_t socketFd, SoftBusFdSet *set); 128 void SoftBusSocketFdClr(int32_t socketFd, SoftBusFdSet *set); 129 int32_t SoftBusSocketFdIsset(int32_t socketFd, SoftBusFdSet *set); 130 131 int32_t SoftBusSocketSelect(int32_t nfds, SoftBusFdSet *readFds, SoftBusFdSet *writeFds, SoftBusFdSet *exceptFds, 132 SoftBusSockTimeOut *timeOut); 133 int32_t SoftBusSocketIoctl(int32_t socketFd, long cmd, void *argp); 134 int32_t SoftBusSocketFcntl(int32_t socketFd, long cmd, long flag); 135 136 int32_t SoftBusSocketSend(int32_t socketFd, const void *buf, uint32_t len, int32_t flags); 137 int32_t SoftBusSocketSendTo(int32_t socketFd, const void *buf, uint32_t len, int32_t flags, 138 const SoftBusSockAddr *toAddr, int32_t toAddrLen); 139 140 int32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags); 141 int32_t SoftBusSocketRecvFrom(int32_t socketFd, void *buf, uint32_t len, int32_t flags, SoftBusSockAddr *fromAddr, 142 int32_t *fromAddrLen); 143 144 145 int32_t SoftBusSocketShutDown(int32_t socketFd, int32_t how); 146 int32_t SoftBusSocketClose(int32_t socketFd); 147 148 int32_t SoftBusInetPtoN(int32_t af, const char *src, void *dst); 149 const char *SoftBusInetNtoP(int32_t af, const void* src, char *dst, int32_t size); 150 151 uint32_t SoftBusHtoNl(uint32_t hostlong); 152 uint16_t SoftBusHtoNs(uint16_t hostshort); 153 uint32_t SoftBusNtoHl(uint32_t netlong); 154 uint16_t SoftBusNtoHs(uint16_t netshort); 155 156 /* host to little-endian */ 157 uint16_t SoftBusHtoLs(uint16_t value); 158 uint32_t SoftBusHtoLl(uint32_t value); 159 uint64_t SoftBusHtoLll(uint64_t value); 160 /* little-endian to host */ 161 uint16_t SoftBusLtoHs(uint16_t value); 162 uint32_t SoftBusLtoHl(uint32_t value); 163 uint64_t SoftBusLtoHll(uint64_t value); 164 uint16_t SoftBusLEtoBEs(uint16_t value); 165 uint16_t SoftBusBEtoLEs(uint16_t value); 166 167 uint32_t SoftBusInetAddr(const char *cp); 168 169 int32_t SoftBusSocketGetError(int32_t socketFd); 170 171 #ifdef __cplusplus 172 #if __cplusplus 173 } 174 #endif /* __cplusplus */ 175 #endif /* __cplusplus */ 176 177 #endif 178