Add the ips driver to GENERIC.
[dragonfly.git] / usr.sbin / ppp / defs.c
... / ...
CommitLineData
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: 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.3 2004/02/03 07:11:47 dillon Exp $
28 */
29
30
31#include <sys/param.h>
32#include <netdb.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <sys/socket.h>
36
37#include <ctype.h>
38#include <errno.h>
39#include <stdarg.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#if defined(__DragonFly__) && !defined(NOKLDLOAD)
44#include <sys/module.h>
45#endif
46#include <termios.h>
47#if !defined(__DragonFly__)
48#include <time.h>
49#endif
50#include <unistd.h>
51
52#if defined(__DragonFly__) && !defined(NOKLDLOAD)
53#include "id.h"
54#include "log.h"
55#endif
56#include "defs.h"
57
58#define issep(c) ((c) == '\t' || (c) == ' ')
59
60#if defined(__NetBSD__)
61void
62randinit()
63{
64#if defined(__DragonFly__)
65 static int initdone; /* srandomdev() call is only required once */
66
67 if (!initdone) {
68 initdone = 1;
69 srandomdev();
70 }
71#else
72 srandom((time(NULL)^getpid())+random());
73#endif
74}
75#endif
76
77ssize_t
78fullread(int fd, void *v, size_t n)
79{
80 size_t got, total;
81
82 for (total = 0; total < n; total += got)
83 switch ((got = read(fd, (char *)v + total, n - total))) {
84 case 0:
85 return total;
86 case -1:
87 if (errno == EINTR)
88 got = 0;
89 else
90 return -1;
91 }
92 return total;
93}
94
95static struct {
96 int mode;
97 const char *name;
98} modes[] = {
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" },
106 { PHYS_ALL, "*" },
107 { 0, 0 }
108};
109
110const char *
111mode2Nam(int mode)
112{
113 int m;
114
115 for (m = 0; modes[m].mode; m++)
116 if (modes[m].mode == mode)
117 return modes[m].name;
118
119 return "unknown";
120}
121
122int
123Nam2mode(const char *name)
124{
125 int m, got, len;
126
127 len = strlen(name);
128 got = -1;
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;
133 if (got != -1)
134 return 0;
135 got = m;
136 }
137
138 return got == -1 ? 0 : modes[got].mode;
139}
140
141struct in_addr
142GetIpAddr(const char *cp)
143{
144 struct in_addr ipaddr;
145
146 if (!strcasecmp(cp, "default"))
147 ipaddr.s_addr = INADDR_ANY;
148 else if (inet_aton(cp, &ipaddr) == 0) {
149 const char *ptr;
150
151 /* Any illegal characters ? */
152 for (ptr = cp; *ptr != '\0'; ptr++)
153 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
154 break;
155
156 if (*ptr == '\0') {
157 struct hostent *hp;
158
159 hp = gethostbyname(cp);
160 if (hp && hp->h_addrtype == AF_INET)
161 memcpy(&ipaddr, hp->h_addr, hp->h_length);
162 else
163 ipaddr.s_addr = INADDR_NONE;
164 } else
165 ipaddr.s_addr = INADDR_NONE;
166 }
167
168 return ipaddr;
169}
170
171static const struct speeds {
172 int nspeed;
173 speed_t speed;
174} speeds[] = {
175#ifdef B50
176 { 50, B50, },
177#endif
178#ifdef B75
179 { 75, B75, },
180#endif
181#ifdef B110
182 { 110, B110, },
183#endif
184#ifdef B134
185 { 134, B134, },
186#endif
187#ifdef B150
188 { 150, B150, },
189#endif
190#ifdef B200
191 { 200, B200, },
192#endif
193#ifdef B300
194 { 300, B300, },
195#endif
196#ifdef B600
197 { 600, B600, },
198#endif
199#ifdef B1200
200 { 1200, B1200, },
201#endif
202#ifdef B1800
203 { 1800, B1800, },
204#endif
205#ifdef B2400
206 { 2400, B2400, },
207#endif
208#ifdef B4800
209 { 4800, B4800, },
210#endif
211#ifdef B9600
212 { 9600, B9600, },
213#endif
214#ifdef B19200
215 { 19200, B19200, },
216#endif
217#ifdef B38400
218 { 38400, B38400, },
219#endif
220#ifndef _POSIX_SOURCE
221#ifdef B7200
222 { 7200, B7200, },
223#endif
224#ifdef B14400
225 { 14400, B14400, },
226#endif
227#ifdef B28800
228 { 28800, B28800, },
229#endif
230#ifdef B57600
231 { 57600, B57600, },
232#endif
233#ifdef B76800
234 { 76800, B76800, },
235#endif
236#ifdef B115200
237 { 115200, B115200, },
238#endif
239#ifdef B230400
240 { 230400, B230400, },
241#endif
242#ifdef B460800
243 { 460800, B460800, },
244#endif
245#ifdef B921600
246 { 921600, B921600, },
247#endif
248#ifdef EXTA
249 { 19200, EXTA, },
250#endif
251#ifdef EXTB
252 { 38400, EXTB, },
253#endif
254#endif /* _POSIX_SOURCE */
255 { 0, 0 }
256};
257
258int
259SpeedToInt(speed_t speed)
260{
261 const struct speeds *sp;
262
263 for (sp = speeds; sp->nspeed; sp++) {
264 if (sp->speed == speed) {
265 return sp->nspeed;
266 }
267 }
268 return 0;
269}
270
271speed_t
272IntToSpeed(int nspeed)
273{
274 const struct speeds *sp;
275
276 for (sp = speeds; sp->nspeed; sp++) {
277 if (sp->nspeed == nspeed) {
278 return sp->speed;
279 }
280 }
281 return B0;
282}
283
284char *
285findblank(char *p, int flags)
286{
287 int instring;
288
289 instring = 0;
290 while (*p) {
291 if (*p == '\\') {
292 if (flags & PARSE_REDUCE) {
293 memmove(p, p + 1, strlen(p));
294 if (!*p)
295 break;
296 } else
297 p++;
298 } else if (*p == '"') {
299 memmove(p, p + 1, strlen(p));
300 instring = !instring;
301 continue;
302 } else if (!instring && (issep(*p) ||
303 (*p == '#' && !(flags & PARSE_NOHASH))))
304 return p;
305 p++;
306 }
307
308 return instring ? NULL : p;
309}
310
311int
312MakeArgs(char *script, char **pvect, int maxargs, int flags)
313{
314 int nargs;
315
316 nargs = 0;
317 while (*script) {
318 script += strspn(script, " \t");
319 if (*script == '#' && !(flags & PARSE_NOHASH)) {
320 *script = '\0';
321 break;
322 }
323 if (*script) {
324 if (nargs >= maxargs - 1)
325 break;
326 *pvect++ = script;
327 nargs++;
328 script = findblank(script, flags);
329 if (script == NULL)
330 return -1;
331 else if (!(flags & PARSE_NOHASH) && *script == '#')
332 *script = '\0';
333 else if (*script)
334 *script++ = '\0';
335 }
336 }
337 *pvect = NULL;
338 return nargs;
339}
340
341const char *
342NumStr(long val, char *buf, size_t sz)
343{
344 static char result[23]; /* handles 64 bit numbers */
345
346 if (buf == NULL || sz == 0) {
347 buf = result;
348 sz = sizeof result;
349 }
350 snprintf(buf, sz, "<%ld>", val);
351 return buf;
352}
353
354const char *
355HexStr(long val, char *buf, size_t sz)
356{
357 static char result[21]; /* handles 64 bit numbers */
358
359 if (buf == NULL || sz == 0) {
360 buf = result;
361 sz = sizeof result;
362 }
363 snprintf(buf, sz, "<0x%lx>", val);
364 return buf;
365}
366
367const char *
368ex_desc(int ex)
369{
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"
375 };
376
377 if (ex >= 0 && ex < sizeof desc / sizeof *desc)
378 return desc[ex];
379 snprintf(num, sizeof num, "%d", ex);
380 return num;
381}
382
383void
384SetTitle(const char *title)
385{
386 if (title == NULL)
387 setproctitle(NULL);
388 else if (title[0] == '-' && title[1] != '\0')
389 setproctitle("-%s", title + 1);
390 else
391 setproctitle("%s", title);
392}
393
394fd_set *
395mkfdset()
396{
397 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
398}
399
400void
401zerofdset(fd_set *s)
402{
403 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
404}
405
406void
407Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
408{
409 int i, n, pos;
410
411 *buf = '\0';
412 for (pos = i = 0; i < argc; i++) {
413 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
414 if (n < 0) {
415 buf[pos] = '\0';
416 break;
417 }
418 if ((pos += n) >= sz)
419 break;
420 }
421}
422
423int
424loadmodules(int how, const char *module, ...)
425{
426 int loaded = 0;
427#if defined(__DragonFly__) && !defined(NOKLDLOAD)
428 va_list ap;
429
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);
436 } else
437 loaded++;
438 }
439 module = va_arg(ap, const char *);
440 }
441 va_end(ap);
442#endif
443 return loaded;
444}