blob: b6107a5769d14781615baec7321c5f50d62a40b5 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043__BEGIN_DECLS
44
Elliott Hughes86ec05a2012-09-11 19:03:02 -070045#define sockaddr_storage __kernel_sockaddr_storage
Elliott Hughes9f87a0b2014-02-07 14:55:58 -080046typedef unsigned short sa_family_t;
Elliott Hughes86ec05a2012-09-11 19:03:02 -070047
Raghu Gandham6437eac2012-08-02 16:50:10 -070048#ifdef __mips__
49#define SOCK_DGRAM 1
50#define SOCK_STREAM 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#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057#define SOCK_STREAM 1
58#define SOCK_DGRAM 2
59#define SOCK_RAW 3
60#define SOCK_RDM 4
61#define SOCK_SEQPACKET 5
62#define SOCK_PACKET 10
Raghu Gandham6437eac2012-08-02 16:50:10 -070063#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030065#define SOCK_CLOEXEC O_CLOEXEC
66#define SOCK_NONBLOCK O_NONBLOCK
67
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068enum {
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030069 SHUT_RD = 0,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070#define SHUT_RD SHUT_RD
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030071 SHUT_WR,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#define SHUT_WR SHUT_WR
Andrei Emeltchenko43d21372013-10-14 16:48:26 +030073 SHUT_RDWR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#define SHUT_RDWR SHUT_RDWR
75};
76
Elliott Hughes86ec05a2012-09-11 19:03:02 -070077struct sockaddr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010078 sa_family_t sa_family;
79 char sa_data[14];
Elliott Hughes86ec05a2012-09-11 19:03:02 -070080};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Elliott Hughes86ec05a2012-09-11 19:03:02 -070082struct linger {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010083 int l_onoff;
84 int l_linger;
Elliott Hughes86ec05a2012-09-11 19:03:02 -070085};
86
87struct msghdr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010088 void* msg_name;
89 socklen_t msg_namelen;
90 struct iovec* msg_iov;
91 size_t msg_iovlen;
92 void* msg_control;
93 size_t msg_controllen;
94 int msg_flags;
95};
96
97struct mmsghdr {
98 struct msghdr msg_hdr;
99 unsigned int msg_len;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700100};
101
102struct cmsghdr {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100103 size_t cmsg_len;
104 int cmsg_level;
105 int cmsg_type;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700106};
107
108#define __CMSG_NXTHDR(ctl, len, cmsg) __cmsg_nxthdr((ctl),(len),(cmsg))
109#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))
110#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100111#define CMSG_DATA(cmsg) ((void*)((char*)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700112#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
113#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100114#define __CMSG_FIRSTHDR(ctl,len) ((len) >= sizeof(struct cmsghdr) ? (struct cmsghdr*)(ctl) : (struct cmsghdr*)NULL)
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700115#define CMSG_FIRSTHDR(msg) __CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100116#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 -0700117
118#ifdef __GNUC__
119#define __KINLINE static __inline__
120#elif defined(__cplusplus)
121#define __KINLINE static inline
122#else
123#define __KINLINE static
124#endif
125
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100126__KINLINE struct cmsghdr* __cmsg_nxthdr(void* __ctl, size_t __size, struct cmsghdr* __cmsg) {
127 struct cmsghdr* __ptr;
128 __ptr = (struct cmsghdr*)(((unsigned char*) __cmsg) + CMSG_ALIGN(__cmsg->cmsg_len));
129 if ((unsigned long)((char*)(__ptr+1) - (char*) __ctl) > __size) {
130 return NULL;
131 }
132 return __ptr;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700133}
134
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100135__KINLINE struct cmsghdr* cmsg_nxthdr (struct msghdr* __msg, struct cmsghdr* __cmsg) {
136 return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg);
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700137}
138
139#define SCM_RIGHTS 0x01
140#define SCM_CREDENTIALS 0x02
141#define SCM_SECURITY 0x03
142
143struct ucred {
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100144 pid_t pid;
145 uid_t uid;
146 gid_t gid;
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700147};
148
149#define AF_UNSPEC 0
150#define AF_UNIX 1
151#define AF_LOCAL 1
152#define AF_INET 2
153#define AF_AX25 3
154#define AF_IPX 4
155#define AF_APPLETALK 5
156#define AF_NETROM 6
157#define AF_BRIDGE 7
158#define AF_ATMPVC 8
159#define AF_X25 9
160#define AF_INET6 10
161#define AF_ROSE 11
162#define AF_DECnet 12
163#define AF_NETBEUI 13
164#define AF_SECURITY 14
165#define AF_KEY 15
166#define AF_NETLINK 16
167#define AF_ROUTE AF_NETLINK
168#define AF_PACKET 17
169#define AF_ASH 18
170#define AF_ECONET 19
171#define AF_ATMSVC 20
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800172#define AF_RDS 21
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700173#define AF_SNA 22
174#define AF_IRDA 23
175#define AF_PPPOX 24
176#define AF_WANPIPE 25
177#define AF_LLC 26
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800178#define AF_CAN 29
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700179#define AF_TIPC 30
180#define AF_BLUETOOTH 31
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800181#define AF_IUCV 32
182#define AF_RXRPC 33
183#define AF_ISDN 34
184#define AF_PHONET 35
185#define AF_IEEE802154 36
186#define AF_CAIF 37
187#define AF_ALG 38
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700188#define AF_MAX 39
189
190#define PF_UNSPEC AF_UNSPEC
191#define PF_UNIX AF_UNIX
192#define PF_LOCAL AF_LOCAL
193#define PF_INET AF_INET
194#define PF_AX25 AF_AX25
195#define PF_IPX AF_IPX
196#define PF_APPLETALK AF_APPLETALK
197#define PF_NETROM AF_NETROM
198#define PF_BRIDGE AF_BRIDGE
199#define PF_ATMPVC AF_ATMPVC
200#define PF_X25 AF_X25
201#define PF_INET6 AF_INET6
202#define PF_ROSE AF_ROSE
203#define PF_DECnet AF_DECnet
204#define PF_NETBEUI AF_NETBEUI
205#define PF_SECURITY AF_SECURITY
206#define PF_KEY AF_KEY
207#define PF_NETLINK AF_NETLINK
208#define PF_ROUTE AF_ROUTE
209#define PF_PACKET AF_PACKET
210#define PF_ASH AF_ASH
211#define PF_ECONET AF_ECONET
212#define PF_ATMSVC AF_ATMSVC
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800213#define PF_RDS AF_RDS
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700214#define PF_SNA AF_SNA
215#define PF_IRDA AF_IRDA
216#define PF_PPPOX AF_PPPOX
217#define PF_WANPIPE AF_WANPIPE
218#define PF_LLC AF_LLC
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800219#define PF_CAN AF_CAN
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700220#define PF_TIPC AF_TIPC
221#define PF_BLUETOOTH AF_BLUETOOTH
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800222#define PF_IUCV AF_IUCV
223#define PF_RXRPC AF_RXRPC
224#define PF_ISDN AF_ISDN
225#define PF_PHONET AF_PHONET
226#define PF_IEEE802154 AF_IEEE802154
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700227#define PF_CAIF AF_CAIF
Elliott Hughesd73c0b32013-01-03 16:25:47 -0800228#define PF_ALG AF_ALG
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700229#define PF_MAX AF_MAX
230
231#define SOMAXCONN 128
232
233#define MSG_OOB 1
234#define MSG_PEEK 2
235#define MSG_DONTROUTE 4
236#define MSG_TRYHARD 4
237#define MSG_CTRUNC 8
238#define MSG_PROBE 0x10
239#define MSG_TRUNC 0x20
240#define MSG_DONTWAIT 0x40
241#define MSG_EOR 0x80
242#define MSG_WAITALL 0x100
243#define MSG_FIN 0x200
244#define MSG_SYN 0x400
245#define MSG_CONFIRM 0x800
246#define MSG_RST 0x1000
247#define MSG_ERRQUEUE 0x2000
248#define MSG_NOSIGNAL 0x4000
249#define MSG_MORE 0x8000
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100250#define MSG_WAITFORONE 0x10000
251#define MSG_FASTOPEN 0x20000000
252#define MSG_CMSG_CLOEXEC 0x40000000
Elliott Hughes86ec05a2012-09-11 19:03:02 -0700253#define MSG_EOF MSG_FIN
254#define MSG_CMSG_COMPAT 0
255
256#define SOL_IP 0
257#define SOL_TCP 6
258#define SOL_UDP 17
259#define SOL_IPV6 41
260#define SOL_ICMPV6 58
261#define SOL_SCTP 132
262#define SOL_RAW 255
263#define SOL_IPX 256
264#define SOL_AX25 257
265#define SOL_ATALK 258
266#define SOL_NETROM 259
267#define SOL_ROSE 260
268#define SOL_DECNET 261
269#define SOL_X25 262
270#define SOL_PACKET 263
271#define SOL_ATM 264
272#define SOL_AAL 265
273#define SOL_IRDA 266
274#define SOL_NETBEUI 267
275#define SOL_LLC 268
276#define SOL_DCCP 269
277#define SOL_NETLINK 270
278#define SOL_TIPC 271
279
280#define IPX_TYPE 1
281
282#ifdef __i386__
283# define __socketcall extern __attribute__((__cdecl__))
284#else
285# define __socketcall extern
286#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100288__socketcall int accept(int, struct sockaddr*, socklen_t*);
289__socketcall int bind(int, const struct sockaddr*, int);
290__socketcall int connect(int, const struct sockaddr*, socklen_t);
291__socketcall int getpeername(int, struct sockaddr*, socklen_t*);
292__socketcall int getsockname(int, struct sockaddr*, socklen_t*);
293__socketcall int getsockopt(int, int, int, void*, socklen_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294__socketcall int listen(int, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100295__socketcall int recvmmsg(int, struct mmsghdr*, unsigned int, int, const struct timespec*);
296__socketcall int recvmsg(int, struct msghdr*, unsigned int);
297__socketcall int sendmmsg(int, const struct mmsghdr*, unsigned int, int);
298__socketcall int sendmsg(int, const struct msghdr*, unsigned int);
299__socketcall int setsockopt(int, int, int, const void*, socklen_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300__socketcall int shutdown(int, int);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100301__socketcall int socket(int, int, int);
302__socketcall int socketpair(int, int, int, int*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100304extern ssize_t send(int, const void*, size_t, unsigned int);
305extern ssize_t recv(int, void*, size_t, unsigned int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100307__socketcall ssize_t sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
308__socketcall ssize_t recvfrom(int, void*, size_t, unsigned int, const struct sockaddr*, socklen_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800309
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700310#if defined(__BIONIC_FORTIFY)
311__errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100312extern ssize_t __recvfrom_chk(int, void*, size_t, size_t, unsigned int, const struct sockaddr*, socklen_t*);
313extern ssize_t __recvfrom_real(int, void*, size_t, unsigned int, const struct sockaddr*, socklen_t*)
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700314 __asm__(__USER_LABEL_PREFIX__ "recvfrom");
315
316__BIONIC_FORTIFY_INLINE
317ssize_t recvfrom(int fd, void* buf, size_t len, unsigned int flags, const struct sockaddr* src_addr, socklen_t* addrlen) {
318 size_t bos = __bos0(buf);
319
320#if !defined(__clang__)
321 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
322 return __recvfrom_real(fd, buf, len, flags, src_addr, addrlen);
323 }
324
325 if (__builtin_constant_p(len) && (len <= bos)) {
326 return __recvfrom_real(fd, buf, len, flags, src_addr, addrlen);
327 }
328
329 if (__builtin_constant_p(len) && (len > bos)) {
330 __recvfrom_error();
331 }
332#endif
333
334 return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addrlen);
335}
336
337__BIONIC_FORTIFY_INLINE
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100338ssize_t recv(int socket, void* buf, size_t buflen, unsigned int flags) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700339 return recvfrom(socket, buf, buflen, flags, NULL, 0);
340}
341
342#endif /* __BIONIC_FORTIFY */
343
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344#undef __socketcall
345
346__END_DECLS
347
348#endif /* _SYS_SOCKET_H */