2 * Acess2 Telnet Server (TCP server test case)
3 * - By John Hodge (thePowersGang)
12 #include <acess/sys.h>
16 typedef struct sClient
25 void Server_NewClient(int FD);
26 void HandleServerBoundData(tClient *Client);
27 void HandleClientBoundData(tClient *Client);
30 // --- Configuration ---
31 int giConfig_MaxClients = 5;
38 int main(int argc, char *argv[])
43 gaClients = calloc(giConfig_MaxClients, sizeof(tClient));
48 int addrtype = Net_ParseAddress("10.0.2.10", data);
50 giServerFD = Net_OpenSocket(addrtype, data, "tcps");
51 _SysIOCtl(giServerFD, 4, &port); // Set port and start listening
60 static void FD_SET_MAX(fd_set *set, int fd, int *maxfd)
63 if(*maxfd < fd) *maxfd = fd;
76 FD_SET_MAX(&fds, giServerFD, &maxfd);
77 for( int i = 0; i < giConfig_MaxClients; i ++ )
79 if( gaClients[i].Socket == 0 ) continue ;
80 _SysDebug("Socket = %i, stdout = %i",
81 gaClients[i].Socket, gaClients[i].stdout);
82 FD_SET_MAX(&fds, gaClients[i].Socket, &maxfd);
83 FD_SET_MAX(&fds, gaClients[i].stdout, &maxfd);
87 _SysSelect( maxfd+1, &fds, NULL, NULL, NULL, 0 );
90 if( FD_ISSET(giServerFD, &fds) )
92 Server_NewClient(giServerFD);
94 for( int i = 0; i < giConfig_MaxClients; i ++ )
96 if( FD_ISSET(gaClients[i].Socket, &fds) )
99 HandleServerBoundData(&gaClients[i]);
101 if( FD_ISSET(gaClients[i].stdout, &fds) )
103 // Handle output from terminal
104 HandleClientBoundData(&gaClients[i]);
110 void Server_NewClient(int FD)
114 // TODO: Is this done in the IPStack?
115 if( giNumClients == giConfig_MaxClients )
118 _SysClose( _SysOpenChild(FD, "", OPENFLAG_READ) );
122 // Allocate client structure and open socket
123 for( int i = 0; i < giConfig_MaxClients; i ++ )
125 if( gaClients[i].Socket == 0 ) {
131 // Accept the connection
132 clt->Socket = _SysOpenChild(FD, "", OPENFLAG_READ|OPENFLAG_WRITE);
135 // Create stdin/stdout
136 clt->stdin = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
137 clt->stdout = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
139 // TODO: Arguments and envp
141 int fds[3] = {clt->stdin, clt->stdout, clt->stdout};
142 const char *argv[] = {NULL};
143 _SysSpawn("/Acess/SBin/login", argv, argv, 3, fds, NULL);
147 void HandleServerBoundData(tClient *Client)
152 len = _SysRead(Client->Socket, buf, BUFSIZ);
153 if( len <= 0 ) return ;
154 _SysWrite(Client->stdin, buf, len);
157 void HandleClientBoundData(tClient *Client)
162 len = _SysRead(Client->stdout, buf, BUFSIZ);
163 if( len <= 0 ) return ;
164 _SysWrite(Client->Socket, buf, len);