blob: f9f6764556210a2925eecff31eb919b98f04c4af [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030028
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#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
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030036#include <asm/fcntl.h>
Elliott Hughes86ec05a2012-09-11 19:03:02 -070037#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
Elliott Hughes508d2922016-07-21 16:38:43 -070043#include <bits/sa_family_t.h>
44
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045__BEGIN_DECLS
46
Elliott Hughes86ec05a2012-09-11 19:03:02 -070047#define sockaddr_storage __kernel_sockaddr_storage
Elliott Hughes86ec05a2012-09-11 19:03:02 -070048
Elliott Hughesa8b1eb32014-02-28 17:59:29 -080049struct timespec;
50
Raghu Gandham6437eac2012-08-02 16:50:10 -070051#ifdef __mips__
52#define SOCK_DGRAM 1
53#define SOCK_STREAM 2
Greg Hackmannd75abb92015-08-25 17:45:46 -070054#else
55#define SOCK_STREAM 1
56#define SOCK_DGRAM 2
57#endif
Raghu Gandham6437eac2012-08-02 16:50:10 -070058#define SOCK_RAW 3
59#define SOCK_RDM 4
60#define SOCK_SEQPACKET 5
61#define SOCK_DCCP 6
62#define SOCK_PACKET 10
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030064#define SOCK_CLOEXEC O_CLOEXEC
65#define SOCK_NONBLOCK O_NONBLOCK
66
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067enum {
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030068 SHUT_RD = 0,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069#define SHUT_RD SHUT_RD
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030070 SHUT_WR,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071#define SHUT_WR SHUT_WR
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030072 SHUT_RDWR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#define SHUT_RDWR SHUT_RDWR
74};
75
Elliott Hughes86ec05a2012-09-11 19:03:02 -070076struct sockaddr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010077 sa_family_t sa_family;
78 char sa_data[14];
Elliott Hughes86ec05a2012-09-11 19:03:02 -070079};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
Elliott Hughes86ec05a2012-09-11 19:03:02 -070081struct linger {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010082 int l_onoff;
83 int l_linger;
Elliott Hughes86ec05a2012-09-11 19:03:02 -070084};
85
86struct msghdr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010087 void* msg_name;
88 socklen_t msg_namelen;
89 struct iovec* msg_iov;
90 size_t msg_iovlen;
91 void* msg_control;
92 size_t msg_controllen;
93 int msg_flags;
94};
95
96struct mmsghdr {
97 struct msghdr msg_hdr;
98 unsigned int msg_len;
Elliott Hughes86ec05a2012-09-11 19:03:02 -070099};
100
101struct cmsghdr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100102 size_t cmsg_len;
103 int cmsg_level;
104 int cmsg_type;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700105};
106
Calin Juravle096b4eb2014-06-19 18:16:17 +0100107#define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr((mhdr), (cmsg))
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700108#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
Dan Albert268af262015-09-11 09:47:27 -0700109#define CMSG_DATA(cmsg) (((unsigned char*)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700110#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
111#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
Calin Juravlef1de90b2014-03-19 17:40:23 +0000112#define CMSG_FIRSTHDR(msg) \
113 ((msg)->msg_controllen >= sizeof(struct cmsghdr) \
114 ? (struct cmsghdr*) (msg)->msg_control : (struct cmsghdr*) NULL)
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100115#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)))
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700116
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800117#if __ANDROID_API__ >= __ANDROID_API_L__
Josh Gao14adff12016-04-29 12:00:55 -0700118struct cmsghdr* __cmsg_nxthdr(struct msghdr*, struct cmsghdr*) __INTRODUCED_IN(21);
Dan Albert11f0e102016-09-08 16:12:09 -0700119#else
120/* TODO(danalbert): Move this into libandroid_support. */
121static inline struct cmsghdr* __cmsg_nxthdr(struct msghdr* msg, struct cmsghdr* cmsg) {
122 struct cmsghdr* ptr =
123 __BIONIC_CAST(reinterpret_cast, struct cmsghdr*,
124 (__BIONIC_CAST(reinterpret_cast, char*, cmsg) + CMSG_ALIGN(cmsg->cmsg_len)));
125 size_t len = __BIONIC_CAST(reinterpret_cast, char*, ptr + 1) -
126 __BIONIC_CAST(reinterpret_cast, char*, msg->msg_control);
127 if (len > msg->msg_controllen) {
128 return NULL;
129 }
130 return ptr;
131}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800132#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700133
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700134#define SCM_RIGHTS 0x01
135#define SCM_CREDENTIALS 0x02
136#define SCM_SECURITY 0x03
137
138struct ucred {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100139 pid_t pid;
140 uid_t uid;
141 gid_t gid;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700142};
143
144#define AF_UNSPEC 0
145#define AF_UNIX 1
146#define AF_LOCAL 1
147#define AF_INET 2
148#define AF_AX25 3
149#define AF_IPX 4
150#define AF_APPLETALK 5
151#define AF_NETROM 6
152#define AF_BRIDGE 7
153#define AF_ATMPVC 8
154#define AF_X25 9
155#define AF_INET6 10
156#define AF_ROSE 11
157#define AF_DECnet 12
158#define AF_NETBEUI 13
159#define AF_SECURITY 14
160#define AF_KEY 15
161#define AF_NETLINK 16
162#define AF_ROUTE AF_NETLINK
163#define AF_PACKET 17
164#define AF_ASH 18
165#define AF_ECONET 19
166#define AF_ATMSVC 20
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800167#define AF_RDS 21
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700168#define AF_SNA 22
169#define AF_IRDA 23
170#define AF_PPPOX 24
171#define AF_WANPIPE 25
172#define AF_LLC 26
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800173#define AF_CAN 29
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700174#define AF_TIPC 30
175#define AF_BLUETOOTH 31
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800176#define AF_IUCV 32
177#define AF_RXRPC 33
178#define AF_ISDN 34
179#define AF_PHONET 35
180#define AF_IEEE802154 36
181#define AF_CAIF 37
182#define AF_ALG 38
Greg Hackmannc7ca8f32016-01-28 11:07:26 -0800183#define AF_NFC 39
184#define AF_VSOCK 40
Elliott Hughes8d768712016-12-13 15:29:58 -0800185#define AF_KCM 41
186#define AF_QIPCRTR 42
187#define AF_MAX 43
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700188
189#define PF_UNSPEC AF_UNSPEC
190#define PF_UNIX AF_UNIX
191#define PF_LOCAL AF_LOCAL
192#define PF_INET AF_INET
193#define PF_AX25 AF_AX25
194#define PF_IPX AF_IPX
195#define PF_APPLETALK AF_APPLETALK
196#define PF_NETROM AF_NETROM
197#define PF_BRIDGE AF_BRIDGE
198#define PF_ATMPVC AF_ATMPVC
199#define PF_X25 AF_X25
200#define PF_INET6 AF_INET6
201#define PF_ROSE AF_ROSE
202#define PF_DECnet AF_DECnet
203#define PF_NETBEUI AF_NETBEUI
204#define PF_SECURITY AF_SECURITY
205#define PF_KEY AF_KEY
206#define PF_NETLINK AF_NETLINK
207#define PF_ROUTE AF_ROUTE
208#define PF_PACKET AF_PACKET
209#define PF_ASH AF_ASH
210#define PF_ECONET AF_ECONET
211#define PF_ATMSVC AF_ATMSVC
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800212#define PF_RDS AF_RDS
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700213#define PF_SNA AF_SNA
214#define PF_IRDA AF_IRDA
215#define PF_PPPOX AF_PPPOX
216#define PF_WANPIPE AF_WANPIPE
217#define PF_LLC AF_LLC
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800218#define PF_CAN AF_CAN
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700219#define PF_TIPC AF_TIPC
220#define PF_BLUETOOTH AF_BLUETOOTH
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800221#define PF_IUCV AF_IUCV
222#define PF_RXRPC AF_RXRPC
223#define PF_ISDN AF_ISDN
224#define PF_PHONET AF_PHONET
225#define PF_IEEE802154 AF_IEEE802154
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700226#define PF_CAIF AF_CAIF
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800227#define PF_ALG AF_ALG
Greg Hackmannc7ca8f32016-01-28 11:07:26 -0800228#define PF_NFC AF_NFC
229#define PF_VSOCK AF_VSOCK
Elliott Hughes8d768712016-12-13 15:29:58 -0800230#define PF_KCM AF_KCM
231#define PF_QIPCRTR AF_QIPCRTR
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700232#define PF_MAX AF_MAX
233
234#define SOMAXCONN 128
235
236#define MSG_OOB 1
237#define MSG_PEEK 2
238#define MSG_DONTROUTE 4
239#define MSG_TRYHARD 4
240#define MSG_CTRUNC 8
241#define MSG_PROBE 0x10
242#define MSG_TRUNC 0x20
243#define MSG_DONTWAIT 0x40
244#define MSG_EOR 0x80
245#define MSG_WAITALL 0x100
246#define MSG_FIN 0x200
247#define MSG_SYN 0x400
248#define MSG_CONFIRM 0x800
249#define MSG_RST 0x1000
250#define MSG_ERRQUEUE 0x2000
251#define MSG_NOSIGNAL 0x4000
252#define MSG_MORE 0x8000
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100253#define MSG_WAITFORONE 0x10000
Elliott Hughes8d768712016-12-13 15:29:58 -0800254#define MSG_BATCH 0x40000
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100255#define MSG_FASTOPEN 0x20000000
256#define MSG_CMSG_CLOEXEC 0x40000000
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700257#define MSG_EOF MSG_FIN
258#define MSG_CMSG_COMPAT 0
259
260#define SOL_IP 0
261#define SOL_TCP 6
262#define SOL_UDP 17
263#define SOL_IPV6 41
264#define SOL_ICMPV6 58
265#define SOL_SCTP 132
266#define SOL_RAW 255
267#define SOL_IPX 256
268#define SOL_AX25 257
269#define SOL_ATALK 258
270#define SOL_NETROM 259
271#define SOL_ROSE 260
272#define SOL_DECNET 261
273#define SOL_X25 262
274#define SOL_PACKET 263
275#define SOL_ATM 264
276#define SOL_AAL 265
277#define SOL_IRDA 266
278#define SOL_NETBEUI 267
279#define SOL_LLC 268
280#define SOL_DCCP 269
281#define SOL_NETLINK 270
282#define SOL_TIPC 271
Elliott Hughes8d768712016-12-13 15:29:58 -0800283#define SOL_RXRPC 272
284#define SOL_PPPOL2TP 273
285#define SOL_BLUETOOTH 274
286#define SOL_PNPIPE 275
287#define SOL_RDS 276
288#define SOL_IUCV 277
289#define SOL_CAIF 278
290#define SOL_ALG 279
291#define SOL_NFC 280
292#define SOL_KCM 281
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700293
294#define IPX_TYPE 1
295
296#ifdef __i386__
297# define __socketcall extern __attribute__((__cdecl__))
298#else
299# define __socketcall extern
300#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100302__socketcall int accept(int, struct sockaddr*, socklen_t*);
Josh Gao14adff12016-04-29 12:00:55 -0700303__socketcall int accept4(int, struct sockaddr*, socklen_t*, int) __INTRODUCED_IN(21);
Elliott Hughes2b9605f2016-08-16 13:37:24 -0700304__socketcall int bind(int, const struct sockaddr*, socklen_t);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100305__socketcall int connect(int, const struct sockaddr*, socklen_t);
306__socketcall int getpeername(int, struct sockaddr*, socklen_t*);
307__socketcall int getsockname(int, struct sockaddr*, socklen_t*);
308__socketcall int getsockopt(int, int, int, void*, socklen_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800309__socketcall int listen(int, int);
Josh Gao14adff12016-04-29 12:00:55 -0700310__socketcall int recvmmsg(int, struct mmsghdr*, unsigned int, int, const struct timespec*)
311 __INTRODUCED_IN(21);
Elliott Hughes2b9605f2016-08-16 13:37:24 -0700312__socketcall ssize_t recvmsg(int, struct msghdr*, int);
Josh Gao14adff12016-04-29 12:00:55 -0700313__socketcall int sendmmsg(int, const struct mmsghdr*, unsigned int, int) __INTRODUCED_IN(21);
Elliott Hughes2b9605f2016-08-16 13:37:24 -0700314__socketcall ssize_t sendmsg(int, const struct msghdr*, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100315__socketcall int setsockopt(int, int, int, const void*, socklen_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316__socketcall int shutdown(int, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100317__socketcall int socket(int, int, int);
318__socketcall int socketpair(int, int, int, int*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700320ssize_t recv(int, void*, size_t, int);
Elliott Hughes2b9605f2016-08-16 13:37:24 -0700321ssize_t send(int, const void*, size_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100323__socketcall ssize_t sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
Elliott Hughes8197aca2016-08-12 09:20:07 -0700324__socketcall ssize_t recvfrom(int, void*, size_t, int, struct sockaddr*, socklen_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700326__errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
Elliott Hughes8197aca2016-08-12 09:20:07 -0700327ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, struct sockaddr*, socklen_t*)
Josh Gao14adff12016-04-29 12:00:55 -0700328 __INTRODUCED_IN(21);
Elliott Hughes8197aca2016-08-12 09:20:07 -0700329ssize_t __recvfrom_real(int, void*, size_t, int, struct sockaddr*, socklen_t*) __RENAME(recvfrom);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700330
Dan Albert658727e2014-10-07 11:10:36 -0700331#if defined(__BIONIC_FORTIFY)
332
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800333#if __ANDROID_API__ >= __ANDROID_API_N__
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700334__BIONIC_FORTIFY_INLINE
Elliott Hughes8197aca2016-08-12 09:20:07 -0700335ssize_t recvfrom(int fd, void* buf, size_t len, int flags, struct sockaddr* src_addr, socklen_t* addr_len) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700336 size_t bos = __bos0(buf);
337
338#if !defined(__clang__)
339 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes247dc912014-03-10 17:17:01 -0700340 return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700341 }
342
343 if (__builtin_constant_p(len) && (len <= bos)) {
Elliott Hughes247dc912014-03-10 17:17:01 -0700344 return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700345 }
346
347 if (__builtin_constant_p(len) && (len > bos)) {
348 __recvfrom_error();
349 }
350#endif
351
Elliott Hughes247dc912014-03-10 17:17:01 -0700352 return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700353}
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800354#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700355
356__BIONIC_FORTIFY_INLINE
Elliott Hughes247dc912014-03-10 17:17:01 -0700357ssize_t recv(int socket, void* buf, size_t len, int flags) {
358 return recvfrom(socket, buf, len, flags, NULL, 0);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700359}
360
361#endif /* __BIONIC_FORTIFY */
362
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363#undef __socketcall
364
365__END_DECLS
366
367#endif /* _SYS_SOCKET_H */