main.c revision 25707
1/* 2 * User Process PPP 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * $Id: main.c,v 1.48 1997/05/10 03:39:53 brian Exp $ 21 * 22 * TODO: 23 * o Add commands for traffic summary, version display, etc. 24 * o Add signal handler for misc controls. 25 */ 26#include "fsm.h" 27#include <fcntl.h> 28#include <paths.h> 29#include <sys/time.h> 30#include <termios.h> 31#include <signal.h> 32#include <sys/wait.h> 33#include <errno.h> 34#include <netdb.h> 35#include <unistd.h> 36#include <sys/socket.h> 37#include <arpa/inet.h> 38#include <netinet/in_systm.h> 39#include <netinet/ip.h> 40#include "modem.h" 41#include "os.h" 42#include "hdlc.h" 43#include "ccp.h" 44#include "lcp.h" 45#include "ipcp.h" 46#include "vars.h" 47#include "auth.h" 48#include "filter.h" 49#include "systems.h" 50#include "ip.h" 51#include "alias.h" 52#include "sig.h" 53 54#define LAUTH_M1 "Warning: No password entry for this host in ppp.secret\n" 55#define LAUTH_M2 "Warning: All manipulation is allowed by anyone in the world\n" 56 57#ifndef O_NONBLOCK 58#ifdef O_NDELAY 59#define O_NONBLOCK O_NDELAY 60#endif 61#endif 62 63extern void VjInit(), AsyncInit(); 64extern void AsyncInput(); 65extern int SelectSystem(); 66 67extern void DecodeCommand(), Prompt(); 68extern int aft_cmd; 69extern int IsInteractive(); 70static void DoLoop(void); 71static void TerminalStop(); 72 73static struct termios oldtio; /* Original tty mode */ 74static struct termios comtio; /* Command level tty mode */ 75int TermMode; 76static int server; 77static pid_t BGPid = 0; 78struct sockaddr_in ifsin; 79static char pid_filename[MAXPATHLEN]; 80static char if_filename[MAXPATHLEN]; 81int tunno; 82 83static void 84TtyInit() 85{ 86 struct termios newtio; 87 int stat; 88 89 stat = fcntl(0, F_GETFL, 0); 90 if (stat > 0) { 91 stat |= O_NONBLOCK; 92 (void)fcntl(0, F_SETFL, stat); 93 } 94 newtio = oldtio; 95 newtio.c_lflag &= ~(ECHO|ISIG|ICANON); 96 newtio.c_iflag = 0; 97 newtio.c_oflag &= ~OPOST; 98 newtio.c_cc[VEOF] = _POSIX_VDISABLE; 99 newtio.c_cc[VINTR] = _POSIX_VDISABLE; 100 newtio.c_cc[VMIN] = 1; 101 newtio.c_cc[VTIME] = 0; 102 newtio.c_cflag |= CS8; 103 tcsetattr(0, TCSADRAIN, &newtio); 104 comtio = newtio; 105} 106 107/* 108 * Set tty into command mode. We allow canonical input and echo processing. 109 */ 110void 111TtyCommandMode(prompt) 112int prompt; 113{ 114 struct termios newtio; 115 int stat; 116 117 if (!(mode & MODE_INTER)) 118 return; 119 tcgetattr(0, &newtio); 120 newtio.c_lflag |= (ECHO|ISIG|ICANON); 121 newtio.c_iflag = oldtio.c_iflag; 122 newtio.c_oflag |= OPOST; 123 tcsetattr(0, TCSADRAIN, &newtio); 124 stat = fcntl(0, F_GETFL, 0); 125 if (stat > 0) { 126 stat |= O_NONBLOCK; 127 (void)fcntl(0, F_SETFL, stat); 128 } 129 TermMode = 0; 130 if(prompt) Prompt(); 131} 132 133/* 134 * Set tty into terminal mode which is used while we invoke term command. 135 */ 136void 137TtyTermMode() 138{ 139 int stat; 140 141 tcsetattr(0, TCSADRAIN, &comtio); 142 stat = fcntl(0, F_GETFL, 0); 143 if (stat > 0) { 144 stat &= ~O_NONBLOCK; 145 (void)fcntl(0, F_SETFL, stat); 146 } 147 TermMode = 1; 148} 149 150void 151TtyOldMode() 152{ 153 int stat; 154 155 stat = fcntl(0, F_GETFL, 0); 156 if (stat > 0) { 157 stat &= ~O_NONBLOCK; 158 (void)fcntl(0, F_SETFL, stat); 159 } 160 tcsetattr(0, TCSANOW, &oldtio); 161} 162 163void 164Cleanup(excode) 165int excode; 166{ 167 168 OsLinkdown(); 169 OsCloseLink(1); 170 sleep(1); 171 if (mode & (MODE_AUTO | MODE_BACKGROUND)) 172 DeleteIfRoutes(1); 173 (void)unlink(pid_filename); 174 (void)unlink(if_filename); 175 OsInterfaceDown(1); 176 if (mode & MODE_BACKGROUND && BGFiledes[1] != -1) { 177 char c = EX_ERRDEAD; 178 if (write(BGFiledes[1],&c,1) == 1) 179 LogPrintf(LOG_PHASE_BIT,"Parent notified of failure.\n"); 180 else 181 LogPrintf(LOG_PHASE_BIT,"Failed to notify parent of failure.\n"); 182 close(BGFiledes[1]); 183 } 184 LogPrintf(LOG_PHASE_BIT, "PPP Terminated %d.\n",excode); 185 LogClose(); 186 if (server >= 0) { 187 close(server); 188 server = -1; 189 } 190 191 TtyOldMode(); 192 193 exit(excode); 194} 195 196static void 197Hangup(signo) 198int signo; 199{ 200 if (signo == SIGSEGV) { 201 LogPrintf(LOG_PHASE_BIT, "Signal %d, core dump.\n", signo); 202 LogClose(); 203 abort(); 204 } 205 if (BGPid) { 206 kill (BGPid, SIGTERM); 207 exit (EX_HANGUP); 208 } 209 else { 210 LogPrintf(LOG_PHASE_BIT, "Signal %d, hangup.\n", signo); 211 Cleanup(EX_HANGUP); 212 } 213} 214 215static void 216CloseSession(signo) 217int signo; 218{ 219 if (BGPid) { 220 kill (BGPid, SIGINT); 221 exit (EX_TERM); 222 } 223 else { 224 LogPrintf(LOG_PHASE_BIT, "Signal %d, terminate.\n", signo); 225 LcpClose(); 226 Cleanup(EX_TERM); 227 } 228} 229 230static void 231TerminalCont() 232{ 233 pending_signal(SIGCONT, SIG_DFL); 234 pending_signal(SIGTSTP, TerminalStop); 235 TtyCommandMode(getpgrp() == tcgetpgrp(0)); 236} 237 238static void 239TerminalStop(signo) 240int signo; 241{ 242 pending_signal(SIGCONT, TerminalCont); 243 TtyOldMode(); 244 pending_signal(SIGTSTP, SIG_DFL); 245 kill(getpid(), signo); 246} 247 248 249void 250Usage() 251{ 252 fprintf(stderr, 253 "Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ] [ -alias ] [system]\n"); 254 exit(EX_START); 255} 256 257void 258ProcessArgs(int argc, char **argv) 259{ 260 int optc; 261 char *cp; 262 263 optc = 0; 264 while (argc > 0 && **argv == '-') { 265 cp = *argv + 1; 266 if (strcmp(cp, "auto") == 0) 267 mode |= MODE_AUTO; 268 else if (strcmp(cp, "background") == 0) 269 mode |= MODE_BACKGROUND; 270 else if (strcmp(cp, "direct") == 0) 271 mode |= MODE_DIRECT; 272 else if (strcmp(cp, "dedicated") == 0) 273 mode |= MODE_DEDICATED; 274 else if (strcmp(cp, "ddial") == 0) 275 mode |= MODE_DDIAL|MODE_AUTO; 276 else if (strcmp(cp, "alias") == 0) { 277 mode |= MODE_ALIAS; 278 optc--; /* this option isn't exclusive */ 279 } 280 else 281 Usage(); 282 optc++; 283 argv++; argc--; 284 } 285 if (argc > 1) { 286 fprintf(stderr, "specify only one system label.\n"); 287 exit(EX_START); 288 } 289 if (argc == 1) dstsystem = *argv; 290 291 if (optc > 1) { 292 fprintf(stderr, "specify only one mode.\n"); 293 exit(EX_START); 294 } 295} 296 297static void 298Greetings() 299{ 300 printf("User Process PPP. Written by Toshiharu OHNO.\r\n"); 301 fflush(stdout); 302} 303 304void 305main(argc, argv) 306int argc; 307char **argv; 308{ 309 FILE *lockfile; 310 argc--; argv++; 311 312 mode = MODE_INTER; /* default operation is interactive mode */ 313 netfd = server = modem = tun_in = -1; 314 ProcessArgs(argc, argv); 315 Greetings(); 316 GetUid(); 317 IpcpDefAddress(); 318 InitAlias(); 319 320 if (SelectSystem("default", CONFFILE) < 0) { 321 fprintf(stderr, "Warning: No default entry is given in config file.\n"); 322 } 323 324 switch ( LocalAuthInit() ) { 325 case NOT_FOUND: 326 fprintf(stderr,LAUTH_M1); 327 fprintf(stderr,LAUTH_M2); 328 fflush (stderr); 329 /* Fall down */ 330 case VALID: 331 VarLocalAuth = LOCAL_AUTH; 332 break; 333 default: 334 break; 335 } 336 337 if (OpenTunnel(&tunno) < 0) { 338 perror("open_tun"); 339 exit(EX_START); 340 } 341 342 if (mode & (MODE_AUTO|MODE_DIRECT|MODE_DEDICATED|MODE_BACKGROUND)) 343 mode &= ~MODE_INTER; 344 if (mode & MODE_INTER) { 345 printf("Interactive mode\n"); 346 netfd = STDIN_FILENO; 347 } else if (mode & MODE_AUTO) { 348 printf("Automatic Dialer mode\n"); 349 if (dstsystem == NULL) { 350 fprintf(stderr, 351 "Destination system must be specified in auto or ddial mode.\n"); 352 exit(EX_START); 353 } 354 } else if (mode & MODE_BACKGROUND) { 355 printf("Background mode\n"); 356 if (dstsystem == NULL) { 357 fprintf(stderr, "Destination system must be specified in background mode.\n"); 358 exit(EX_START); 359 } 360 } 361 362 tcgetattr(0, &oldtio); /* Save original tty mode */ 363 364 pending_signal(SIGHUP, LogReOpen); 365 pending_signal(SIGTERM, CloseSession); 366 pending_signal(SIGINT, CloseSession); 367 pending_signal(SIGQUIT, CloseSession); 368#ifdef SIGSEGV 369 signal(SIGSEGV, Hangup); 370#endif 371#ifdef SIGPIPE 372 signal(SIGPIPE, SIG_IGN); 373#endif 374#ifdef SIGALRM 375 pending_signal(SIGALRM, SIG_IGN); 376#endif 377 if(mode & MODE_INTER) 378 { 379#ifdef SIGTSTP 380 pending_signal(SIGTSTP, TerminalStop); 381#endif 382#ifdef SIGTTIN 383 pending_signal(SIGTTIN, TerminalStop); 384#endif 385#ifdef SIGTTOU 386 pending_signal(SIGTTOU, SIG_IGN); 387#endif 388 } 389 390 if (dstsystem) { 391 if (SelectSystem(dstsystem, CONFFILE) < 0) { 392 fprintf(stderr, "Destination system not found in conf file.\n"); 393 Cleanup(EX_START); 394 } 395 if ((mode & MODE_AUTO) && DefHisAddress.ipaddr.s_addr == INADDR_ANY) { 396 fprintf(stderr, "Must specify dstaddr with auto or ddial mode.\n"); 397 Cleanup(EX_START); 398 } 399 } 400 if (mode & MODE_DIRECT) 401 printf("Packet mode enabled.\n"); 402 403 if (!(mode & MODE_INTER)) { 404 int port = SERVER_PORT + tunno; 405 if (mode & MODE_BACKGROUND) { 406 if (pipe (BGFiledes)) { 407 perror("pipe"); 408 Cleanup(EX_SOCK); 409 } 410 } 411 else { 412 /* 413 * Create server socket and listen at there. 414 */ 415 server = socket(PF_INET, SOCK_STREAM, 0); 416 if (server < 0) { 417 perror("socket"); 418 Cleanup(EX_SOCK); 419 } 420 ifsin.sin_family = AF_INET; 421 ifsin.sin_addr.s_addr = INADDR_ANY; 422 ifsin.sin_port = htons(port); 423 if (bind(server, (struct sockaddr *) &ifsin, sizeof(ifsin)) < 0) { 424 perror("bind"); 425 if (errno == EADDRINUSE) 426 fprintf(stderr, "Wait for a while, then try again.\n"); 427 Cleanup(EX_SOCK); 428 } 429 if (listen(server, 5) != 0) { 430 fprintf(stderr, "Unable to listen to socket - OS overload?\n"); 431 } 432 } 433 434 DupLog(); 435 if (!(mode & MODE_DIRECT)) { 436 pid_t bgpid; 437 438 bgpid = fork (); 439 if (bgpid == -1) { 440 perror ("fork"); 441 Cleanup (EX_SOCK); 442 } 443 if (bgpid) { 444 char c = EX_NORMAL; 445 446 if (mode & MODE_BACKGROUND) { 447 /* Wait for our child to close its pipe before we exit. */ 448 BGPid = bgpid; 449 close (BGFiledes[1]); 450 if (read(BGFiledes[0], &c, 1) != 1) 451 LogPrintf (LOG_PHASE_BIT, "Parent: Child exit, no status.\n"); 452 else if (c == EX_NORMAL) 453 LogPrintf (LOG_PHASE_BIT, "Parent: PPP enabled.\n"); 454 else 455 LogPrintf (LOG_PHASE_BIT, "Parent: Child failed %d.\n",(int)c); 456 close (BGFiledes[0]); 457 } 458 exit(c); 459 } else if (mode & MODE_BACKGROUND) 460 close(BGFiledes[0]); 461 } 462 463 snprintf(pid_filename, sizeof (pid_filename), "%s/tun%d.pid", 464 _PATH_VARRUN, tunno); 465 (void)unlink(pid_filename); 466 467 if ((lockfile = fopen(pid_filename, "w")) != NULL) { 468 fprintf(lockfile, "%d\n", (int)getpid()); 469 fclose(lockfile); 470 } else 471 logprintf("Warning: Can't create %s: %s\n", pid_filename, strerror(errno)); 472 473 snprintf(if_filename, sizeof if_filename, "%s%s.if", 474 _PATH_VARRUN, VarBaseDevice); 475 (void)unlink(if_filename); 476 477 if ((lockfile = fopen(if_filename, "w")) != NULL) { 478 fprintf(lockfile, "tun%d\n", tunno); 479 fclose(lockfile); 480 } else 481 logprintf("Warning: Can't create %s: %s\n", if_filename, strerror(errno)); 482 483 if (server >= 0) 484 LogPrintf(LOG_PHASE_BIT, "Listening at %d.\n", port); 485#ifdef DOTTYINIT 486 if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */ 487#else 488 if (mode & MODE_DIRECT) { 489#endif 490 TtyInit(); 491 } else { 492 int fd; 493 494 setsid(); /* detach control tty */ 495 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 496 (void)dup2(fd, STDIN_FILENO); 497 (void)dup2(fd, STDOUT_FILENO); 498 (void)dup2(fd, STDERR_FILENO); 499 if (fd > 2) 500 (void)close (fd); 501 } 502 } 503 } else { 504 TtyInit(); 505 TtyCommandMode(1); 506 } 507 LogPrintf(LOG_PHASE_BIT, "PPP Started.\n"); 508 509 510 do 511 DoLoop(); 512 while (mode & MODE_DEDICATED); 513 514 Cleanup(EX_DONE); 515} 516 517/* 518 * Turn into packet mode, where we speak PPP. 519 */ 520void 521PacketMode() 522{ 523 if (RawModem(modem) < 0) { 524 fprintf(stderr, "Not connected.\r\n"); 525 return; 526 } 527 528 AsyncInit(); 529 VjInit(); 530 LcpInit(); 531 IpcpInit(); 532 CcpInit(); 533 LcpUp(); 534 535 if (mode & (MODE_DIRECT|MODE_DEDICATED)) 536 LcpOpen(OPEN_ACTIVE); 537 else 538 LcpOpen(VarOpenMode); 539 if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER) { 540 TtyCommandMode(1); 541 fprintf(stderr, "Packet mode.\r\n"); 542 aft_cmd = 1; 543 } 544} 545 546static void 547ShowHelp() 548{ 549 fprintf(stderr, "The following commands are available:\r\n"); 550 fprintf(stderr, " ~p\tEnter to Packet mode\r\n"); 551 fprintf(stderr, " ~-\tDecrease log level\r\n"); 552 fprintf(stderr, " ~+\tIncrease log level\r\n"); 553 fprintf(stderr, " ~.\tTerminate program\r\n"); 554 fprintf(stderr, " ~?\tThis help\r\n"); 555} 556 557static void 558ReadTty() 559{ 560 int n; 561 char ch; 562 static int ttystate; 563#define MAXLINESIZE 200 564 char linebuff[MAXLINESIZE]; 565 566#ifdef DEBUG 567 logprintf("termode = %d, netfd = %d, mode = %d\n", TermMode, netfd, mode); 568#endif 569 if (!TermMode) { 570 n = read(netfd, linebuff, sizeof(linebuff)-1); 571 aft_cmd = 1; 572 if (n > 0) { 573 DecodeCommand(linebuff, n, 1); 574 } else { 575 LogPrintf(LOG_PHASE_BIT, "client connection closed.\n"); 576 VarLocalAuth = LOCAL_NO_AUTH; 577 close(netfd); 578 close(1); 579 dup2(2, 1); /* Have to have something here or the modem will be 1 */ 580 netfd = -1; 581 mode &= ~MODE_INTER; 582 } 583 return; 584 } 585 586 /* 587 * We are in terminal mode, decode special sequences 588 */ 589 n = read(0, &ch, 1); 590#ifdef DEBUG 591 logprintf("got %d bytes\n", n); 592#endif 593 594 if (n > 0) { 595 switch (ttystate) { 596 case 0: 597 if (ch == '~') 598 ttystate++; 599 else 600 write(modem, &ch, n); 601 break; 602 case 1: 603 switch (ch) { 604 case '?': 605 ShowHelp(); 606 break; 607 case '-': 608 if (loglevel > 0) { 609 loglevel--; 610 fprintf(stderr, "New loglevel is %d\r\n", loglevel); 611 } 612 break; 613 case '+': 614 loglevel++; 615 fprintf(stderr, "New loglevel is %d\r\n", loglevel); 616 break; 617#ifdef DEBUG 618 case 'm': 619 ShowMemMap(); 620 break; 621#endif 622 case 'p': 623 /* 624 * XXX: Should check carrier. 625 */ 626 if (LcpFsm.state <= ST_CLOSED) { 627 VarOpenMode = OPEN_ACTIVE; 628 PacketMode(); 629 } 630 break; 631#ifdef DEBUG 632 case 't': 633 ShowTimers(); 634 break; 635#endif 636 case '.': 637 TermMode = 1; 638 TtyCommandMode(1); 639 break; 640 default: 641 if (write(modem, &ch, n) < 0) 642 fprintf(stderr, "err in write.\r\n"); 643 break; 644 } 645 ttystate = 0; 646 break; 647 } 648 } 649} 650 651 652/* 653 * Here, we'll try to detect HDLC frame 654 */ 655 656static char *FrameHeaders[] = { 657 "\176\377\003\300\041", 658 "\176\377\175\043\300\041", 659 "\176\177\175\043\100\041", 660 "\176\175\337\175\043\300\041", 661 "\176\175\137\175\043\100\041", 662 NULL, 663}; 664 665u_char * 666HdlcDetect(cp, n) 667u_char *cp; 668int n; 669{ 670 char *ptr, *fp, **hp; 671 672 cp[n] = '\0'; /* be sure to null terminated */ 673 ptr = NULL; 674 for (hp = FrameHeaders; *hp; hp++) { 675 fp = *hp; 676 if (DEV_IS_SYNC) 677 fp++; 678 ptr = strstr((char *)cp, fp); 679 if (ptr) 680 break; 681 } 682 return((u_char *)ptr); 683} 684 685static struct pppTimer RedialTimer; 686 687static void 688RedialTimeout() 689{ 690 StopTimer(&RedialTimer); 691 LogPrintf(LOG_PHASE_BIT, "Redialing timer expired.\n"); 692} 693 694static void 695StartRedialTimer(Timeout) 696 int Timeout; 697{ 698 StopTimer(&RedialTimer); 699 700 if (Timeout) { 701 RedialTimer.state = TIMER_STOPPED; 702 703 if (Timeout > 0) 704 RedialTimer.load = Timeout * SECTICKS; 705 else 706 RedialTimer.load = (random() % REDIAL_PERIOD) * SECTICKS; 707 708 LogPrintf(LOG_PHASE_BIT, "Enter pause (%d) for redialing.\n", 709 RedialTimer.load / SECTICKS); 710 711 RedialTimer.func = RedialTimeout; 712 StartTimer(&RedialTimer); 713 } 714} 715 716 717static void 718DoLoop() 719{ 720 fd_set rfds, wfds, efds; 721 int pri, i, n, wfd, nfds; 722 struct sockaddr_in hisaddr; 723 struct timeval timeout, *tp; 724 int ssize = sizeof(hisaddr); 725 u_char *cp; 726 u_char rbuff[MAX_MRU]; 727 int dial_up; 728 int tries; 729 int qlen; 730 pid_t pgroup; 731 732 pgroup = getpgrp(); 733 734 if (mode & (MODE_DIRECT|MODE_BACKGROUND)) { 735 modem = OpenModem(mode); 736 LogPrintf(LOG_PHASE_BIT, "Packet mode enabled\n"); 737 fflush(stderr); 738 PacketMode(); 739 } else if (mode & MODE_DEDICATED) { 740 if (modem < 0) 741 modem = OpenModem(mode); 742 } 743 744 fflush(stdout); 745 746 timeout.tv_sec = 0; 747 timeout.tv_usec = 0; 748 lostCarrier = 0; 749 750 if (mode & MODE_BACKGROUND) 751 dial_up = TRUE; /* Bring the line up */ 752 else 753 dial_up = FALSE; /* XXXX */ 754 tries = 0; 755 for (;;) { 756 nfds = 0; 757 FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); 758 759 /* 760 * If the link is down and we're in DDIAL mode, bring it back 761 * up. 762 */ 763 if (mode & MODE_DDIAL && LcpFsm.state <= ST_CLOSED) 764 dial_up = TRUE; 765 766 /* 767 * If we lost carrier and want to re-establish the connection 768 * due to the "set reconnect" value, we'd better bring the line 769 * back up now. 770 */ 771 if (LcpFsm.state <= ST_CLOSED && dial_up != TRUE 772 && lostCarrier && lostCarrier <= VarReconnectTries) { 773 LogPrintf(LOG_PHASE_BIT, "Connection lost, re-establish (%d/%d)\n", 774 lostCarrier, VarReconnectTries); 775 StartRedialTimer(VarReconnectTimer); 776 dial_up = TRUE; 777 } 778 779 /* 780 * If Ip packet for output is enqueued and require dial up, 781 * Just do it! 782 */ 783 if ( dial_up && RedialTimer.state != TIMER_RUNNING ) { 784#ifdef DEBUG 785 logprintf("going to dial: modem = %d\n", modem); 786#endif 787 modem = OpenModem(mode); 788 if (modem < 0) { 789 StartRedialTimer(VarRedialTimeout); 790 } else { 791 tries++; /* Tries are per number, not per list of numbers. */ 792 if (VarDialTries) 793 LogPrintf(LOG_CHAT_BIT, "Dial attempt %u of %d\n", tries, 794 VarDialTries); 795 else 796 LogPrintf(LOG_CHAT_BIT, "Dial attempt %u\n", tries); 797 if (DialModem()) { 798 sleep(1); /* little pause to allow peer starts */ 799 ModemTimeout(); 800 PacketMode(); 801 dial_up = FALSE; 802 tries = 0; 803 } else { 804 CloseModem(); 805 if (mode & MODE_BACKGROUND) { 806 if (VarNextPhone == NULL) 807 Cleanup(EX_DIAL); /* Tried all numbers - no luck */ 808 else 809 /* Try all numbers in background mode */ 810 StartRedialTimer(VarRedialNextTimeout); 811 } else if (VarDialTries && tries >= VarDialTries) { 812 /* I give up ! Can't get through :( */ 813 StartRedialTimer(VarRedialTimeout); 814 dial_up = FALSE; 815 tries = 0; 816 } else if (VarNextPhone == NULL) 817 /* Dial failed. Keep quite during redial wait period. */ 818 StartRedialTimer(VarRedialTimeout); 819 else 820 StartRedialTimer(VarRedialNextTimeout); 821 } 822 } 823 } 824 qlen = ModemQlen(); 825 826 if (qlen == 0) { 827 IpStartOutput(); 828 qlen = ModemQlen(); 829 } 830 831 if (modem >= 0) { 832 if (modem + 1 > nfds) 833 nfds = modem + 1; 834 FD_SET(modem, &rfds); 835 FD_SET(modem, &efds); 836 if (qlen > 0) { 837 FD_SET(modem, &wfds); 838 } 839 } 840 if (server >= 0) { 841 if (server + 1 > nfds) 842 nfds = server + 1; 843 FD_SET(server, &rfds); 844 } 845 846 /* *** IMPORTANT *** 847 * 848 * CPU is serviced every TICKUNIT micro seconds. 849 * This value must be chosen with great care. If this values is 850 * too big, it results loss of characters from modem and poor responce. 851 * If this values is too small, ppp process eats many CPU time. 852 */ 853#ifndef SIGALRM 854 usleep(TICKUNIT); 855 TimerService(); 856#else 857 handle_signals(); 858#endif 859 860 /* If there are aren't many packets queued, look for some more. */ 861 if (qlen < 20 && tun_in >= 0) { 862 if (tun_in + 1 > nfds) 863 nfds = tun_in + 1; 864 FD_SET(tun_in, &rfds); 865 } 866 867 if (netfd >= 0) { 868 if (netfd + 1 > nfds) 869 nfds = netfd + 1; 870 FD_SET(netfd, &rfds); 871 FD_SET(netfd, &efds); 872 } 873 874#ifndef SIGALRM 875 /* 876 * Normally, select() will not block because modem is writable. 877 * In AUTO mode, select will block until we find packet from tun 878 */ 879 tp = (RedialTimer.state == TIMER_RUNNING)? &timeout : NULL; 880 i = select(nfds, &rfds, &wfds, &efds, tp); 881#else 882 /* 883 * When SIGALRM timer is running, a select function will be 884 * return -1 and EINTR after a Time Service signal hundler 885 * is done. If the redial timer is not running and we are 886 * trying to dial, poll with a 0 value timer. 887 */ 888 tp = (dial_up && RedialTimer.state != TIMER_RUNNING) ? &timeout : NULL; 889 i = select(nfds, &rfds, &wfds, &efds, tp); 890#endif 891 892 if ( i == 0 ) { 893 continue; 894 } 895 896 if ( i < 0 ) { 897 if ( errno == EINTR ) { 898 handle_signals(); 899 continue; 900 } 901 perror("select"); 902 break; 903 } 904 905 if ((netfd >= 0 && FD_ISSET(netfd, &efds)) || (modem >= 0 && FD_ISSET(modem, &efds))) { 906 logprintf("Exception detected.\n"); 907 break; 908 } 909 910 if (server >= 0 && FD_ISSET(server, &rfds)) { 911 LogPrintf(LOG_PHASE_BIT, "connected to client.\n"); 912 wfd = accept(server, (struct sockaddr *)&hisaddr, &ssize); 913 if (wfd < 0) { 914 perror("accept"); 915 continue; 916 } 917 if (netfd >= 0) { 918 write(wfd, "already in use.\n", 16); 919 close(wfd); 920 continue; 921 } else 922 netfd = wfd; 923 if (dup2(netfd, 1) < 0) { 924 perror("dup2"); 925 close(netfd); 926 netfd = -1; 927 continue; 928 } 929 mode |= MODE_INTER; 930 Greetings(); 931 switch ( LocalAuthInit() ) { 932 case NOT_FOUND: 933 fprintf(stdout,LAUTH_M1); 934 fprintf(stdout,LAUTH_M2); 935 fflush(stdout); 936 /* Fall down */ 937 case VALID: 938 VarLocalAuth = LOCAL_AUTH; 939 break; 940 default: 941 break; 942 } 943 (void) IsInteractive(); 944 Prompt(); 945 } 946 947 if ((mode & MODE_INTER) && (netfd >= 0 && FD_ISSET(netfd, &rfds)) && 948 ((mode & MODE_AUTO) || pgroup == tcgetpgrp(0))) { 949 /* something to read from tty */ 950 ReadTty(); 951 } 952 if (modem >= 0) { 953 if (FD_ISSET(modem, &wfds)) { /* ready to write into modem */ 954 ModemStartOutput(modem); 955 } 956 if (FD_ISSET(modem, &rfds)) { /* something to read from modem */ 957 if (LcpFsm.state <= ST_CLOSED) 958 usleep(10000); 959 n = read(modem, rbuff, sizeof(rbuff)); 960 if ((mode & MODE_DIRECT) && n <= 0) { 961 DownConnection(); 962 } else 963 LogDumpBuff(LOG_ASYNC, "ReadFromModem", rbuff, n); 964 965 if (LcpFsm.state <= ST_CLOSED) { 966 /* 967 * In dedicated mode, we just discard input until LCP is started. 968 */ 969 if (!(mode & MODE_DEDICATED)) { 970 cp = HdlcDetect(rbuff, n); 971 if (cp) { 972 /* 973 * LCP packet is detected. Turn ourselves into packet mode. 974 */ 975 if (cp != rbuff) { 976 write(1, rbuff, cp - rbuff); 977 write(1, "\r\n", 2); 978 } 979 PacketMode(); 980 } else 981 write(1, rbuff, n); 982 } 983 } else { 984 if (n > 0) 985 AsyncInput(rbuff, n); 986 } 987 } 988 } 989 990 if (tun_in >= 0 && FD_ISSET(tun_in, &rfds)) { /* something to read from tun */ 991 n = read(tun_in, rbuff, sizeof(rbuff)); 992 if (n < 0) { 993 perror("read from tun"); 994 continue; 995 } 996 /* 997 * Process on-demand dialup. Output packets are queued within tunnel 998 * device until IPCP is opened. 999 */ 1000 if (LcpFsm.state <= ST_CLOSED && (mode & MODE_AUTO)) { 1001 pri = PacketCheck(rbuff, n, FL_DIAL); 1002 if (pri >= 0) { 1003 if (mode & MODE_ALIAS) { 1004 PacketAliasOut((struct ip *)rbuff); 1005 n = ntohs(((struct ip *)rbuff)->ip_len); 1006 } 1007 IpEnqueue(pri, rbuff, n); 1008 dial_up = TRUE; /* XXX */ 1009 } 1010 continue; 1011 } 1012 pri = PacketCheck(rbuff, n, FL_OUT); 1013 if (pri >= 0) { 1014 if (mode & MODE_ALIAS) { 1015 PacketAliasOut((struct ip *)rbuff); 1016 n = ntohs(((struct ip *)rbuff)->ip_len); 1017 } 1018 IpEnqueue(pri, rbuff, n); 1019 } 1020 } 1021 } 1022 logprintf("job done.\n"); 1023} 1024