defs.c revision 78275
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 78275 2001-06-15 13:58:06Z 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 <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <termios.h> 42#if !defined(__FreeBSD__) || __FreeBSD__ < 3 43#include <time.h> 44#endif 45#include <unistd.h> 46 47#include "defs.h" 48 49#define issep(c) ((c) == '\t' || (c) == ' ') 50 51#if defined(__NetBSD__) || __FreeBSD__ < 3 52void 53randinit() 54{ 55#if defined(__FreeBSD__) 56 static int initdone; /* srandomdev() call is only required once */ 57 58 if (!initdone) { 59 initdone = 1; 60 srandomdev(); 61 } 62#else 63 srandom((time(NULL)^getpid())+random()); 64#endif 65} 66#endif 67 68ssize_t 69fullread(int fd, void *v, size_t n) 70{ 71 size_t got, total; 72 73 for (total = 0; total < n; total += got) 74 switch ((got = read(fd, (char *)v + total, n - total))) { 75 case 0: 76 return total; 77 case -1: 78 if (errno == EINTR) 79 got = 0; 80 else 81 return -1; 82 } 83 return total; 84} 85 86static struct { 87 int mode; 88 const char *name; 89} modes[] = { 90 { PHYS_INTERACTIVE, "interactive" }, 91 { PHYS_AUTO, "auto" }, 92 { PHYS_DIRECT, "direct" }, 93 { PHYS_DEDICATED, "dedicated" }, 94 { PHYS_DDIAL, "ddial" }, 95 { PHYS_BACKGROUND, "background" }, 96 { PHYS_FOREGROUND, "foreground" }, 97 { PHYS_ALL, "*" }, 98 { 0, 0 } 99}; 100 101const char * 102mode2Nam(int mode) 103{ 104 int m; 105 106 for (m = 0; modes[m].mode; m++) 107 if (modes[m].mode == mode) 108 return modes[m].name; 109 110 return "unknown"; 111} 112 113int 114Nam2mode(const char *name) 115{ 116 int m, got, len; 117 118 len = strlen(name); 119 got = -1; 120 for (m = 0; modes[m].mode; m++) 121 if (!strncasecmp(name, modes[m].name, len)) { 122 if (modes[m].name[len] == '\0') 123 return modes[m].mode; 124 if (got != -1) 125 return 0; 126 got = m; 127 } 128 129 return got == -1 ? 0 : modes[got].mode; 130} 131 132struct in_addr 133GetIpAddr(const char *cp) 134{ 135 struct in_addr ipaddr; 136 137 if (!strcasecmp(cp, "default")) 138 ipaddr.s_addr = INADDR_ANY; 139 else if (inet_aton(cp, &ipaddr) == 0) { 140 const char *ptr; 141 142 /* Any illegal characters ? */ 143 for (ptr = cp; *ptr != '\0'; ptr++) 144 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL) 145 break; 146 147 if (*ptr == '\0') { 148 struct hostent *hp; 149 150 hp = gethostbyname(cp); 151 if (hp && hp->h_addrtype == AF_INET) 152 memcpy(&ipaddr, hp->h_addr, hp->h_length); 153 else 154 ipaddr.s_addr = INADDR_NONE; 155 } else 156 ipaddr.s_addr = INADDR_NONE; 157 } 158 159 return ipaddr; 160} 161 162static const struct speeds { 163 int nspeed; 164 speed_t speed; 165} speeds[] = { 166#ifdef B50 167 { 50, B50, }, 168#endif 169#ifdef B75 170 { 75, B75, }, 171#endif 172#ifdef B110 173 { 110, B110, }, 174#endif 175#ifdef B134 176 { 134, B134, }, 177#endif 178#ifdef B150 179 { 150, B150, }, 180#endif 181#ifdef B200 182 { 200, B200, }, 183#endif 184#ifdef B300 185 { 300, B300, }, 186#endif 187#ifdef B600 188 { 600, B600, }, 189#endif 190#ifdef B1200 191 { 1200, B1200, }, 192#endif 193#ifdef B1800 194 { 1800, B1800, }, 195#endif 196#ifdef B2400 197 { 2400, B2400, }, 198#endif 199#ifdef B4800 200 { 4800, B4800, }, 201#endif 202#ifdef B9600 203 { 9600, B9600, }, 204#endif 205#ifdef B19200 206 { 19200, B19200, }, 207#endif 208#ifdef B38400 209 { 38400, B38400, }, 210#endif 211#ifndef _POSIX_SOURCE 212#ifdef B7200 213 { 7200, B7200, }, 214#endif 215#ifdef B14400 216 { 14400, B14400, }, 217#endif 218#ifdef B28800 219 { 28800, B28800, }, 220#endif 221#ifdef B57600 222 { 57600, B57600, }, 223#endif 224#ifdef B76800 225 { 76800, B76800, }, 226#endif 227#ifdef B115200 228 { 115200, B115200, }, 229#endif 230#ifdef B230400 231 { 230400, B230400, }, 232#endif 233#ifdef EXTA 234 { 19200, EXTA, }, 235#endif 236#ifdef EXTB 237 { 38400, EXTB, }, 238#endif 239#endif /* _POSIX_SOURCE */ 240 { 0, 0 } 241}; 242 243int 244SpeedToInt(speed_t speed) 245{ 246 const struct speeds *sp; 247 248 for (sp = speeds; sp->nspeed; sp++) { 249 if (sp->speed == speed) { 250 return sp->nspeed; 251 } 252 } 253 return 0; 254} 255 256speed_t 257IntToSpeed(int nspeed) 258{ 259 const struct speeds *sp; 260 261 for (sp = speeds; sp->nspeed; sp++) { 262 if (sp->nspeed == nspeed) { 263 return sp->speed; 264 } 265 } 266 return B0; 267} 268 269char * 270findblank(char *p, int flags) 271{ 272 int instring; 273 274 instring = 0; 275 while (*p) { 276 if (*p == '\\') { 277 if (flags & PARSE_REDUCE) { 278 memmove(p, p + 1, strlen(p)); 279 if (!*p) 280 break; 281 } else 282 p++; 283 } else if (*p == '"') { 284 memmove(p, p + 1, strlen(p)); 285 instring = !instring; 286 continue; 287 } else if (!instring && (issep(*p) || 288 (*p == '#' && !(flags & PARSE_NOHASH)))) 289 return p; 290 p++; 291 } 292 293 return instring ? NULL : p; 294} 295 296int 297MakeArgs(char *script, char **pvect, int maxargs, int flags) 298{ 299 int nargs; 300 301 nargs = 0; 302 while (*script) { 303 script += strspn(script, " \t"); 304 if (*script == '#' && !(flags & PARSE_NOHASH)) { 305 *script = '\0'; 306 break; 307 } 308 if (*script) { 309 if (nargs >= maxargs - 1) 310 break; 311 *pvect++ = script; 312 nargs++; 313 script = findblank(script, flags); 314 if (script == NULL) 315 return -1; 316 else if (!(flags & PARSE_NOHASH) && *script == '#') 317 *script = '\0'; 318 else if (*script) 319 *script++ = '\0'; 320 } 321 } 322 *pvect = NULL; 323 return nargs; 324} 325 326const char * 327NumStr(long val, char *buf, size_t sz) 328{ 329 static char result[23]; /* handles 64 bit numbers */ 330 331 if (buf == NULL || sz == 0) { 332 buf = result; 333 sz = sizeof result; 334 } 335 snprintf(buf, sz, "<%ld>", val); 336 return buf; 337} 338 339const char * 340HexStr(long val, char *buf, size_t sz) 341{ 342 static char result[21]; /* handles 64 bit numbers */ 343 344 if (buf == NULL || sz == 0) { 345 buf = result; 346 sz = sizeof result; 347 } 348 snprintf(buf, sz, "<0x%lx>", val); 349 return buf; 350} 351 352const char * 353ex_desc(int ex) 354{ 355 static char num[12]; /* Used immediately if returned */ 356 static const char * const desc[] = { 357 "normal", "start", "sock", "modem", "dial", "dead", "done", 358 "reboot", "errdead", "hangup", "term", "nodial", "nologin", 359 "redial", "reconnect" 360 }; 361 362 if (ex >= 0 && ex < sizeof desc / sizeof *desc) 363 return desc[ex]; 364 snprintf(num, sizeof num, "%d", ex); 365 return num; 366} 367 368void 369SetTitle(const char *title) 370{ 371 if (title == NULL) 372 setproctitle(NULL); 373 else if (title[0] == '-' && title[1] != '\0') 374 setproctitle("-%s", title + 1); 375 else 376 setproctitle("%s", title); 377} 378 379fd_set * 380mkfdset() 381{ 382 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask)); 383} 384 385void 386zerofdset(fd_set *s) 387{ 388 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask)); 389} 390