defs.c revision 50479
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 50479 1999-08-28 01:35:59Z peter $ 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> 3830715Sbrian#include <stdlib.h> 3931121Sbrian#include <string.h> 4046686Sbrian#include <termios.h> 4137192Sbrian#if !defined(__FreeBSD__) || __FreeBSD__ < 3 4234539Sbrian#include <time.h> 4337192Sbrian#endif 4431196Sbrian#include <unistd.h> 4530715Sbrian 4630715Sbrian#include "defs.h" 4730715Sbrian 4846686Sbrian#define issep(c) ((c) == '\t' || (c) == ' ') 4946686Sbrian 5030715Sbrianvoid 5130715Sbrianrandinit() 5230715Sbrian{ 5336453Sbrian#if __FreeBSD__ >= 3 5437010Sbrian static int initdone; /* srandomdev() call is only required once */ 5530715Sbrian 5630715Sbrian if (!initdone) { 5730715Sbrian initdone = 1; 5830715Sbrian srandomdev(); 5930715Sbrian } 6031343Sbrian#else 6136285Sbrian srandom((time(NULL)^getpid())+random()); 6231343Sbrian#endif 6330715Sbrian} 6431196Sbrian 6536285Sbrianssize_t 6636285Sbrianfullread(int fd, void *v, size_t n) 6736285Sbrian{ 6836285Sbrian size_t got, total; 6931196Sbrian 7036285Sbrian for (total = 0; total < n; total += got) 7136285Sbrian switch ((got = read(fd, (char *)v + total, n - total))) { 7236285Sbrian case 0: 7336285Sbrian return total; 7436285Sbrian case -1: 7536285Sbrian if (errno == EINTR) 7636285Sbrian got = 0; 7736285Sbrian else 7836285Sbrian return -1; 7936285Sbrian } 8036285Sbrian return total; 8136285Sbrian} 8236285Sbrian 8336285Sbrianstatic struct { 8436285Sbrian int mode; 8536285Sbrian const char *name; 8636285Sbrian} modes[] = { 8736465Sbrian { PHYS_INTERACTIVE, "interactive" }, 8836465Sbrian { PHYS_AUTO, "auto" }, 8936285Sbrian { PHYS_DIRECT, "direct" }, 9036285Sbrian { PHYS_DEDICATED, "dedicated" }, 9136465Sbrian { PHYS_DDIAL, "ddial" }, 9236465Sbrian { PHYS_BACKGROUND, "background" }, 9336285Sbrian { PHYS_ALL, "*" }, 9436285Sbrian { 0, 0 } 9536285Sbrian}; 9636285Sbrian 9736285Sbrianconst char * 9836285Sbrianmode2Nam(int mode) 9931196Sbrian{ 10036285Sbrian int m; 10131196Sbrian 10236285Sbrian for (m = 0; modes[m].mode; m++) 10336285Sbrian if (modes[m].mode == mode) 10436285Sbrian return modes[m].name; 10531196Sbrian 10636285Sbrian return "unknown"; 10731196Sbrian} 10831203Sbrian 10936285Sbrianint 11036285SbrianNam2mode(const char *name) 11131203Sbrian{ 11236285Sbrian int m, got, len; 11331203Sbrian 11436285Sbrian len = strlen(name); 11536285Sbrian got = -1; 11636285Sbrian for (m = 0; modes[m].mode; m++) 11736285Sbrian if (!strncasecmp(name, modes[m].name, len)) { 11836285Sbrian if (modes[m].name[len] == '\0') 11936285Sbrian return modes[m].mode; 12036285Sbrian if (got != -1) 12136285Sbrian return 0; 12236285Sbrian got = m; 12336285Sbrian } 12436285Sbrian 12536285Sbrian return got == -1 ? 0 : modes[got].mode; 12631203Sbrian} 12744279Sbrian 12844279Sbrianstruct in_addr 12944279SbrianGetIpAddr(const char *cp) 13044279Sbrian{ 13144279Sbrian struct in_addr ipaddr; 13244279Sbrian 13344279Sbrian if (!strcasecmp(cp, "default")) 13444279Sbrian ipaddr.s_addr = INADDR_ANY; 13544279Sbrian else if (inet_aton(cp, &ipaddr) == 0) { 13644279Sbrian const char *ptr; 13744279Sbrian 13844279Sbrian /* Any illegal characters ? */ 13944279Sbrian for (ptr = cp; *ptr != '\0'; ptr++) 14044279Sbrian if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL) 14144279Sbrian break; 14244279Sbrian 14344279Sbrian if (*ptr == '\0') { 14444279Sbrian struct hostent *hp; 14544279Sbrian 14644279Sbrian hp = gethostbyname(cp); 14744279Sbrian if (hp && hp->h_addrtype == AF_INET) 14844279Sbrian memcpy(&ipaddr, hp->h_addr, hp->h_length); 14944279Sbrian else 15044279Sbrian ipaddr.s_addr = INADDR_NONE; 15144279Sbrian } else 15244279Sbrian ipaddr.s_addr = INADDR_NONE; 15344279Sbrian } 15444279Sbrian 15544279Sbrian return ipaddr; 15644279Sbrian} 15746686Sbrian 15846686Sbrianstatic const struct speeds { 15946686Sbrian int nspeed; 16046686Sbrian speed_t speed; 16146686Sbrian} speeds[] = { 16246686Sbrian#ifdef B50 16346686Sbrian { 50, B50, }, 16446686Sbrian#endif 16546686Sbrian#ifdef B75 16646686Sbrian { 75, B75, }, 16746686Sbrian#endif 16846686Sbrian#ifdef B110 16946686Sbrian { 110, B110, }, 17046686Sbrian#endif 17146686Sbrian#ifdef B134 17246686Sbrian { 134, B134, }, 17346686Sbrian#endif 17446686Sbrian#ifdef B150 17546686Sbrian { 150, B150, }, 17646686Sbrian#endif 17746686Sbrian#ifdef B200 17846686Sbrian { 200, B200, }, 17946686Sbrian#endif 18046686Sbrian#ifdef B300 18146686Sbrian { 300, B300, }, 18246686Sbrian#endif 18346686Sbrian#ifdef B600 18446686Sbrian { 600, B600, }, 18546686Sbrian#endif 18646686Sbrian#ifdef B1200 18746686Sbrian { 1200, B1200, }, 18846686Sbrian#endif 18946686Sbrian#ifdef B1800 19046686Sbrian { 1800, B1800, }, 19146686Sbrian#endif 19246686Sbrian#ifdef B2400 19346686Sbrian { 2400, B2400, }, 19446686Sbrian#endif 19546686Sbrian#ifdef B4800 19646686Sbrian { 4800, B4800, }, 19746686Sbrian#endif 19846686Sbrian#ifdef B9600 19946686Sbrian { 9600, B9600, }, 20046686Sbrian#endif 20146686Sbrian#ifdef B19200 20246686Sbrian { 19200, B19200, }, 20346686Sbrian#endif 20446686Sbrian#ifdef B38400 20546686Sbrian { 38400, B38400, }, 20646686Sbrian#endif 20746686Sbrian#ifndef _POSIX_SOURCE 20846686Sbrian#ifdef B7200 20946686Sbrian { 7200, B7200, }, 21046686Sbrian#endif 21146686Sbrian#ifdef B14400 21246686Sbrian { 14400, B14400, }, 21346686Sbrian#endif 21446686Sbrian#ifdef B28800 21546686Sbrian { 28800, B28800, }, 21646686Sbrian#endif 21746686Sbrian#ifdef B57600 21846686Sbrian { 57600, B57600, }, 21946686Sbrian#endif 22046686Sbrian#ifdef B76800 22146686Sbrian { 76800, B76800, }, 22246686Sbrian#endif 22346686Sbrian#ifdef B115200 22446686Sbrian { 115200, B115200, }, 22546686Sbrian#endif 22646686Sbrian#ifdef B230400 22746686Sbrian { 230400, B230400, }, 22846686Sbrian#endif 22946686Sbrian#ifdef EXTA 23046686Sbrian { 19200, EXTA, }, 23146686Sbrian#endif 23246686Sbrian#ifdef EXTB 23346686Sbrian { 38400, EXTB, }, 23446686Sbrian#endif 23546686Sbrian#endif /* _POSIX_SOURCE */ 23646686Sbrian { 0, 0 } 23746686Sbrian}; 23846686Sbrian 23946686Sbrianint 24046686SbrianSpeedToInt(speed_t speed) 24146686Sbrian{ 24246686Sbrian const struct speeds *sp; 24346686Sbrian 24446686Sbrian for (sp = speeds; sp->nspeed; sp++) { 24546686Sbrian if (sp->speed == speed) { 24646686Sbrian return sp->nspeed; 24746686Sbrian } 24846686Sbrian } 24946686Sbrian return 0; 25046686Sbrian} 25146686Sbrian 25246686Sbrianspeed_t 25346686SbrianIntToSpeed(int nspeed) 25446686Sbrian{ 25546686Sbrian const struct speeds *sp; 25646686Sbrian 25746686Sbrian for (sp = speeds; sp->nspeed; sp++) { 25846686Sbrian if (sp->nspeed == nspeed) { 25946686Sbrian return sp->speed; 26046686Sbrian } 26146686Sbrian } 26246686Sbrian return B0; 26346686Sbrian} 26446686Sbrian 26546686Sbrianstatic char * 26646686Sbrianfindblank(char *p, int instring) 26746686Sbrian{ 26846686Sbrian if (instring) { 26946686Sbrian while (*p) { 27046686Sbrian if (*p == '\\') { 27147551Sbrian memmove(p, p + 1, strlen(p)); 27246686Sbrian if (!*p) 27346686Sbrian break; 27446686Sbrian } else if (*p == '"') 27546686Sbrian return (p); 27646686Sbrian p++; 27746686Sbrian } 27846686Sbrian } else { 27946686Sbrian while (*p) { 28046686Sbrian if (issep(*p)) 28146686Sbrian return (p); 28246686Sbrian p++; 28346686Sbrian } 28446686Sbrian } 28546686Sbrian 28646686Sbrian return p; 28746686Sbrian} 28846686Sbrian 28946686Sbrianint 29046686SbrianMakeArgs(char *script, char **pvect, int maxargs) 29146686Sbrian{ 29246686Sbrian int nargs, nb; 29346686Sbrian int instring; 29446686Sbrian 29546686Sbrian nargs = 0; 29646686Sbrian while (*script) { 29746686Sbrian nb = strspn(script, " \t"); 29846686Sbrian script += nb; 29946686Sbrian if (*script) { 30046686Sbrian if (*script == '"') { 30146686Sbrian instring = 1; 30246686Sbrian script++; 30346686Sbrian if (*script == '\0') 30447551Sbrian break; /* Shouldn't return here. Need to NULL 30546686Sbrian * terminate below */ 30646686Sbrian } else 30746686Sbrian instring = 0; 30846686Sbrian if (nargs >= maxargs - 1) 30946686Sbrian break; 31046686Sbrian *pvect++ = script; 31146686Sbrian nargs++; 31246686Sbrian script = findblank(script, instring); 31346686Sbrian if (*script) 31446686Sbrian *script++ = '\0'; 31546686Sbrian } 31646686Sbrian } 31746686Sbrian *pvect = NULL; 31846686Sbrian return nargs; 31946686Sbrian} 320