2 * Acess2 Telnet Server (TCP server test case)
3 * - By John Hodge (thePowersGang)
12 #include <acess/sys.h>
15 typedef struct sClient
24 void Server_NewClient(int FD);
25 void HandleServerBoundData(tClient *Client);
26 void HandleClientBoundData(tClient *Client);
29 // --- Configuration ---
30 int giConfig_MaxClients = 5;
37 int main(int argc, char *argv[])
42 gaClients = calloc(giConfig_MaxClients, sizeof(tClient));
47 int addrtype = Net_ParseAddress("10.0.2.10", data);
49 giServerFD = Net_OpenSocket(addrtype, data, "tcps");
50 _SysIOCtl(giServerFD, 4, &port); // Set port and start listening
59 static void FD_SET_MAX(fd_set *set, int fd, int *maxfd)
62 if(*maxfd < fd) *maxfd = fd;
75 FD_SET_MAX(&fds, giServerFD, &maxfd);
76 for( int i = 0; i < giConfig_MaxClients; i ++ )
78 if( gaClients[i].Socket == 0 ) continue ;
79 _SysDebug("Socket = %i, stdout = %i",
80 gaClients[i].Socket, gaClients[i].stdout);
81 FD_SET_MAX(&fds, gaClients[i].Socket, &maxfd);
82 FD_SET_MAX(&fds, gaClients[i].stdout, &maxfd);
86 _SysSelect( maxfd+1, &fds, NULL, NULL, NULL, 0 );
89 if( FD_ISSET(giServerFD, &fds) )
91 Server_NewClient(giServerFD);
93 for( int i = 0; i < giConfig_MaxClients; i ++ )
95 if( FD_ISSET(gaClients[i].Socket, &fds) )
98 HandleServerBoundData(&gaClients[i]);
100 if( FD_ISSET(gaClients[i].stdout, &fds) )
102 // Handle output from terminal
103 HandleClientBoundData(&gaClients[i]);
109 void Server_NewClient(int FD)
113 // TODO: Is this done in the IPStack?
114 if( giNumClients == giConfig_MaxClients )
117 _SysClose( _SysOpenChild(FD, "", OPENFLAG_READ) );
121 // Allocate client structure and open socket
122 for( int i = 0; i < giConfig_MaxClients; i ++ )
124 if( gaClients[i].Socket == 0 ) {
129 // Accept the connection
130 clt->Socket = _SysOpenChild(FD, "", OPENFLAG_READ|OPENFLAG_WRITE);
133 // Create stdin/stdout
134 clt->stdin = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
135 clt->stdout = _SysOpen("/Devices/fifo/anon", OPENFLAG_READ|OPENFLAG_WRITE);
137 // TODO: Arguments and envp
139 int fds[3] = {clt->stdin, clt->stdout, clt->stdout};
140 const char *argv[] = {NULL};
141 _SysSpawn("/Acess/SBin/login", argv, argv, 3, fds, NULL);
145 void HandleServerBoundData(tClient *Client)
150 len = _SysRead(Client->Socket, buf, BUFSIZ);
151 if( len <= 0 ) return ;
152 _SysWrite(Client->stdin, buf, len);
155 void HandleClientBoundData(tClient *Client)
160 len = _SysRead(Client->stdout, buf, BUFSIZ);
161 if( len <= 0 ) return ;
162 _SysWrite(Client->Socket, buf, len);