Deleted Added
full compact
main.c (6764) main.c (7001)
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 *
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.2 1995/02/26 12:17:41 amurai Exp $
20 * $Id: main.c,v 1.3 1995/02/27 10:57:50 amurai 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 <sys/time.h>

--- 6 unchanged lines hidden (view full) ---

35#include <arpa/inet.h>
36#include "modem.h"
37#include "os.h"
38#include "hdlc.h"
39#include "lcp.h"
40#include "ipcp.h"
41#include "vars.h"
42#include "auth.h"
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 <sys/time.h>

--- 6 unchanged lines hidden (view full) ---

35#include <arpa/inet.h>
36#include "modem.h"
37#include "os.h"
38#include "hdlc.h"
39#include "lcp.h"
40#include "ipcp.h"
41#include "vars.h"
42#include "auth.h"
43#include "filter.h"
43
44#define LAUTH_M1 "Warning: No password entry for this host in ppp.secret\n"
45#define LAUTH_M2 "Warning: All manipulation is allowed by anyone in a world\n"
46
47#ifndef O_NONBLOCK
48#ifdef O_NDELAY
49#define O_NONBLOCK O_NDELAY
50#endif

--- 495 unchanged lines hidden (view full) ---

546{
547 fd_set rfds, wfds, efds;
548 int pri, i, n, wfd;
549 struct sockaddr_in hisaddr;
550 struct timeval timeout, *tp;
551 int ssize = sizeof(hisaddr);
552 u_char *cp;
553 u_char rbuff[MAX_MRU];
44
45#define LAUTH_M1 "Warning: No password entry for this host in ppp.secret\n"
46#define LAUTH_M2 "Warning: All manipulation is allowed by anyone in a world\n"
47
48#ifndef O_NONBLOCK
49#ifdef O_NDELAY
50#define O_NONBLOCK O_NDELAY
51#endif

--- 495 unchanged lines hidden (view full) ---

547{
548 fd_set rfds, wfds, efds;
549 int pri, i, n, wfd;
550 struct sockaddr_in hisaddr;
551 struct timeval timeout, *tp;
552 int ssize = sizeof(hisaddr);
553 u_char *cp;
554 u_char rbuff[MAX_MRU];
554 struct itimerval itimer;
555 int dial_up;
555
556 if (mode & MODE_DIRECT) {
557 modem = OpenModem(mode);
558 fprintf(stderr, "Packet mode enabled\n");
559 PacketMode();
560 } else if (mode & MODE_DEDICATED) {
561 if (!modem)
562 modem = OpenModem(mode);
563 }
564
565 fflush(stdout);
566
556
557 if (mode & MODE_DIRECT) {
558 modem = OpenModem(mode);
559 fprintf(stderr, "Packet mode enabled\n");
560 PacketMode();
561 } else if (mode & MODE_DEDICATED) {
562 if (!modem)
563 modem = OpenModem(mode);
564 }
565
566 fflush(stdout);
567
567 timeout.tv_sec = 0;;
568#ifdef SIGALRM
568#ifdef SIGALRM
569 signal(SIGALRM, (void (*)(int))TimerService);
570 itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
571 itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
572 setitimer(ITIMER_REAL, &itimer, NULL);
569 timeout.tv_sec = 0;
573#else
574 timeout.tv_usec = 0;
575#endif
576
570#else
571 timeout.tv_usec = 0;
572#endif
573
574 dial_up = FALSE; /* XXXX */
577 for (;;) {
575 for (;;) {
578 IpStartOutput();
576 if ( modem )
577 IpStartOutput();
579 FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
578 FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
580 FD_SET(tun_in, &rfds);
579
580 /*
581 * If Ip packet for output is enqueued and require dial up,
582 * Just do it!
583 */
584 if ( dial_up && RedialTimer.state != TIMER_RUNNING ) { /* XXX */
585#ifdef DEBUG
586 logprintf("going to dial: modem = %d\n", modem);
587#endif
588 modem = OpenModem(mode);
589 if (modem < 0) {
590 modem = 0; /* Set intial value for next OpenModem */
591 StartRedialTimer();
592 } else {
593 if (DialModem()) {
594 sleep(1); /* little pause to allow peer starts */
595 ModemTimeout();
596 PacketMode();
597 dial_up = FALSE;
598 } else {
599 CloseModem();
600 /* Dial failed. Keep quite during redial wait period. */
601 StartRedialTimer();
602 }
603 }
604 }
605 if (modem) {
606 FD_SET(modem, &rfds);
607 FD_SET(modem, &efds);
608 if (ModemQlen() > 0) {
609 FD_SET(modem, &wfds);
610 }
611 }
581 if (server > 0) FD_SET(server, &rfds);
582
583 /* *** IMPORTANT ***
584 *
585 * CPU is serviced every TICKUNIT micro seconds.
586 * This value must be chosen with great care. If this values is
587 * too big, it results loss of characters from modem and poor responce.
588 * If this values is too small, ppp process eats many CPU time.
589 */
590#ifndef SIGALRM
591 usleep(TICKUNIT);
592 TimerService();
593#endif
594
612 if (server > 0) FD_SET(server, &rfds);
613
614 /* *** IMPORTANT ***
615 *
616 * CPU is serviced every TICKUNIT micro seconds.
617 * This value must be chosen with great care. If this values is
618 * too big, it results loss of characters from modem and poor responce.
619 * If this values is too small, ppp process eats many CPU time.
620 */
621#ifndef SIGALRM
622 usleep(TICKUNIT);
623 TimerService();
624#endif
625
595 if (modem) {
596 FD_SET(modem, &rfds);
597 FD_SET(modem, &efds);
598 if (ModemQlen() > 0) {
599 FD_SET(modem, &wfds);
600 }
601 }
626 FD_SET(tun_in, &rfds);
602 if (netfd > -1) {
603 FD_SET(netfd, &rfds);
604 FD_SET(netfd, &efds);
605 }
627 if (netfd > -1) {
628 FD_SET(netfd, &rfds);
629 FD_SET(netfd, &efds);
630 }
631
632
606#ifndef SIGALRM
607 /*
633#ifndef SIGALRM
634 /*
608 * Normally, slect() will not block because modem is writable.
609 * In AUTO mode, select will block until we find packet from tun.
610 * However, we have to run ourselves while we are in redial wait state.
635 * Normally, select() will not block because modem is writable.
636 * In AUTO mode, select will block until we find packet from tun
611 */
612 tp = (RedialTimer.state == TIMER_RUNNING)? &timeout : NULL;
613 i = select(tun_in+10, &rfds, &wfds, &efds, tp);
614#else
637 */
638 tp = (RedialTimer.state == TIMER_RUNNING)? &timeout : NULL;
639 i = select(tun_in+10, &rfds, &wfds, &efds, tp);
640#else
641 /*
642 * When SIGALRM timer is running, a select function will be
643 * return -1 and EINTR after a Time Service signal hundler
644 * is done.
645 */
615 i = select(tun_in+10, &rfds, &wfds, &efds, NULL);
616#endif
646 i = select(tun_in+10, &rfds, &wfds, &efds, NULL);
647#endif
617 if (i == 0) {
618 continue;
648 if ( i == 0 ) {
649 continue;
619 }
620
650 }
651
621 if (i < 0) {
622 if (errno == EINTR)
623 continue;
624 perror("select");
625 break;
626 }
652 if ( i < 0 ) {
653 if ( errno == EINTR ) {
654 continue; /* Got SIGALRM, Do check a queue for dailing */
655 }
656 perror("select");
657 break;
658 }
659
627 if ((netfd > 0 && FD_ISSET(netfd, &efds)) || FD_ISSET(modem, &efds)) {
628 logprintf("Exception detected.\n");
629 break;
630 }
631
632 if (server > 0 && FD_ISSET(server, &rfds)) {
633#ifdef DEBUG
634 logprintf("connected to client.\n");

--- 67 unchanged lines hidden (view full) ---

702 if (n > 0)
703 AsyncInput(rbuff, n);
704#ifdef notdef
705 continue; /* THIS LINE RESULT AS POOR PERFORMANCE */
706#endif
707 }
708 }
709 }
660 if ((netfd > 0 && FD_ISSET(netfd, &efds)) || FD_ISSET(modem, &efds)) {
661 logprintf("Exception detected.\n");
662 break;
663 }
664
665 if (server > 0 && FD_ISSET(server, &rfds)) {
666#ifdef DEBUG
667 logprintf("connected to client.\n");

--- 67 unchanged lines hidden (view full) ---

735 if (n > 0)
736 AsyncInput(rbuff, n);
737#ifdef notdef
738 continue; /* THIS LINE RESULT AS POOR PERFORMANCE */
739#endif
740 }
741 }
742 }
743
710 if (FD_ISSET(tun_in, &rfds)) { /* something to read from tun */
711 /*
712 * If there are many packets queued, wait until they are drained.
713 */
714 if (ModemQlen() > 5)
715 continue;
744 if (FD_ISSET(tun_in, &rfds)) { /* something to read from tun */
745 /*
746 * If there are many packets queued, wait until they are drained.
747 */
748 if (ModemQlen() > 5)
749 continue;
716
750
717 n = read(tun_in, rbuff, sizeof(rbuff));
718 if (n < 0) {
719 perror("read from tun");
720 continue;
721 }
722 /*
723 * Process on-demand dialup. Output packets are queued within tunnel
724 * device until IPCP is opened.
725 */
726 if (LcpFsm.state <= ST_CLOSED && (mode & MODE_AUTO)) {
751 n = read(tun_in, rbuff, sizeof(rbuff));
752 if (n < 0) {
753 perror("read from tun");
754 continue;
755 }
756 /*
757 * Process on-demand dialup. Output packets are queued within tunnel
758 * device until IPCP is opened.
759 */
760 if (LcpFsm.state <= ST_CLOSED && (mode & MODE_AUTO)) {
727 pri = PacketCheck(rbuff, n, 2);
761 pri = PacketCheck(rbuff, n, FL_DIAL);
728 if (pri >= 0) {
762 if (pri >= 0) {
729 if (RedialTimer.state == TIMER_RUNNING) {
730 /*
731 * We are in redial wait state. Ignore packet.
732 */
733 continue;
734 }
735 modem = OpenModem(mode);
736#ifdef DEBUG
737 logprintf("going to dial: modem = %d\n", modem);
738#endif
739 if (modem < 0) {
740 printf("failed to open modem.\n");
741 Cleanup(EX_MODEM);
742 }
743
744 if (DialModem()) {
745 sleep(1); /* little pause to allow peer starts */
746 ModemTimeout();
747 PacketMode();
748 } else {
749 CloseModem();
750 /* Dial failed. Keep quite during redial wait period. */
751 /* XXX: We shoud implement re-dial */
752 StartRedialTimer();
753 continue;
754 }
755 IpEnqueue(pri, rbuff, n);
763 IpEnqueue(pri, rbuff, n);
764 dial_up = TRUE; /* XXX */
756 }
757 continue;
758 }
765 }
766 continue;
767 }
759 pri = PacketCheck(rbuff, n, 1);
768 pri = PacketCheck(rbuff, n, FL_OUT);
760 if (pri >= 0)
761 IpEnqueue(pri, rbuff, n);
762 }
763 }
769 if (pri >= 0)
770 IpEnqueue(pri, rbuff, n);
771 }
772 }
764#ifdef SIGALRM
765 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
766 setitimer(ITIMER_REAL, &itimer, NULL);
767 signal(SIGALRM, SIG_DFL);
768#endif
769 logprintf("job done.\n");
770}
773 logprintf("job done.\n");
774}