2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/usr.sbin/ppp/defs.c,v 1.31.2.12 2002/09/01 02:12:26 brian Exp $
30 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
42 #if defined(__DragonFly__) && !defined(NOKLDLOAD)
43 #include <sys/module.h>
46 #if !defined(__DragonFly__)
51 #if defined(__DragonFly__) && !defined(NOKLDLOAD)
57 #define issep(c) ((c) == '\t' || (c) == ' ')
59 #if defined(__NetBSD__)
63 #if defined(__DragonFly__)
64 static int initdone; /* srandomdev() call is only required once */
71 srandom((time(NULL)^getpid())+random());
77 fullread(int fd, void *v, size_t n)
81 for (total = 0; total < n; total += got)
82 switch ((got = read(fd, (char *)v + total, n - total))) {
98 { PHYS_INTERACTIVE, "interactive" },
99 { PHYS_AUTO, "auto" },
100 { PHYS_DIRECT, "direct" },
101 { PHYS_DEDICATED, "dedicated" },
102 { PHYS_DDIAL, "ddial" },
103 { PHYS_BACKGROUND, "background" },
104 { PHYS_FOREGROUND, "foreground" },
114 for (m = 0; modes[m].mode; m++)
115 if (modes[m].mode == mode)
116 return modes[m].name;
122 Nam2mode(const char *name)
128 for (m = 0; modes[m].mode; m++)
129 if (!strncasecmp(name, modes[m].name, len)) {
130 if (modes[m].name[len] == '\0')
131 return modes[m].mode;
137 return got == -1 ? 0 : modes[got].mode;
141 GetIpAddr(const char *cp)
143 struct in_addr ipaddr;
145 if (!strcasecmp(cp, "default"))
146 ipaddr.s_addr = INADDR_ANY;
147 else if (inet_aton(cp, &ipaddr) == 0) {
150 /* Any illegal characters ? */
151 for (ptr = cp; *ptr != '\0'; ptr++)
152 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
158 hp = gethostbyname(cp);
159 if (hp && hp->h_addrtype == AF_INET)
160 memcpy(&ipaddr, hp->h_addr, hp->h_length);
162 ipaddr.s_addr = INADDR_NONE;
164 ipaddr.s_addr = INADDR_NONE;
170 static const struct speeds {
235 { 115200, B115200, },
238 { 230400, B230400, },
241 { 460800, B460800, },
244 { 921600, B921600, },
256 SpeedToUnsigned(speed_t speed)
258 const struct speeds *sp;
260 for (sp = speeds; sp->nspeed; sp++) {
261 if (sp->speed == speed) {
269 UnsignedToSpeed(unsigned nspeed)
271 const struct speeds *sp;
273 for (sp = speeds; sp->nspeed; sp++) {
274 if (sp->nspeed == nspeed) {
282 findblank(char *p, int flags)
289 if (flags & PARSE_REDUCE) {
290 memmove(p, p + 1, strlen(p));
295 } else if (*p == '"') {
296 memmove(p, p + 1, strlen(p));
297 instring = !instring;
299 } else if (!instring && (issep(*p) ||
300 (*p == '#' && !(flags & PARSE_NOHASH))))
305 return instring ? NULL : p;
309 MakeArgs(char *script, char **pvect, int maxargs, int flags)
315 script += strspn(script, " \t");
316 if (*script == '#' && !(flags & PARSE_NOHASH)) {
321 if (nargs >= maxargs - 1)
325 script = findblank(script, flags);
328 else if (!(flags & PARSE_NOHASH) && *script == '#')
339 NumStr(long val, char *buf, size_t sz)
341 static char result[23]; /* handles 64 bit numbers */
343 if (buf == NULL || sz == 0) {
347 snprintf(buf, sz, "<%ld>", val);
352 HexStr(long val, char *buf, size_t sz)
354 static char result[21]; /* handles 64 bit numbers */
356 if (buf == NULL || sz == 0) {
360 snprintf(buf, sz, "<0x%lx>", val);
367 static char num[12]; /* Used immediately if returned */
368 static const char * const desc[] = {
369 "normal", "start", "sock", "modem", "dial", "dead", "done",
370 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
371 "redial", "reconnect"
374 if (ex >= 0 && ex < (int)NELEM(desc))
376 snprintf(num, sizeof num, "%d", ex);
381 SetTitle(const char *title)
385 else if (title[0] == '-' && title[1] != '\0')
386 setproctitle("-%s", title + 1);
388 setproctitle("%s", title);
394 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
400 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
404 Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
410 for (pos = i = 0; i < argc; i++) {
411 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
416 if ((pos += n) >= sz)
421 #if defined(__DragonFly__) && !defined(NOKLDLOAD)
423 loadmodules(int how, const char *module, ...)
428 va_start(ap, module);
429 while (module != NULL) {
430 if (modfind(module) == -1) {
431 if (ID0kldload(module) == -1) {
432 if (how == LOAD_VERBOSLY)
433 log_Printf(LogWARN, "%s: Cannot load module\n", module);
437 module = va_arg(ap, const char *);
444 loadmodules(int how __unused, const char *module __unused, ...)