From 77ff88ad4bb6f3a307795d6f590534f223b49833 Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Mon, 9 Feb 1998 19:24:03 +0000 Subject: [PATCH] Make the server descriptor into a `sort' of struct descriptor. --- usr.sbin/ppp/command.c | 11 ++-- usr.sbin/ppp/descriptor.h | 3 +- usr.sbin/ppp/main.c | 71 +++++------------------ usr.sbin/ppp/mbuf.c | 5 +- usr.sbin/ppp/server.c | 116 +++++++++++++++++++++++++++++++++++--- usr.sbin/ppp/server.h | 15 ++++- usr.sbin/ppp/systems.c | 3 +- 7 files changed, 145 insertions(+), 79 deletions(-) diff --git a/usr.sbin/ppp/command.c b/usr.sbin/ppp/command.c index 6f0ca63360a5..019c1c728d7e 100644 --- a/usr.sbin/ppp/command.c +++ b/usr.sbin/ppp/command.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: command.c,v 1.131.2.11 1998/02/07 20:49:32 brian Exp $ + * $Id: command.c,v 1.131.2.12 1998/02/09 19:20:41 brian Exp $ * */ #include @@ -66,7 +66,6 @@ #include "systems.h" #include "chat.h" #include "bundle.h" -#include "server.h" #include "main.h" #include "route.h" #include "ccp.h" @@ -77,6 +76,7 @@ #include "link.h" #include "descriptor.h" #include "physical.h" +#include "server.h" struct in_addr ifnetmask; static const char *HIDDEN = "********"; @@ -1054,10 +1054,9 @@ SetServer(struct cmdargs const *arg) if (mask != NULL || passwd != NULL) return -1; - oserver = server; - ServerClose(); - if (oserver != -1) - LogPrintf(LogPHASE, "Disabling server port.\n"); + + if (ServerClose()) + LogPrintf(LogPHASE, "Disabled server port.\n"); res = 0; } else if (*port == '/') { mode_t imask; diff --git a/usr.sbin/ppp/descriptor.h b/usr.sbin/ppp/descriptor.h index 37a8fd4d8ca2..ab58141c3e1a 100644 --- a/usr.sbin/ppp/descriptor.h +++ b/usr.sbin/ppp/descriptor.h @@ -23,10 +23,11 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: descriptor.h,v 1.1.2.1 1998/02/09 19:20:43 brian Exp $ */ #define PHYSICAL_DESCRIPTOR (1) +#define SERVER_DESCRIPTOR (1) struct descriptor { int type; diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index 072cf51de87d..964fc9c6d76f 100644 --- a/usr.sbin/ppp/main.c +++ b/usr.sbin/ppp/main.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: main.c,v 1.121.2.15 1998/02/08 19:29:45 brian Exp $ + * $Id: main.c,v 1.121.2.16 1998/02/09 19:21:01 brian Exp $ * * TODO: * o Add commands for traffic summary, version display, etc. @@ -68,7 +68,6 @@ #include "systems.h" #include "ip.h" #include "sig.h" -#include "server.h" #include "main.h" #include "vjcomp.h" #include "async.h" @@ -78,6 +77,7 @@ #include "link.h" #include "descriptor.h" #include "physical.h" +#include "server.h" #ifndef O_NONBLOCK #ifdef O_NDELAY @@ -724,20 +724,12 @@ StartRedialTimer(int Timeout) } } -#define IN_SIZE sizeof(struct sockaddr_in) -#define UN_SIZE sizeof(struct sockaddr_in) -#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE) - static void DoLoop(struct bundle *bundle) { fd_set rfds, wfds, efds; int pri, i, n, wfd, nfds; - char hisaddr[ADDRSZ]; - struct sockaddr *sa = (struct sockaddr *)hisaddr; - struct sockaddr_in *sin = (struct sockaddr_in *)hisaddr; struct timeval timeout, *tp; - int ssize = ADDRSZ; const u_char *cp; int tries; int qlen; @@ -871,12 +863,7 @@ DoLoop(struct bundle *bundle) } descriptor_UpdateSet(&bundle->physical->desc, &rfds, &wfds, &efds, &nfds); - - if (server >= 0) { - if (server + 1 > nfds) - nfds = server + 1; - FD_SET(server, &rfds); - } + descriptor_UpdateSet(&server.desc, &rfds, &wfds, &efds, &nfds); #ifndef SIGALRM /* @@ -888,8 +875,6 @@ DoLoop(struct bundle *bundle) */ usleep(TICKUNIT); TimerService(); -#else - handle_signals(); #endif /* If there are aren't many packets queued, look for some more. */ @@ -898,12 +883,16 @@ DoLoop(struct bundle *bundle) nfds = bundle->tun_fd + 1; FD_SET(bundle->tun_fd, &rfds); } + if (netfd >= 0) { if (netfd + 1 > nfds) nfds = netfd + 1; FD_SET(netfd, &rfds); FD_SET(netfd, &efds); } + + handle_signals(); + #ifndef SIGALRM /* @@ -915,8 +904,8 @@ DoLoop(struct bundle *bundle) #else /* - * When SIGALRM timer is running, a select function will be return -1 and - * EINTR after a Time Service signal hundler is done. If the redial + * When SIGALRM timer is running, the select function will return -1 and + * EINTR after the Time Service signal handler is done. If the redial * timer is not running and we are trying to dial, poll with a 0 value * timer. */ @@ -927,6 +916,7 @@ DoLoop(struct bundle *bundle) if (i == 0) { continue; } + if (i < 0) { if (errno == EINTR) { handle_signals(); @@ -935,48 +925,15 @@ DoLoop(struct bundle *bundle) LogPrintf(LogERROR, "DoLoop: select(): %s\n", strerror(errno)); break; } + if ((netfd >= 0 && FD_ISSET(netfd, &efds)) || descriptor_IsSet(&bundle->physical->desc, &efds)) { LogPrintf(LogALERT, "Exception detected.\n"); break; } - if (server >= 0 && FD_ISSET(server, &rfds)) { - wfd = accept(server, sa, &ssize); - if (wfd < 0) { - LogPrintf(LogERROR, "DoLoop: accept(): %s\n", strerror(errno)); - continue; - } - switch (sa->sa_family) { - case AF_LOCAL: - LogPrintf(LogPHASE, "Connected to local client.\n"); - break; - case AF_INET: - if (ntohs(sin->sin_port) < 1024) { - LogPrintf(LogALERT, "Rejected client connection from %s:%u" - "(invalid port number) !\n", - inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); - close(wfd); - continue; - } - LogPrintf(LogPHASE, "Connected to client from %s:%u\n", - inet_ntoa(sin->sin_addr), sin->sin_port); - break; - default: - write(wfd, "Unrecognised access !\n", 22); - close(wfd); - continue; - } - if (netfd >= 0) { - write(wfd, "Connection already in use.\n", 27); - close(wfd); - continue; - } - netfd = wfd; - VarTerm = fdopen(netfd, "a+"); - LocalAuthInit(); - IsInteractive(1); - Prompt(bundle); - } + + if (descriptor_IsSet(&server.desc, &rfds)) + descriptor_Read(&server.desc, bundle); if (netfd >= 0 && FD_ISSET(netfd, &rfds)) /* something to read from tty */ diff --git a/usr.sbin/ppp/mbuf.c b/usr.sbin/ppp/mbuf.c index fc9c781e9bba..4610005586d4 100644 --- a/usr.sbin/ppp/mbuf.c +++ b/usr.sbin/ppp/mbuf.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: mbuf.c,v 1.13.2.1 1998/01/30 19:45:54 brian Exp $ + * $Id: mbuf.c,v 1.13.2.2 1998/02/06 02:23:40 brian Exp $ * */ #include @@ -33,12 +33,11 @@ #include "defs.h" #include "loadalias.h" #include "vars.h" -#include "server.h" static struct memmap { struct mbuf *queue; int count; -} MemMap[MB_MAX + 2]; +} MemMap[MB_MAX + 2]; static int totalalloced; diff --git a/usr.sbin/ppp/server.c b/usr.sbin/ppp/server.c index 069843f77e05..fe1ecc850a53 100644 --- a/usr.sbin/ppp/server.c +++ b/usr.sbin/ppp/server.c @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: server.c,v 1.16 1998/01/21 02:15:27 brian Exp $ + * $Id: server.c,v 1.16.2.1 1998/02/02 19:32:15 brian Exp $ */ #include @@ -45,10 +45,108 @@ #include "loadalias.h" #include "defs.h" #include "vars.h" +#include "descriptor.h" #include "server.h" #include "id.h" -int server = -1; +static int +server_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) +{ + struct server *s = descriptor2server(d); + + LogPrintf(LogDEBUG, "descriptor2server; %p -> %p\n", d, s); + if (s->fd >= 0) { + FD_SET(s->fd, r); + if (*n < s->fd + 1) + *n = s->fd + 1; + return 1; + } + return 0; +} + +static int +server_IsSet(struct descriptor *d, fd_set *fdset) +{ + struct server *s = descriptor2server(d); + + LogPrintf(LogDEBUG, "descriptor2server; %p -> %p\n", d, s); + return s->fd >= 0 && FD_ISSET(s->fd, fdset); +} + +#define IN_SIZE sizeof(struct sockaddr_in) +#define UN_SIZE sizeof(struct sockaddr_in) +#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE) + +static void +server_Read(struct descriptor *d, struct bundle *bundle) +{ + struct server *s = descriptor2server(d); + char hisaddr[ADDRSZ]; + struct sockaddr *sa = (struct sockaddr *)hisaddr; + struct sockaddr_in *sin = (struct sockaddr_in *)hisaddr; + int ssize = ADDRSZ, wfd; + + LogPrintf(LogDEBUG, "descriptor2server; %p -> %p\n", d, s); + + wfd = accept(s->fd, sa, &ssize); + if (wfd < 0) { + LogPrintf(LogERROR, "server_Read: accept(): %s\n", strerror(errno)); + return; + } + + switch (sa->sa_family) { + case AF_LOCAL: + LogPrintf(LogPHASE, "Connected to local client.\n"); + break; + + case AF_INET: + if (ntohs(sin->sin_port) < 1024) { + LogPrintf(LogALERT, "Rejected client connection from %s:%u" + "(invalid port number) !\n", + inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); + close(wfd); + return; + } + LogPrintf(LogPHASE, "Connected to client from %s:%u\n", + inet_ntoa(sin->sin_addr), sin->sin_port); + break; + + default: + write(wfd, "Unrecognised access !\n", 22); + close(wfd); + return; + } + + if (netfd >= 0) { + write(wfd, "Connection already in use.\n", 27); + close(wfd); + } else { + netfd = wfd; + VarTerm = fdopen(netfd, "a+"); + LocalAuthInit(); + IsInteractive(1); + Prompt(bundle); + } +} + +static void +server_Write(struct descriptor *d) +{ + /* We never want to write here ! */ + LogPrintf(LogERROR, "server_Write: Internal error: Bad call !\n"); +} + +struct server server = { + { + SERVER_DESCRIPTOR, + NULL, + server_UpdateSet, + server_IsSet, + server_Read, + server_Write + }, + -1 +}; static struct sockaddr_un ifsun; static char *rm; @@ -104,7 +202,7 @@ ServerLocalOpen(const char *name, mode_t mask) return 5; } ServerClose(); - server = s; + server.fd = s; rm = ifsun.sun_path; LogPrintf(LogPHASE, "Listening at local socket %s.\n", name); return 0; @@ -150,20 +248,22 @@ ServerTcpOpen(int port) return 9; } ServerClose(); - server = s; + server.fd = s; LogPrintf(LogPHASE, "Listening at port %d.\n", port); return 0; } -void +int ServerClose() { - if (server >= 0) { - close(server); + if (server.fd >= 0) { + close(server.fd); if (rm) { ID0unlink(rm); rm = 0; } + server.fd = -1; + return 1; } - server = -1; + return 0; } diff --git a/usr.sbin/ppp/server.h b/usr.sbin/ppp/server.h index c0598bde82d6..6f406b899a1c 100644 --- a/usr.sbin/ppp/server.h +++ b/usr.sbin/ppp/server.h @@ -23,11 +23,20 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: server.h,v 1.4 1997/12/21 12:11:08 brian Exp $ */ -extern int server; +struct server { + struct descriptor desc; + int fd; +}; + +#define server2descriptor(s) (&(s)->desc) +#define descriptor2server(d) \ + ((d)->type == SERVER_DESCRIPTOR ? (struct server *)(d) : NULL) + +extern struct server server; extern int ServerLocalOpen(const char *, mode_t); extern int ServerTcpOpen(int); -extern void ServerClose(void); +extern int ServerClose(void); diff --git a/usr.sbin/ppp/systems.c b/usr.sbin/ppp/systems.c index 013b13a2215f..c3ad6fba465e 100644 --- a/usr.sbin/ppp/systems.c +++ b/usr.sbin/ppp/systems.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: systems.c,v 1.35.2.1 1998/02/02 19:32:15 brian Exp $ + * $Id: systems.c,v 1.35.2.2 1998/02/04 01:03:36 brian Exp $ * * TODO: */ @@ -41,6 +41,7 @@ #include "loadalias.h" #include "pathnames.h" #include "vars.h" +#include "descriptor.h" #include "server.h" #include "systems.h"