blob: 55656562a216d82b8fccf7306e76a612cd95e81a [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
138int main(int argc, char **argv) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400139 char buffer[512];
Sean Middleditchda394be2009-03-15 14:19:40 -0400140 int rs;
141 int sock;
142 struct sockaddr_in addr;
143 struct pollfd pfd[2];
Sean Middleditchda394be2009-03-15 14:19:40 -0400144 struct addrinfo *ai;
145 struct addrinfo hints;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400146 struct termios tios;
Sean Middleditchda394be2009-03-15 14:19:40 -0400147
148 /* check usage */
149 if (argc != 3) {
150 fprintf(stderr, "Usage:\n ./telnet-client <host> <port>\n");
151 return 1;
152 }
153
154 /* look up server host */
155 memset(&hints, 0, sizeof(hints));
156 hints.ai_family = AF_UNSPEC;
157 hints.ai_socktype = SOCK_STREAM;
158 if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) {
159 fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1],
160 gai_strerror(rs));
161 return 1;
162 }
163
164 /* create server socket */
165 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
166 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
167 return 1;
168 }
169
170 /* bind server socket */
171 memset(&addr, 0, sizeof(addr));
172 addr.sin_family = AF_INET;
173 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
174 fprintf(stderr, "bind() failed: %s\n", strerror(errno));
175 return 1;
176 }
177
178 /* connect */
179 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
180 fprintf(stderr, "server() failed: %s\n", strerror(errno));
181 return 1;
182 }
183
184 /* free address lookup info */
185 freeaddrinfo(ai);
186
Sean Middleditcha7896d12009-03-15 20:41:24 -0400187 /* get current terminal settings, set raw mode, make sure we
188 * register atexit handler to restore terminal settings
189 */
190 tcgetattr(STDOUT_FILENO, &orig_tios);
191 atexit(_cleanup);
192 tios = orig_tios;
193 cfmakeraw(&tios);
194 tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios);
195
196 /* set input echoing on by default */
197 do_echo = 1;
198
Sean Middleditchda394be2009-03-15 14:19:40 -0400199 /* initialize telnet box */
Sean Middleditchd2466a02009-09-19 14:35:48 -0700200 telnet = telnet_init(telopts, _event_handler, 0, &sock);
Sean Middleditchda394be2009-03-15 14:19:40 -0400201
202 /* initialize poll descriptors */
203 memset(pfd, 0, sizeof(pfd));
204 pfd[0].fd = STDIN_FILENO;
205 pfd[0].events = POLLIN;
206 pfd[1].fd = sock;
207 pfd[1].events = POLLIN;
208
209 /* loop while both connections are open */
210 while (poll(pfd, 2, -1) != -1) {
211 /* read from stdin */
212 if (pfd[0].revents & POLLIN) {
213 if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
Sean Middleditche7c44262009-03-15 20:59:48 -0400214 _input(buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400215 } else if (rs == 0) {
216 break;
217 } else {
218 fprintf(stderr, "recv(server) failed: %s\n",
219 strerror(errno));
220 exit(1);
221 }
222 }
223
224 /* read from client */
225 if (pfd[1].revents & POLLIN) {
226 if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
Sean Middleditchd2466a02009-09-19 14:35:48 -0700227 telnet_recv(telnet, buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400228 } else if (rs == 0) {
229 break;
230 } else {
231 fprintf(stderr, "recv(client) failed: %s\n",
232 strerror(errno));
233 exit(1);
234 }
235 }
236 }
237
238 /* clean up */
Sean Middleditchd2466a02009-09-19 14:35:48 -0700239 telnet_free(telnet);
Sean Middleditchda394be2009-03-15 14:19:40 -0400240 close(sock);
241
242 return 0;
243}