1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_SOCKET_H_
30 #define _SYS_SOCKET_H_
31 
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <linux/socket.h>
35 
36 #include <asm/fcntl.h>
37 #include <asm/socket.h>
38 #include <linux/sockios.h>
39 #include <linux/uio.h>
40 #include <linux/types.h>
41 #include <linux/compiler.h>
42 
43 #include <bits/sa_family_t.h>
44 
45 __BEGIN_DECLS
46 
47 struct timespec;
48 
49 #define SOCK_STREAM     1
50 #define SOCK_DGRAM      2
51 #define SOCK_RAW        3
52 #define SOCK_RDM        4
53 #define SOCK_SEQPACKET  5
54 #define SOCK_DCCP       6
55 #define SOCK_PACKET     10
56 
57 #define SOCK_CLOEXEC O_CLOEXEC
58 #define SOCK_NONBLOCK O_NONBLOCK
59 
60 enum {
61   SHUT_RD = 0,
62 #define SHUT_RD         SHUT_RD
63   SHUT_WR,
64 #define SHUT_WR         SHUT_WR
65   SHUT_RDWR
66 #define SHUT_RDWR       SHUT_RDWR
67 };
68 
69 struct sockaddr {
70   sa_family_t sa_family;
71   char sa_data[14];
72 };
73 
74 struct linger {
75   int l_onoff;
76   int l_linger;
77 };
78 
79 struct msghdr {
80   void* msg_name;
81   socklen_t msg_namelen;
82   struct iovec* msg_iov;
83   size_t msg_iovlen;
84   void* msg_control;
85   size_t msg_controllen;
86   int msg_flags;
87 };
88 
89 struct mmsghdr {
90   struct msghdr msg_hdr;
91   unsigned int msg_len;
92 };
93 
94 struct cmsghdr {
95   size_t cmsg_len;
96   int cmsg_level;
97   int cmsg_type;
98 };
99 
100 #define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr((mhdr), (cmsg))
101 #define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
102 #define CMSG_DATA(cmsg) (((unsigned char*)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
103 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
104 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
105 #define CMSG_FIRSTHDR(msg) \
106   ((msg)->msg_controllen >= sizeof(struct cmsghdr) \
107    ? (struct cmsghdr*) (msg)->msg_control : (struct cmsghdr*) NULL)
108 #define CMSG_OK(mhdr, cmsg) ((cmsg)->cmsg_len >= sizeof(struct cmsghdr) &&   (cmsg)->cmsg_len <= (unsigned long)   ((mhdr)->msg_controllen -   ((char*)(cmsg) - (char*)(mhdr)->msg_control)))
109 
110 #if __ANDROID_API__ >= 21
111 struct cmsghdr* __cmsg_nxthdr(struct msghdr* __msg, struct cmsghdr* __cmsg) __INTRODUCED_IN(21);
112 #else
113 /* TODO(danalbert): Move this into libandroid_support. */
__cmsg_nxthdr(struct msghdr * msg,struct cmsghdr * cmsg)114 static inline struct cmsghdr* __cmsg_nxthdr(struct msghdr* msg, struct cmsghdr* cmsg) {
115   struct cmsghdr* ptr =
116       __BIONIC_CAST(reinterpret_cast, struct cmsghdr*,
117                     (__BIONIC_CAST(reinterpret_cast, char*, cmsg) + CMSG_ALIGN(cmsg->cmsg_len)));
118   size_t len = __BIONIC_CAST(reinterpret_cast, char*, ptr + 1) -
119                __BIONIC_CAST(reinterpret_cast, char*, msg->msg_control);
120   if (len > msg->msg_controllen) {
121     return NULL;
122   }
123   return ptr;
124 }
125 #endif
126 
127 #define SCM_RIGHTS 0x01
128 #define SCM_CREDENTIALS 0x02
129 #define SCM_SECURITY 0x03
130 
131 struct ucred {
132   pid_t pid;
133   uid_t uid;
134   gid_t gid;
135 };
136 
137 #define AF_UNSPEC 0
138 #define AF_UNIX 1
139 #define AF_LOCAL 1
140 #define AF_INET 2
141 #define AF_AX25 3
142 #define AF_IPX 4
143 #define AF_APPLETALK 5
144 #define AF_NETROM 6
145 #define AF_BRIDGE 7
146 #define AF_ATMPVC 8
147 #define AF_X25 9
148 #define AF_INET6 10
149 #define AF_ROSE 11
150 #define AF_DECnet 12
151 #define AF_NETBEUI 13
152 #define AF_SECURITY 14
153 #define AF_KEY 15
154 #define AF_NETLINK 16
155 #define AF_ROUTE AF_NETLINK
156 #define AF_PACKET 17
157 #define AF_ASH 18
158 #define AF_ECONET 19
159 #define AF_ATMSVC 20
160 #define AF_RDS 21
161 #define AF_SNA 22
162 #define AF_IRDA 23
163 #define AF_PPPOX 24
164 #define AF_WANPIPE 25
165 #define AF_LLC 26
166 #define AF_CAN 29
167 #define AF_TIPC 30
168 #define AF_BLUETOOTH 31
169 #define AF_IUCV 32
170 #define AF_RXRPC 33
171 #define AF_ISDN 34
172 #define AF_PHONET 35
173 #define AF_IEEE802154 36
174 #define AF_CAIF 37
175 #define AF_ALG 38
176 #define AF_NFC 39
177 #define AF_VSOCK 40
178 #define AF_KCM 41
179 #define AF_QIPCRTR 42
180 #define AF_MAX 43
181 
182 #define PF_UNSPEC AF_UNSPEC
183 #define PF_UNIX AF_UNIX
184 #define PF_LOCAL AF_LOCAL
185 #define PF_INET AF_INET
186 #define PF_AX25 AF_AX25
187 #define PF_IPX AF_IPX
188 #define PF_APPLETALK AF_APPLETALK
189 #define PF_NETROM AF_NETROM
190 #define PF_BRIDGE AF_BRIDGE
191 #define PF_ATMPVC AF_ATMPVC
192 #define PF_X25 AF_X25
193 #define PF_INET6 AF_INET6
194 #define PF_ROSE AF_ROSE
195 #define PF_DECnet AF_DECnet
196 #define PF_NETBEUI AF_NETBEUI
197 #define PF_SECURITY AF_SECURITY
198 #define PF_KEY AF_KEY
199 #define PF_NETLINK AF_NETLINK
200 #define PF_ROUTE AF_ROUTE
201 #define PF_PACKET AF_PACKET
202 #define PF_ASH AF_ASH
203 #define PF_ECONET AF_ECONET
204 #define PF_ATMSVC AF_ATMSVC
205 #define PF_RDS AF_RDS
206 #define PF_SNA AF_SNA
207 #define PF_IRDA AF_IRDA
208 #define PF_PPPOX AF_PPPOX
209 #define PF_WANPIPE AF_WANPIPE
210 #define PF_LLC AF_LLC
211 #define PF_CAN AF_CAN
212 #define PF_TIPC AF_TIPC
213 #define PF_BLUETOOTH AF_BLUETOOTH
214 #define PF_IUCV AF_IUCV
215 #define PF_RXRPC AF_RXRPC
216 #define PF_ISDN AF_ISDN
217 #define PF_PHONET AF_PHONET
218 #define PF_IEEE802154 AF_IEEE802154
219 #define PF_CAIF AF_CAIF
220 #define PF_ALG AF_ALG
221 #define PF_NFC AF_NFC
222 #define PF_VSOCK AF_VSOCK
223 #define PF_KCM AF_KCM
224 #define PF_QIPCRTR AF_QIPCRTR
225 #define PF_MAX AF_MAX
226 
227 #define SOMAXCONN 128
228 
229 #define MSG_OOB 1
230 #define MSG_PEEK 2
231 #define MSG_DONTROUTE 4
232 #define MSG_TRYHARD 4
233 #define MSG_CTRUNC 8
234 #define MSG_PROBE 0x10
235 #define MSG_TRUNC 0x20
236 #define MSG_DONTWAIT 0x40
237 #define MSG_EOR 0x80
238 #define MSG_WAITALL 0x100
239 #define MSG_FIN 0x200
240 #define MSG_SYN 0x400
241 #define MSG_CONFIRM 0x800
242 #define MSG_RST 0x1000
243 #define MSG_ERRQUEUE 0x2000
244 #define MSG_NOSIGNAL 0x4000
245 #define MSG_MORE 0x8000
246 #define MSG_WAITFORONE 0x10000
247 #define MSG_BATCH 0x40000
248 #define MSG_FASTOPEN 0x20000000
249 #define MSG_CMSG_CLOEXEC 0x40000000
250 #define MSG_EOF MSG_FIN
251 #define MSG_CMSG_COMPAT 0
252 
253 #define SOL_IP 0
254 #define SOL_TCP 6
255 #define SOL_UDP 17
256 #define SOL_IPV6 41
257 #define SOL_ICMPV6 58
258 #define SOL_SCTP 132
259 #define SOL_RAW 255
260 #define SOL_IPX 256
261 #define SOL_AX25 257
262 #define SOL_ATALK 258
263 #define SOL_NETROM 259
264 #define SOL_ROSE 260
265 #define SOL_DECNET 261
266 #define SOL_X25 262
267 #define SOL_PACKET 263
268 #define SOL_ATM 264
269 #define SOL_AAL 265
270 #define SOL_IRDA 266
271 #define SOL_NETBEUI 267
272 #define SOL_LLC 268
273 #define SOL_DCCP 269
274 #define SOL_NETLINK 270
275 #define SOL_TIPC 271
276 #define SOL_RXRPC 272
277 #define SOL_PPPOL2TP 273
278 #define SOL_BLUETOOTH 274
279 #define SOL_PNPIPE 275
280 #define SOL_RDS 276
281 #define SOL_IUCV 277
282 #define SOL_CAIF 278
283 #define SOL_ALG 279
284 #define SOL_NFC 280
285 #define SOL_KCM 281
286 #define SOL_TLS 282
287 
288 #define IPX_TYPE 1
289 
290 #ifdef __i386__
291 # define __socketcall extern __attribute__((__cdecl__))
292 #else
293 # define __socketcall extern
294 #endif
295 
296 __socketcall int accept(int __fd, struct sockaddr* __addr, socklen_t* __addr_length);
297 __socketcall int accept4(int __fd, struct sockaddr* __addr, socklen_t* __addr_length, int __flags) __INTRODUCED_IN(21);
298 __socketcall int bind(int __fd, const struct sockaddr* __addr, socklen_t __addr_length);
299 __socketcall int connect(int __fd, const struct sockaddr* __addr, socklen_t __addr_length);
300 __socketcall int getpeername(int __fd, struct sockaddr* __addr, socklen_t* __addr_length);
301 __socketcall int getsockname(int __fd, struct sockaddr* __addr, socklen_t* __addr_length);
302 __socketcall int getsockopt(int __fd, int __level, int __option, void* __value, socklen_t* __value_length);
303 __socketcall int listen(int __fd, int __backlog);
304 __socketcall int recvmmsg(int __fd, struct mmsghdr* __msgs, unsigned int __msg_count, int __flags, const struct timespec* __timeout)
305   __INTRODUCED_IN(21);
306 __socketcall ssize_t recvmsg(int __fd, struct msghdr* __msg, int __flags);
307 __socketcall int sendmmsg(int __fd, const struct mmsghdr* __msgs, unsigned int __msg_count, int __flags) __INTRODUCED_IN(21);
308 __socketcall ssize_t sendmsg(int __fd, const struct msghdr* __msg, int __flags);
309 __socketcall int setsockopt(int __fd, int __level, int __option, const void* __value, socklen_t __value_length);
310 __socketcall int shutdown(int __fd, int __how);
311 __socketcall int socket(int __af, int __type, int __protocol);
312 __socketcall int socketpair(int __af, int __type, int __protocol, int __fds[2]);
313 
314 ssize_t recv(int __fd, void* __buf, size_t __n, int __flags);
315 ssize_t send(int __fd, const void* __buf, size_t __n, int __flags);
316 
317 __socketcall ssize_t sendto(int __fd, const void* __buf, size_t __n, int __flags, const struct sockaddr* __dst_addr, socklen_t __dst_addr_length);
318 __socketcall ssize_t recvfrom(int __fd, void* __buf, size_t __n, int __flags, struct sockaddr* __src_addr, socklen_t* __src_addr_length);
319 
320 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
321 #include <bits/fortify/socket.h>
322 #endif
323 
324 #undef __socketcall
325 
326 __END_DECLS
327 
328 #endif
329