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