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$ 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 52 void 53 randinit() 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 68 ssize_t 69 fullread(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 86 static 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 101 const char * 102 mode2Nam(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 113 int 114 Nam2mode(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 132 struct in_addr 133 GetIpAddr(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 162 static 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 243 int 244 SpeedToInt(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 256 speed_t 257 IntToSpeed(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 269 char * 270 findblank(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 296 int 297 MakeArgs(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 nargs--; 319 } else if (*script) 320 *script++ = '\0'; 321 } 322 } 323 *pvect = NULL; 324 return nargs; 325 } 326 327 const char * 328 NumStr(long val, char *buf, size_t sz) 329 { 330 static char result[23]; /* handles 64 bit numbers */ 331 332 if (buf == NULL || sz == 0) { 333 buf = result; 334 sz = sizeof result; 335 } 336 snprintf(buf, sz, "<%ld>", val); 337 return buf; 338 } 339 340 const char * 341 HexStr(long val, char *buf, size_t sz) 342 { 343 static char result[21]; /* handles 64 bit numbers */ 344 345 if (buf == NULL || sz == 0) { 346 buf = result; 347 sz = sizeof result; 348 } 349 snprintf(buf, sz, "<0x%lx>", val); 350 return buf; 351 } 352 353 const char * 354 ex_desc(int ex) 355 { 356 static char num[12]; /* Used immediately if returned */ 357 static const char * const desc[] = { 358 "normal", "start", "sock", "modem", "dial", "dead", "done", 359 "reboot", "errdead", "hangup", "term", "nodial", "nologin", 360 "redial", "reconnect" 361 }; 362 363 if (ex >= 0 && ex < sizeof desc / sizeof *desc) 364 return desc[ex]; 365 snprintf(num, sizeof num, "%d", ex); 366 return num; 367 } 368 369 void 370 SetTitle(const char *title) 371 { 372 if (title == NULL) 373 setproctitle(NULL); 374 else if (title[0] == '-' && title[1] != '\0') 375 setproctitle("-%s", title + 1); 376 else 377 setproctitle("%s", title); 378 } 379 380 fd_set * 381 mkfdset() 382 { 383 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask)); 384 } 385 386 void 387 zerofdset(fd_set *s) 388 { 389 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask)); 390 } 391