15adca30aSAxel Dörfler/*
2173f54f1SMatt Madia * Copyright 2002-2012 Haiku, Inc. All Rights Reserved.
35adca30aSAxel Dörfler * Distributed under the terms of the MIT License.
45adca30aSAxel Dörfler */
51f058e39SDavid Reid#ifndef _SYS_SOCKET_H
61f058e39SDavid Reid#define _SYS_SOCKET_H
752a38012Sejakowatz
85adca30aSAxel Dörfler
948103d34Sshatty#include <stdint.h>
10ac4630e1SIngo Weinhold#include <sys/param.h>
110b5170a4Sbeveloper#include <sys/types.h>
125adca30aSAxel Dörfler#include <sys/uio.h>
1352a38012Sejakowatz
14c4766723SMichael Pfeiffer
1548103d34Sshattytypedef uint32_t socklen_t;
16ef769e5eSJérôme Duvaltypedef uint8_t sa_family_t;
170b5170a4Sbeveloper
185adca30aSAxel Dörfler/* Address families */
195adca30aSAxel Dörfler#define AF_UNSPEC		0
205adca30aSAxel Dörfler#define AF_INET			1
215adca30aSAxel Dörfler#define AF_APPLETALK	2
225adca30aSAxel Dörfler#define AF_ROUTE		3
235adca30aSAxel Dörfler#define AF_LINK			4
245adca30aSAxel Dörfler#define AF_INET6		5
255adca30aSAxel Dörfler#define AF_DLI			6
265adca30aSAxel Dörfler#define AF_IPX			7
275adca30aSAxel Dörfler#define AF_NOTIFY		8
285adca30aSAxel Dörfler#define AF_LOCAL		9
295adca30aSAxel Dörfler#define AF_UNIX			AF_LOCAL
300501b7cdSOliver Ruiz Dorantes#define AF_BLUETOOTH	10
310501b7cdSOliver Ruiz Dorantes#define AF_MAX			11
325adca30aSAxel Dörfler
335adca30aSAxel Dörfler/* Protocol families, deprecated */
3452a38012Sejakowatz#define PF_UNSPEC		AF_UNSPEC
3552a38012Sejakowatz#define PF_INET			AF_INET
3652a38012Sejakowatz#define PF_ROUTE		AF_ROUTE
3752a38012Sejakowatz#define PF_LINK			AF_LINK
385adca30aSAxel Dörfler#define PF_INET6		AF_INET6
39479a9264SIngo Weinhold#define PF_LOCAL		AF_LOCAL
40479a9264SIngo Weinhold#define PF_UNIX			AF_UNIX
410501b7cdSOliver Ruiz Dorantes#define PF_BLUETOOTH	AF_BLUETOOTH
4252a38012Sejakowatz
435adca30aSAxel Dörfler/* Socket types */
445adca30aSAxel Dörfler#define	SOCK_STREAM	1
455adca30aSAxel Dörfler#define	SOCK_DGRAM	2
465adca30aSAxel Dörfler#define	SOCK_RAW	3
47505a5551SJérôme Duval#define	SOCK_SEQPACKET	5
485adca30aSAxel Dörfler#define SOCK_MISC	255
4952a38012Sejakowatz
505adca30aSAxel Dörfler/* Socket options for SOL_SOCKET level */
51173f54f1SMatt Madia#define	SOL_SOCKET		-1
52fde2fe1cSWaldemar Kornewald
53fde2fe1cSWaldemar Kornewald#define SO_ACCEPTCONN	0x00000001	/* socket has had listen() */
54fde2fe1cSWaldemar Kornewald#define SO_BROADCAST	0x00000002	/* permit sending of broadcast msgs */
55fde2fe1cSWaldemar Kornewald#define	SO_DEBUG		0x00000004	/* turn on debugging info recording */
56fde2fe1cSWaldemar Kornewald#define	SO_DONTROUTE	0x00000008	/* just use interface addresses */
57fde2fe1cSWaldemar Kornewald#define	SO_KEEPALIVE	0x00000010	/* keep connections alive */
58f1730f62SWaldemar Kornewald#define SO_OOBINLINE	0x00000020	/* leave received OOB data in line */
59fde2fe1cSWaldemar Kornewald#define	SO_REUSEADDR	0x00000040	/* allow local address reuse */
60fde2fe1cSWaldemar Kornewald#define SO_REUSEPORT	0x00000080	/* allow local address & port reuse */
61fde2fe1cSWaldemar Kornewald#define SO_USELOOPBACK	0x00000100	/* bypass hardware when possible */
62fde2fe1cSWaldemar Kornewald#define SO_LINGER		0x00000200	/* linger on close if data present */
6352a38012Sejakowatz
64fde2fe1cSWaldemar Kornewald#define SO_SNDBUF		0x40000001	/* send buffer size */
65fde2fe1cSWaldemar Kornewald#define SO_SNDLOWAT		0x40000002	/* send low-water mark */
66fde2fe1cSWaldemar Kornewald#define SO_SNDTIMEO		0x40000003	/* send timeout */
67fde2fe1cSWaldemar Kornewald#define SO_RCVBUF		0x40000004	/* receive buffer size */
68fde2fe1cSWaldemar Kornewald#define SO_RCVLOWAT		0x40000005	/* receive low-water mark */
69fde2fe1cSWaldemar Kornewald#define SO_RCVTIMEO		0x40000006	/* receive timeout */
70fde2fe1cSWaldemar Kornewald#define	SO_ERROR		0x40000007	/* get error status and clear */
71fde2fe1cSWaldemar Kornewald#define	SO_TYPE			0x40000008	/* get socket type */
72fde2fe1cSWaldemar Kornewald#define SO_NONBLOCK		0x40000009
73051bb0bbSAxel Dörfler#define SO_BINDTODEVICE	0x4000000a	/* binds the socket to a specific device index */
74d29deffaSIngo Weinhold#define SO_PEERCRED		0x4000000b	/* get peer credentials, param: ucred */
75fde2fe1cSWaldemar Kornewald
765adca30aSAxel Dörfler/* Shutdown options */
77f1730f62SWaldemar Kornewald#define SHUT_RD			0
78f1730f62SWaldemar Kornewald#define SHUT_WR			1
79f1730f62SWaldemar Kornewald#define SHUT_RDWR		2
80fde2fe1cSWaldemar Kornewald
815adca30aSAxel Dörfler#define SOMAXCONN		32		/* Max listen queue for a socket */
8252a38012Sejakowatz
8352a38012Sejakowatzstruct linger {
845adca30aSAxel Dörfler	int			l_onoff;
855adca30aSAxel Dörfler	int			l_linger;
8652a38012Sejakowatz};
8752a38012Sejakowatz
8852a38012Sejakowatzstruct sockaddr {
89173f54f1SMatt Madia	uint8_t		sa_len;
90ef769e5eSJérôme Duval	sa_family_t	sa_family;
915adca30aSAxel Dörfler	uint8_t		sa_data[30];
9252a38012Sejakowatz};
9352a38012Sejakowatz
9452a38012Sejakowatzstruct sockaddr_storage {
9515ab0bcfSAxel Dörfler	uint8_t		ss_len;			/* total length */
96ef769e5eSJérôme Duval	sa_family_t	ss_family;		/* address family */
9715ab0bcfSAxel Dörfler	uint8_t		__ss_pad1[6];	/* align to quad */
9815ab0bcfSAxel Dörfler	uint64_t	__ss_pad2;		/* force alignment to 64 bit */
9915ab0bcfSAxel Dörfler	uint8_t		__ss_pad3[112];	/* pad to a total of 128 bytes */
10052a38012Sejakowatz};
10152a38012Sejakowatz
10252a38012Sejakowatzstruct msghdr {
103bab4c3bbSIngo Weinhold	void		*msg_name;		/* address we're using (optional) */
1045adca30aSAxel Dörfler	socklen_t	msg_namelen;	/* length of address */
1055adca30aSAxel Dörfler	struct iovec *msg_iov;		/* scatter/gather array we'll use */
1065adca30aSAxel Dörfler	int			msg_iovlen;		/* # elements in msg_iov */
107bab4c3bbSIngo Weinhold	void		*msg_control;	/* extra data */
1085adca30aSAxel Dörfler	socklen_t	msg_controllen;	/* length of extra data */
1095adca30aSAxel Dörfler	int			msg_flags;		/* flags */
11052a38012Sejakowatz};
11152a38012Sejakowatz
1125adca30aSAxel Dörfler/* Flags for the msghdr.msg_flags field */
1135adca30aSAxel Dörfler#define MSG_OOB			0x0001	/* process out-of-band data */
1145adca30aSAxel Dörfler#define MSG_PEEK		0x0002	/* peek at incoming message */
1155adca30aSAxel Dörfler#define MSG_DONTROUTE	0x0004	/* send without using routing tables */
1165adca30aSAxel Dörfler#define MSG_EOR			0x0008	/* data completes record */
1175adca30aSAxel Dörfler#define MSG_TRUNC		0x0010	/* data discarded before delivery */
1185adca30aSAxel Dörfler#define MSG_CTRUNC		0x0020	/* control data lost before delivery */
1195adca30aSAxel Dörfler#define MSG_WAITALL		0x0040	/* wait for full request or error */
1205adca30aSAxel Dörfler#define MSG_DONTWAIT	0x0080	/* this message should be nonblocking */
1215adca30aSAxel Dörfler#define MSG_BCAST		0x0100	/* this message rec'd as broadcast */
1225adca30aSAxel Dörfler#define MSG_MCAST		0x0200	/* this message rec'd as multicast */
1235adca30aSAxel Dörfler#define	MSG_EOF			0x0400	/* data completes connection */
1244b2d018bSAdrien Destugues#define MSG_NOSIGNAL	0x0800	/* don't raise SIGPIPE if socket is closed */
12552a38012Sejakowatz
12652a38012Sejakowatzstruct cmsghdr {
1275adca30aSAxel Dörfler	socklen_t	cmsg_len;
1285adca30aSAxel Dörfler	int			cmsg_level;
1295adca30aSAxel Dörfler	int			cmsg_type;
1305adca30aSAxel Dörfler	/* data follows */
13152a38012Sejakowatz};
13252a38012Sejakowatz
1335e1d6fb2SAxel Dörfler/* cmsghdr access macros */
1345e1d6fb2SAxel Dörfler#define	CMSG_DATA(cmsg) ((unsigned char *)(cmsg) \
1355e1d6fb2SAxel Dörfler	+ _ALIGN(sizeof(struct cmsghdr)))
1365e1d6fb2SAxel Dörfler#define	CMSG_NXTHDR(mhdr, cmsg)	\
1375e1d6fb2SAxel Dörfler	(((char *)(cmsg) + _ALIGN((cmsg)->cmsg_len) \
1385e1d6fb2SAxel Dörfler	+ _ALIGN(sizeof(struct cmsghdr)) \
1395e1d6fb2SAxel Dörfler		> (char *)(mhdr)->msg_control + (mhdr)->msg_controllen) \
1405e1d6fb2SAxel Dörfler		? (struct cmsghdr *)NULL \
1415e1d6fb2SAxel Dörfler		: (struct cmsghdr *)((char *)(cmsg) + _ALIGN((cmsg)->cmsg_len)))
1425e1d6fb2SAxel Dörfler#define	CMSG_FIRSTHDR(mhdr) \
1435e1d6fb2SAxel Dörfler	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) \
1445e1d6fb2SAxel Dörfler	? (struct cmsghdr *)(mhdr)->msg_control \
1455e1d6fb2SAxel Dörfler	: (struct cmsghdr *)NULL)
146ac4630e1SIngo Weinhold#define	CMSG_SPACE(len) (_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(len))
147ac4630e1SIngo Weinhold#define	CMSG_LEN(len)	(_ALIGN(sizeof(struct cmsghdr)) + (len))
148ac4630e1SIngo Weinhold#define	CMSG_ALIGN(len)	_ALIGN(len)
149ac4630e1SIngo Weinhold
150ac4630e1SIngo Weinhold/* SOL_SOCKET control message types */
151ac4630e1SIngo Weinhold#define SCM_RIGHTS	0x01
1525e1d6fb2SAxel Dörfler
153d29deffaSIngo Weinhold/* parameter to SO_PEERCRED */
154d29deffaSIngo Weinholdstruct ucred {
155d29deffaSIngo Weinhold	pid_t	pid;	/* PID of sender */
156d29deffaSIngo Weinhold	uid_t	uid;	/* UID of sender */
157d29deffaSIngo Weinhold	gid_t	gid;	/* GID of sender */
158d29deffaSIngo Weinhold};
159d29deffaSIngo Weinhold
160fde2fe1cSWaldemar Kornewald
1615adca30aSAxel Dörfler#if __cplusplus
1625adca30aSAxel Dörflerextern "C" {
1635adca30aSAxel Dörfler#endif
1645adca30aSAxel Dörfler
1655adca30aSAxel Dörflerint 	accept(int socket, struct sockaddr *address, socklen_t *_addressLength);
1665e1d6fb2SAxel Dörflerint		bind(int socket, const struct sockaddr *address,
1675e1d6fb2SAxel Dörfler			socklen_t addressLength);
1685e1d6fb2SAxel Dörflerint		connect(int socket, const struct sockaddr *address,
1695e1d6fb2SAxel Dörfler			socklen_t addressLength);
1705e1d6fb2SAxel Dörflerint     getpeername(int socket, struct sockaddr *address,
1715e1d6fb2SAxel Dörfler			socklen_t *_addressLength);
1725e1d6fb2SAxel Dörflerint     getsockname(int socket, struct sockaddr *address,
1735e1d6fb2SAxel Dörfler			socklen_t *_addressLength);
1745e1d6fb2SAxel Dörflerint     getsockopt(int socket, int level, int option, void *value,
1755e1d6fb2SAxel Dörfler			socklen_t *_length);
1765adca30aSAxel Dörflerint		listen(int socket, int backlog);
1775adca30aSAxel Dörflerssize_t recv(int socket, void *buffer, size_t length, int flags);
1785adca30aSAxel Dörflerssize_t recvfrom(int socket, void *buffer, size_t bufferLength, int flags,
1795adca30aSAxel Dörfler			struct sockaddr *address, socklen_t *_addressLength);
1805adca30aSAxel Dörflerssize_t recvmsg(int socket, struct msghdr *message, int flags);
1815adca30aSAxel Dörflerssize_t send(int socket, const void *buffer, size_t length, int flags);
1825adca30aSAxel Dörflerssize_t	sendmsg(int socket, const struct msghdr *message, int flags);
1835adca30aSAxel Dörflerssize_t sendto(int socket, const void *message, size_t length, int flags,
1845adca30aSAxel Dörfler			const struct sockaddr *address, socklen_t addressLength);
1855e1d6fb2SAxel Dörflerint     setsockopt(int socket, int level, int option, const void *value,
1865e1d6fb2SAxel Dörfler			socklen_t length);
1875adca30aSAxel Dörflerint		shutdown(int socket, int how);
1885adca30aSAxel Dörflerint		socket(int domain, int type, int protocol);
189e4ca3629SScott McCrearyint		sockatmark(int descriptor);
1905adca30aSAxel Dörflerint		socketpair(int domain, int type, int protocol, int socketVector[2]);
19152a38012Sejakowatz
192c4766723SMichael Pfeiffer#if __cplusplus
193c4766723SMichael Pfeiffer}
1945adca30aSAxel Dörfler#endif
19552a38012Sejakowatz
1965adca30aSAxel Dörfler#endif	/* _SYS_SOCKET_H */
197