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 __BEGIN_DECLS
44 
45 #define sockaddr_storage __kernel_sockaddr_storage
46 typedef unsigned short sa_family_t;
47 
48 struct timespec;
49 
50 #ifdef __mips__
51 #define SOCK_DGRAM      1
52 #define SOCK_STREAM     2
53 #define SOCK_RAW        3
54 #define SOCK_RDM        4
55 #define SOCK_SEQPACKET  5
56 #define SOCK_DCCP       6
57 #define SOCK_PACKET     10
58 #else
59 #define SOCK_STREAM      1
60 #define SOCK_DGRAM       2
61 #define SOCK_RAW         3
62 #define SOCK_RDM         4
63 #define SOCK_SEQPACKET   5
64 #define SOCK_PACKET      10
65 #endif
66 
67 #define SOCK_CLOEXEC O_CLOEXEC
68 #define SOCK_NONBLOCK O_NONBLOCK
69 
70 enum {
71   SHUT_RD = 0,
72 #define SHUT_RD         SHUT_RD
73   SHUT_WR,
74 #define SHUT_WR         SHUT_WR
75   SHUT_RDWR
76 #define SHUT_RDWR       SHUT_RDWR
77 };
78 
79 struct sockaddr {
80   sa_family_t sa_family;
81   char sa_data[14];
82 };
83 
84 struct linger {
85   int l_onoff;
86   int l_linger;
87 };
88 
89 struct msghdr {
90   void* msg_name;
91   socklen_t msg_namelen;
92   struct iovec* msg_iov;
93   size_t msg_iovlen;
94   void* msg_control;
95   size_t msg_controllen;
96   int msg_flags;
97 };
98 
99 struct mmsghdr {
100   struct msghdr msg_hdr;
101   unsigned int msg_len;
102 };
103 
104 struct cmsghdr {
105   size_t cmsg_len;
106   int cmsg_level;
107   int cmsg_type;
108 };
109 
110 #define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr((mhdr), (cmsg))
111 #define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
112 #define CMSG_DATA(cmsg) ((void*)((char*)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
113 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
114 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
115 #define CMSG_FIRSTHDR(msg) \
116   ((msg)->msg_controllen >= sizeof(struct cmsghdr) \
117    ? (struct cmsghdr*) (msg)->msg_control : (struct cmsghdr*) NULL)
118 #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)))
119 
120 struct cmsghdr* __cmsg_nxthdr(struct msghdr*, struct cmsghdr*);
121 
122 #define SCM_RIGHTS 0x01
123 #define SCM_CREDENTIALS 0x02
124 #define SCM_SECURITY 0x03
125 
126 struct ucred {
127   pid_t pid;
128   uid_t uid;
129   gid_t gid;
130 };
131 
132 #define AF_UNSPEC 0
133 #define AF_UNIX 1
134 #define AF_LOCAL 1
135 #define AF_INET 2
136 #define AF_AX25 3
137 #define AF_IPX 4
138 #define AF_APPLETALK 5
139 #define AF_NETROM 6
140 #define AF_BRIDGE 7
141 #define AF_ATMPVC 8
142 #define AF_X25 9
143 #define AF_INET6 10
144 #define AF_ROSE 11
145 #define AF_DECnet 12
146 #define AF_NETBEUI 13
147 #define AF_SECURITY 14
148 #define AF_KEY 15
149 #define AF_NETLINK 16
150 #define AF_ROUTE AF_NETLINK
151 #define AF_PACKET 17
152 #define AF_ASH 18
153 #define AF_ECONET 19
154 #define AF_ATMSVC 20
155 #define AF_RDS 21
156 #define AF_SNA 22
157 #define AF_IRDA 23
158 #define AF_PPPOX 24
159 #define AF_WANPIPE 25
160 #define AF_LLC 26
161 #define AF_CAN 29
162 #define AF_TIPC 30
163 #define AF_BLUETOOTH 31
164 #define AF_IUCV 32
165 #define AF_RXRPC 33
166 #define AF_ISDN 34
167 #define AF_PHONET 35
168 #define AF_IEEE802154 36
169 #define AF_CAIF 37
170 #define AF_ALG 38
171 #define AF_MAX 39
172 
173 #define PF_UNSPEC AF_UNSPEC
174 #define PF_UNIX AF_UNIX
175 #define PF_LOCAL AF_LOCAL
176 #define PF_INET AF_INET
177 #define PF_AX25 AF_AX25
178 #define PF_IPX AF_IPX
179 #define PF_APPLETALK AF_APPLETALK
180 #define PF_NETROM AF_NETROM
181 #define PF_BRIDGE AF_BRIDGE
182 #define PF_ATMPVC AF_ATMPVC
183 #define PF_X25 AF_X25
184 #define PF_INET6 AF_INET6
185 #define PF_ROSE AF_ROSE
186 #define PF_DECnet AF_DECnet
187 #define PF_NETBEUI AF_NETBEUI
188 #define PF_SECURITY AF_SECURITY
189 #define PF_KEY AF_KEY
190 #define PF_NETLINK AF_NETLINK
191 #define PF_ROUTE AF_ROUTE
192 #define PF_PACKET AF_PACKET
193 #define PF_ASH AF_ASH
194 #define PF_ECONET AF_ECONET
195 #define PF_ATMSVC AF_ATMSVC
196 #define PF_RDS AF_RDS
197 #define PF_SNA AF_SNA
198 #define PF_IRDA AF_IRDA
199 #define PF_PPPOX AF_PPPOX
200 #define PF_WANPIPE AF_WANPIPE
201 #define PF_LLC AF_LLC
202 #define PF_CAN AF_CAN
203 #define PF_TIPC AF_TIPC
204 #define PF_BLUETOOTH AF_BLUETOOTH
205 #define PF_IUCV AF_IUCV
206 #define PF_RXRPC AF_RXRPC
207 #define PF_ISDN AF_ISDN
208 #define PF_PHONET AF_PHONET
209 #define PF_IEEE802154 AF_IEEE802154
210 #define PF_CAIF AF_CAIF
211 #define PF_ALG AF_ALG
212 #define PF_MAX AF_MAX
213 
214 #define SOMAXCONN 128
215 
216 #define MSG_OOB 1
217 #define MSG_PEEK 2
218 #define MSG_DONTROUTE 4
219 #define MSG_TRYHARD 4
220 #define MSG_CTRUNC 8
221 #define MSG_PROBE 0x10
222 #define MSG_TRUNC 0x20
223 #define MSG_DONTWAIT 0x40
224 #define MSG_EOR 0x80
225 #define MSG_WAITALL 0x100
226 #define MSG_FIN 0x200
227 #define MSG_SYN 0x400
228 #define MSG_CONFIRM 0x800
229 #define MSG_RST 0x1000
230 #define MSG_ERRQUEUE 0x2000
231 #define MSG_NOSIGNAL 0x4000
232 #define MSG_MORE 0x8000
233 #define MSG_WAITFORONE 0x10000
234 #define MSG_FASTOPEN 0x20000000
235 #define MSG_CMSG_CLOEXEC 0x40000000
236 #define MSG_EOF MSG_FIN
237 #define MSG_CMSG_COMPAT 0
238 
239 #define SOL_IP 0
240 #define SOL_TCP 6
241 #define SOL_UDP 17
242 #define SOL_IPV6 41
243 #define SOL_ICMPV6 58
244 #define SOL_SCTP 132
245 #define SOL_RAW 255
246 #define SOL_IPX 256
247 #define SOL_AX25 257
248 #define SOL_ATALK 258
249 #define SOL_NETROM 259
250 #define SOL_ROSE 260
251 #define SOL_DECNET 261
252 #define SOL_X25 262
253 #define SOL_PACKET 263
254 #define SOL_ATM 264
255 #define SOL_AAL 265
256 #define SOL_IRDA 266
257 #define SOL_NETBEUI 267
258 #define SOL_LLC 268
259 #define SOL_DCCP 269
260 #define SOL_NETLINK 270
261 #define SOL_TIPC 271
262 
263 #define IPX_TYPE 1
264 
265 #ifdef __i386__
266 # define __socketcall extern __attribute__((__cdecl__))
267 #else
268 # define __socketcall extern
269 #endif
270 
271 __socketcall int accept(int, struct sockaddr*, socklen_t*);
272 __socketcall int accept4(int, struct sockaddr*, socklen_t*, int);
273 __socketcall int bind(int, const struct sockaddr*, int);
274 __socketcall int connect(int, const struct sockaddr*, socklen_t);
275 __socketcall int getpeername(int, struct sockaddr*, socklen_t*);
276 __socketcall int getsockname(int, struct sockaddr*, socklen_t*);
277 __socketcall int getsockopt(int, int, int, void*, socklen_t*);
278 __socketcall int listen(int, int);
279 __socketcall int recvmmsg(int, struct mmsghdr*, unsigned int, int, const struct timespec*);
280 __socketcall int recvmsg(int, struct msghdr*, int);
281 __socketcall int sendmmsg(int, const struct mmsghdr*, unsigned int, int);
282 __socketcall int sendmsg(int, const struct msghdr*, int);
283 __socketcall int setsockopt(int, int, int, const void*, socklen_t);
284 __socketcall int shutdown(int, int);
285 __socketcall int socket(int, int, int);
286 __socketcall int socketpair(int, int, int, int*);
287 
288 extern ssize_t send(int, const void*, size_t, int);
289 extern ssize_t recv(int, void*, size_t, int);
290 
291 __socketcall ssize_t sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
292 __socketcall ssize_t recvfrom(int, void*, size_t, int, const struct sockaddr*, socklen_t*);
293 
294 #if defined(__BIONIC_FORTIFY)
295 __errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
296 extern ssize_t __recvfrom_chk(int, void*, size_t, size_t, int, const struct sockaddr*, socklen_t*);
297 extern ssize_t __recvfrom_real(int, void*, size_t, int, const struct sockaddr*, socklen_t*)
298     __asm__(__USER_LABEL_PREFIX__ "recvfrom");
299 
300 __BIONIC_FORTIFY_INLINE
recvfrom(int fd,void * buf,size_t len,int flags,const struct sockaddr * src_addr,socklen_t * addr_len)301 ssize_t recvfrom(int fd, void* buf, size_t len, int flags, const struct sockaddr* src_addr, socklen_t* addr_len) {
302   size_t bos = __bos0(buf);
303 
304 #if !defined(__clang__)
305   if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
306     return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
307   }
308 
309   if (__builtin_constant_p(len) && (len <= bos)) {
310     return __recvfrom_real(fd, buf, len, flags, src_addr, addr_len);
311   }
312 
313   if (__builtin_constant_p(len) && (len > bos)) {
314     __recvfrom_error();
315   }
316 #endif
317 
318   return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addr_len);
319 }
320 
321 __BIONIC_FORTIFY_INLINE
recv(int socket,void * buf,size_t len,int flags)322 ssize_t recv(int socket, void* buf, size_t len, int flags) {
323   return recvfrom(socket, buf, len, flags, NULL, 0);
324 }
325 
326 #endif /* __BIONIC_FORTIFY */
327 
328 #undef __socketcall
329 
330 __END_DECLS
331 
332 #endif /* _SYS_SOCKET_H */
333