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