Added telnet client
authorJohn Hodge <[email protected]>
Tue, 1 Mar 2011 07:02:09 +0000 (15:02 +0800)
committerJohn Hodge <[email protected]>
Tue, 1 Mar 2011 07:02:09 +0000 (15:02 +0800)
Usermode/Applications/telnet_src/Makefile [new file with mode: 0644]
Usermode/Applications/telnet_src/main.c [new file with mode: 0644]

diff --git a/Usermode/Applications/telnet_src/Makefile b/Usermode/Applications/telnet_src/Makefile
new file mode 100644 (file)
index 0000000..ff415ee
--- /dev/null
@@ -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 (file)
index 0000000..6c4f892
--- /dev/null
@@ -0,0 +1,123 @@
+/*\r
+ AcessOS Test Network Application\r
+*/\r
+#include <acess/sys.h>\r
+#include <stdio.h>\r
+#include <net.h>\r
+\r
+#define BUFSIZ 128\r
+\r
+// === PROTOTYPES ===\r
+ int   main(int argc, char *argv[], char *envp[]);\r
+ int   OpenTCP(const char *AddressString, short PortNumber);\r
+\r
+// ==== CODE ====\r
+int main(int argc, char *argv[], char *envp[])\r
+{\r
+        int    server_fd, rv;\r
+        int    client_running = 1;\r
+       \r
+       // Connect to the remove server\r
+       server_fd = OpenTCP( argv[1], atoi(argv[2]) );\r
+       if( server_fd == -1 ) {\r
+               fprintf(stderr, "Unable to create socket\n");\r
+               return -1;\r
+       }\r
+       \r
+       while( client_running )\r
+       {\r
+               fd_set  fds;\r
+               char    buffer[BUFSIZ];\r
+                int    len;\r
+               \r
+               FD_ZERO(&fds);\r
+               FD_SET(0, &fds);\r
+               FD_SET(server_fd, &fds);\r
+               \r
+               rv = select(server_fd+1, &fds, NULL, NULL, NULL);\r
+               if( rv < 0 )    break;\r
+               \r
+               if( FD_ISSET(server_fd, &fds) )\r
+               {\r
+                       // Read from server, and write to stdout\r
+                       do\r
+                       {\r
+                               len = read(server_fd, BUFSIZ, buffer);\r
+                               write(1, len, buffer);\r
+                       } while( len == BUFSIZ );\r
+               }\r
+               \r
+               if( FD_ISSET(0, &fds) )\r
+               {\r
+                       // Read from stdin, and write to server\r
+                       do\r
+                       {\r
+                               len = read(0, BUFSIZ, buffer);\r
+                               write(server_fd, len, buffer);\r
+                               write(1, len, buffer);\r
+                       } while( len == BUFSIZ );\r
+               }\r
+       }\r
+       \r
+       close(server_fd);\r
+       return 0;\r
+}\r
+\r
+/**\r
+ * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber\r
+ */\r
+int OpenTCP(const char *AddressString, short PortNumber)\r
+{\r
+        int    fd, addrType;\r
+       char    *iface;\r
+       char    addrBuffer[8];\r
+       \r
+       // Parse IP Address\r
+       addrType = Net_ParseAddress(AddressString, addrBuffer);\r
+       if( addrType == 0 ) {\r
+               fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);\r
+               return -1;\r
+       }\r
+       \r
+       // Finds the interface for the destination address\r
+       iface = Net_GetInterface(addrType, addrBuffer);\r
+       if( iface == NULL ) {\r
+               fprintf(stderr, "Unable to find a route to '%s'\n", AddressString);\r
+               return -1;\r
+       }\r
+       \r
+       printf("iface = '%s'\n", iface);\r
+       \r
+       // Open client socket\r
+       // TODO: Move this out to libnet?\r
+       {\r
+                int    len = snprintf(NULL, 100, "/Devices/ip/%s/tcpc", iface);\r
+               char    path[len+1];\r
+               snprintf(path, 100, "/Devices/ip/%s/tcpc", iface);\r
+               fd = open(path, OPENFLAG_READ|OPENFLAG_WRITE);\r
+       }\r
+       \r
+       free(iface);\r
+       \r
+       if( fd == -1 ) {\r
+               fprintf(stderr, "Unable to open TCP Client for reading\n");\r
+               return -1;\r
+       }\r
+       \r
+       // Set remote port and address\r
+       printf("Setting port and remote address\n");\r
+       ioctl(fd, 5, &PortNumber);\r
+       ioctl(fd, 6, addrBuffer);\r
+       \r
+       // Connect\r
+       printf("Initiating connection\n");\r
+       if( ioctl(fd, 7, NULL) == 0 ) {\r
+               // Shouldn't happen :(\r
+               fprintf(stderr, "Unable to start connection\n");\r
+               return -1;\r
+       }\r
+       \r
+       // Return descriptor\r
+       return fd;\r
+}\r
+\r

UCC git Repository :: git.ucc.asn.au