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 <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 50 void 51 randinit() 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 65 ssize_t 66 fullread(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 83 static 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_ALL, "*" }, 94 { 0, 0 } 95 }; 96 97 const char * 98 mode2Nam(int mode) 99 { 100 int m; 101 102 for (m = 0; modes[m].mode; m++) 103 if (modes[m].mode == mode) 104 return modes[m].name; 105 106 return "unknown"; 107 } 108 109 int 110 Nam2mode(const char *name) 111 { 112 int m, got, len; 113 114 len = strlen(name); 115 got = -1; 116 for (m = 0; modes[m].mode; m++) 117 if (!strncasecmp(name, modes[m].name, len)) { 118 if (modes[m].name[len] == '\0') 119 return modes[m].mode; 120 if (got != -1) 121 return 0; 122 got = m; 123 } 124 125 return got == -1 ? 0 : modes[got].mode; 126 } 127 128 struct in_addr 129 GetIpAddr(const char *cp) 130 { 131 struct in_addr ipaddr; 132 133 if (!strcasecmp(cp, "default")) 134 ipaddr.s_addr = INADDR_ANY; 135 else if (inet_aton(cp, &ipaddr) == 0) { 136 const char *ptr; 137 138 /* Any illegal characters ? */ 139 for (ptr = cp; *ptr != '\0'; ptr++) 140 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL) 141 break; 142 143 if (*ptr == '\0') { 144 struct hostent *hp; 145 146 hp = gethostbyname(cp); 147 if (hp && hp->h_addrtype == AF_INET) 148 memcpy(&ipaddr, hp->h_addr, hp->h_length); 149 else 150 ipaddr.s_addr = INADDR_NONE; 151 } else 152 ipaddr.s_addr = INADDR_NONE; 153 } 154 155 return ipaddr; 156 } 157 158 static const struct speeds { 159 int nspeed; 160 speed_t speed; 161 } speeds[] = { 162 #ifdef B50 163 { 50, B50, }, 164 #endif 165 #ifdef B75 166 { 75, B75, }, 167 #endif 168 #ifdef B110 169 { 110, B110, }, 170 #endif 171 #ifdef B134 172 { 134, B134, }, 173 #endif 174 #ifdef B150 175 { 150, B150, }, 176 #endif 177 #ifdef B200 178 { 200, B200, }, 179 #endif 180 #ifdef B300 181 { 300, B300, }, 182 #endif 183 #ifdef B600 184 { 600, B600, }, 185 #endif 186 #ifdef B1200 187 { 1200, B1200, }, 188 #endif 189 #ifdef B1800 190 { 1800, B1800, }, 191 #endif 192 #ifdef B2400 193 { 2400, B2400, }, 194 #endif 195 #ifdef B4800 196 { 4800, B4800, }, 197 #endif 198 #ifdef B9600 199 { 9600, B9600, }, 200 #endif 201 #ifdef B19200 202 { 19200, B19200, }, 203 #endif 204 #ifdef B38400 205 { 38400, B38400, }, 206 #endif 207 #ifndef _POSIX_SOURCE 208 #ifdef B7200 209 { 7200, B7200, }, 210 #endif 211 #ifdef B14400 212 { 14400, B14400, }, 213 #endif 214 #ifdef B28800 215 { 28800, B28800, }, 216 #endif 217 #ifdef B57600 218 { 57600, B57600, }, 219 #endif 220 #ifdef B76800 221 { 76800, B76800, }, 222 #endif 223 #ifdef B115200 224 { 115200, B115200, }, 225 #endif 226 #ifdef B230400 227 { 230400, B230400, }, 228 #endif 229 #ifdef EXTA 230 { 19200, EXTA, }, 231 #endif 232 #ifdef EXTB 233 { 38400, EXTB, }, 234 #endif 235 #endif /* _POSIX_SOURCE */ 236 { 0, 0 } 237 }; 238 239 int 240 SpeedToInt(speed_t speed) 241 { 242 const struct speeds *sp; 243 244 for (sp = speeds; sp->nspeed; sp++) { 245 if (sp->speed == speed) { 246 return sp->nspeed; 247 } 248 } 249 return 0; 250 } 251 252 speed_t 253 IntToSpeed(int nspeed) 254 { 255 const struct speeds *sp; 256 257 for (sp = speeds; sp->nspeed; sp++) { 258 if (sp->nspeed == nspeed) { 259 return sp->speed; 260 } 261 } 262 return B0; 263 } 264 265 static char * 266 findblank(char *p, int instring) 267 { 268 if (instring) { 269 while (*p) { 270 if (*p == '\\') { 271 memmove(p, p + 1, strlen(p)); 272 if (!*p) 273 break; 274 } else if (*p == '"') 275 return (p); 276 p++; 277 } 278 } else { 279 while (*p) { 280 if (issep(*p)) 281 return (p); 282 p++; 283 } 284 } 285 286 return p; 287 } 288 289 int 290 MakeArgs(char *script, char **pvect, int maxargs) 291 { 292 int nargs, nb; 293 int instring; 294 295 nargs = 0; 296 while (*script) { 297 nb = strspn(script, " \t"); 298 script += nb; 299 if (*script) { 300 if (*script == '"') { 301 instring = 1; 302 script++; 303 if (*script == '\0') 304 break; /* Shouldn't return here. Need to NULL 305 * terminate below */ 306 } else 307 instring = 0; 308 if (nargs >= maxargs - 1) 309 break; 310 *pvect++ = script; 311 nargs++; 312 script = findblank(script, instring); 313 if (*script) 314 *script++ = '\0'; 315 } 316 } 317 *pvect = NULL; 318 return nargs; 319 } 320