defs.c revision 53830
1/*- 2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/usr.sbin/ppp/defs.c 53830 1999-11-28 15:50:08Z brian $ 27 */ 28 29 30#include <sys/types.h> 31#include <netdb.h> 32#include <netinet/in.h> 33#include <arpa/inet.h> 34#include <sys/socket.h> 35 36#include <ctype.h> 37#include <errno.h> 38#include <stdlib.h> 39#include <string.h> 40#include <termios.h> 41#if !defined(__FreeBSD__) || __FreeBSD__ < 3 42#include <time.h> 43#endif 44#include <unistd.h> 45 46#include "defs.h" 47 48#define issep(c) ((c) == '\t' || (c) == ' ') 49 50void 51randinit() 52{ 53#if __FreeBSD__ >= 3 54 static int initdone; /* srandomdev() call is only required once */ 55 56 if (!initdone) { 57 initdone = 1; 58 srandomdev(); 59 } 60#else 61 srandom((time(NULL)^getpid())+random()); 62#endif 63} 64 65ssize_t 66fullread(int fd, void *v, size_t n) 67{ 68 size_t got, total; 69 70 for (total = 0; total < n; total += got) 71 switch ((got = read(fd, (char *)v + total, n - total))) { 72 case 0: 73 return total; 74 case -1: 75 if (errno == EINTR) 76 got = 0; 77 else 78 return -1; 79 } 80 return total; 81} 82 83static struct { 84 int mode; 85 const char *name; 86} modes[] = { 87 { PHYS_INTERACTIVE, "interactive" }, 88 { PHYS_AUTO, "auto" }, 89 { PHYS_DIRECT, "direct" }, 90 { PHYS_DEDICATED, "dedicated" }, 91 { PHYS_DDIAL, "ddial" }, 92 { PHYS_BACKGROUND, "background" }, 93 { PHYS_FOREGROUND, "foreground" }, 94 { PHYS_ALL, "*" }, 95 { 0, 0 } 96}; 97 98const char * 99mode2Nam(int mode) 100{ 101 int m; 102 103 for (m = 0; modes[m].mode; m++) 104 if (modes[m].mode == mode) 105 return modes[m].name; 106 107 return "unknown"; 108} 109 110int 111Nam2mode(const char *name) 112{ 113 int m, got, len; 114 115 len = strlen(name); 116 got = -1; 117 for (m = 0; modes[m].mode; m++) 118 if (!strncasecmp(name, modes[m].name, len)) { 119 if (modes[m].name[len] == '\0') 120 return modes[m].mode; 121 if (got != -1) 122 return 0; 123 got = m; 124 } 125 126 return got == -1 ? 0 : modes[got].mode; 127} 128 129struct in_addr 130GetIpAddr(const char *cp) 131{ 132 struct in_addr ipaddr; 133 134 if (!strcasecmp(cp, "default")) 135 ipaddr.s_addr = INADDR_ANY; 136 else if (inet_aton(cp, &ipaddr) == 0) { 137 const char *ptr; 138 139 /* Any illegal characters ? */ 140 for (ptr = cp; *ptr != '\0'; ptr++) 141 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL) 142 break; 143 144 if (*ptr == '\0') { 145 struct hostent *hp; 146 147 hp = gethostbyname(cp); 148 if (hp && hp->h_addrtype == AF_INET) 149 memcpy(&ipaddr, hp->h_addr, hp->h_length); 150 else 151 ipaddr.s_addr = INADDR_NONE; 152 } else 153 ipaddr.s_addr = INADDR_NONE; 154 } 155 156 return ipaddr; 157} 158 159static const struct speeds { 160 int nspeed; 161 speed_t speed; 162} speeds[] = { 163#ifdef B50 164 { 50, B50, }, 165#endif 166#ifdef B75 167 { 75, B75, }, 168#endif 169#ifdef B110 170 { 110, B110, }, 171#endif 172#ifdef B134 173 { 134, B134, }, 174#endif 175#ifdef B150 176 { 150, B150, }, 177#endif 178#ifdef B200 179 { 200, B200, }, 180#endif 181#ifdef B300 182 { 300, B300, }, 183#endif 184#ifdef B600 185 { 600, B600, }, 186#endif 187#ifdef B1200 188 { 1200, B1200, }, 189#endif 190#ifdef B1800 191 { 1800, B1800, }, 192#endif 193#ifdef B2400 194 { 2400, B2400, }, 195#endif 196#ifdef B4800 197 { 4800, B4800, }, 198#endif 199#ifdef B9600 200 { 9600, B9600, }, 201#endif 202#ifdef B19200 203 { 19200, B19200, }, 204#endif 205#ifdef B38400 206 { 38400, B38400, }, 207#endif 208#ifndef _POSIX_SOURCE 209#ifdef B7200 210 { 7200, B7200, }, 211#endif 212#ifdef B14400 213 { 14400, B14400, }, 214#endif 215#ifdef B28800 216 { 28800, B28800, }, 217#endif 218#ifdef B57600 219 { 57600, B57600, }, 220#endif 221#ifdef B76800 222 { 76800, B76800, }, 223#endif 224#ifdef B115200 225 { 115200, B115200, }, 226#endif 227#ifdef B230400 228 { 230400, B230400, }, 229#endif 230#ifdef EXTA 231 { 19200, EXTA, }, 232#endif 233#ifdef EXTB 234 { 38400, EXTB, }, 235#endif 236#endif /* _POSIX_SOURCE */ 237 { 0, 0 } 238}; 239 240int 241SpeedToInt(speed_t speed) 242{ 243 const struct speeds *sp; 244 245 for (sp = speeds; sp->nspeed; sp++) { 246 if (sp->speed == speed) { 247 return sp->nspeed; 248 } 249 } 250 return 0; 251} 252 253speed_t 254IntToSpeed(int nspeed) 255{ 256 const struct speeds *sp; 257 258 for (sp = speeds; sp->nspeed; sp++) { 259 if (sp->nspeed == nspeed) { 260 return sp->speed; 261 } 262 } 263 return B0; 264} 265 266static char * 267findblank(char *p, int instring) 268{ 269 if (instring) { 270 while (*p) { 271 if (*p == '\\') { 272 memmove(p, p + 1, strlen(p)); 273 if (!*p) 274 break; 275 } else if (*p == '"') 276 return (p); 277 p++; 278 } 279 } else { 280 while (*p) { 281 if (issep(*p)) 282 return (p); 283 p++; 284 } 285 } 286 287 return p; 288} 289 290int 291MakeArgs(char *script, char **pvect, int maxargs) 292{ 293 int nargs, nb; 294 int instring; 295 296 nargs = 0; 297 while (*script) { 298 nb = strspn(script, " \t"); 299 script += nb; 300 if (*script) { 301 if (*script == '"') { 302 instring = 1; 303 script++; 304 if (*script == '\0') 305 break; /* Shouldn't return here. Need to NULL 306 * terminate below */ 307 } else 308 instring = 0; 309 if (nargs >= maxargs - 1) 310 break; 311 *pvect++ = script; 312 nargs++; 313 script = findblank(script, instring); 314 if (*script) 315 *script++ = '\0'; 316 } 317 } 318 *pvect = NULL; 319 return nargs; 320} 321