main.c (42d4d396689ff18f91456f636cf531b2f116e79b) main.c (77ff88ad4bb6f3a307795d6f590534f223b49833)
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.121.2.15 1998/02/08 19:29:45 brian Exp $
20 * $Id: main.c,v 1.121.2.16 1998/02/09 19:21:01 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 <sys/param.h>
27#include <sys/time.h>
28#include <sys/select.h>

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

63#include "ipcp.h"
64#include "loadalias.h"
65#include "vars.h"
66#include "auth.h"
67#include "filter.h"
68#include "systems.h"
69#include "ip.h"
70#include "sig.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 <sys/param.h>
27#include <sys/time.h>
28#include <sys/select.h>

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

63#include "ipcp.h"
64#include "loadalias.h"
65#include "vars.h"
66#include "auth.h"
67#include "filter.h"
68#include "systems.h"
69#include "ip.h"
70#include "sig.h"
71#include "server.h"
72#include "main.h"
73#include "vjcomp.h"
74#include "async.h"
75#include "pathnames.h"
76#include "tun.h"
77#include "route.h"
78#include "link.h"
79#include "descriptor.h"
80#include "physical.h"
71#include "main.h"
72#include "vjcomp.h"
73#include "async.h"
74#include "pathnames.h"
75#include "tun.h"
76#include "route.h"
77#include "link.h"
78#include "descriptor.h"
79#include "physical.h"
80#include "server.h"
81
82#ifndef O_NONBLOCK
83#ifdef O_NDELAY
84#define O_NONBLOCK O_NDELAY
85#endif
86#endif
87
88int TermMode = 0;

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

719 LogPrintf(LogPHASE, "Enter pause (%d) for redialing.\n",
720 RedialTimer.load / SECTICKS);
721
722 RedialTimer.func = RedialTimeout;
723 StartTimer(&RedialTimer);
724 }
725}
726
81
82#ifndef O_NONBLOCK
83#ifdef O_NDELAY
84#define O_NONBLOCK O_NDELAY
85#endif
86#endif
87
88int TermMode = 0;

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

719 LogPrintf(LogPHASE, "Enter pause (%d) for redialing.\n",
720 RedialTimer.load / SECTICKS);
721
722 RedialTimer.func = RedialTimeout;
723 StartTimer(&RedialTimer);
724 }
725}
726
727#define IN_SIZE sizeof(struct sockaddr_in)
728#define UN_SIZE sizeof(struct sockaddr_in)
729#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE)
730
731static void
732DoLoop(struct bundle *bundle)
733{
734 fd_set rfds, wfds, efds;
735 int pri, i, n, wfd, nfds;
727static void
728DoLoop(struct bundle *bundle)
729{
730 fd_set rfds, wfds, efds;
731 int pri, i, n, wfd, nfds;
736 char hisaddr[ADDRSZ];
737 struct sockaddr *sa = (struct sockaddr *)hisaddr;
738 struct sockaddr_in *sin = (struct sockaddr_in *)hisaddr;
739 struct timeval timeout, *tp;
732 struct timeval timeout, *tp;
740 int ssize = ADDRSZ;
741 const u_char *cp;
742 int tries;
743 int qlen;
744 int res;
745 struct tun_data tun;
746#define rbuff tun.data
747
748 if (mode & MODE_DIRECT) {

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

866
867 qlen = link_QueueLen(physical2link(bundle->physical));
868 if (qlen == 0) {
869 IpStartOutput(physical2link(bundle->physical));
870 qlen = link_QueueLen(physical2link(bundle->physical));
871 }
872
873 descriptor_UpdateSet(&bundle->physical->desc, &rfds, &wfds, &efds, &nfds);
733 const u_char *cp;
734 int tries;
735 int qlen;
736 int res;
737 struct tun_data tun;
738#define rbuff tun.data
739
740 if (mode & MODE_DIRECT) {

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

858
859 qlen = link_QueueLen(physical2link(bundle->physical));
860 if (qlen == 0) {
861 IpStartOutput(physical2link(bundle->physical));
862 qlen = link_QueueLen(physical2link(bundle->physical));
863 }
864
865 descriptor_UpdateSet(&bundle->physical->desc, &rfds, &wfds, &efds, &nfds);
866 descriptor_UpdateSet(&server.desc, &rfds, &wfds, &efds, &nfds);
874
867
875 if (server >= 0) {
876 if (server + 1 > nfds)
877 nfds = server + 1;
878 FD_SET(server, &rfds);
879 }
880
881#ifndef SIGALRM
882 /*
883 * *** IMPORTANT ***
884 * CPU is serviced every TICKUNIT micro seconds. This value must be chosen
885 * with great care. If this values is too big, it results in loss of
886 * characters from the modem and poor response. If this value is too
887 * small, ppp eats too much CPU time.
888 */
889 usleep(TICKUNIT);
890 TimerService();
868#ifndef SIGALRM
869 /*
870 * *** IMPORTANT ***
871 * CPU is serviced every TICKUNIT micro seconds. This value must be chosen
872 * with great care. If this values is too big, it results in loss of
873 * characters from the modem and poor response. If this value is too
874 * small, ppp eats too much CPU time.
875 */
876 usleep(TICKUNIT);
877 TimerService();
891#else
892 handle_signals();
893#endif
894
895 /* If there are aren't many packets queued, look for some more. */
896 if (qlen < 20 && bundle->tun_fd >= 0) {
897 if (bundle->tun_fd + 1 > nfds)
898 nfds = bundle->tun_fd + 1;
899 FD_SET(bundle->tun_fd, &rfds);
900 }
878#endif
879
880 /* If there are aren't many packets queued, look for some more. */
881 if (qlen < 20 && bundle->tun_fd >= 0) {
882 if (bundle->tun_fd + 1 > nfds)
883 nfds = bundle->tun_fd + 1;
884 FD_SET(bundle->tun_fd, &rfds);
885 }
886
901 if (netfd >= 0) {
902 if (netfd + 1 > nfds)
903 nfds = netfd + 1;
904 FD_SET(netfd, &rfds);
905 FD_SET(netfd, &efds);
906 }
887 if (netfd >= 0) {
888 if (netfd + 1 > nfds)
889 nfds = netfd + 1;
890 FD_SET(netfd, &rfds);
891 FD_SET(netfd, &efds);
892 }
893
894 handle_signals();
895
907#ifndef SIGALRM
908
909 /*
910 * Normally, select() will not block because modem is writable. In AUTO
911 * mode, select will block until we find packet from tun
912 */
913 tp = (RedialTimer.state == TIMER_RUNNING) ? &timeout : NULL;
914 i = select(nfds, &rfds, &wfds, &efds, tp);
915#else
916
917 /*
896#ifndef SIGALRM
897
898 /*
899 * Normally, select() will not block because modem is writable. In AUTO
900 * mode, select will block until we find packet from tun
901 */
902 tp = (RedialTimer.state == TIMER_RUNNING) ? &timeout : NULL;
903 i = select(nfds, &rfds, &wfds, &efds, tp);
904#else
905
906 /*
918 * When SIGALRM timer is running, a select function will be return -1 and
919 * EINTR after a Time Service signal hundler is done. If the redial
907 * When SIGALRM timer is running, the select function will return -1 and
908 * EINTR after the Time Service signal handler is done. If the redial
920 * timer is not running and we are trying to dial, poll with a 0 value
921 * timer.
922 */
923 tp = (dial_up && RedialTimer.state != TIMER_RUNNING) ? &timeout : NULL;
924 i = select(nfds, &rfds, &wfds, &efds, tp);
925#endif
926
927 if (i == 0) {
928 continue;
929 }
909 * timer is not running and we are trying to dial, poll with a 0 value
910 * timer.
911 */
912 tp = (dial_up && RedialTimer.state != TIMER_RUNNING) ? &timeout : NULL;
913 i = select(nfds, &rfds, &wfds, &efds, tp);
914#endif
915
916 if (i == 0) {
917 continue;
918 }
919
930 if (i < 0) {
931 if (errno == EINTR) {
932 handle_signals();
933 continue;
934 }
935 LogPrintf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
936 break;
937 }
920 if (i < 0) {
921 if (errno == EINTR) {
922 handle_signals();
923 continue;
924 }
925 LogPrintf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
926 break;
927 }
928
938 if ((netfd >= 0 && FD_ISSET(netfd, &efds)) ||
939 descriptor_IsSet(&bundle->physical->desc, &efds)) {
940 LogPrintf(LogALERT, "Exception detected.\n");
941 break;
942 }
929 if ((netfd >= 0 && FD_ISSET(netfd, &efds)) ||
930 descriptor_IsSet(&bundle->physical->desc, &efds)) {
931 LogPrintf(LogALERT, "Exception detected.\n");
932 break;
933 }
943 if (server >= 0 && FD_ISSET(server, &rfds)) {
944 wfd = accept(server, sa, &ssize);
945 if (wfd < 0) {
946 LogPrintf(LogERROR, "DoLoop: accept(): %s\n", strerror(errno));
947 continue;
948 }
949 switch (sa->sa_family) {
950 case AF_LOCAL:
951 LogPrintf(LogPHASE, "Connected to local client.\n");
952 break;
953 case AF_INET:
954 if (ntohs(sin->sin_port) < 1024) {
955 LogPrintf(LogALERT, "Rejected client connection from %s:%u"
956 "(invalid port number) !\n",
957 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
958 close(wfd);
959 continue;
960 }
961 LogPrintf(LogPHASE, "Connected to client from %s:%u\n",
962 inet_ntoa(sin->sin_addr), sin->sin_port);
963 break;
964 default:
965 write(wfd, "Unrecognised access !\n", 22);
966 close(wfd);
967 continue;
968 }
969 if (netfd >= 0) {
970 write(wfd, "Connection already in use.\n", 27);
971 close(wfd);
972 continue;
973 }
974 netfd = wfd;
975 VarTerm = fdopen(netfd, "a+");
976 LocalAuthInit();
977 IsInteractive(1);
978 Prompt(bundle);
979 }
980
934
935 if (descriptor_IsSet(&server.desc, &rfds))
936 descriptor_Read(&server.desc, bundle);
937
981 if (netfd >= 0 && FD_ISSET(netfd, &rfds))
982 /* something to read from tty */
983 ReadTty(bundle);
984
985 if (descriptor_IsSet(&bundle->physical->desc, &wfds)) {
986 /* ready to write into modem */
987 descriptor_Write(&bundle->physical->desc);
988 if (!link_IsActive(physical2link(bundle->physical)))

--- 65 unchanged lines hidden ---
938 if (netfd >= 0 && FD_ISSET(netfd, &rfds))
939 /* something to read from tty */
940 ReadTty(bundle);
941
942 if (descriptor_IsSet(&bundle->physical->desc, &wfds)) {
943 /* ready to write into modem */
944 descriptor_Write(&bundle->physical->desc);
945 if (!link_IsActive(physical2link(bundle->physical)))

--- 65 unchanged lines hidden ---