Kernel/VFS - Added warning (instead of LOG) when GetNodeFromInode is unsupported
[tpg/acess2.git] / Usermode / Applications / telnet_src / main.c
1 /*\r
2  AcessOS Test Network Application\r
3 */\r
4 #include <acess/sys.h>\r
5 #include <stdio.h>\r
6 #include <stdlib.h>\r
7 #include <net.h>\r
8 #include <readline.h>\r
9 #include <string.h>\r
10 \r
11 // === PROTOTYPES ===\r
12  int    main(int argc, char *argv[], char *envp[]);\r
13  int    OpenTCP(const char *AddressString, short PortNumber);\r
14 \r
15 // ==== CODE ====\r
16 int main(int argc, char *argv[], char *envp[])\r
17 {\r
18          int    server_fd, rv;\r
19          int    client_running = 1;\r
20          int    bUseReadline = !!argv[3];       // HACK: If third argument is present, use ReadLine\r
21         tReadline       *readline_info;\r
22          int    port;\r
23         \r
24         if( argc < 2 || argc > 3 ) {\r
25                 fprintf(stderr, "Usage: telnet <host> [<port>]\n Port defaults to 23\n");\r
26                 return 0;\r
27         }\r
28         \r
29         if(argc == 3)\r
30                 port = atoi(argv[2]);\r
31         else\r
32                 port = 23;\r
33         \r
34         // Connect to the remove server\r
35         server_fd = OpenTCP( argv[1], port );\r
36         if( server_fd == -1 ) {\r
37                 fprintf(stderr, "Unable to create socket\n");\r
38                 return -1;\r
39         }\r
40         \r
41         // Create a ReadLine instance with no history\r
42         readline_info = Readline_Init(0);\r
43         \r
44         while( client_running )\r
45         {\r
46                 fd_set  fds;\r
47                 fd_set  err_fds;\r
48                 char    buffer[BUFSIZ];\r
49                  int    len;\r
50                 \r
51                 // Prepare FD sets\r
52                 FD_ZERO(&fds);  FD_ZERO(&err_fds);\r
53                 FD_SET(0, &fds);        FD_SET(0, &err_fds);\r
54                 FD_SET(server_fd, &fds);        FD_SET(server_fd, &err_fds);\r
55                 \r
56                 // Wait for data (no timeout)\r
57                 rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);\r
58                 if( rv < 0 )    break;\r
59                 \r
60                 // Check for remote data avaliable\r
61                 if( FD_ISSET(server_fd, &fds) )\r
62                 {\r
63                         // Read from server, and write to stdout\r
64                         do\r
65                         {\r
66                                 len = _SysRead(server_fd, buffer, BUFSIZ);\r
67                                 _SysWrite(1, buffer, len);\r
68                         } while( len == BUFSIZ );\r
69                 }\r
70                 \r
71                 // Check for local data input\r
72                 if( FD_ISSET(0, &fds) )\r
73                 {\r
74                         // Read from stdin, and write to server\r
75                         if( bUseReadline )\r
76                         {\r
77                                 char    *line = Readline_NonBlock(readline_info);\r
78                                 if( line )\r
79                                 {\r
80                                         _SysWrite(server_fd, line, strlen(line));\r
81                                         _SysWrite(server_fd, "\n", 1);\r
82                                 }\r
83                         }\r
84                         else\r
85                         {\r
86                                 do\r
87                                 {\r
88                                         len = _SysRead(0, buffer, BUFSIZ);\r
89                                         _SysWrite(server_fd, buffer, len);\r
90                                         _SysWrite(1, buffer, len);\r
91                                 } while( len == BUFSIZ );\r
92                         }\r
93                 }\r
94                 \r
95                 // If there was an error, quit\r
96                 if( FD_ISSET(server_fd, &err_fds) )\r
97                 {\r
98                         printf("\nRemote connection lost\n");\r
99                         break ;\r
100                 }\r
101         }\r
102         \r
103         _SysClose(server_fd);\r
104         return 0;\r
105 }\r
106 \r
107 /**\r
108  * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber\r
109  */\r
110 int OpenTCP(const char *AddressString, short PortNumber)\r
111 {\r
112          int    fd, addrType;\r
113         uint8_t addrBuffer[16];\r
114         \r
115         // Parse IP Address\r
116         addrType = Net_ParseAddress(AddressString, addrBuffer);\r
117         if( addrType == 0 ) {\r
118                 fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);\r
119                 return -1;\r
120         }\r
121 \r
122         // Opens a R/W handle\r
123         fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");\r
124         if( fd == -1 )\r
125         {\r
126                 fprintf(stderr, "Unable to open TCP Client\n");\r
127                 return -1;\r
128         }\r
129         \r
130         // Set remote port and address\r
131         _SysIOCtl(fd, 5, &PortNumber);\r
132         _SysIOCtl(fd, 6, addrBuffer);\r
133         \r
134         // Connect\r
135         if( _SysIOCtl(fd, 7, NULL) == 0 ) {\r
136                 fprintf(stderr, "Unable to start connection\n");\r
137                 return -1;\r
138         }\r
139         \r
140         printf("Connection opened\n");\r
141         \r
142         // Return descriptor\r
143         return fd;\r
144 }\r
145 \r

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