2 * Acess2 POSIX Sockets Emulation
3 * - By John Hodge (thePowersGang)
8 #ifndef _SYS_SOCKETS_H_
9 #define _SYS_SOCKETS_H_
11 #include <sys/types.h>
13 #include <stddef.h> // size_t
15 #include <stdint.h> // uint32_t
21 typedef uint32_t socklen_t;
31 #define AF_UNIX AF_LOCAL
32 typedef uint8_t sa_family_t; // I would use an enum, but cast issues
36 sa_family_t sa_family;
43 socklen_t msg_namelen;
44 struct iovec *msg_iov;
47 socklen_t msg_controllen;
58 #define SCM_RIGHTS 0x1
60 #define CMSG_DATA(cmsg) ((unsigned char*)(cmsg + 1))
61 #define CMSG_NXTHDR(mhdr, cmsg) 0
62 #define CMSG_FIRSTHDR(mhdr) 0
72 SOCK_STREAM, //!< Stream (TCP)
73 SOCK_DGRAM, //!< Datagram (UDP)
75 SOCK_RAW, //!< Raw packet access
76 SOCK_RDM //!< Reliable non-ordered datagrams
80 * \brief Values for \a domain of socket()
85 PF_LOCAL, //!< Machine-local comms
88 PF_PACKET //!< Low level packet interface
90 #define PF_UNIX PF_LOCAL
92 // getsockopt/setsockopt level
99 // SOL_SOCKET getsockopt/setsockopt names
117 //#define MSG_CONFIRM 0x01
118 #define MSG_DONTROUTE 0x02
121 #define MSG_PEEK 0x100
124 * \brief Create a new socket descriptor
125 * \param domain Address family
127 extern int socket(int domain, int type, int protocol);
132 extern int shutdown(int socket, int how);
135 * \brief Bind a socket to an address
137 extern int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
140 * \brief Connect to another socket
142 extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
145 * \brief Listen for incoming connections
147 extern int listen(int sockfd, int backlog);
149 #define SOMAXCONN 128 // Maximum size of backlog (actually far higher)
152 * \brief Accept an incoming connection
154 extern int accept(int sockfd, struct sockaddr *clientaddr, socklen_t *addrlen);
156 extern int recvfrom(int sockfd, void *buffer, size_t length, int flags, struct sockaddr *clientaddr, socklen_t *addrlen);
157 extern int recv(int sockfd, void *buffer, size_t length, int flags);
158 extern int sendto(int sockfd, const void *buffer, size_t length, int flags, const struct sockaddr *clientaddr, socklen_t addrlen);
159 extern int send(int sockfd, void *buffer, size_t length, int flags);
161 extern int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len);
162 extern int getsockopt(int socket, int level, int option_name, void *option_value, socklen_t *option_len);
164 extern int getsockname(int socket, struct sockaddr *addr, socklen_t *addrlen);
165 extern int getpeername(int socket, struct sockaddr *addr, socklen_t *addrlen);
167 extern struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);
168 extern void sethostent(int stayopen);
169 extern void endhostent(void);
170 extern void herror(const char *s);
171 extern const char *hstrerror(int err);
172 extern struct hostent *gethostent(void);