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 $
27 * $DragonFly: src/usr.sbin/ppp/defs.c,v 1.4 2005/11/24 23:42:54 swildner Exp $
31 #include <sys/param.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <sys/socket.h>
43 #if defined(__DragonFly__) && !defined(NOKLDLOAD)
44 #include <sys/module.h>
47 #if !defined(__DragonFly__)
52 #if defined(__DragonFly__) && !defined(NOKLDLOAD)
58 #define issep(c) ((c) == '\t' || (c) == ' ')
60 #if defined(__NetBSD__)
64 #if defined(__DragonFly__)
65 static int initdone; /* srandomdev() call is only required once */
72 srandom((time(NULL)^getpid())+random());
78 fullread(int fd, void *v, size_t n)
82 for (total = 0; total < n; total += got)
83 switch ((got = read(fd, (char *)v + total, n - total))) {
99 { PHYS_INTERACTIVE, "interactive" },
100 { PHYS_AUTO, "auto" },
101 { PHYS_DIRECT, "direct" },
102 { PHYS_DEDICATED, "dedicated" },
103 { PHYS_DDIAL, "ddial" },
104 { PHYS_BACKGROUND, "background" },
105 { PHYS_FOREGROUND, "foreground" },
115 for (m = 0; modes[m].mode; m++)
116 if (modes[m].mode == mode)
117 return modes[m].name;
123 Nam2mode(const char *name)
129 for (m = 0; modes[m].mode; m++)
130 if (!strncasecmp(name, modes[m].name, len)) {
131 if (modes[m].name[len] == '\0')
132 return modes[m].mode;
138 return got == -1 ? 0 : modes[got].mode;
142 GetIpAddr(const char *cp)
144 struct in_addr ipaddr;
146 if (!strcasecmp(cp, "default"))
147 ipaddr.s_addr = INADDR_ANY;
148 else if (inet_aton(cp, &ipaddr) == 0) {
151 /* Any illegal characters ? */
152 for (ptr = cp; *ptr != '\0'; ptr++)
153 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
159 hp = gethostbyname(cp);
160 if (hp && hp->h_addrtype == AF_INET)
161 memcpy(&ipaddr, hp->h_addr, hp->h_length);
163 ipaddr.s_addr = INADDR_NONE;
165 ipaddr.s_addr = INADDR_NONE;
171 static const struct speeds {
220 #ifndef _POSIX_SOURCE
237 { 115200, B115200, },
240 { 230400, B230400, },
243 { 460800, B460800, },
246 { 921600, B921600, },
254 #endif /* _POSIX_SOURCE */
259 SpeedToInt(speed_t speed)
261 const struct speeds *sp;
263 for (sp = speeds; sp->nspeed; sp++) {
264 if (sp->speed == speed) {
272 IntToSpeed(int nspeed)
274 const struct speeds *sp;
276 for (sp = speeds; sp->nspeed; sp++) {
277 if (sp->nspeed == nspeed) {
285 findblank(char *p, int flags)
292 if (flags & PARSE_REDUCE) {
293 memmove(p, p + 1, strlen(p));
298 } else if (*p == '"') {
299 memmove(p, p + 1, strlen(p));
300 instring = !instring;
302 } else if (!instring && (issep(*p) ||
303 (*p == '#' && !(flags & PARSE_NOHASH))))
308 return instring ? NULL : p;
312 MakeArgs(char *script, char **pvect, int maxargs, int flags)
318 script += strspn(script, " \t");
319 if (*script == '#' && !(flags & PARSE_NOHASH)) {
324 if (nargs >= maxargs - 1)
328 script = findblank(script, flags);
331 else if (!(flags & PARSE_NOHASH) && *script == '#')
342 NumStr(long val, char *buf, size_t sz)
344 static char result[23]; /* handles 64 bit numbers */
346 if (buf == NULL || sz == 0) {
350 snprintf(buf, sz, "<%ld>", val);
355 HexStr(long val, char *buf, size_t sz)
357 static char result[21]; /* handles 64 bit numbers */
359 if (buf == NULL || sz == 0) {
363 snprintf(buf, sz, "<0x%lx>", val);
370 static char num[12]; /* Used immediately if returned */
371 static const char * const desc[] = {
372 "normal", "start", "sock", "modem", "dial", "dead", "done",
373 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
374 "redial", "reconnect"
377 if (ex >= 0 && ex < sizeof desc / sizeof *desc)
379 snprintf(num, sizeof num, "%d", ex);
384 SetTitle(const char *title)
388 else if (title[0] == '-' && title[1] != '\0')
389 setproctitle("-%s", title + 1);
391 setproctitle("%s", title);
397 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
403 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
407 Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
412 for (pos = i = 0; i < argc; i++) {
413 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
418 if ((pos += n) >= sz)
424 loadmodules(int how, const char *module, ...)
427 #if defined(__DragonFly__) && !defined(NOKLDLOAD)
430 va_start(ap, module);
431 while (module != NULL) {
432 if (modfind(module) == -1) {
433 if (ID0kldload(module) == -1) {
434 if (how == LOAD_VERBOSLY)
435 log_Printf(LogWARN, "%s: Cannot load module\n", module);
439 module = va_arg(ap, const char *);