1 /* 2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of 8 * conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef _ADAPT_SYS_SOCKET_H 32 #define _ADAPT_SYS_SOCKET_H 33 34 #include <sys/features.h> 35 #include <sys/types.h> 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 typedef unsigned socklen_t; 42 typedef unsigned short sa_family_t; 43 44 struct iovec { 45 void *iov_base; 46 size_t iov_len; 47 }; 48 49 struct msghdr { 50 void *msg_name; 51 socklen_t msg_namelen; 52 struct iovec *msg_iov; 53 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN 54 int __pad1; 55 #endif 56 int msg_iovlen; 57 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN 58 int __pad1; 59 #endif 60 void *msg_control; 61 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN 62 int __pad2; 63 #endif 64 socklen_t msg_controllen; 65 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN 66 int __pad2; 67 #endif 68 int msg_flags; 69 }; 70 71 struct cmsghdr { 72 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN 73 int __pad1; 74 #endif 75 socklen_t cmsg_len; 76 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN 77 int __pad1; 78 #endif 79 int cmsg_level; 80 int cmsg_type; 81 }; 82 83 #ifdef _GNU_SOURCE 84 struct ucred { 85 pid_t pid; 86 uid_t uid; 87 gid_t gid; 88 }; 89 90 struct mmsghdr { 91 struct msghdr msg_hdr; 92 unsigned int msg_len; 93 }; 94 95 struct timespec; 96 97 int sendmmsg (int, struct mmsghdr *, unsigned int, unsigned int); 98 int recvmmsg (int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *); 99 #endif 100 101 struct linger { 102 int l_onoff; 103 int l_linger; 104 }; 105 106 #define SHUT_RD 0 107 #define SHUT_WR 1 108 #define SHUT_RDWR 2 109 110 #ifndef SOCK_STREAM 111 #define SOCK_STREAM 1 112 #define SOCK_DGRAM 2 113 #endif 114 115 #define SOCK_RAW 3 116 #define SOCK_RDM 4 117 #define SOCK_SEQPACKET 5 118 #define SOCK_DCCP 6 119 #define SOCK_PACKET 10 120 121 #ifndef SOCK_CLOEXEC 122 #define SOCK_CLOEXEC 02000000 123 #define SOCK_NONBLOCK 04000 124 #endif 125 126 #define PF_UNSPEC 0 127 #define PF_LOCAL 1 128 #define PF_UNIX PF_LOCAL 129 #define PF_FILE PF_LOCAL 130 #define PF_INET 2 131 #define PF_INET6 10 132 #define PF_IPX 4 133 134 #define AF_UNSPEC PF_UNSPEC 135 #define AF_LOCAL PF_LOCAL 136 #define AF_UNIX AF_LOCAL 137 #define AF_FILE AF_LOCAL 138 #define AF_INET PF_INET 139 #define AF_INET6 PF_INET6 140 #define AF_IPX PF_IPX 141 142 #ifndef SO_DEBUG 143 #define SO_DEBUG 1 144 #define SO_REUSEADDR 2 145 #define SO_TYPE 3 146 #define SO_ERROR 4 147 #define SO_DONTROUTE 5 148 #define SO_BROADCAST 6 149 #define SO_SNDBUF 7 150 #define SO_RCVBUF 8 151 #define SO_KEEPALIVE 9 152 #define SO_OOBINLINE 10 153 #define SO_NO_CHECK 11 154 #define SO_PRIORITY 12 155 #define SO_LINGER 13 156 #define SO_BSDCOMPAT 14 157 #define SO_REUSEPORT 15 158 #define SO_PASSCRED 16 159 #define SO_PEERCRED 17 160 #define SO_RCVLOWAT 18 161 #define SO_SNDLOWAT 19 162 #define SO_ACCEPTCONN 30 163 #define SO_PEERSEC 31 164 #define SO_SNDBUFFORCE 32 165 #define SO_RCVBUFFORCE 33 166 #define SO_PROTOCOL 38 167 #define SO_DOMAIN 39 168 #endif 169 170 #ifndef SO_RCVTIMEO 171 #if LONG_MAX == 0x7fffffff 172 #define SO_RCVTIMEO 66 173 #define SO_SNDTIMEO 67 174 #else 175 #define SO_RCVTIMEO 20 176 #define SO_SNDTIMEO 21 177 #endif 178 #endif 179 180 #define SO_BINDTODEVICE 25 181 182 #ifndef SOL_SOCKET 183 #define SOL_SOCKET 1 184 #endif 185 #define SOL_IP 0 186 187 #define MSG_OOB 0x0001 188 #define MSG_PEEK 0x0002 189 #define MSG_DONTROUTE 0x0004 190 #define MSG_CTRUNC 0x0008 191 #define MSG_PROXY 0x0010 192 #define MSG_TRUNC 0x0020 193 #define MSG_DONTWAIT 0x0040 194 #define MSG_EOR 0x0080 195 #define MSG_WAITALL 0x0100 196 #define MSG_FIN 0x0200 197 #define MSG_SYN 0x0400 198 #define MSG_CONFIRM 0x0800 199 #define MSG_RST 0x1000 200 #define MSG_ERRQUEUE 0x2000 201 #define MSG_NOSIGNAL 0x4000 202 #define MSG_MORE 0x8000 203 #define MSG_WAITFORONE 0x10000 204 #define MSG_BATCH 0x40000 205 #define MSG_ZEROCOPY 0x4000000 206 #define MSG_FASTOPEN 0x20000000 207 #define MSG_CMSG_CLOEXEC 0x40000000 208 209 #define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) 210 #define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) 211 #define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) 212 213 #define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) 214 #define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ 215 __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ 216 ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) 217 #define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) 218 219 #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) 220 #define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) 221 #define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) 222 223 #define SCM_RIGHTS 0x01 224 #define SCM_CREDENTIALS 0x02 225 226 struct sockaddr { 227 sa_family_t sa_family; 228 char sa_data[14]; 229 }; 230 231 struct sockaddr_storage { 232 sa_family_t ss_family; 233 char __ss_padding[128 - sizeof(long) - sizeof(sa_family_t)]; 234 unsigned long __ss_align; 235 }; 236 237 int socket(int, int, int); 238 int socketpair(int, int, int, int [2]); 239 240 int shutdown(int, int); 241 242 int bind(int, const struct sockaddr *, socklen_t); 243 int connect(int, const struct sockaddr *, socklen_t); 244 int listen(int, int); 245 int accept(int, struct sockaddr *__restrict, socklen_t *__restrict); 246 int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); 247 248 int getsockname(int, struct sockaddr *__restrict, socklen_t *__restrict); 249 int getpeername(int, struct sockaddr *__restrict, socklen_t *__restrict); 250 251 ssize_t send(int, const void *, size_t, int); 252 ssize_t recv(int, void *, size_t, int); 253 ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t); 254 ssize_t recvfrom(int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict); 255 ssize_t sendmsg(int, const struct msghdr *, int); 256 ssize_t recvmsg(int, struct msghdr *, int); 257 258 int getsockopt(int, int, int, void *__restrict, socklen_t *__restrict); 259 int setsockopt(int, int, int, const void *, socklen_t); 260 261 int sockatmark(int); 262 263 #ifdef __cplusplus 264 } 265 #endif 266 267 #endif /* !_ADAPT_SYS_SOCKET_H */ 268