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 #include <sys/uio.h> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 typedef unsigned socklen_t; 43 typedef unsigned short sa_family_t; 44 45 struct msghdr { 46 void *msg_name; 47 socklen_t msg_namelen; 48 struct iovec *msg_iov; 49 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN 50 int __pad1; 51 #endif 52 int msg_iovlen; 53 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN 54 int __pad1; 55 #endif 56 void *msg_control; 57 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN 58 int __pad2; 59 #endif 60 socklen_t msg_controllen; 61 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN 62 int __pad2; 63 #endif 64 int msg_flags; 65 }; 66 67 struct cmsghdr { 68 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN 69 int __pad1; 70 #endif 71 socklen_t cmsg_len; 72 #if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN 73 int __pad1; 74 #endif 75 int cmsg_level; 76 int cmsg_type; 77 }; 78 79 #ifdef _GNU_SOURCE 80 struct ucred { 81 pid_t pid; 82 uid_t uid; 83 gid_t gid; 84 }; 85 86 struct mmsghdr { 87 struct msghdr msg_hdr; 88 unsigned int msg_len; 89 }; 90 91 struct timespec; 92 93 int sendmmsg (int, struct mmsghdr *, unsigned int, unsigned int); 94 int recvmmsg (int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *); 95 #endif 96 97 struct linger { 98 int l_onoff; 99 int l_linger; 100 }; 101 102 #define SHUT_RD 0 103 #define SHUT_WR 1 104 #define SHUT_RDWR 2 105 106 #ifndef SOCK_STREAM 107 #define SOCK_STREAM 1 108 #define SOCK_DGRAM 2 109 #endif 110 111 #define SOCK_RAW 3 112 #define SOCK_RDM 4 113 #define SOCK_SEQPACKET 5 114 #define SOCK_DCCP 6 115 #define SOCK_PACKET 10 116 117 #ifndef SOCK_CLOEXEC 118 #define SOCK_CLOEXEC 02000000 119 #define SOCK_NONBLOCK 04000 120 #endif 121 122 #define PF_UNSPEC 0 123 #define PF_LOCAL 1 124 #define PF_UNIX PF_LOCAL 125 #define PF_FILE PF_LOCAL 126 #define PF_INET 2 127 #define PF_INET6 10 128 #define PF_IPX 4 129 130 #define AF_UNSPEC PF_UNSPEC 131 #define AF_LOCAL PF_LOCAL 132 #define AF_UNIX AF_LOCAL 133 #define AF_FILE AF_LOCAL 134 #define AF_INET PF_INET 135 #define AF_INET6 PF_INET6 136 #define AF_IPX PF_IPX 137 138 #ifndef SO_DEBUG 139 #define SO_DEBUG 1 140 #define SO_REUSEADDR 2 141 #define SO_TYPE 3 142 #define SO_ERROR 4 143 #define SO_DONTROUTE 5 144 #define SO_BROADCAST 6 145 #define SO_SNDBUF 7 146 #define SO_RCVBUF 8 147 #define SO_KEEPALIVE 9 148 #define SO_OOBINLINE 10 149 #define SO_NO_CHECK 11 150 #define SO_PRIORITY 12 151 #define SO_LINGER 13 152 #define SO_BSDCOMPAT 14 153 #define SO_REUSEPORT 15 154 #define SO_PASSCRED 16 155 #define SO_PEERCRED 17 156 #define SO_RCVLOWAT 18 157 #define SO_SNDLOWAT 19 158 #define SO_ACCEPTCONN 30 159 #define SO_PEERSEC 31 160 #define SO_SNDBUFFORCE 32 161 #define SO_RCVBUFFORCE 33 162 #define SO_PROTOCOL 38 163 #define SO_DOMAIN 39 164 #endif 165 166 #ifndef SO_RCVTIMEO 167 #if LONG_MAX == 0x7fffffff 168 #define SO_RCVTIMEO 66 169 #define SO_SNDTIMEO 67 170 #else 171 #define SO_RCVTIMEO 20 172 #define SO_SNDTIMEO 21 173 #endif 174 #endif 175 176 #define SO_BINDTODEVICE 25 177 178 #ifndef SOL_SOCKET 179 #define SOL_SOCKET 1 180 #endif 181 #define SOL_IP 0 182 183 #define MSG_OOB 0x0001 184 #define MSG_PEEK 0x0002 185 #define MSG_DONTROUTE 0x0004 186 #define MSG_CTRUNC 0x0008 187 #define MSG_PROXY 0x0010 188 #define MSG_TRUNC 0x0020 189 #define MSG_DONTWAIT 0x0040 190 #define MSG_EOR 0x0080 191 #define MSG_WAITALL 0x0100 192 #define MSG_FIN 0x0200 193 #define MSG_SYN 0x0400 194 #define MSG_CONFIRM 0x0800 195 #define MSG_RST 0x1000 196 #define MSG_ERRQUEUE 0x2000 197 #define MSG_NOSIGNAL 0x4000 198 #define MSG_MORE 0x8000 199 #define MSG_WAITFORONE 0x10000 200 #define MSG_BATCH 0x40000 201 #define MSG_ZEROCOPY 0x4000000 202 #define MSG_FASTOPEN 0x20000000 203 #define MSG_CMSG_CLOEXEC 0x40000000 204 205 #define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) 206 #define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) 207 #define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) 208 209 #define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) 210 #define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ 211 __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ 212 ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) 213 #define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) 214 215 #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) 216 #define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) 217 #define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) 218 219 #define SCM_RIGHTS 0x01 220 #define SCM_CREDENTIALS 0x02 221 222 struct sockaddr { 223 sa_family_t sa_family; 224 char sa_data[14]; 225 }; 226 227 struct sockaddr_storage { 228 sa_family_t ss_family; 229 char __ss_padding[128 - sizeof(long) - sizeof(sa_family_t)]; 230 unsigned long __ss_align; 231 }; 232 233 int socket(int, int, int); 234 int socketpair(int, int, int, int [2]); 235 236 int shutdown(int, int); 237 238 int bind(int, const struct sockaddr *, socklen_t); 239 int connect(int, const struct sockaddr *, socklen_t); 240 int listen(int, int); 241 int accept(int, struct sockaddr *__restrict, socklen_t *__restrict); 242 int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); 243 244 int getsockname(int, struct sockaddr *__restrict, socklen_t *__restrict); 245 int getpeername(int, struct sockaddr *__restrict, socklen_t *__restrict); 246 247 ssize_t send(int, const void *, size_t, int); 248 ssize_t recv(int, void *, size_t, int); 249 ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t); 250 ssize_t recvfrom(int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict); 251 ssize_t sendmsg(int, const struct msghdr *, int); 252 ssize_t recvmsg(int, struct msghdr *, int); 253 254 int getsockopt(int, int, int, void *__restrict, socklen_t *__restrict); 255 int setsockopt(int, int, int, const void *, socklen_t); 256 257 int sockatmark(int); 258 259 #ifdef __cplusplus 260 } 261 #endif 262 263 #endif /* !_ADAPT_SYS_SOCKET_H */ 264