xref: /haiku/headers/posix/sys/socket.h (revision 6beff0d1633e7e60dce1ec31ce7023fd84e1d638)
1 /*
2  * Copyright 2002-2012 Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _SYS_SOCKET_H
6 #define _SYS_SOCKET_H
7 
8 
9 #include <stdint.h>
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/uio.h>
13 
14 
15 typedef uint32_t socklen_t;
16 typedef uint8_t sa_family_t;
17 
18 /* Address families */
19 #define AF_UNSPEC		0
20 #define AF_INET			1
21 #define AF_APPLETALK	2
22 #define AF_ROUTE		3
23 #define AF_LINK			4
24 #define AF_INET6		5
25 #define AF_DLI			6
26 #define AF_IPX			7
27 #define AF_NOTIFY		8
28 #define AF_LOCAL		9
29 #define AF_UNIX			AF_LOCAL
30 #define AF_BLUETOOTH	10
31 #define AF_MAX			11
32 
33 /* Protocol families, deprecated */
34 #define PF_UNSPEC		AF_UNSPEC
35 #define PF_INET			AF_INET
36 #define PF_ROUTE		AF_ROUTE
37 #define PF_LINK			AF_LINK
38 #define PF_INET6		AF_INET6
39 #define PF_LOCAL		AF_LOCAL
40 #define PF_UNIX			AF_UNIX
41 #define PF_BLUETOOTH	AF_BLUETOOTH
42 
43 /* Socket types */
44 #define	SOCK_STREAM	1
45 #define	SOCK_DGRAM	2
46 #define	SOCK_RAW	3
47 #define	SOCK_SEQPACKET	5
48 #define SOCK_MISC	255
49 
50 #define SOCK_NONBLOCK 	0x00040000
51 #define SOCK_CLOEXEC 	0x00080000
52 
53 /* Socket options for SOL_SOCKET level */
54 #define	SOL_SOCKET		-1
55 
56 #define SO_ACCEPTCONN	0x00000001	/* socket has had listen() */
57 #define SO_BROADCAST	0x00000002	/* permit sending of broadcast msgs */
58 #define	SO_DEBUG		0x00000004	/* turn on debugging info recording */
59 #define	SO_DONTROUTE	0x00000008	/* just use interface addresses */
60 #define	SO_KEEPALIVE	0x00000010	/* keep connections alive */
61 #define SO_OOBINLINE	0x00000020	/* leave received OOB data in line */
62 #define	SO_REUSEADDR	0x00000040	/* allow local address reuse */
63 #define SO_REUSEPORT	0x00000080	/* allow local address & port reuse */
64 #define SO_USELOOPBACK	0x00000100	/* bypass hardware when possible */
65 #define SO_LINGER		0x00000200	/* linger on close if data present */
66 
67 #define SO_SNDBUF		0x40000001	/* send buffer size */
68 #define SO_SNDLOWAT		0x40000002	/* send low-water mark */
69 #define SO_SNDTIMEO		0x40000003	/* send timeout */
70 #define SO_RCVBUF		0x40000004	/* receive buffer size */
71 #define SO_RCVLOWAT		0x40000005	/* receive low-water mark */
72 #define SO_RCVTIMEO		0x40000006	/* receive timeout */
73 #define	SO_ERROR		0x40000007	/* get error status and clear */
74 #define	SO_TYPE			0x40000008	/* get socket type */
75 #define SO_NONBLOCK		0x40000009
76 #define SO_BINDTODEVICE	0x4000000a	/* binds the socket to a specific device index */
77 #define SO_PEERCRED		0x4000000b	/* get peer credentials, param: ucred */
78 
79 /* Shutdown options */
80 #define SHUT_RD			0
81 #define SHUT_WR			1
82 #define SHUT_RDWR		2
83 
84 #define SOMAXCONN		32		/* Max listen queue for a socket */
85 
86 struct linger {
87 	int			l_onoff;
88 	int			l_linger;
89 };
90 
91 struct sockaddr {
92 	uint8_t		sa_len;
93 	sa_family_t	sa_family;
94 	uint8_t		sa_data[30];
95 };
96 
97 struct sockaddr_storage {
98 	uint8_t		ss_len;			/* total length */
99 	sa_family_t	ss_family;		/* address family */
100 	uint8_t		__ss_pad1[6];	/* align to quad */
101 	uint64_t	__ss_pad2;		/* force alignment to 64 bit */
102 	uint8_t		__ss_pad3[112];	/* pad to a total of 128 bytes */
103 };
104 
105 struct msghdr {
106 	void		*msg_name;		/* address we're using (optional) */
107 	socklen_t	msg_namelen;	/* length of address */
108 	struct iovec *msg_iov;		/* scatter/gather array we'll use */
109 	int			msg_iovlen;		/* # elements in msg_iov */
110 	void		*msg_control;	/* extra data */
111 	socklen_t	msg_controllen;	/* length of extra data */
112 	int			msg_flags;		/* flags */
113 };
114 
115 /* Flags for the msghdr.msg_flags field */
116 #define MSG_OOB			0x0001	/* process out-of-band data */
117 #define MSG_PEEK		0x0002	/* peek at incoming message */
118 #define MSG_DONTROUTE	0x0004	/* send without using routing tables */
119 #define MSG_EOR			0x0008	/* data completes record */
120 #define MSG_TRUNC		0x0010	/* data discarded before delivery */
121 #define MSG_CTRUNC		0x0020	/* control data lost before delivery */
122 #define MSG_WAITALL		0x0040	/* wait for full request or error */
123 #define MSG_DONTWAIT	0x0080	/* this message should be nonblocking */
124 #define MSG_BCAST		0x0100	/* this message rec'd as broadcast */
125 #define MSG_MCAST		0x0200	/* this message rec'd as multicast */
126 #define	MSG_EOF			0x0400	/* data completes connection */
127 #define MSG_NOSIGNAL	0x0800	/* don't raise SIGPIPE if socket is closed */
128 
129 struct cmsghdr {
130 	socklen_t	cmsg_len;
131 	int			cmsg_level;
132 	int			cmsg_type;
133 	/* data follows */
134 };
135 
136 /* cmsghdr access macros */
137 #define	CMSG_DATA(cmsg) ((unsigned char *)(cmsg) \
138 	+ _ALIGN(sizeof(struct cmsghdr)))
139 #define	CMSG_NXTHDR(mhdr, cmsg)	\
140 	(((char *)(cmsg) + _ALIGN((cmsg)->cmsg_len) \
141 	+ _ALIGN(sizeof(struct cmsghdr)) \
142 		> (char *)(mhdr)->msg_control + (mhdr)->msg_controllen) \
143 		? (struct cmsghdr *)NULL \
144 		: (struct cmsghdr *)((char *)(cmsg) + _ALIGN((cmsg)->cmsg_len)))
145 #define	CMSG_FIRSTHDR(mhdr) \
146 	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) \
147 	? (struct cmsghdr *)(mhdr)->msg_control \
148 	: (struct cmsghdr *)NULL)
149 #define	CMSG_SPACE(len) (_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(len))
150 #define	CMSG_LEN(len)	(_ALIGN(sizeof(struct cmsghdr)) + (len))
151 #define	CMSG_ALIGN(len)	_ALIGN(len)
152 
153 /* SOL_SOCKET control message types */
154 #define SCM_RIGHTS	0x01
155 
156 /* parameter to SO_PEERCRED */
157 struct ucred {
158 	pid_t	pid;	/* PID of sender */
159 	uid_t	uid;	/* UID of sender */
160 	gid_t	gid;	/* GID of sender */
161 };
162 
163 
164 #if __cplusplus
165 extern "C" {
166 #endif
167 
168 int 	accept(int socket, struct sockaddr *address, socklen_t *_addressLength);
169 int 	accept4(int socket, struct sockaddr *address, socklen_t *_addressLength, int flags);
170 int		bind(int socket, const struct sockaddr *address,
171 			socklen_t addressLength);
172 int		connect(int socket, const struct sockaddr *address,
173 			socklen_t addressLength);
174 int     getpeername(int socket, struct sockaddr *address,
175 			socklen_t *_addressLength);
176 int     getsockname(int socket, struct sockaddr *address,
177 			socklen_t *_addressLength);
178 int     getsockopt(int socket, int level, int option, void *value,
179 			socklen_t *_length);
180 int		listen(int socket, int backlog);
181 ssize_t recv(int socket, void *buffer, size_t length, int flags);
182 ssize_t recvfrom(int socket, void *buffer, size_t bufferLength, int flags,
183 			struct sockaddr *address, socklen_t *_addressLength);
184 ssize_t recvmsg(int socket, struct msghdr *message, int flags);
185 ssize_t send(int socket, const void *buffer, size_t length, int flags);
186 ssize_t	sendmsg(int socket, const struct msghdr *message, int flags);
187 ssize_t sendto(int socket, const void *message, size_t length, int flags,
188 			const struct sockaddr *address, socklen_t addressLength);
189 int     setsockopt(int socket, int level, int option, const void *value,
190 			socklen_t length);
191 int		shutdown(int socket, int how);
192 int		socket(int domain, int type, int protocol);
193 int		sockatmark(int descriptor);
194 int		socketpair(int domain, int type, int protocol, int socketVector[2]);
195 
196 #if __cplusplus
197 }
198 #endif
199 
200 #endif	/* _SYS_SOCKET_H */
201