blob: d09a9d558a5a86d17263433e085b99da8f770fce [file] [log] [blame]
Eric Andersen28c70b32000-06-14 20:42:57 +00001/* vi: set sw=4 ts=4: */
Erik Andersenf7c49ef2000-02-22 17:17:45 +00002/*
Eric Andersen28c70b32000-06-14 20:42:57 +00003 * telnet implementation for busybox
Erik Andersenf7c49ef2000-02-22 17:17:45 +00004 *
Eric Andersen28c70b32000-06-14 20:42:57 +00005 * Author: Tomi Ollila <too@iki.fi>
6 * Copyright (C) 1994-2000 by Tomi Ollila
7 *
8 * Created: Thu Apr 7 13:29:41 1994 too
9 * Last modified: Fri Jun 9 14:34:24 2000 too
Erik Andersenf7c49ef2000-02-22 17:17:45 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
Eric Andersen28c70b32000-06-14 20:42:57 +000025 * HISTORY
26 * Revision 3.1 1994/04/17 11:31:54 too
27 * initial revision
28 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen
29 * <andersen@lineo.com>
Eric Andersen7e1273e2001-05-07 17:57:45 +000030 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
31 * <jam@ltsp.org>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000032 *
Erik Andersenf7c49ef2000-02-22 17:17:45 +000033 */
34
Erik Andersenf7c49ef2000-02-22 17:17:45 +000035#include <termios.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000036#include <unistd.h>
37#include <errno.h>
38#include <stdlib.h>
39#include <stdarg.h>
40#include <string.h>
41#include <signal.h>
42#include <arpa/telnet.h>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000043#include <sys/types.h>
44#include <sys/socket.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000045#include <netinet/in.h>
46#include <netdb.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000047#include "busybox.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000048
Eric Andersen28c70b32000-06-14 20:42:57 +000049#if 0
Mark Whitley59ab0252001-01-23 22:30:04 +000050static const int DOTRACE = 1;
Eric Andersen28c70b32000-06-14 20:42:57 +000051#endif
52
Pavel Roskin616d13b2000-07-28 19:38:27 +000053#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000054#include <arpa/inet.h> /* for inet_ntoa()... */
55#define TRACE(x, y) do { if (x) printf y; } while (0)
56#else
57#define TRACE(x, y)
58#endif
59
60#if 0
61#define USE_POLL
62#include <sys/poll.h>
63#else
64#include <sys/time.h>
65#endif
66
Glenn L McGrath78b0e372001-06-26 02:06:08 +000067#define DATABUFSIZE 128
68#define IACBUFSIZE 128
Eric Andersen28c70b32000-06-14 20:42:57 +000069
Mark Whitley59ab0252001-01-23 22:30:04 +000070static const int CHM_TRY = 0;
71static const int CHM_ON = 1;
72static const int CHM_OFF = 2;
Eric Andersen28c70b32000-06-14 20:42:57 +000073
Mark Whitley59ab0252001-01-23 22:30:04 +000074static const int UF_ECHO = 0x01;
75static const int UF_SGA = 0x02;
Eric Andersen28c70b32000-06-14 20:42:57 +000076
Mark Whitley59ab0252001-01-23 22:30:04 +000077enum {
78 TS_0 = 1,
79 TS_IAC = 2,
80 TS_OPT = 3,
81 TS_SUB1 = 4,
82 TS_SUB2 = 5,
83};
Eric Andersen28c70b32000-06-14 20:42:57 +000084
85#define WriteCS(fd, str) write(fd, str, sizeof str -1)
86
87typedef unsigned char byte;
88
89/* use globals to reduce size ??? */ /* test this hypothesis later */
Eric Andersen92d23242001-03-19 23:49:41 +000090static struct Globalvars {
Eric Andersen28c70b32000-06-14 20:42:57 +000091 int netfd; /* console fd:s are 0 and 1 (and 2) */
92 /* same buffer used both for network and console read/write */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000093 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
Eric Andersen28c70b32000-06-14 20:42:57 +000094 byte telstate; /* telnet negotiation state from network input */
95 byte telwish; /* DO, DONT, WILL, WONT */
96 byte charmode;
97 byte telflags;
98 byte gotsig;
99 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000100 char iacbuf[IACBUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000101 short iaclen; /* could even use byte */
102 struct termios termios_def;
103 struct termios termios_raw;
104} G;
105
106#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
107
108#ifdef USE_GLOBALVAR_PTR
109struct Globalvars * Gptr;
110#define G (*Gptr)
111#else
Eric Andersen92d23242001-03-19 23:49:41 +0000112static struct Globalvars G;
Eric Andersen28c70b32000-06-14 20:42:57 +0000113#endif
114
Eric Andersencd8c4362001-11-10 11:22:46 +0000115static inline void iacflush(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000116{
117 write(G.netfd, G.iacbuf, G.iaclen);
118 G.iaclen = 0;
119}
120
121/* Function prototypes */
122static int getport(char * p);
123static struct in_addr getserver(char * p);
Eric Andersen28c70b32000-06-14 20:42:57 +0000124static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
125static int remote_connect(struct in_addr addr, int port);
Eric Andersencd8c4362001-11-10 11:22:46 +0000126static void rawmode(void);
127static void cookmode(void);
128static void do_linemode(void);
129static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +0000130static void telopt(byte c);
131static int subneg(byte c);
132#if 0
133static int local_bind(int port);
134#endif
135
136/* Some globals */
137static int one = 1;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000138
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000139#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000140static char *ttype;
141#endif
142
Eric Andersen28c70b32000-06-14 20:42:57 +0000143static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000144{
Eric Andersen28c70b32000-06-14 20:42:57 +0000145 cookmode();
146 exit(ev);
147}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000148
Eric Andersencd8c4362001-11-10 11:22:46 +0000149static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000150{
Eric Andersen28c70b32000-06-14 20:42:57 +0000151 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000152
Eric Andersen28c70b32000-06-14 20:42:57 +0000153 if (G.gotsig) /* came from line mode... go raw */
154 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000155
Eric Andersen28c70b32000-06-14 20:42:57 +0000156 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
157 " l go to line mode\r\n"
158 " c go to character mode\r\n"
159 " z suspend telnet\r\n"
160 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000161
Eric Andersen28c70b32000-06-14 20:42:57 +0000162 if (read(0, &b, 1) <= 0)
163 doexit(1);
164
165 switch (b)
166 {
167 case 'l':
168 if (!G.gotsig)
169 {
170 do_linemode();
171 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000172 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000173 break;
174 case 'c':
175 if (G.gotsig)
176 {
177 will_charmode();
178 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000179 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000180 break;
181 case 'z':
182 cookmode();
183 kill(0, SIGTSTP);
184 rawmode();
185 break;
186 case 'e':
187 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000188 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000189
190 WriteCS(1, "continuing...\r\n");
191
192 if (G.gotsig)
193 cookmode();
194
195 rrturn:
196 G.gotsig = 0;
197
198}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000199static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000200{
201 /* here we could do smart tricks how to handle 0xFF:s in output
202 * stream like writing twice every sequence of FF:s (thus doing
203 * many write()s. But I think interactive telnet application does
204 * not need to be 100% 8-bit clean, so changing every 0xff:s to
205 * 0x7f:s */
206
207 int i;
208 byte * p = G.buf;
209
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000210 for (i = len; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000211 {
212 if (*p == 0x1d)
213 {
214 conescape();
215 return;
216 }
217 if (*p == 0xff)
218 *p = 0x7f;
219 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000220 write(G.netfd, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000221}
222
Eric Andersen28c70b32000-06-14 20:42:57 +0000223
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000224static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000225{
Eric Andersen28c70b32000-06-14 20:42:57 +0000226 int i;
227 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000228
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000229 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000230 {
231 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000232
Eric Andersen28c70b32000-06-14 20:42:57 +0000233 if (G.telstate == 0) /* most of the time state == 0 */
234 {
235 if (c == IAC)
236 {
237 cstart = i;
238 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000239 }
240 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000241 else
242 switch (G.telstate)
243 {
244 case TS_0:
245 if (c == IAC)
246 G.telstate = TS_IAC;
247 else
248 G.buf[cstart++] = c;
249 break;
250
251 case TS_IAC:
252 if (c == IAC) /* IAC IAC -> 0xFF */
253 {
254 G.buf[cstart++] = c;
255 G.telstate = TS_0;
256 break;
257 }
258 /* else */
259 switch (c)
260 {
261 case SB:
262 G.telstate = TS_SUB1;
263 break;
264 case DO:
265 case DONT:
266 case WILL:
267 case WONT:
268 G.telwish = c;
269 G.telstate = TS_OPT;
270 break;
271 default:
272 G.telstate = TS_0; /* DATA MARK must be added later */
273 }
274 break;
275 case TS_OPT: /* WILL, WONT, DO, DONT */
276 telopt(c);
277 G.telstate = TS_0;
278 break;
279 case TS_SUB1: /* Subnegotiation */
280 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000281 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000282 G.telstate = TS_0;
283 break;
284 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000285 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000286 if (G.telstate)
287 {
288 if (G.iaclen) iacflush();
289 if (G.telstate == TS_0) G.telstate = 0;
290
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000291 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000292 }
293
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000294 if (len)
295 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000296}
297
Eric Andersen28c70b32000-06-14 20:42:57 +0000298
299/* ******************************* */
300
301static inline void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000302{
Eric Andersen28c70b32000-06-14 20:42:57 +0000303 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000304}
305
Eric Andersen28c70b32000-06-14 20:42:57 +0000306
307static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000308{
Eric Andersen28c70b32000-06-14 20:42:57 +0000309 if (G.iaclen + 3 > IACBUFSIZE)
310 iacflush();
311
312 putiac(IAC);
313 putiac(wwdd);
314 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000315}
316
Eric Andersen28c70b32000-06-14 20:42:57 +0000317#if 0
318static void putiac1(byte c)
319{
320 if (G.iaclen + 2 > IACBUFSIZE)
321 iacflush();
322
323 putiac(IAC);
324 putiac(c);
325}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000326#endif
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000327
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000328#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000329static void putiac_subopt(byte c, char *str)
330{
331 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
332
333 if (G.iaclen + len > IACBUFSIZE)
334 iacflush();
335
336 putiac(IAC);
337 putiac(SB);
338 putiac(c);
339 putiac(0);
340
341 while(*str)
342 putiac(*str++);
343
344 putiac(IAC);
345 putiac(SE);
346}
347#endif
348
Eric Andersen28c70b32000-06-14 20:42:57 +0000349/* void putiacstring (subneg strings) */
350
351/* ******************************* */
352
Eric Andersen3e6ff902001-03-09 21:24:12 +0000353static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000354
Eric Andersencd8c4362001-11-10 11:22:46 +0000355static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000356{
357 if (G.telflags & UF_ECHO)
358 {
359 if (G.charmode == CHM_TRY) {
360 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000361 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000362 rawmode();
363 }
364 }
365 else
366 {
367 if (G.charmode != CHM_OFF) {
368 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000369 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000370 cookmode();
371 }
372 }
373}
374
375/* ******************************* */
376
Eric Andersencd8c4362001-11-10 11:22:46 +0000377static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000378{
379 G.charmode = CHM_TRY;
380 G.telflags |= (UF_ECHO | UF_SGA);
381 setConMode();
382
383 putiac2(DO, TELOPT_ECHO);
384 putiac2(DO, TELOPT_SGA);
385 iacflush();
386}
387
Eric Andersencd8c4362001-11-10 11:22:46 +0000388static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000389{
390 G.charmode = CHM_TRY;
391 G.telflags &= ~(UF_ECHO | UF_SGA);
392 setConMode();
393
394 putiac2(DONT, TELOPT_ECHO);
395 putiac2(DONT, TELOPT_SGA);
396 iacflush();
397}
398
399/* ******************************* */
400
401static inline void to_notsup(char c)
402{
403 if (G.telwish == WILL) putiac2(DONT, c);
404 else if (G.telwish == DO) putiac2(WONT, c);
405}
406
Eric Andersencd8c4362001-11-10 11:22:46 +0000407static inline void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000408{
409 /* if server requests ECHO, don't agree */
410 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
411 else if (G.telwish == DONT) return;
412
413 if (G.telflags & UF_ECHO)
414 {
415 if (G.telwish == WILL)
416 return;
417 }
418 else
419 if (G.telwish == WONT)
420 return;
421
422 if (G.charmode != CHM_OFF)
423 G.telflags ^= UF_ECHO;
424
425 if (G.telflags & UF_ECHO)
426 putiac2(DO, TELOPT_ECHO);
427 else
428 putiac2(DONT, TELOPT_ECHO);
429
430 setConMode();
431 WriteCS(1, "\r\n"); /* sudden modec */
432}
433
Eric Andersencd8c4362001-11-10 11:22:46 +0000434static inline void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000435{
436 /* daemon always sends will/wont, client do/dont */
437
438 if (G.telflags & UF_SGA)
439 {
440 if (G.telwish == WILL)
441 return;
442 }
443 else
444 if (G.telwish == WONT)
445 return;
446
447 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
448 putiac2(DO, TELOPT_SGA);
449 else
450 putiac2(DONT, TELOPT_SGA);
451
452 return;
453}
454
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000455#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersencd8c4362001-11-10 11:22:46 +0000456static inline void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000457{
458 /* Tell server we will (or won't) do TTYPE */
459
460 if(ttype)
461 putiac2(WILL, TELOPT_TTYPE);
462 else
463 putiac2(WONT, TELOPT_TTYPE);
464
465 return;
466}
467#endif
468
Eric Andersen28c70b32000-06-14 20:42:57 +0000469static void telopt(byte c)
470{
471 switch (c)
472 {
Eric Andersencd8c4362001-11-10 11:22:46 +0000473 case TELOPT_ECHO: to_echo(); break;
474 case TELOPT_SGA: to_sga(); break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000475#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersencd8c4362001-11-10 11:22:46 +0000476 case TELOPT_TTYPE: to_ttype(); break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000477#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000478 default: to_notsup(c); break;
479 }
480}
481
482
483/* ******************************* */
484
Eric Andersen7e1273e2001-05-07 17:57:45 +0000485/* subnegotiation -- ignore all (except TTYPE) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000486
487static int subneg(byte c)
488{
489 switch (G.telstate)
490 {
491 case TS_SUB1:
492 if (c == IAC)
493 G.telstate = TS_SUB2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000494#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000495 else
496 if (c == TELOPT_TTYPE)
497 putiac_subopt(TELOPT_TTYPE,ttype);
498#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000499 break;
500 case TS_SUB2:
501 if (c == SE)
502 return TRUE;
503 G.telstate = TS_SUB1;
504 /* break; */
505 }
506 return FALSE;
507}
508
509/* ******************************* */
510
511static void fgotsig(int sig)
512{
513 G.gotsig = sig;
514}
515
516
Eric Andersencd8c4362001-11-10 11:22:46 +0000517static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000518{
519 tcsetattr(0, TCSADRAIN, &G.termios_raw);
520}
521
Eric Andersencd8c4362001-11-10 11:22:46 +0000522static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000523{
524 tcsetattr(0, TCSADRAIN, &G.termios_def);
525}
526
527extern int telnet_main(int argc, char** argv)
528{
529 struct in_addr host;
530 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000531 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000532#ifdef USE_POLL
533 struct pollfd ufds[2];
534#else
535 fd_set readfds;
536 int maxfd;
537#endif
538
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000539#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000540 ttype = getenv("TERM");
541#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000542
543 memset(&G, 0, sizeof G);
544
545 if (tcgetattr(0, &G.termios_def) < 0)
546 exit(1);
547
548 G.termios_raw = G.termios_def;
Eric Andersen28c70b32000-06-14 20:42:57 +0000549 cfmakeraw(&G.termios_raw);
550
Eric Andersen67991cf2001-02-14 21:23:06 +0000551 if (argc < 2) show_usage();
Eric Andersen28c70b32000-06-14 20:42:57 +0000552 port = (argc > 2)? getport(argv[2]): 23;
553
Eric Andersen28c70b32000-06-14 20:42:57 +0000554 host = getserver(argv[1]);
555
556 G.netfd = remote_connect(host, port);
557
558 signal(SIGINT, fgotsig);
559
560#ifdef USE_POLL
561 ufds[0].fd = 0; ufds[1].fd = G.netfd;
562 ufds[0].events = ufds[1].events = POLLIN;
563#else
564 FD_ZERO(&readfds);
565 FD_SET(0, &readfds);
566 FD_SET(G.netfd, &readfds);
567 maxfd = G.netfd + 1;
568#endif
569
570 while (1)
571 {
572#ifndef USE_POLL
573 fd_set rfds = readfds;
574
575 switch (select(maxfd, &rfds, NULL, NULL, NULL))
576#else
577 switch (poll(ufds, 2, -1))
578#endif
579 {
580 case 0:
581 /* timeout */
582 case -1:
583 /* error, ignore and/or log something, bay go to loop */
584 if (G.gotsig)
585 conescape();
586 else
587 sleep(1);
588 break;
589 default:
590
591#ifdef USE_POLL
592 if (ufds[0].revents) /* well, should check POLLIN, but ... */
593#else
594 if (FD_ISSET(0, &rfds))
595#endif
596 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000597 len = read(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000598
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000599 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000600 doexit(0);
601
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000602 TRACE(0, ("Read con: %d\n", len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000603
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000604 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000605 }
606
607#ifdef USE_POLL
608 if (ufds[1].revents) /* well, should check POLLIN, but ... */
609#else
610 if (FD_ISSET(G.netfd, &rfds))
611#endif
612 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000613 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000614
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000615 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000616 {
617 WriteCS(1, "Connection closed by foreign host.\r\n");
618 doexit(1);
619 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000620 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000621
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000622 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000623 }
624 }
625 }
626}
627
628static int getport(char * p)
629{
630 unsigned int port = atoi(p);
631
632 if ((unsigned)(port - 1 ) > 65534)
633 {
Matt Kraaidd19c692001-01-31 19:00:21 +0000634 error_msg_and_die("%s: bad port number", p);
Eric Andersen28c70b32000-06-14 20:42:57 +0000635 }
636 return port;
637}
638
639static struct in_addr getserver(char * host)
640{
641 struct in_addr addr;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000642
Eric Andersen28c70b32000-06-14 20:42:57 +0000643 struct hostent * he;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000644 he = xgethostbyname(host);
Eric Andersen28c70b32000-06-14 20:42:57 +0000645 memcpy(&addr, he->h_addr, sizeof addr);
646
647 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
Matt Kraaic55b8d42001-05-16 15:40:51 +0000648
Eric Andersen28c70b32000-06-14 20:42:57 +0000649 return addr;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000650}
Eric Andersen28c70b32000-06-14 20:42:57 +0000651
Eric Andersencd8c4362001-11-10 11:22:46 +0000652static int create_socket(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000653{
654 return socket(AF_INET, SOCK_STREAM, 0);
655}
656
657static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
658{
Eric Andersen8b960322001-03-17 05:43:39 +0000659 memset(addr, 0, sizeof(struct sockaddr_in));
Eric Andersen28c70b32000-06-14 20:42:57 +0000660 addr->sin_family = AF_INET;
661 addr->sin_port = htons(port);
662}
663
664#if 0
665static int local_bind(int port)
666{
667 struct sockaddr_in s_addr;
668 int s = create_socket();
669
670 setup_sockaddr_in(&s_addr, port);
671
672 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
673
674 if (bind(s, &s_addr, sizeof s_addr) < 0)
675 {
676 char * e = sys_errlist[errno];
677 syserrorexit("bind");
678 exit(1);
679 }
680 listen(s, 1);
681
682 return s;
683}
684#endif
685
686static int remote_connect(struct in_addr addr, int port)
687{
688 struct sockaddr_in s_addr;
689 int s = create_socket();
690
691 setup_sockaddr_in(&s_addr, port);
692 s_addr.sin_addr = addr;
693
694 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
695
696 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
697 {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000698 perror_msg_and_die("Unable to connect to remote host");
Eric Andersen28c70b32000-06-14 20:42:57 +0000699 }
700 return s;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000701}
702
703/*
Eric Andersen28c70b32000-06-14 20:42:57 +0000704Local Variables:
705c-file-style: "linux"
706c-basic-offset: 4
707tab-width: 4
708End:
709*/