2 * Acess2 POSIX Sockets Library
3 * - By John Hodge (thePowersGang)
8 #include <sys/socket.h>
10 #include <stdlib.h> // malloc/free
12 #include <netinet/in.h>
16 typedef struct s_sockinfo
22 struct sockaddr *local;
23 struct sockaddr *remote;
26 struct s_sockinfo gSockInfo[MAXFD];
27 static int giNumPreinit = 0;
30 static t_sockinfo *_GetInfo(int FD)
33 if( FD == 0 && giNumPreinit == MAXFD )
35 for( i = 0; i < MAXFD; i ++ )
37 if( gSockInfo[i].fd == FD )
41 if( FD != 0 && nInit == giNumPreinit )
47 int socket(int domain, int type, int protocol)
49 t_sockinfo *si = NULL;
51 if( domain < 0 || domain > AF_INET6 ) return -1;
52 if( type < 0 || type > SOCK_RDM ) return -1;
54 // Allocate an info struct
58 int fd = open("/Devices/null", O_RDWR);
59 if( fd == -1 ) return -1;
65 si->protocol = protocol;
72 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
74 t_sockinfo *si = _GetInfo(sockfd);;
82 si->local = malloc( addrlen );
83 memcpy(si->local, addr, addrlen);
88 int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
90 t_sockinfo *si = _GetInfo(sockfd);;
98 si->remote = malloc( addrlen );
99 memcpy(si->remote, addr, addrlen);
104 int listen(int sockfd, int backlog)
106 // TODO: Save this to the t_sockinfo structure
110 void _ClearInfo(t_sockinfo *si)
126 void _CommitServer(int sockfd)
128 t_sockinfo *si = _GetInfo(sockfd);
140 uint8_t *addrBuffer = NULL;
142 switch( si->local->sa_family )
145 addrBuffer = (void*)&((struct sockaddr_in*)si->local)->sin_addr;
149 addrBuffer = (void*)&((struct sockaddr_in6*)si->local)->sin6_addr;
156 char hexAddr[addrLen*2+1];
158 for( i = 0; i < addrLen; i ++ ) {
159 hexAddr[i*2+0] = "0123456789ABCDEF"[addrBuffer[i] >> 4];
160 hexAddr[i*2+1] = "0123456789ABCDEF"[addrBuffer[i] & 15];
161 if(addrBuffer[i]) bNonZero = 1;
166 path = mkstr("/Devices/ip/routes/@%i:%s/%s", si->local->sa_family, file);
168 path = mkstr("/Devices/ip/*%i/%s", si->local->sa_family, file);
170 reopen(si->fd, path, O_RDWR);
175 // TODO: Set up socket
180 void _CommitClient(int sockfd)
182 t_sockinfo *si = _GetInfo(sockfd);
188 int accept(int sockfd, struct sockaddr *clientaddr, socklen_t addrlen)
190 _CommitServer(sockfd);
194 child = _SysOpenChild(sockfd, "", O_RDWR);
195 if( child == -1 ) return -1;
197 ioctl(child, 8, clientaddr);
202 int recvfrom(int sockfd, void *buffer, size_t length, int flags, struct sockaddr *clientaddr, socklen_t *addrlen)
204 _CommitClient(sockfd);
205 // TODO: Determine socket type (TCP/UDP) and use a bounce-buffer for UDP
206 return read(sockfd, buffer, length);
209 int recv(int sockfd, void *buffer, size_t length, int flags)
211 _CommitClient(sockfd);
212 return read(sockfd, buffer, length);
215 int sendto(int sockfd, const void *buffer, size_t length, int flags, const struct sockaddr *clientaddr, socklen_t addrlen)
217 _CommitClient(sockfd);
218 // TODO: Determine socket type (TCP/UDP) and use a bounce-buffer for UDP
219 return write(sockfd, buffer, length);
222 int send(int sockfd, void *buffer, size_t length, int flags)
224 _CommitClient(sockfd);
225 return write(sockfd, buffer, length);