2 * Acess2 Telnet Server (TCP server test case)
3 * - By John Hodge (thePowersGang)
11 #include <acess/sys.h>
14 typedef struct sClient
23 void Server_NewClient(int FD);
24 void HandleServerBoundData(tClient *Client);
25 void HandleClientBoundData(tClient *Client);
28 // --- Configuration ---
29 int giConfig_MaxClients = 5;
36 int main(int argc, char *argv[])
41 gaClients = calloc(giConfig_MaxClients, sizeof(tClient));
46 int addrtype = Net_ParseAddress("10.0.2.10", data);
48 giServerFD = Net_OpenSocket(addrtype, data, "tcps");
49 _SysIOCtl(giServerFD, 4, &port); // Set port and start listening
63 void FD_SET_MAX(fd_set *set, int fd, int *maxfd)
66 if(*maxfd < fd) *maxfd = fd;
74 FD_SET_MAX(&fds, giServerFD, &maxfd);
75 for( int i = 0; i < giConfig_MaxClients; i ++ )
77 if( gaClients[i].Socket == 0 ) continue ;
78 _SysDebug("Socket = %i, stdout = %i",
79 gaClients[i].Socket, gaClients[i].stdout);
80 FD_SET_MAX(&fds, gaClients[i].Socket, &maxfd);
81 FD_SET_MAX(&fds, gaClients[i].stdout, &maxfd);
85 _SysSelect( maxfd+1, &fds, NULL, NULL, NULL, 0 );
88 if( FD_ISSET(giServerFD, &fds) )
90 Server_NewClient(giServerFD);
92 for( int i = 0; i < giConfig_MaxClients; i ++ )
94 if( FD_ISSET(gaClients[i].Socket, &fds) )
97 HandleServerBoundData(&gaClients[i]);
99 if( FD_ISSET(gaClients[i].stdout, &fds) )
101 // Handle output from terminal
102 HandleClientBoundData(&gaClients[i]);
108 void Server_NewClient(int FD)
112 // TODO: Is this done in the IPStack?
113 if( giNumClients == giConfig_MaxClients )
116 _SysClose( _SysOpenChild(FD, "", OPENFLAG_READ) );
120 // Allocate client structure and open socket
121 for( int i = 0; i < giConfig_MaxClients; i ++ )
123 if( gaClients[i].Socket == 0 ) {
128 // Accept the connection
129 clt->Socket = _SysOpenChild(FD, "", OPENFLAG_READ|OPENFLAG_WRITE);
132 // Create stdin/stdout
133 clt->stdin = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
134 clt->stdout = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
136 // TODO: Arguments and envp
138 int fds[3] = {clt->stdin, clt->stdout, clt->stdout};
139 const char *argv[] = {NULL};
140 _SysSpawn("/Acess/SBin/login", argv, argv, 3, fds, NULL);
144 void HandleServerBoundData(tClient *Client)
149 len = _SysRead(Client->Socket, buf, BUFSIZ);
150 if( len <= 0 ) return ;
151 _SysWrite(Client->stdin, buf, len);
154 void HandleClientBoundData(tClient *Client)
159 len = _SysRead(Client->stdout, buf, BUFSIZ);
160 if( len <= 0 ) return ;
161 _SysWrite(Client->Socket, buf, len);