blob: f380cd363a3432472e598d0fd04cd53ef677849e [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 Middleditchf65f27d2009-03-19 02:32:04 -040032static 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 },
37 { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
38 { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
Sean Middleditch34bb0992009-03-21 00:20:44 -040039 { -1, 0, 0 }
40};
41
Sean Middleditcha7896d12009-03-15 20:41:24 -040042static void _cleanup(void) {
43 tcsetattr(STDOUT_FILENO, TCSADRAIN, &orig_tios);
44}
45
Sean Middleditch8daf7742009-03-19 02:05:24 -040046static void _input(char *buffer, int size) {
47 static char crlf[] = { '\r', '\n' };
Sean Middleditche7c44262009-03-15 20:59:48 -040048 int i;
49
50 for (i = 0; i != size; ++i) {
51 /* if we got a CR or LF, replace with CRLF
52 * NOTE that usually you'd get a CR in UNIX, but in raw
53 * mode we get LF instead (not sure why)
54 */
55 if (buffer[i] == '\r' || buffer[i] == '\n') {
56 if (do_echo)
57 write(STDOUT_FILENO, crlf, 2);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -040058 telnet_send(&telnet, crlf, 2);
Sean Middleditche7c44262009-03-15 20:59:48 -040059 } else {
60 if (do_echo)
61 write(STDOUT_FILENO, buffer + i, 1);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -040062 telnet_send(&telnet, buffer + i, 1);
Sean Middleditche7c44262009-03-15 20:59:48 -040063 }
64 }
65}
66
Sean Middleditch340a51b2009-03-19 02:08:46 -040067static void _send(int sock, const char *buffer, size_t size) {
Sean Middleditchda394be2009-03-15 14:19:40 -040068 int rs;
69
70 /* send data */
71 while (size > 0) {
72 if ((rs = send(sock, buffer, size, 0)) == -1) {
73 fprintf(stderr, "send() failed: %s\n", strerror(errno));
74 exit(1);
75 } else if (rs == 0) {
76 fprintf(stderr, "send() unexpectedly returned 0\n");
77 exit(1);
78 }
79
80 /* update pointer and size to see if we've got more to send */
81 buffer += rs;
82 size -= rs;
83 }
84}
85
Sean Middleditchf65f27d2009-03-19 02:32:04 -040086static void _event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -040087 void *user_data) {
Sean Middleditchda394be2009-03-15 14:19:40 -040088 int sock = *(int*)user_data;
89
90 switch (ev->type) {
91 /* data received */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040092 case TELNET_EV_DATA:
Sean Middleditchda394be2009-03-15 14:19:40 -040093 write(STDOUT_FILENO, ev->buffer, ev->size);
94 break;
95 /* data must be sent */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040096 case TELNET_EV_SEND:
Sean Middleditchda394be2009-03-15 14:19:40 -040097 _send(sock, ev->buffer, ev->size);
98 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040099 /* request to enable remote feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400100 case TELNET_EV_WILL:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400101 /* we'll agree to turn off our echo if server wants us to stop */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400102 if (ev->telopt == TELNET_TELOPT_ECHO)
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400103 do_echo = 0;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400104 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400105 /* notification of disabling remote feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400106 case TELNET_EV_WONT:
107 if (ev->telopt == TELNET_TELOPT_ECHO)
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400108 do_echo = 1;
109 break;
110 /* request to enable local feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400111 case TELNET_EV_DO:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400112 break;
113 /* demand to disable local feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400114 case TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400115 break;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400116 /* respond to particular subnegotiations */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400117 case TELNET_EV_SUBNEGOTIATION:
Sean Middleditcha7896d12009-03-15 20:41:24 -0400118 /* respond with our terminal type */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400119 if (ev->telopt == TELNET_TELOPT_TTYPE) {
Sean Middleditcha7896d12009-03-15 20:41:24 -0400120 /* NOTE: we just assume the server sent a legitimate
121 * sub-negotiation, as there really isn't anything else
122 * it's allowed to send
123 */
124 char buffer[64];
125 buffer[0] = 0; /* IS code for RFC 1091 */
126 snprintf(buffer + 1, sizeof(buffer) - 1, "%s", getenv("TERM"));
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400127 telnet_subnegotiation(telnet, TELNET_TELOPT_TTYPE, buffer,
128 1 + strlen(buffer + 1));
Sean Middleditcha7896d12009-03-15 20:41:24 -0400129 }
Sean Middleditchda394be2009-03-15 14:19:40 -0400130 break;
131 /* error */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400132 case TELNET_EV_ERROR:
Sean Middleditch340a51b2009-03-19 02:08:46 -0400133 fprintf(stderr, "ERROR: %s\n", ev->buffer);
Sean Middleditchda394be2009-03-15 14:19:40 -0400134 exit(1);
135 default:
136 /* ignore */
137 break;
138 }
139}
140
141int main(int argc, char **argv) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400142 char buffer[512];
Sean Middleditchda394be2009-03-15 14:19:40 -0400143 int rs;
144 int sock;
145 struct sockaddr_in addr;
146 struct pollfd pfd[2];
Sean Middleditchda394be2009-03-15 14:19:40 -0400147 struct addrinfo *ai;
148 struct addrinfo hints;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400149 struct termios tios;
Sean Middleditchda394be2009-03-15 14:19:40 -0400150
151 /* check usage */
152 if (argc != 3) {
153 fprintf(stderr, "Usage:\n ./telnet-client <host> <port>\n");
154 return 1;
155 }
156
157 /* look up server host */
158 memset(&hints, 0, sizeof(hints));
159 hints.ai_family = AF_UNSPEC;
160 hints.ai_socktype = SOCK_STREAM;
161 if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) {
162 fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1],
163 gai_strerror(rs));
164 return 1;
165 }
166
167 /* create server socket */
168 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
169 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
170 return 1;
171 }
172
173 /* bind server socket */
174 memset(&addr, 0, sizeof(addr));
175 addr.sin_family = AF_INET;
176 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
177 fprintf(stderr, "bind() failed: %s\n", strerror(errno));
178 return 1;
179 }
180
181 /* connect */
182 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
183 fprintf(stderr, "server() failed: %s\n", strerror(errno));
184 return 1;
185 }
186
187 /* free address lookup info */
188 freeaddrinfo(ai);
189
Sean Middleditcha7896d12009-03-15 20:41:24 -0400190 /* get current terminal settings, set raw mode, make sure we
191 * register atexit handler to restore terminal settings
192 */
193 tcgetattr(STDOUT_FILENO, &orig_tios);
194 atexit(_cleanup);
195 tios = orig_tios;
196 cfmakeraw(&tios);
197 tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios);
198
199 /* set input echoing on by default */
200 do_echo = 1;
201
Sean Middleditchda394be2009-03-15 14:19:40 -0400202 /* initialize telnet box */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400203 telnet_init(&telnet, telopts, _event_handler, 0, &sock);
Sean Middleditchda394be2009-03-15 14:19:40 -0400204
205 /* initialize poll descriptors */
206 memset(pfd, 0, sizeof(pfd));
207 pfd[0].fd = STDIN_FILENO;
208 pfd[0].events = POLLIN;
209 pfd[1].fd = sock;
210 pfd[1].events = POLLIN;
211
212 /* loop while both connections are open */
213 while (poll(pfd, 2, -1) != -1) {
214 /* read from stdin */
215 if (pfd[0].revents & POLLIN) {
216 if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
Sean Middleditche7c44262009-03-15 20:59:48 -0400217 _input(buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400218 } else if (rs == 0) {
219 break;
220 } else {
221 fprintf(stderr, "recv(server) failed: %s\n",
222 strerror(errno));
223 exit(1);
224 }
225 }
226
227 /* read from client */
228 if (pfd[1].revents & POLLIN) {
229 if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400230 telnet_recv(&telnet, buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400231 } else if (rs == 0) {
232 break;
233 } else {
234 fprintf(stderr, "recv(client) failed: %s\n",
235 strerror(errno));
236 exit(1);
237 }
238 }
239 }
240
241 /* clean up */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400242 telnet_free(&telnet);
Sean Middleditchda394be2009-03-15 14:19:40 -0400243 close(sock);
244
245 return 0;
246}