defs.c revision 64698
1270346Sjfv/*- 2270346Sjfv * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3319182Sngie * All rights reserved. 4270346Sjfv * 5270346Sjfv * Redistribution and use in source and binary forms, with or without 6318357Serj * modification, are permitted provided that the following conditions 7318357Serj * are met: 8270346Sjfv * 1. Redistributions of source code must retain the above copyright 9270346Sjfv * notice, this list of conditions and the following disclaimer. 10270346Sjfv * 2. Redistributions in binary form must reproduce the above copyright 11284049Sjfv * notice, this list of conditions and the following disclaimer in the 12270346Sjfv * documentation and/or other materials provided with the distribution. 13270346Sjfv * 14303967Ssbruno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15270346Sjfv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16270346Sjfv * 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 64698 2000-08-16 09:07:23Z 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 __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 && (*script != '#' || (flags & PARSE_NOHASH))) { 306 script += strspn(script, " \t"); 307 if (*script) { 308 if (nargs >= maxargs - 1) 309 break; 310 *pvect++ = script; 311 nargs++; 312 script = findblank(script, flags); 313 if (script == NULL) 314 return -1; 315 else if (!(flags & PARSE_NOHASH) && *script == '#') 316 *script = '\0'; 317 else if (*script) 318 *script++ = '\0'; 319 } 320 } 321 *pvect = NULL; 322 return nargs; 323} 324 325const char * 326NumStr(long val, char *buf, size_t sz) 327{ 328 static char result[23]; /* handles 64 bit numbers */ 329 330 if (buf == NULL || sz == 0) { 331 buf = result; 332 sz = sizeof result; 333 } 334 snprintf(buf, sz, "<%ld>", val); 335 return buf; 336} 337 338const char * 339HexStr(long val, char *buf, size_t sz) 340{ 341 static char result[21]; /* handles 64 bit numbers */ 342 343 if (buf == NULL || sz == 0) { 344 buf = result; 345 sz = sizeof result; 346 } 347 snprintf(buf, sz, "<0x%lx>", val); 348 return buf; 349} 350 351const char * 352ex_desc(int ex) 353{ 354 static char num[12]; /* Used immediately if returned */ 355 static const char * const desc[] = { 356 "normal", "start", "sock", "modem", "dial", "dead", "done", 357 "reboot", "errdead", "hangup", "term", "nodial", "nologin", 358 "redial", "reconnect" 359 }; 360 361 if (ex >= 0 && ex < sizeof desc / sizeof *desc) 362 return desc[ex]; 363 snprintf(num, sizeof num, "%d", ex); 364 return num; 365} 366 367void 368SetTitle(const char *title) 369{ 370 if (title == NULL) 371 setproctitle(NULL); 372 else if (title[0] == '-' && title[1] != '\0') 373 setproctitle("-%s", title + 1); 374 else 375 setproctitle("%s", title); 376} 377