X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Ftelnet_src%2Fmain.c;h=2658548ecc4c8f52df3ad91fdbb2bb4f60c019e5;hb=16452b14885a8046adef7ec4ca9b9083a9ba2818;hp=6c4f892ec69a195e386674abe6a9cc8c5de0ae76;hpb=7b3897ab98e2a562aacaacb80e3ac2a227ca0161;p=tpg%2Facess2.git diff --git a/Usermode/Applications/telnet_src/main.c b/Usermode/Applications/telnet_src/main.c index 6c4f892e..2658548e 100644 --- a/Usermode/Applications/telnet_src/main.c +++ b/Usermode/Applications/telnet_src/main.c @@ -3,9 +3,10 @@ */ #include #include +#include #include - -#define BUFSIZ 128 +#include +#include // === PROTOTYPES === int main(int argc, char *argv[], char *envp[]); @@ -16,50 +17,90 @@ int main(int argc, char *argv[], char *envp[]) { int server_fd, rv; int client_running = 1; + int bUseReadline = !!argv[3]; // HACK: If third argument is present, use ReadLine + tReadline *readline_info; + int port; + + if( argc < 2 || argc > 3 ) { + fprintf(stderr, "Usage: telnet []\n Port defaults to 23\n"); + return 0; + } + + if(argc == 3) + port = atoi(argv[2]); + else + port = 23; // Connect to the remove server - server_fd = OpenTCP( argv[1], atoi(argv[2]) ); + server_fd = OpenTCP( argv[1], port ); if( server_fd == -1 ) { fprintf(stderr, "Unable to create socket\n"); return -1; } + // Create a ReadLine instance with no history + readline_info = Readline_Init(0); + while( client_running ) { fd_set fds; + fd_set err_fds; char buffer[BUFSIZ]; int len; - FD_ZERO(&fds); - FD_SET(0, &fds); - FD_SET(server_fd, &fds); + // Prepare FD sets + FD_ZERO(&fds); FD_ZERO(&err_fds); + FD_SET(0, &fds); FD_SET(0, &err_fds); + FD_SET(server_fd, &fds); FD_SET(server_fd, &err_fds); - rv = select(server_fd+1, &fds, NULL, NULL, NULL); + // Wait for data (no timeout) + rv = select(server_fd+1, &fds, NULL, &err_fds, NULL); if( rv < 0 ) break; + // Check for remote data avaliable if( FD_ISSET(server_fd, &fds) ) { // Read from server, and write to stdout do { - len = read(server_fd, BUFSIZ, buffer); - write(1, len, buffer); + len = _SysRead(server_fd, buffer, BUFSIZ); + _SysWrite(1, buffer, len); } while( len == BUFSIZ ); } + // Check for local data input if( FD_ISSET(0, &fds) ) { // Read from stdin, and write to server - do + if( bUseReadline ) { - len = read(0, BUFSIZ, buffer); - write(server_fd, len, buffer); - write(1, len, buffer); - } while( len == BUFSIZ ); + char *line = Readline_NonBlock(readline_info); + if( line ) + { + _SysWrite(server_fd, line, strlen(line)); + _SysWrite(server_fd, "\n", 1); + } + } + else + { + do + { + len = _SysRead(0, buffer, BUFSIZ); + _SysWrite(server_fd, buffer, len); + _SysWrite(1, buffer, len); + } while( len == BUFSIZ ); + } + } + + // If there was an error, quit + if( FD_ISSET(server_fd, &err_fds) ) + { + printf("\nRemote connection lost\n"); + break ; } } - close(server_fd); + _SysClose(server_fd); return 0; } @@ -69,8 +110,7 @@ int main(int argc, char *argv[], char *envp[]) int OpenTCP(const char *AddressString, short PortNumber) { int fd, addrType; - char *iface; - char addrBuffer[8]; + uint8_t addrBuffer[16]; // Parse IP Address addrType = Net_ParseAddress(AddressString, addrBuffer); @@ -78,45 +118,27 @@ int OpenTCP(const char *AddressString, short PortNumber) fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString); return -1; } - - // Finds the interface for the destination address - iface = Net_GetInterface(addrType, addrBuffer); - if( iface == NULL ) { - fprintf(stderr, "Unable to find a route to '%s'\n", AddressString); - return -1; - } - - printf("iface = '%s'\n", iface); - - // Open client socket - // TODO: Move this out to libnet? + + // Opens a R/W handle + fd = Net_OpenSocket(addrType, addrBuffer, "tcpc"); + if( fd == -1 ) { - int len = snprintf(NULL, 100, "/Devices/ip/%s/tcpc", iface); - char path[len+1]; - snprintf(path, 100, "/Devices/ip/%s/tcpc", iface); - fd = open(path, OPENFLAG_READ|OPENFLAG_WRITE); - } - - free(iface); - - if( fd == -1 ) { - fprintf(stderr, "Unable to open TCP Client for reading\n"); + fprintf(stderr, "Unable to open TCP Client\n"); return -1; } // Set remote port and address - printf("Setting port and remote address\n"); - ioctl(fd, 5, &PortNumber); - ioctl(fd, 6, addrBuffer); + _SysIOCtl(fd, 5, &PortNumber); + _SysIOCtl(fd, 6, addrBuffer); // Connect - printf("Initiating connection\n"); - if( ioctl(fd, 7, NULL) == 0 ) { - // Shouldn't happen :( + if( _SysIOCtl(fd, 7, NULL) == 0 ) { fprintf(stderr, "Unable to start connection\n"); return -1; } + printf("Connection opened\n"); + // Return descriptor return fd; }