From 7b3897ab98e2a562aacaacb80e3ac2a227ca0161 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 1 Mar 2011 15:02:09 +0800 Subject: [PATCH] Added telnet client --- Usermode/Applications/telnet_src/Makefile | 10 ++ Usermode/Applications/telnet_src/main.c | 123 ++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 Usermode/Applications/telnet_src/Makefile create mode 100644 Usermode/Applications/telnet_src/main.c diff --git a/Usermode/Applications/telnet_src/Makefile b/Usermode/Applications/telnet_src/Makefile new file mode 100644 index 00000000..ff415eec --- /dev/null +++ b/Usermode/Applications/telnet_src/Makefile @@ -0,0 +1,10 @@ +# Project: telnet + +-include ../Makefile.cfg + +LDFLAGS += -lnet + +OBJ = main.o +BIN = telnet + +-include ../Makefile.tpl diff --git a/Usermode/Applications/telnet_src/main.c b/Usermode/Applications/telnet_src/main.c new file mode 100644 index 00000000..6c4f892e --- /dev/null +++ b/Usermode/Applications/telnet_src/main.c @@ -0,0 +1,123 @@ +/* + AcessOS Test Network Application +*/ +#include +#include +#include + +#define BUFSIZ 128 + +// === PROTOTYPES === + int main(int argc, char *argv[], char *envp[]); + int OpenTCP(const char *AddressString, short PortNumber); + +// ==== CODE ==== +int main(int argc, char *argv[], char *envp[]) +{ + int server_fd, rv; + int client_running = 1; + + // Connect to the remove server + server_fd = OpenTCP( argv[1], atoi(argv[2]) ); + if( server_fd == -1 ) { + fprintf(stderr, "Unable to create socket\n"); + return -1; + } + + while( client_running ) + { + fd_set fds; + char buffer[BUFSIZ]; + int len; + + FD_ZERO(&fds); + FD_SET(0, &fds); + FD_SET(server_fd, &fds); + + rv = select(server_fd+1, &fds, NULL, NULL, NULL); + if( rv < 0 ) break; + + if( FD_ISSET(server_fd, &fds) ) + { + // Read from server, and write to stdout + do + { + len = read(server_fd, BUFSIZ, buffer); + write(1, len, buffer); + } while( len == BUFSIZ ); + } + + if( FD_ISSET(0, &fds) ) + { + // Read from stdin, and write to server + do + { + len = read(0, BUFSIZ, buffer); + write(server_fd, len, buffer); + write(1, len, buffer); + } while( len == BUFSIZ ); + } + } + + close(server_fd); + return 0; +} + +/** + * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber + */ +int OpenTCP(const char *AddressString, short PortNumber) +{ + int fd, addrType; + char *iface; + char addrBuffer[8]; + + // Parse IP Address + addrType = Net_ParseAddress(AddressString, addrBuffer); + if( addrType == 0 ) { + 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? + { + 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"); + return -1; + } + + // Set remote port and address + printf("Setting port and remote address\n"); + ioctl(fd, 5, &PortNumber); + ioctl(fd, 6, addrBuffer); + + // Connect + printf("Initiating connection\n"); + if( ioctl(fd, 7, NULL) == 0 ) { + // Shouldn't happen :( + fprintf(stderr, "Unable to start connection\n"); + return -1; + } + + // Return descriptor + return fd; +} + -- 2.20.1