Usermode/telnet - Moved to new routing API
authorJohn Hodge <[email protected]>
Wed, 23 Mar 2011 05:36:08 +0000 (13:36 +0800)
committerJohn Hodge <[email protected]>
Wed, 23 Mar 2011 05:36:08 +0000 (13:36 +0800)
Usermode/Applications/telnet_src/Makefile
Usermode/Applications/telnet_src/main.c

index ff415ee..bbcac85 100644 (file)
@@ -2,7 +2,7 @@
 
 -include ../Makefile.cfg
 
-LDFLAGS += -lnet
+LDFLAGS += -lnet -lreadline
 
 OBJ = main.o
 BIN = telnet
index f4f048a..519978d 100644 (file)
@@ -4,6 +4,8 @@
 #include <acess/sys.h>\r
 #include <stdio.h>\r
 #include <net.h>\r
+#include <readline.h>\r
+#include <string.h>\r
 \r
 #define BUFSIZ 128\r
 \r
@@ -16,6 +18,8 @@ int main(int argc, char *argv[], char *envp[])
 {\r
         int    server_fd, rv;\r
         int    client_running = 1;\r
+        int    bUseReadline = !!argv[3];       // HACK: If third argument is present, use ReadLine\r
+       tReadline       *readline_info;\r
        \r
        // Connect to the remove server\r
        server_fd = OpenTCP( argv[1], atoi(argv[2]) );\r
@@ -24,6 +28,9 @@ int main(int argc, char *argv[], char *envp[])
                return -1;\r
        }\r
        \r
+       // Create a ReadLine instance with no history\r
+       readline_info = Readline_Init(0);\r
+       \r
        while( client_running )\r
        {\r
                fd_set  fds;\r
@@ -31,13 +38,16 @@ int main(int argc, char *argv[], char *envp[])
                char    buffer[BUFSIZ];\r
                 int    len;\r
                \r
+               // Prepare FD sets\r
                FD_ZERO(&fds);  FD_ZERO(&err_fds);\r
                FD_SET(0, &fds);        FD_SET(0, &err_fds);\r
                FD_SET(server_fd, &fds);        FD_SET(server_fd, &err_fds);\r
                \r
+               // Wait for data (no timeout)\r
                rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);\r
                if( rv < 0 )    break;\r
                \r
+               // Check for remote data avaliable\r
                if( FD_ISSET(server_fd, &fds) )\r
                {\r
                        // Read from server, and write to stdout\r
@@ -48,19 +58,34 @@ int main(int argc, char *argv[], char *envp[])
                        } while( len == BUFSIZ );\r
                }\r
                \r
+               // Check for local data input\r
                if( FD_ISSET(0, &fds) )\r
                {\r
                        // Read from stdin, and write to server\r
-                       do\r
+                       if( bUseReadline )\r
                        {\r
-                               len = read(0, BUFSIZ, buffer);\r
-                               write(server_fd, len, buffer);\r
-                               write(1, len, buffer);\r
-                       } while( len == BUFSIZ );\r
+                               char    *line = Readline_NonBlock(readline_info);\r
+                               if( line )\r
+                               {\r
+                                       write(server_fd, strlen(line), line);\r
+                                       write(server_fd, 1, "\n");\r
+                               }\r
+                       }\r
+                       else\r
+                       {\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
+               // If there was an error, quit\r
                if( FD_ISSET(server_fd, &err_fds) )\r
                {\r
+                       printf("\nRemote connection lost\n");\r
                        break ;\r
                }\r
        }\r
@@ -76,7 +101,7 @@ int OpenTCP(const char *AddressString, short PortNumber)
 {\r
         int    fd, addrType;\r
        char    *iface;\r
-       char    addrBuffer[8];\r
+       uint8_t addrBuffer[16];\r
        \r
        // Parse IP Address\r
        addrType = Net_ParseAddress(AddressString, addrBuffer);\r
@@ -84,45 +109,27 @@ int OpenTCP(const char *AddressString, short PortNumber)
                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
+       // Opens a R/W handle\r
+       fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");\r
+       if( fd == -1 )\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
+               fprintf(stderr, "Unable to open TCP Client\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
+       printf("Connection opened\n");\r
+       \r
        // Return descriptor\r
        return fd;\r
 }\r

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