blob: ce82a0ee8829699aa584160d189e7eac4f3cca0b [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
115static inline void iacflush()
116{
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);
124static int create_socket();
125static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
126static int remote_connect(struct in_addr addr, int port);
127static void rawmode();
128static void cookmode();
129static void do_linemode();
130static void will_charmode();
131static void telopt(byte c);
132static int subneg(byte c);
133#if 0
134static int local_bind(int port);
135#endif
136
137/* Some globals */
138static int one = 1;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000139
Eric Andersen7e1273e2001-05-07 17:57:45 +0000140#ifdef BB_FEATURE_TELNET_TTYPE
141static char *ttype;
142#endif
143
Eric Andersen28c70b32000-06-14 20:42:57 +0000144static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000145{
Eric Andersen28c70b32000-06-14 20:42:57 +0000146 cookmode();
147 exit(ev);
148}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000149
Eric Andersen28c70b32000-06-14 20:42:57 +0000150static void conescape()
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000151{
Eric Andersen28c70b32000-06-14 20:42:57 +0000152 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000153
Eric Andersen28c70b32000-06-14 20:42:57 +0000154 if (G.gotsig) /* came from line mode... go raw */
155 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000156
Eric Andersen28c70b32000-06-14 20:42:57 +0000157 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
158 " l go to line mode\r\n"
159 " c go to character mode\r\n"
160 " z suspend telnet\r\n"
161 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000162
Eric Andersen28c70b32000-06-14 20:42:57 +0000163 if (read(0, &b, 1) <= 0)
164 doexit(1);
165
166 switch (b)
167 {
168 case 'l':
169 if (!G.gotsig)
170 {
171 do_linemode();
172 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000173 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000174 break;
175 case 'c':
176 if (G.gotsig)
177 {
178 will_charmode();
179 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000180 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000181 break;
182 case 'z':
183 cookmode();
184 kill(0, SIGTSTP);
185 rawmode();
186 break;
187 case 'e':
188 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000189 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000190
191 WriteCS(1, "continuing...\r\n");
192
193 if (G.gotsig)
194 cookmode();
195
196 rrturn:
197 G.gotsig = 0;
198
199}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000200static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000201{
202 /* here we could do smart tricks how to handle 0xFF:s in output
203 * stream like writing twice every sequence of FF:s (thus doing
204 * many write()s. But I think interactive telnet application does
205 * not need to be 100% 8-bit clean, so changing every 0xff:s to
206 * 0x7f:s */
207
208 int i;
209 byte * p = G.buf;
210
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000211 for (i = len; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000212 {
213 if (*p == 0x1d)
214 {
215 conescape();
216 return;
217 }
218 if (*p == 0xff)
219 *p = 0x7f;
220 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000221 write(G.netfd, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000222}
223
Eric Andersen28c70b32000-06-14 20:42:57 +0000224
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000225static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000226{
Eric Andersen28c70b32000-06-14 20:42:57 +0000227 int i;
228 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000229
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000230 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000231 {
232 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000233
Eric Andersen28c70b32000-06-14 20:42:57 +0000234 if (G.telstate == 0) /* most of the time state == 0 */
235 {
236 if (c == IAC)
237 {
238 cstart = i;
239 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000240 }
241 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000242 else
243 switch (G.telstate)
244 {
245 case TS_0:
246 if (c == IAC)
247 G.telstate = TS_IAC;
248 else
249 G.buf[cstart++] = c;
250 break;
251
252 case TS_IAC:
253 if (c == IAC) /* IAC IAC -> 0xFF */
254 {
255 G.buf[cstart++] = c;
256 G.telstate = TS_0;
257 break;
258 }
259 /* else */
260 switch (c)
261 {
262 case SB:
263 G.telstate = TS_SUB1;
264 break;
265 case DO:
266 case DONT:
267 case WILL:
268 case WONT:
269 G.telwish = c;
270 G.telstate = TS_OPT;
271 break;
272 default:
273 G.telstate = TS_0; /* DATA MARK must be added later */
274 }
275 break;
276 case TS_OPT: /* WILL, WONT, DO, DONT */
277 telopt(c);
278 G.telstate = TS_0;
279 break;
280 case TS_SUB1: /* Subnegotiation */
281 case TS_SUB2: /* Subnegotiation */
282 if (subneg(c) == TRUE)
283 G.telstate = TS_0;
284 break;
285 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000286 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000287 if (G.telstate)
288 {
289 if (G.iaclen) iacflush();
290 if (G.telstate == TS_0) G.telstate = 0;
291
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000292 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000293 }
294
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000295 if (len)
296 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000297}
298
Eric Andersen28c70b32000-06-14 20:42:57 +0000299
300/* ******************************* */
301
302static inline void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000303{
Eric Andersen28c70b32000-06-14 20:42:57 +0000304 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000305}
306
Eric Andersen28c70b32000-06-14 20:42:57 +0000307
308static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000309{
Eric Andersen28c70b32000-06-14 20:42:57 +0000310 if (G.iaclen + 3 > IACBUFSIZE)
311 iacflush();
312
313 putiac(IAC);
314 putiac(wwdd);
315 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000316}
317
Eric Andersen28c70b32000-06-14 20:42:57 +0000318#if 0
319static void putiac1(byte c)
320{
321 if (G.iaclen + 2 > IACBUFSIZE)
322 iacflush();
323
324 putiac(IAC);
325 putiac(c);
326}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000327#endif
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000328
Eric Andersen7e1273e2001-05-07 17:57:45 +0000329#ifdef BB_FEATURE_TELNET_TTYPE
330static void putiac_subopt(byte c, char *str)
331{
332 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
333
334 if (G.iaclen + len > IACBUFSIZE)
335 iacflush();
336
337 putiac(IAC);
338 putiac(SB);
339 putiac(c);
340 putiac(0);
341
342 while(*str)
343 putiac(*str++);
344
345 putiac(IAC);
346 putiac(SE);
347}
348#endif
349
Eric Andersen28c70b32000-06-14 20:42:57 +0000350/* void putiacstring (subneg strings) */
351
352/* ******************************* */
353
Eric Andersen3e6ff902001-03-09 21:24:12 +0000354static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000355
356static void setConMode()
357{
358 if (G.telflags & UF_ECHO)
359 {
360 if (G.charmode == CHM_TRY) {
361 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000362 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000363 rawmode();
364 }
365 }
366 else
367 {
368 if (G.charmode != CHM_OFF) {
369 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000370 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000371 cookmode();
372 }
373 }
374}
375
376/* ******************************* */
377
378static void will_charmode()
379{
380 G.charmode = CHM_TRY;
381 G.telflags |= (UF_ECHO | UF_SGA);
382 setConMode();
383
384 putiac2(DO, TELOPT_ECHO);
385 putiac2(DO, TELOPT_SGA);
386 iacflush();
387}
388
389static void do_linemode()
390{
391 G.charmode = CHM_TRY;
392 G.telflags &= ~(UF_ECHO | UF_SGA);
393 setConMode();
394
395 putiac2(DONT, TELOPT_ECHO);
396 putiac2(DONT, TELOPT_SGA);
397 iacflush();
398}
399
400/* ******************************* */
401
402static inline void to_notsup(char c)
403{
404 if (G.telwish == WILL) putiac2(DONT, c);
405 else if (G.telwish == DO) putiac2(WONT, c);
406}
407
408static inline void to_echo()
409{
410 /* if server requests ECHO, don't agree */
411 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
412 else if (G.telwish == DONT) return;
413
414 if (G.telflags & UF_ECHO)
415 {
416 if (G.telwish == WILL)
417 return;
418 }
419 else
420 if (G.telwish == WONT)
421 return;
422
423 if (G.charmode != CHM_OFF)
424 G.telflags ^= UF_ECHO;
425
426 if (G.telflags & UF_ECHO)
427 putiac2(DO, TELOPT_ECHO);
428 else
429 putiac2(DONT, TELOPT_ECHO);
430
431 setConMode();
432 WriteCS(1, "\r\n"); /* sudden modec */
433}
434
435static inline void to_sga()
436{
437 /* daemon always sends will/wont, client do/dont */
438
439 if (G.telflags & UF_SGA)
440 {
441 if (G.telwish == WILL)
442 return;
443 }
444 else
445 if (G.telwish == WONT)
446 return;
447
448 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
449 putiac2(DO, TELOPT_SGA);
450 else
451 putiac2(DONT, TELOPT_SGA);
452
453 return;
454}
455
Eric Andersen7e1273e2001-05-07 17:57:45 +0000456#ifdef BB_FEATURE_TELNET_TTYPE
457static inline void to_ttype()
458{
459 /* Tell server we will (or won't) do TTYPE */
460
461 if(ttype)
462 putiac2(WILL, TELOPT_TTYPE);
463 else
464 putiac2(WONT, TELOPT_TTYPE);
465
466 return;
467}
468#endif
469
Eric Andersen28c70b32000-06-14 20:42:57 +0000470static void telopt(byte c)
471{
472 switch (c)
473 {
474 case TELOPT_ECHO: to_echo(c); break;
475 case TELOPT_SGA: to_sga(c); break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000476#ifdef BB_FEATURE_TELNET_TTYPE
477 case TELOPT_TTYPE: to_ttype(c); break;
478#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000479 default: to_notsup(c); break;
480 }
481}
482
483
484/* ******************************* */
485
Eric Andersen7e1273e2001-05-07 17:57:45 +0000486/* subnegotiation -- ignore all (except TTYPE) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000487
488static int subneg(byte c)
489{
490 switch (G.telstate)
491 {
492 case TS_SUB1:
493 if (c == IAC)
494 G.telstate = TS_SUB2;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000495#ifdef BB_FEATURE_TELNET_TTYPE
496 else
497 if (c == TELOPT_TTYPE)
498 putiac_subopt(TELOPT_TTYPE,ttype);
499#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000500 break;
501 case TS_SUB2:
502 if (c == SE)
503 return TRUE;
504 G.telstate = TS_SUB1;
505 /* break; */
506 }
507 return FALSE;
508}
509
510/* ******************************* */
511
512static void fgotsig(int sig)
513{
514 G.gotsig = sig;
515}
516
517
518static void rawmode()
519{
520 tcsetattr(0, TCSADRAIN, &G.termios_raw);
521}
522
523static void cookmode()
524{
525 tcsetattr(0, TCSADRAIN, &G.termios_def);
526}
527
528extern int telnet_main(int argc, char** argv)
529{
530 struct in_addr host;
531 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000532 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000533#ifdef USE_POLL
534 struct pollfd ufds[2];
535#else
536 fd_set readfds;
537 int maxfd;
538#endif
539
Eric Andersen7e1273e2001-05-07 17:57:45 +0000540#ifdef BB_FEATURE_TELNET_TTYPE
541 ttype = getenv("TERM");
542#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000543
544 memset(&G, 0, sizeof G);
545
546 if (tcgetattr(0, &G.termios_def) < 0)
547 exit(1);
548
549 G.termios_raw = G.termios_def;
Eric Andersen28c70b32000-06-14 20:42:57 +0000550 cfmakeraw(&G.termios_raw);
551
Eric Andersen67991cf2001-02-14 21:23:06 +0000552 if (argc < 2) show_usage();
Eric Andersen28c70b32000-06-14 20:42:57 +0000553 port = (argc > 2)? getport(argv[2]): 23;
554
Eric Andersen28c70b32000-06-14 20:42:57 +0000555 host = getserver(argv[1]);
556
557 G.netfd = remote_connect(host, port);
558
559 signal(SIGINT, fgotsig);
560
561#ifdef USE_POLL
562 ufds[0].fd = 0; ufds[1].fd = G.netfd;
563 ufds[0].events = ufds[1].events = POLLIN;
564#else
565 FD_ZERO(&readfds);
566 FD_SET(0, &readfds);
567 FD_SET(G.netfd, &readfds);
568 maxfd = G.netfd + 1;
569#endif
570
571 while (1)
572 {
573#ifndef USE_POLL
574 fd_set rfds = readfds;
575
576 switch (select(maxfd, &rfds, NULL, NULL, NULL))
577#else
578 switch (poll(ufds, 2, -1))
579#endif
580 {
581 case 0:
582 /* timeout */
583 case -1:
584 /* error, ignore and/or log something, bay go to loop */
585 if (G.gotsig)
586 conescape();
587 else
588 sleep(1);
589 break;
590 default:
591
592#ifdef USE_POLL
593 if (ufds[0].revents) /* well, should check POLLIN, but ... */
594#else
595 if (FD_ISSET(0, &rfds))
596#endif
597 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000598 len = read(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000599
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000600 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000601 doexit(0);
602
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000603 TRACE(0, ("Read con: %d\n", len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000604
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000605 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000606 }
607
608#ifdef USE_POLL
609 if (ufds[1].revents) /* well, should check POLLIN, but ... */
610#else
611 if (FD_ISSET(G.netfd, &rfds))
612#endif
613 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000614 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000615
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000616 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000617 {
618 WriteCS(1, "Connection closed by foreign host.\r\n");
619 doexit(1);
620 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000621 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000622
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000623 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000624 }
625 }
626 }
627}
628
629static int getport(char * p)
630{
631 unsigned int port = atoi(p);
632
633 if ((unsigned)(port - 1 ) > 65534)
634 {
Matt Kraaidd19c692001-01-31 19:00:21 +0000635 error_msg_and_die("%s: bad port number", p);
Eric Andersen28c70b32000-06-14 20:42:57 +0000636 }
637 return port;
638}
639
640static struct in_addr getserver(char * host)
641{
642 struct in_addr addr;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000643
Eric Andersen28c70b32000-06-14 20:42:57 +0000644 struct hostent * he;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000645 he = xgethostbyname(host);
Eric Andersen28c70b32000-06-14 20:42:57 +0000646 memcpy(&addr, he->h_addr, sizeof addr);
647
648 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
Matt Kraaic55b8d42001-05-16 15:40:51 +0000649
Eric Andersen28c70b32000-06-14 20:42:57 +0000650 return addr;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000651}
Eric Andersen28c70b32000-06-14 20:42:57 +0000652
653static int create_socket()
654{
655 return socket(AF_INET, SOCK_STREAM, 0);
656}
657
658static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
659{
Eric Andersen8b960322001-03-17 05:43:39 +0000660 memset(addr, 0, sizeof(struct sockaddr_in));
Eric Andersen28c70b32000-06-14 20:42:57 +0000661 addr->sin_family = AF_INET;
662 addr->sin_port = htons(port);
663}
664
665#if 0
666static int local_bind(int port)
667{
668 struct sockaddr_in s_addr;
669 int s = create_socket();
670
671 setup_sockaddr_in(&s_addr, port);
672
673 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
674
675 if (bind(s, &s_addr, sizeof s_addr) < 0)
676 {
677 char * e = sys_errlist[errno];
678 syserrorexit("bind");
679 exit(1);
680 }
681 listen(s, 1);
682
683 return s;
684}
685#endif
686
687static int remote_connect(struct in_addr addr, int port)
688{
689 struct sockaddr_in s_addr;
690 int s = create_socket();
691
692 setup_sockaddr_in(&s_addr, port);
693 s_addr.sin_addr = addr;
694
695 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
696
697 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
698 {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000699 perror_msg_and_die("Unable to connect to remote host");
Eric Andersen28c70b32000-06-14 20:42:57 +0000700 }
701 return s;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000702}
703
704/*
Eric Andersen28c70b32000-06-14 20:42:57 +0000705Local Variables:
706c-file-style: "linux"
707c-basic-offset: 4
708tab-width: 4
709End:
710*/
711