From: John Hodge Date: Mon, 8 Aug 2011 13:16:55 +0000 (+0800) Subject: Usermode/libc - Implementing POSIX/BSD sockets X-Git-Tag: rel0.11~157 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=4405b4fa990821a88cbded9fdf85d226ef3b3f66;p=tpg%2Facess2.git Usermode/libc - Implementing POSIX/BSD sockets --- diff --git a/Usermode/Libraries/libc.so_src/sockets.c b/Usermode/Libraries/libc.so_src/sockets.c new file mode 100644 index 00000000..854d6d97 --- /dev/null +++ b/Usermode/Libraries/libc.so_src/sockets.c @@ -0,0 +1,70 @@ +/* + * Acess2 C Library + * - By John Hodge (thePowersGang) + * + * sockets.c + * - POSIX Sockets translation/emulation + */ +#include +#include + +#define MAX_SOCKS 64 + +struct { + int AddrType; + const char *ListenFile; + const char *ConnectFile; + char RemoteAddr[16]; // If != 0, bind to a remote addr +} gaSockInfo[MAX_SOCKS] + +// === CODE === +int socket(int domain, int type, int protocol) +{ + int fd, addrType=0; + const char *listenFile, *connectFile; + + // Validation + switch(domain) + { + case PF_INET: + addrType = AF_INET; + case PF_INET6: + if(addrType == 0) addrType = AF_INET6; + switch(type) + { + case SOCK_STREAM: + listenFile = "tcps"; + connectFile = "tcpc"; + break; + case SOCK_DGRAM: + listenFile = "udp"; + connectFile = "udp"; + break; + default: + errno = EINVAL; + return -1; + } + break; + default: + errno = EINVAL; + return -1; + } + + fd = open("/Devices/null", 0); + if(fd == -1) { + return -1; + } + + if( fd >= MAX_SOCKS ) { + close(fd); + errno = ENFILE; + return -1; + } + + gaSockInfo[fd].AddrType = 0; + gaSockInfo[fd].ListenFile = listenFile; + gaSockInfo[fd].ConnectFile = connectFile; + memset( gaSockInfo[fd].RemoteAddr, 0, sizeof(gaSockInfo[fd].RemoteAddr) ); + + return fd; +} diff --git a/Usermode/include/sys/socket.h b/Usermode/include/sys/socket.h new file mode 100644 index 00000000..43cb3d66 --- /dev/null +++ b/Usermode/include/sys/socket.h @@ -0,0 +1,75 @@ +/* + * Acess2 C Library + * + * sys/sockets.h + * - POSIX Sockets + * + * By John Hodge (thePowersGang) + */ +#ifndef _SYS_SOCKETS_H_ +#define _SYS_SOCKETS_H_ + +typedef int socklen_t; + +typedef enum +{ + AF_UNSPEC = 0, + AF_INET = 4, + AF_INET6 = 6 +} sa_family_t; + +struct sockaddr +{ + sa_family_t sa_family; + char sa_data[16]; +}; + +/** + * \brief Values for \a domain of socket() + */ +enum eSocketDomains +{ + PF_LOCAL, //!< Machine-local comms + PF_INET, //!< IPv4 + PF_INET6, //!< IPv6 + PF_PACKET //!< Low level packet interface +}; +#define PF_UNIX PF_LOCAL + +enum eSocketTypes +{ + SOCK_STREAM, //!< Stream (TCP) + SOCK_DGRAM, //!< Datagram (UDP) + SOCK_SEQPACKET, //!< + SOCK_RAW, //!< Raw packet access + SOCK_RDM //!< Reliable non-ordered datagrams +}; + +/** + * \brief Create a new socket descriptor + * \param domain Address family + */ +extern int socket(int domain, int type, int protocol); + +/** + * \brief Bind a socket to an address + */ +extern int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); + +/** + * \brief Connect to another socket + */ +extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); + +/** + * \brief Listen for incoming connections + */ +extern int listen(int sockfd, int backlog); + +/** + * \brief Accept an incoming connection + */ +extern int accept(int sockfd, struct sockaddr *clientaddr, socklen_t addrlen); + +#endif +