blob: f2fcedbcf6936f689a31940fdf1096c149a57aa5 [file] [log] [blame]
Sean Middleditchda394be2009-03-15 14:19:40 -04001/*
2 * Sean Middleditch
3 * sean@sourcemud.org
4 *
5 * The author or authors of this code dedicate any and all copyright interest
6 * in this code to the public domain. We make this dedication for the benefit
7 * of the public at large and to the detriment of our heirs and successors. We
8 * intend this dedication to be an overt act of relinquishment in perpetuity of
9 * all present and future rights to this code under copyright law.
10 */
11
12#include <sys/socket.h>
13#include <netinet/in.h>
14#include <arpa/inet.h>
15#include <netdb.h>
16#include <poll.h>
17#include <errno.h>
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <ctype.h>
Sean Middleditcha7896d12009-03-15 20:41:24 -040022#include <termios.h>
Sean Middleditchda394be2009-03-15 14:19:40 -040023#include <unistd.h>
24
25#ifdef HAVE_ZLIB
26#include "zlib.h"
27#endif
28
29#include "libtelnet.h"
30
Sean Middleditcha7896d12009-03-15 20:41:24 -040031static struct termios orig_tios;
Sean Middleditchd2466a02009-09-19 14:35:48 -070032static telnet_t *telnet;
Sean Middleditcha7896d12009-03-15 20:41:24 -040033static int do_echo;
34
Sean Middleditch34bb0992009-03-21 00:20:44 -040035static const telnet_telopt_t telopts[] = {
Sean Middleditchbfc641e2009-03-22 16:26:06 -040036 { TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO },
Sean Middleditchbfc641e2009-03-22 16:26:06 -040037 { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
Sean Middleditch2d5c4992009-03-22 22:21:42 -040038 { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
39 { TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
Sean Middleditch34bb0992009-03-21 00:20:44 -040040 { -1, 0, 0 }
41};
42
Sean Middleditcha7896d12009-03-15 20:41:24 -040043static void _cleanup(void) {
44 tcsetattr(STDOUT_FILENO, TCSADRAIN, &orig_tios);
45}
46
Sean Middleditch8daf7742009-03-19 02:05:24 -040047static void _input(char *buffer, int size) {
48 static char crlf[] = { '\r', '\n' };
Sean Middleditche7c44262009-03-15 20:59:48 -040049 int i;
50
51 for (i = 0; i != size; ++i) {
52 /* if we got a CR or LF, replace with CRLF
53 * NOTE that usually you'd get a CR in UNIX, but in raw
54 * mode we get LF instead (not sure why)
55 */
56 if (buffer[i] == '\r' || buffer[i] == '\n') {
57 if (do_echo)
Sean Middleditch54c1e3e2009-08-31 15:04:39 -070058 printf("\r\n");
Sean Middleditchd2466a02009-09-19 14:35:48 -070059 telnet_send(telnet, crlf, 2);
Sean Middleditche7c44262009-03-15 20:59:48 -040060 } else {
61 if (do_echo)
Sean Middleditch54c1e3e2009-08-31 15:04:39 -070062 putchar(buffer[i]);
Sean Middleditchd2466a02009-09-19 14:35:48 -070063 telnet_send(telnet, buffer + i, 1);
Sean Middleditche7c44262009-03-15 20:59:48 -040064 }
65 }
Sean Middleditch54c1e3e2009-08-31 15:04:39 -070066 fflush(stdout);
Sean Middleditche7c44262009-03-15 20:59:48 -040067}
68
Sean Middleditch340a51b2009-03-19 02:08:46 -040069static void _send(int sock, const char *buffer, size_t size) {
Sean Middleditchda394be2009-03-15 14:19:40 -040070 int rs;
71
72 /* send data */
73 while (size > 0) {
74 if ((rs = send(sock, buffer, size, 0)) == -1) {
75 fprintf(stderr, "send() failed: %s\n", strerror(errno));
76 exit(1);
77 } else if (rs == 0) {
78 fprintf(stderr, "send() unexpectedly returned 0\n");
79 exit(1);
80 }
81
82 /* update pointer and size to see if we've got more to send */
83 buffer += rs;
84 size -= rs;
85 }
86}
87
Sean Middleditchf65f27d2009-03-19 02:32:04 -040088static void _event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -040089 void *user_data) {
Sean Middleditchda394be2009-03-15 14:19:40 -040090 int sock = *(int*)user_data;
91
92 switch (ev->type) {
93 /* data received */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040094 case TELNET_EV_DATA:
Sean Middleditch4adfdf32009-08-31 15:06:09 -070095 printf("%.*s", (int)ev->size, ev->buffer);
Sean Middleditchda394be2009-03-15 14:19:40 -040096 break;
97 /* data must be sent */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040098 case TELNET_EV_SEND:
Sean Middleditchda394be2009-03-15 14:19:40 -040099 _send(sock, ev->buffer, ev->size);
100 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400101 /* request to enable remote feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400102 case TELNET_EV_WILL:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400103 /* we'll agree to turn off our echo if server wants us to stop */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400104 if (ev->telopt == TELNET_TELOPT_ECHO)
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400105 do_echo = 0;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400106 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400107 /* notification of disabling remote feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400108 case TELNET_EV_WONT:
109 if (ev->telopt == TELNET_TELOPT_ECHO)
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400110 do_echo = 1;
111 break;
112 /* request to enable local feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400113 case TELNET_EV_DO:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400114 break;
115 /* demand to disable local feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400116 case TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400117 break;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400118 /* respond to particular subnegotiations */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400119 case TELNET_EV_SUBNEGOTIATION:
Sean Middleditcheb950a82009-03-22 23:04:28 -0400120 /* if they just asked for our terminal type, response with it */
Sean Middleditcha7896d12009-03-15 20:41:24 -0400121 /* respond with our terminal type */
Sean Middleditcheb950a82009-03-22 23:04:28 -0400122 if (ev->telopt == TELNET_TELOPT_TTYPE &&
123 ev->argc >= 1 && ev->argv[0][0] == TELNET_TTYPE_SEND) {
124 telnet_format_sb(telnet, TELNET_TELOPT_TTYPE, 1,
125 TELNET_TTYPE_IS, getenv("TERM"));
Sean Middleditcha7896d12009-03-15 20:41:24 -0400126 }
Sean Middleditchda394be2009-03-15 14:19:40 -0400127 break;
128 /* error */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400129 case TELNET_EV_ERROR:
Sean Middleditch340a51b2009-03-19 02:08:46 -0400130 fprintf(stderr, "ERROR: %s\n", ev->buffer);
Sean Middleditchda394be2009-03-15 14:19:40 -0400131 exit(1);
132 default:
133 /* ignore */
134 break;
135 }
136}
137
Harald Weltea3c62232010-03-25 21:33:21 +0800138extern int ipaccess_telnet_auth(int sock);
139
Sean Middleditchda394be2009-03-15 14:19:40 -0400140int main(int argc, char **argv) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400141 char buffer[512];
Sean Middleditchda394be2009-03-15 14:19:40 -0400142 int rs;
143 int sock;
144 struct sockaddr_in addr;
145 struct pollfd pfd[2];
Sean Middleditchda394be2009-03-15 14:19:40 -0400146 struct addrinfo *ai;
147 struct addrinfo hints;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400148 struct termios tios;
Sean Middleditchda394be2009-03-15 14:19:40 -0400149
150 /* check usage */
151 if (argc != 3) {
152 fprintf(stderr, "Usage:\n ./telnet-client <host> <port>\n");
153 return 1;
154 }
155
156 /* look up server host */
157 memset(&hints, 0, sizeof(hints));
158 hints.ai_family = AF_UNSPEC;
159 hints.ai_socktype = SOCK_STREAM;
160 if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) {
161 fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1],
162 gai_strerror(rs));
163 return 1;
164 }
165
166 /* create server socket */
167 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
168 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
169 return 1;
170 }
171
172 /* bind server socket */
173 memset(&addr, 0, sizeof(addr));
174 addr.sin_family = AF_INET;
175 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
176 fprintf(stderr, "bind() failed: %s\n", strerror(errno));
177 return 1;
178 }
179
180 /* connect */
181 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
182 fprintf(stderr, "server() failed: %s\n", strerror(errno));
183 return 1;
184 }
185
186 /* free address lookup info */
187 freeaddrinfo(ai);
188
Harald Weltea3c62232010-03-25 21:33:21 +0800189#if NANO_BTS_CLI_CLIENT
190 ipaccess_telnet_auth(sock);
191#endif
192
Sean Middleditcha7896d12009-03-15 20:41:24 -0400193 /* get current terminal settings, set raw mode, make sure we
194 * register atexit handler to restore terminal settings
195 */
196 tcgetattr(STDOUT_FILENO, &orig_tios);
197 atexit(_cleanup);
198 tios = orig_tios;
199 cfmakeraw(&tios);
200 tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios);
201
202 /* set input echoing on by default */
203 do_echo = 1;
204
Sean Middleditchda394be2009-03-15 14:19:40 -0400205 /* initialize telnet box */
Sean Middleditchd2466a02009-09-19 14:35:48 -0700206 telnet = telnet_init(telopts, _event_handler, 0, &sock);
Sean Middleditchda394be2009-03-15 14:19:40 -0400207
208 /* initialize poll descriptors */
209 memset(pfd, 0, sizeof(pfd));
210 pfd[0].fd = STDIN_FILENO;
211 pfd[0].events = POLLIN;
212 pfd[1].fd = sock;
213 pfd[1].events = POLLIN;
214
215 /* loop while both connections are open */
216 while (poll(pfd, 2, -1) != -1) {
217 /* read from stdin */
218 if (pfd[0].revents & POLLIN) {
219 if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
Sean Middleditche7c44262009-03-15 20:59:48 -0400220 _input(buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400221 } else if (rs == 0) {
222 break;
223 } else {
224 fprintf(stderr, "recv(server) failed: %s\n",
225 strerror(errno));
226 exit(1);
227 }
228 }
229
230 /* read from client */
231 if (pfd[1].revents & POLLIN) {
232 if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
Sean Middleditchd2466a02009-09-19 14:35:48 -0700233 telnet_recv(telnet, buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400234 } else if (rs == 0) {
235 break;
236 } else {
237 fprintf(stderr, "recv(client) failed: %s\n",
238 strerror(errno));
239 exit(1);
240 }
241 }
242 }
243
244 /* clean up */
Sean Middleditchd2466a02009-09-19 14:35:48 -0700245 telnet_free(telnet);
Sean Middleditchda394be2009-03-15 14:19:40 -0400246 close(sock);
247
248 return 0;
249}