blob: 3b11d202dfacecd55dbd1b204faea33ee1f196df [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 Hughes2ba28882014-07-11 21:10:15 -070043/* This is an Android extension that won't be in the uapi headers for the common kernel. */
44#if !defined(SIOCKILLADDR)
45#define SIOCKILLADDR 0x8939
46#else
47#error SIOCKILLADDR defined twice
48#endif
49
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050__BEGIN_DECLS
51
Elliott Hughes86ec05a2012-09-11 19:03:02 -070052#define sockaddr_storage __kernel_sockaddr_storage
Elliott Hughes9f87a0b2014-02-07 14:55:58 -080053typedef unsigned short sa_family_t;
Elliott Hughes86ec05a2012-09-11 19:03:02 -070054
Elliott Hughesa8b1eb32014-02-28 17:59:29 -080055struct timespec;
56
Raghu Gandham6437eac2012-08-02 16:50:10 -070057#ifdef __mips__
58#define SOCK_DGRAM 1
59#define SOCK_STREAM 2
60#define SOCK_RAW 3
61#define SOCK_RDM 4
62#define SOCK_SEQPACKET 5
63#define SOCK_DCCP 6
64#define SOCK_PACKET 10
65#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066#define SOCK_STREAM 1
67#define SOCK_DGRAM 2
68#define SOCK_RAW 3
69#define SOCK_RDM 4
70#define SOCK_SEQPACKET 5
71#define SOCK_PACKET 10
Raghu Gandham6437eac2012-08-02 16:50:10 -070072#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030074#define SOCK_CLOEXEC O_CLOEXEC
75#define SOCK_NONBLOCK O_NONBLOCK
76
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077enum {
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030078 SHUT_RD = 0,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079#define SHUT_RD SHUT_RD
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030080 SHUT_WR,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081#define SHUT_WR SHUT_WR
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030082 SHUT_RDWR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083#define SHUT_RDWR SHUT_RDWR
84};
85
Elliott Hughes86ec05a2012-09-11 19:03:02 -070086struct sockaddr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010087 sa_family_t sa_family;
88 char sa_data[14];
Elliott Hughes86ec05a2012-09-11 19:03:02 -070089};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090
Elliott Hughes86ec05a2012-09-11 19:03:02 -070091struct linger {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010092 int l_onoff;
93 int l_linger;
Elliott Hughes86ec05a2012-09-11 19:03:02 -070094};
95
96struct msghdr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010097 void* msg_name;
98 socklen_t msg_namelen;
99 struct iovec* msg_iov;
100 size_t msg_iovlen;
101 void* msg_control;
102 size_t msg_controllen;
103 int msg_flags;
104};
105
106struct mmsghdr {
107 struct msghdr msg_hdr;
108 unsigned int msg_len;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700109};
110
111struct cmsghdr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100112 size_t cmsg_len;
113 int cmsg_level;
114 int cmsg_type;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700115};
116
Calin Juravle096b4eb2014-06-19 18:16:17 +0100117#define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr((mhdr), (cmsg))
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700118#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100119#define CMSG_DATA(cmsg) ((void*)((char*)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700120#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
121#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
Calin Juravlef1de90b2014-03-19 17:40:23 +0000122#define CMSG_FIRSTHDR(msg) \
123 ((msg)->msg_controllen >= sizeof(struct cmsghdr) \
124 ? (struct cmsghdr*) (msg)->msg_control : (struct cmsghdr*) NULL)
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100125#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 -0700126
Calin Juravle096b4eb2014-06-19 18:16:17 +0100127struct cmsghdr* __cmsg_nxthdr(struct msghdr*, struct cmsghdr*);
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700128
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700129#define SCM_RIGHTS 0x01
130#define SCM_CREDENTIALS 0x02
131#define SCM_SECURITY 0x03
132
133struct ucred {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100134 pid_t pid;
135 uid_t uid;
136 gid_t gid;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700137};
138
139#define AF_UNSPEC 0
140#define AF_UNIX 1
141#define AF_LOCAL 1
142#define AF_INET 2
143#define AF_AX25 3
144#define AF_IPX 4
145#define AF_APPLETALK 5
146#define AF_NETROM 6
147#define AF_BRIDGE 7
148#define AF_ATMPVC 8
149#define AF_X25 9
150#define AF_INET6 10
151#define AF_ROSE 11
152#define AF_DECnet 12
153#define AF_NETBEUI 13
154#define AF_SECURITY 14
155#define AF_KEY 15
156#define AF_NETLINK 16
157#define AF_ROUTE AF_NETLINK
158#define AF_PACKET 17
159#define AF_ASH 18
160#define AF_ECONET 19
161#define AF_ATMSVC 20
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800162#define AF_RDS 21
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700163#define AF_SNA 22
164#define AF_IRDA 23
165#define AF_PPPOX 24
166#define AF_WANPIPE 25
167#define AF_LLC 26
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800168#define AF_CAN 29
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700169#define AF_TIPC 30
170#define AF_BLUETOOTH 31
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800171#define AF_IUCV 32
172#define AF_RXRPC 33
173#define AF_ISDN 34
174#define AF_PHONET 35
175#define AF_IEEE802154 36
176#define AF_CAIF 37
177#define AF_ALG 38
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700178#define AF_MAX 39
179
180#define PF_UNSPEC AF_UNSPEC
181#define PF_UNIX AF_UNIX
182#define PF_LOCAL AF_LOCAL
183#define PF_INET AF_INET
184#define PF_AX25 AF_AX25
185#define PF_IPX AF_IPX
186#define PF_APPLETALK AF_APPLETALK
187#define PF_NETROM AF_NETROM
188#define PF_BRIDGE AF_BRIDGE
189#define PF_ATMPVC AF_ATMPVC
190#define PF_X25 AF_X25
191#define PF_INET6 AF_INET6
192#define PF_ROSE AF_ROSE
193#define PF_DECnet AF_DECnet
194#define PF_NETBEUI AF_NETBEUI
195#define PF_SECURITY AF_SECURITY
196#define PF_KEY AF_KEY
197#define PF_NETLINK AF_NETLINK
198#define PF_ROUTE AF_ROUTE
199#define PF_PACKET AF_PACKET
200#define PF_ASH AF_ASH
201#define PF_ECONET AF_ECONET
202#define PF_ATMSVC AF_ATMSVC
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800203#define PF_RDS AF_RDS
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700204#define PF_SNA AF_SNA
205#define PF_IRDA AF_IRDA
206#define PF_PPPOX AF_PPPOX
207#define PF_WANPIPE AF_WANPIPE
208#define PF_LLC AF_LLC
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800209#define PF_CAN AF_CAN
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700210#define PF_TIPC AF_TIPC
211#define PF_BLUETOOTH AF_BLUETOOTH
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800212#define PF_IUCV AF_IUCV
213#define PF_RXRPC AF_RXRPC
214#define PF_ISDN AF_ISDN
215#define PF_PHONET AF_PHONET
216#define PF_IEEE802154 AF_IEEE802154
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700217#define PF_CAIF AF_CAIF
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800218#define PF_ALG AF_ALG
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700219#define PF_MAX AF_MAX
220
221#define SOMAXCONN 128
222
223#define MSG_OOB 1
224#define MSG_PEEK 2
225#define MSG_DONTROUTE 4
226#define MSG_TRYHARD 4
227#define MSG_CTRUNC 8
228#define MSG_PROBE 0x10
229#define MSG_TRUNC 0x20
230#define MSG_DONTWAIT 0x40
231#define MSG_EOR 0x80
232#define MSG_WAITALL 0x100
233#define MSG_FIN 0x200
234#define MSG_SYN 0x400
235#define MSG_CONFIRM 0x800
236#define MSG_RST 0x1000
237#define MSG_ERRQUEUE 0x2000
238#define MSG_NOSIGNAL 0x4000
239#define MSG_MORE 0x8000
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100240#define MSG_WAITFORONE 0x10000
241#define MSG_FASTOPEN 0x20000000
242#define MSG_CMSG_CLOEXEC 0x40000000
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700243#define MSG_EOF MSG_FIN
244#define MSG_CMSG_COMPAT 0
245
246#define SOL_IP 0
247#define SOL_TCP 6
248#define SOL_UDP 17
249#define SOL_IPV6 41
250#define SOL_ICMPV6 58
251#define SOL_SCTP 132
252#define SOL_RAW 255
253#define SOL_IPX 256
254#define SOL_AX25 257
255#define SOL_ATALK 258
256#define SOL_NETROM 259
257#define SOL_ROSE 260
258#define SOL_DECNET 261
259#define SOL_X25 262
260#define SOL_PACKET 263
261#define SOL_ATM 264
262#define SOL_AAL 265
263#define SOL_IRDA 266
264#define SOL_NETBEUI 267
265#define SOL_LLC 268
266#define SOL_DCCP 269
267#define SOL_NETLINK 270
268#define SOL_TIPC 271
269
270#define IPX_TYPE 1
271
272#ifdef __i386__
273# define __socketcall extern __attribute__((__cdecl__))
274#else
275# define __socketcall extern
276#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100278__socketcall int accept(int, struct sockaddr*, socklen_t*);
Christopher Ferris9cea3772014-03-31 11:23:39 -0700279__socketcall int accept4(int, struct sockaddr*, socklen_t*, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100280__socketcall int bind(int, const struct sockaddr*, int);
281__socketcall int connect(int, const struct sockaddr*, socklen_t);
282__socketcall int getpeername(int, struct sockaddr*, socklen_t*);
283__socketcall int getsockname(int, struct sockaddr*, socklen_t*);
284__socketcall int getsockopt(int, int, int, void*, socklen_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285__socketcall int listen(int, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100286__socketcall int recvmmsg(int, struct mmsghdr*, unsigned int, int, const struct timespec*);
Elliott Hughes247dc912014-03-10 17:17:01 -0700287__socketcall int recvmsg(int, struct msghdr*, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100288__socketcall int sendmmsg(int, const struct mmsghdr*, unsigned int, int);
Elliott Hughes247dc912014-03-10 17:17:01 -0700289__socketcall int sendmsg(int, const struct msghdr*, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100290__socketcall int setsockopt(int, int, int, const void*, socklen_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291__socketcall int shutdown(int, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100292__socketcall int socket(int, int, int);
293__socketcall int socketpair(int, int, int, int*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294
Elliott Hughes247dc912014-03-10 17:17:01 -0700295extern ssize_t send(int, const void*, size_t, int);
296extern ssize_t recv(int, void*, size_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100298__socketcall ssize_t sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
Elliott Hughes247dc912014-03-10 17:17:01 -0700299__socketcall ssize_t recvfrom(int, void*, size_t, int, const struct sockaddr*, socklen_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700301#if defined(__BIONIC_FORTIFY)
302__errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
Elliott Hughes247dc912014-03-10 17:17:01 -0700303extern ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, const struct sockaddr*, socklen_t*);
304extern ssize_t __recvfrom_real(int, void*, size_t, int, const struct sockaddr*, socklen_t*)
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700305 __asm__(__USER_LABEL_PREFIX__ "recvfrom");
306
307__BIONIC_FORTIFY_INLINE
Elliott Hughes247dc912014-03-10 17:17:01 -0700308ssize_t recvfrom(int fd, void* buf, size_t len, int flags, const struct sockaddr* src_addr, socklen_t* addr_len) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700309 size_t bos = __bos0(buf);
310
311#if !defined(__clang__)
312 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Elliott Hughes247dc912014-03-10 17:17:01 -0700313 return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700314 }
315
316 if (__builtin_constant_p(len) && (len <= bos)) {
Elliott Hughes247dc912014-03-10 17:17:01 -0700317 return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700318 }
319
320 if (__builtin_constant_p(len) && (len > bos)) {
321 __recvfrom_error();
322 }
323#endif
324
Elliott Hughes247dc912014-03-10 17:17:01 -0700325 return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700326}
327
328__BIONIC_FORTIFY_INLINE
Elliott Hughes247dc912014-03-10 17:17:01 -0700329ssize_t recv(int socket, void* buf, size_t len, int flags) {
330 return recvfrom(socket, buf, len, flags, NULL, 0);
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700331}
332
333#endif /* __BIONIC_FORTIFY */
334
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335#undef __socketcall
336
337__END_DECLS
338
339#endif /* _SYS_SOCKET_H */