Usermode/libc - Fix time conversion code
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / sockets.c
1 /*
2  * Acess2 C Library
3  * - By John Hodge (thePowersGang)
4  *
5  * sockets.c
6  * - POSIX Sockets translation/emulation
7  */
8 #include <acess/sys.h>
9 #include <sys/socket.h>
10
11 #define MAX_SOCKS       64
12
13 struct {
14          int    AddrType;
15         const char      *ListenFile;
16         const char      *ConnectFile;
17         char    RemoteAddr[16]; // If != 0, bind to a remote addr
18 } gaSockInfo[MAX_SOCKS]
19
20 // === CODE ===
21 int socket(int domain, int type, int protocol)
22 {
23          int    fd, addrType=0;
24         const char      *listenFile, *connectFile;
25
26         // Validation
27         switch(domain)
28         {
29         case PF_INET:
30                 addrType = AF_INET;
31         case PF_INET6:
32                 if(addrType == 0)       addrType = AF_INET6;
33                 switch(type)
34                 {
35                 case SOCK_STREAM:
36                         listenFile = "tcps";
37                         connectFile = "tcpc";
38                         break;
39                 case SOCK_DGRAM:
40                         listenFile = "udp";
41                         connectFile = "udp";
42                         break;
43                 default:
44                         errno = EINVAL;
45                         return -1;
46                 }
47                 break;
48         default:
49                 errno = EINVAL;
50                 return -1;
51         }
52
53         fd = open("/Devices/null", 0);
54         if(fd == -1) {
55                 return -1;
56         }
57
58         if( fd >= MAX_SOCKS ) {
59                 close(fd);
60                 errno = ENFILE;
61                 return -1;
62         }
63
64         gaSockInfo[fd].AddrType = 0;
65         gaSockInfo[fd].ListenFile = listenFile;
66         gaSockInfo[fd].ConnectFile = connectFile;
67         memset( gaSockInfo[fd].RemoteAddr, 0, sizeof(gaSockInfo[fd].RemoteAddr) );
68
69         return fd;
70 }

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