Usermode/libc - Implementing POSIX/BSD sockets
authorJohn Hodge <[email protected]>
Mon, 8 Aug 2011 13:16:55 +0000 (21:16 +0800)
committerJohn Hodge <[email protected]>
Mon, 8 Aug 2011 13:16:55 +0000 (21:16 +0800)
Usermode/Libraries/libc.so_src/sockets.c [new file with mode: 0644]
Usermode/include/sys/socket.h [new file with mode: 0644]

diff --git a/Usermode/Libraries/libc.so_src/sockets.c b/Usermode/Libraries/libc.so_src/sockets.c
new file mode 100644 (file)
index 0000000..854d6d9
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Acess2 C Library
+ * - By John Hodge (thePowersGang)
+ *
+ * sockets.c
+ * - POSIX Sockets translation/emulation
+ */
+#include <acess/sys.h>
+#include <sys/socket.h>
+
+#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 (file)
index 0000000..43cb3d6
--- /dev/null
@@ -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
+

UCC git Repository :: git.ucc.asn.au