defs.c revision 67825
131921Sbrian/*- 231921Sbrian * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 331921Sbrian * All rights reserved. 431921Sbrian * 531921Sbrian * Redistribution and use in source and binary forms, with or without 631921Sbrian * modification, are permitted provided that the following conditions 731921Sbrian * are met: 831921Sbrian * 1. Redistributions of source code must retain the above copyright 931921Sbrian * notice, this list of conditions and the following disclaimer. 1031921Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1131921Sbrian * notice, this list of conditions and the following disclaimer in the 1231921Sbrian * documentation and/or other materials provided with the distribution. 1331921Sbrian * 1431921Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1531921Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1631921Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1731921Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1831921Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1931921Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2031921Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2131921Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2231921Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2331921Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2431921Sbrian * SUCH DAMAGE. 2531921Sbrian * 2650479Speter * $FreeBSD: head/usr.sbin/ppp/defs.c 67825 2000-10-28 23:56:03Z brian $ 2730715Sbrian */ 2830715Sbrian 2931196Sbrian 3044279Sbrian#include <sys/types.h> 3144279Sbrian#include <netdb.h> 3244279Sbrian#include <netinet/in.h> 3344279Sbrian#include <arpa/inet.h> 3444279Sbrian#include <sys/socket.h> 3544279Sbrian 3644279Sbrian#include <ctype.h> 3746085Sbrian#include <errno.h> 3858034Sbrian#include <stdio.h> 3930715Sbrian#include <stdlib.h> 4031121Sbrian#include <string.h> 4146686Sbrian#include <termios.h> 4237192Sbrian#if !defined(__FreeBSD__) || __FreeBSD__ < 3 4334539Sbrian#include <time.h> 4437192Sbrian#endif 4531196Sbrian#include <unistd.h> 4630715Sbrian 4730715Sbrian#include "defs.h" 4830715Sbrian 4946686Sbrian#define issep(c) ((c) == '\t' || (c) == ' ') 5046686Sbrian 5165269Sbrian#if defined(__NetBSD__) || __FreeBSD__ < 3 5230715Sbrianvoid 5330715Sbrianrandinit() 5430715Sbrian{ 5565269Sbrian#if defined(__FreeBSD__) 5637010Sbrian static int initdone; /* srandomdev() call is only required once */ 5730715Sbrian 5830715Sbrian if (!initdone) { 5930715Sbrian initdone = 1; 6030715Sbrian srandomdev(); 6130715Sbrian } 6231343Sbrian#else 6336285Sbrian srandom((time(NULL)^getpid())+random()); 6431343Sbrian#endif 6530715Sbrian} 6665269Sbrian#endif 6731196Sbrian 6836285Sbrianssize_t 6936285Sbrianfullread(int fd, void *v, size_t n) 7036285Sbrian{ 7136285Sbrian size_t got, total; 7231196Sbrian 7336285Sbrian for (total = 0; total < n; total += got) 7436285Sbrian switch ((got = read(fd, (char *)v + total, n - total))) { 7536285Sbrian case 0: 7636285Sbrian return total; 7736285Sbrian case -1: 7836285Sbrian if (errno == EINTR) 7936285Sbrian got = 0; 8036285Sbrian else 8136285Sbrian return -1; 8236285Sbrian } 8336285Sbrian return total; 8436285Sbrian} 8536285Sbrian 8636285Sbrianstatic struct { 8736285Sbrian int mode; 8836285Sbrian const char *name; 8936285Sbrian} modes[] = { 9036465Sbrian { PHYS_INTERACTIVE, "interactive" }, 9136465Sbrian { PHYS_AUTO, "auto" }, 9236285Sbrian { PHYS_DIRECT, "direct" }, 9336285Sbrian { PHYS_DEDICATED, "dedicated" }, 9436465Sbrian { PHYS_DDIAL, "ddial" }, 9536465Sbrian { PHYS_BACKGROUND, "background" }, 9653830Sbrian { PHYS_FOREGROUND, "foreground" }, 9736285Sbrian { PHYS_ALL, "*" }, 9836285Sbrian { 0, 0 } 9936285Sbrian}; 10036285Sbrian 10136285Sbrianconst char * 10236285Sbrianmode2Nam(int mode) 10331196Sbrian{ 10436285Sbrian int m; 10531196Sbrian 10636285Sbrian for (m = 0; modes[m].mode; m++) 10736285Sbrian if (modes[m].mode == mode) 10836285Sbrian return modes[m].name; 10931196Sbrian 11036285Sbrian return "unknown"; 11131196Sbrian} 11231203Sbrian 11336285Sbrianint 11436285SbrianNam2mode(const char *name) 11531203Sbrian{ 11636285Sbrian int m, got, len; 11731203Sbrian 11836285Sbrian len = strlen(name); 11936285Sbrian got = -1; 12036285Sbrian for (m = 0; modes[m].mode; m++) 12136285Sbrian if (!strncasecmp(name, modes[m].name, len)) { 12236285Sbrian if (modes[m].name[len] == '\0') 12336285Sbrian return modes[m].mode; 12436285Sbrian if (got != -1) 12536285Sbrian return 0; 12636285Sbrian got = m; 12736285Sbrian } 12836285Sbrian 12936285Sbrian return got == -1 ? 0 : modes[got].mode; 13031203Sbrian} 13144279Sbrian 13244279Sbrianstruct in_addr 13344279SbrianGetIpAddr(const char *cp) 13444279Sbrian{ 13544279Sbrian struct in_addr ipaddr; 13644279Sbrian 13744279Sbrian if (!strcasecmp(cp, "default")) 13844279Sbrian ipaddr.s_addr = INADDR_ANY; 13944279Sbrian else if (inet_aton(cp, &ipaddr) == 0) { 14044279Sbrian const char *ptr; 14144279Sbrian 14244279Sbrian /* Any illegal characters ? */ 14344279Sbrian for (ptr = cp; *ptr != '\0'; ptr++) 14444279Sbrian if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL) 14544279Sbrian break; 14644279Sbrian 14744279Sbrian if (*ptr == '\0') { 14844279Sbrian struct hostent *hp; 14944279Sbrian 15044279Sbrian hp = gethostbyname(cp); 15144279Sbrian if (hp && hp->h_addrtype == AF_INET) 15244279Sbrian memcpy(&ipaddr, hp->h_addr, hp->h_length); 15344279Sbrian else 15444279Sbrian ipaddr.s_addr = INADDR_NONE; 15544279Sbrian } else 15644279Sbrian ipaddr.s_addr = INADDR_NONE; 15744279Sbrian } 15844279Sbrian 15944279Sbrian return ipaddr; 16044279Sbrian} 16146686Sbrian 16246686Sbrianstatic const struct speeds { 16346686Sbrian int nspeed; 16446686Sbrian speed_t speed; 16546686Sbrian} speeds[] = { 16646686Sbrian#ifdef B50 16746686Sbrian { 50, B50, }, 16846686Sbrian#endif 16946686Sbrian#ifdef B75 17046686Sbrian { 75, B75, }, 17146686Sbrian#endif 17246686Sbrian#ifdef B110 17346686Sbrian { 110, B110, }, 17446686Sbrian#endif 17546686Sbrian#ifdef B134 17646686Sbrian { 134, B134, }, 17746686Sbrian#endif 17846686Sbrian#ifdef B150 17946686Sbrian { 150, B150, }, 18046686Sbrian#endif 18146686Sbrian#ifdef B200 18246686Sbrian { 200, B200, }, 18346686Sbrian#endif 18446686Sbrian#ifdef B300 18546686Sbrian { 300, B300, }, 18646686Sbrian#endif 18746686Sbrian#ifdef B600 18846686Sbrian { 600, B600, }, 18946686Sbrian#endif 19046686Sbrian#ifdef B1200 19146686Sbrian { 1200, B1200, }, 19246686Sbrian#endif 19346686Sbrian#ifdef B1800 19446686Sbrian { 1800, B1800, }, 19546686Sbrian#endif 19646686Sbrian#ifdef B2400 19746686Sbrian { 2400, B2400, }, 19846686Sbrian#endif 19946686Sbrian#ifdef B4800 20046686Sbrian { 4800, B4800, }, 20146686Sbrian#endif 20246686Sbrian#ifdef B9600 20346686Sbrian { 9600, B9600, }, 20446686Sbrian#endif 20546686Sbrian#ifdef B19200 20646686Sbrian { 19200, B19200, }, 20746686Sbrian#endif 20846686Sbrian#ifdef B38400 20946686Sbrian { 38400, B38400, }, 21046686Sbrian#endif 21146686Sbrian#ifndef _POSIX_SOURCE 21246686Sbrian#ifdef B7200 21346686Sbrian { 7200, B7200, }, 21446686Sbrian#endif 21546686Sbrian#ifdef B14400 21646686Sbrian { 14400, B14400, }, 21746686Sbrian#endif 21846686Sbrian#ifdef B28800 21946686Sbrian { 28800, B28800, }, 22046686Sbrian#endif 22146686Sbrian#ifdef B57600 22246686Sbrian { 57600, B57600, }, 22346686Sbrian#endif 22446686Sbrian#ifdef B76800 22546686Sbrian { 76800, B76800, }, 22646686Sbrian#endif 22746686Sbrian#ifdef B115200 22846686Sbrian { 115200, B115200, }, 22946686Sbrian#endif 23046686Sbrian#ifdef B230400 23146686Sbrian { 230400, B230400, }, 23246686Sbrian#endif 23346686Sbrian#ifdef EXTA 23446686Sbrian { 19200, EXTA, }, 23546686Sbrian#endif 23646686Sbrian#ifdef EXTB 23746686Sbrian { 38400, EXTB, }, 23846686Sbrian#endif 23946686Sbrian#endif /* _POSIX_SOURCE */ 24046686Sbrian { 0, 0 } 24146686Sbrian}; 24246686Sbrian 24346686Sbrianint 24446686SbrianSpeedToInt(speed_t speed) 24546686Sbrian{ 24646686Sbrian const struct speeds *sp; 24746686Sbrian 24846686Sbrian for (sp = speeds; sp->nspeed; sp++) { 24946686Sbrian if (sp->speed == speed) { 25046686Sbrian return sp->nspeed; 25146686Sbrian } 25246686Sbrian } 25346686Sbrian return 0; 25446686Sbrian} 25546686Sbrian 25646686Sbrianspeed_t 25746686SbrianIntToSpeed(int nspeed) 25846686Sbrian{ 25946686Sbrian const struct speeds *sp; 26046686Sbrian 26146686Sbrian for (sp = speeds; sp->nspeed; sp++) { 26246686Sbrian if (sp->nspeed == nspeed) { 26346686Sbrian return sp->speed; 26446686Sbrian } 26546686Sbrian } 26646686Sbrian return B0; 26746686Sbrian} 26846686Sbrian 26954915Sbrianchar * 27055145Sbrianfindblank(char *p, int flags) 27146686Sbrian{ 27255065Sbrian int instring; 27355065Sbrian 27455065Sbrian instring = 0; 27554915Sbrian while (*p) { 27654915Sbrian if (*p == '\\') { 27755145Sbrian if (flags & PARSE_REDUCE) { 27855013Sbrian memmove(p, p + 1, strlen(p)); 27955013Sbrian if (!*p) 28055013Sbrian break; 28155013Sbrian } else 28255013Sbrian p++; 28355065Sbrian } else if (*p == '"') { 28455065Sbrian memmove(p, p + 1, strlen(p)); 28555065Sbrian instring = !instring; 28655065Sbrian continue; 28755145Sbrian } else if (!instring && (issep(*p) || 28855145Sbrian (*p == '#' && !(flags & PARSE_NOHASH)))) 28954915Sbrian return p; 29054915Sbrian p++; 29146686Sbrian } 29246686Sbrian 29354915Sbrian return instring ? NULL : p; 29446686Sbrian} 29546686Sbrian 29646686Sbrianint 29755145SbrianMakeArgs(char *script, char **pvect, int maxargs, int flags) 29846686Sbrian{ 29955065Sbrian int nargs; 30046686Sbrian 30146686Sbrian nargs = 0; 30265264Sbrian while (*script) { 30355065Sbrian script += strspn(script, " \t"); 30467825Sbrian if (*script == '#' && !(flags & PARSE_NOHASH)) { 30565264Sbrian *script = '\0'; 30665264Sbrian break; 30765264Sbrian } 30846686Sbrian if (*script) { 30946686Sbrian if (nargs >= maxargs - 1) 31065264Sbrian break; 31146686Sbrian *pvect++ = script; 31246686Sbrian nargs++; 31355145Sbrian script = findblank(script, flags); 31454914Sbrian if (script == NULL) 31554914Sbrian return -1; 31665264Sbrian else if (!(flags & PARSE_NOHASH) && *script == '#') { 31765264Sbrian *script = '\0'; 31865264Sbrian nargs--; 31965264Sbrian } else if (*script) 32065264Sbrian *script++ = '\0'; 32146686Sbrian } 32246686Sbrian } 32346686Sbrian *pvect = NULL; 32446686Sbrian return nargs; 32546686Sbrian} 32658034Sbrian 32758034Sbrianconst char * 32858034SbrianNumStr(long val, char *buf, size_t sz) 32958034Sbrian{ 33058034Sbrian static char result[23]; /* handles 64 bit numbers */ 33158034Sbrian 33258034Sbrian if (buf == NULL || sz == 0) { 33358034Sbrian buf = result; 33458034Sbrian sz = sizeof result; 33558034Sbrian } 33658034Sbrian snprintf(buf, sz, "<%ld>", val); 33758034Sbrian return buf; 33858034Sbrian} 33958034Sbrian 34058034Sbrianconst char * 34158034SbrianHexStr(long val, char *buf, size_t sz) 34258034Sbrian{ 34358034Sbrian static char result[21]; /* handles 64 bit numbers */ 34458034Sbrian 34558034Sbrian if (buf == NULL || sz == 0) { 34658034Sbrian buf = result; 34758034Sbrian sz = sizeof result; 34858034Sbrian } 34958034Sbrian snprintf(buf, sz, "<0x%lx>", val); 35058034Sbrian return buf; 35158034Sbrian} 35259084Sbrian 35359084Sbrianconst char * 35459084Sbrianex_desc(int ex) 35559084Sbrian{ 35659084Sbrian static char num[12]; /* Used immediately if returned */ 35759084Sbrian static const char * const desc[] = { 35859084Sbrian "normal", "start", "sock", "modem", "dial", "dead", "done", 35959084Sbrian "reboot", "errdead", "hangup", "term", "nodial", "nologin", 36059084Sbrian "redial", "reconnect" 36159084Sbrian }; 36259084Sbrian 36359084Sbrian if (ex >= 0 && ex < sizeof desc / sizeof *desc) 36459084Sbrian return desc[ex]; 36559084Sbrian snprintf(num, sizeof num, "%d", ex); 36659084Sbrian return num; 36759084Sbrian} 36864698Sbrian 36964698Sbrianvoid 37064698SbrianSetTitle(const char *title) 37164698Sbrian{ 37264698Sbrian if (title == NULL) 37364698Sbrian setproctitle(NULL); 37464698Sbrian else if (title[0] == '-' && title[1] != '\0') 37564698Sbrian setproctitle("-%s", title + 1); 37664698Sbrian else 37764698Sbrian setproctitle("%s", title); 37864698Sbrian} 37966898Sbrian 38066898Sbrianfd_set * 38166898Sbrianmkfdset() 38266898Sbrian{ 38366898Sbrian return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask)); 38466898Sbrian} 38566898Sbrian 38666898Sbrianvoid 38766898Sbrianzerofdset(fd_set *s) 38866898Sbrian{ 38966898Sbrian memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask)); 39066898Sbrian} 391