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