18 void Server_NewClient(int FD);
19 void HandleServerBoundData(tClient *Client);
20 void HandleClientBoundData(tClient *Client);
23 // --- Configuration ---
24 int giConfig_MaxClients = 5;
31 int main(int argc, char *argv[])
36 gaClients = calloc(giConfig_MaxClients, sizeof(tClient));
41 int addrtype = Net_ParseAddress("10.0.2.10", data);
43 giServerFD = Net_OpenSocket(addrtype, data, "tcps");
44 ioctl(giServerFD, 4, &port); // Set port and start listening
58 void FD_SET_MAX(fd_set *set, int fd, int *maxfd)
61 if(*maxfd < fd) *maxfd = fd;
69 FD_SET_MAX(&fds, giServerFD, &maxfd);
70 for( int i = 0; i < giConfig_MaxClients; i ++ )
72 if( gaClients[i].Socket == 0 ) continue ;
73 _SysDebug("Socket = %i, stdout = %i",
74 gaClients[i].Socket, gaClients[i].stdout);
75 FD_SET_MAX(&fds, gaClients[i].Socket, &maxfd);
76 FD_SET_MAX(&fds, gaClients[i].stdout, &maxfd);
80 select( maxfd+1, &fds, NULL, NULL, NULL );
83 if( FD_ISSET(giServerFD, &fds) )
85 Server_NewClient(giServerFD);
87 for( int i = 0; i < giConfig_MaxClients; i ++ )
89 if( FD_ISSET(gaClients[i].Socket, &fds) )
92 HandleServerBoundData(&gaClients[i]);
94 if( FD_ISSET(gaClients[i].stdout, &fds) )
96 // Handle output from terminal
97 HandleClientBoundData(&gaClients[i]);
103 void Server_NewClient(int FD)
107 // TODO: Is this done in the IPStack?
108 if( giNumClients == giConfig_MaxClients )
111 close( _SysOpenChild(FD, "", O_RDWR) );
115 // Allocate client structure and open socket
116 for( int i = 0; i < giConfig_MaxClients; i ++ )
118 if( gaClients[i].Socket == 0 ) {
123 // Accept the connection
124 clt->Socket = _SysOpenChild(FD, "", O_RDWR);
127 // Create stdin/stdout
128 clt->stdin = open("/Devices/fifo/anon", O_RDWR);
129 clt->stdout = open("/Devices/fifo/anon", O_RDWR);
131 // TODO: Arguments and envp
133 int fds[3] = {clt->stdin, clt->stdout, clt->stdout};
134 const char *argv[] = {NULL};
135 _SysSpawn("/Acess/SBin/login", argv, argv, 3, fds);
139 void HandleServerBoundData(tClient *Client)
144 len = read(Client->Socket, buf, BUFSIZ);
145 if( len <= 0 ) return ;
146 write(Client->stdin, buf, len);
149 void HandleClientBoundData(tClient *Client)
154 len = read(Client->stdout, buf, BUFSIZ);
155 if( len <= 0 ) return ;
156 write(Client->Socket, buf, len);