defs.c revision 92142
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: head/usr.sbin/ppp/defs.c 92142 2002-03-12 16:05:26Z brian $
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
52void
53randinit()
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
68ssize_t
69fullread(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
86static 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
101const char *
102mode2Nam(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
113int
114Nam2mode(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
132struct in_addr
133GetIpAddr(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
162static 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 B460800
234  { 460800, B460800, },
235#endif
236#ifdef B921600
237  { 921600, B921600, },
238#endif
239#ifdef EXTA
240  { 19200, EXTA, },
241#endif
242#ifdef EXTB
243  { 38400, EXTB, },
244#endif
245#endif				/* _POSIX_SOURCE */
246  { 0, 0 }
247};
248
249int
250SpeedToInt(speed_t speed)
251{
252  const struct speeds *sp;
253
254  for (sp = speeds; sp->nspeed; sp++) {
255    if (sp->speed == speed) {
256      return sp->nspeed;
257    }
258  }
259  return 0;
260}
261
262speed_t
263IntToSpeed(int nspeed)
264{
265  const struct speeds *sp;
266
267  for (sp = speeds; sp->nspeed; sp++) {
268    if (sp->nspeed == nspeed) {
269      return sp->speed;
270    }
271  }
272  return B0;
273}
274
275char *
276findblank(char *p, int flags)
277{
278  int instring;
279
280  instring = 0;
281  while (*p) {
282    if (*p == '\\') {
283      if (flags & PARSE_REDUCE) {
284        memmove(p, p + 1, strlen(p));
285        if (!*p)
286          break;
287      } else
288        p++;
289    } else if (*p == '"') {
290      memmove(p, p + 1, strlen(p));
291      instring = !instring;
292      continue;
293    } else if (!instring && (issep(*p) ||
294                             (*p == '#' && !(flags & PARSE_NOHASH))))
295      return p;
296    p++;
297  }
298
299  return instring ? NULL : p;
300}
301
302int
303MakeArgs(char *script, char **pvect, int maxargs, int flags)
304{
305  int nargs;
306
307  nargs = 0;
308  while (*script) {
309    script += strspn(script, " \t");
310    if (*script == '#' && !(flags & PARSE_NOHASH)) {
311      *script = '\0';
312      break;
313    }
314    if (*script) {
315      if (nargs >= maxargs - 1)
316        break;
317      *pvect++ = script;
318      nargs++;
319      script = findblank(script, flags);
320      if (script == NULL)
321        return -1;
322      else if (!(flags & PARSE_NOHASH) && *script == '#')
323        *script = '\0';
324      else if (*script)
325        *script++ = '\0';
326    }
327  }
328  *pvect = NULL;
329  return nargs;
330}
331
332const char *
333NumStr(long val, char *buf, size_t sz)
334{
335  static char result[23];		/* handles 64 bit numbers */
336
337  if (buf == NULL || sz == 0) {
338    buf = result;
339    sz = sizeof result;
340  }
341  snprintf(buf, sz, "<%ld>", val);
342  return buf;
343}
344
345const char *
346HexStr(long val, char *buf, size_t sz)
347{
348  static char result[21];		/* handles 64 bit numbers */
349
350  if (buf == NULL || sz == 0) {
351    buf = result;
352    sz = sizeof result;
353  }
354  snprintf(buf, sz, "<0x%lx>", val);
355  return buf;
356}
357
358const char *
359ex_desc(int ex)
360{
361  static char num[12];		/* Used immediately if returned */
362  static const char * const desc[] = {
363    "normal", "start", "sock", "modem", "dial", "dead", "done",
364    "reboot", "errdead", "hangup", "term", "nodial", "nologin",
365    "redial", "reconnect"
366  };
367
368  if (ex >= 0 && ex < sizeof desc / sizeof *desc)
369    return desc[ex];
370  snprintf(num, sizeof num, "%d", ex);
371  return num;
372}
373
374void
375SetTitle(const char *title)
376{
377  if (title == NULL)
378    setproctitle(NULL);
379  else if (title[0] == '-' && title[1] != '\0')
380    setproctitle("-%s", title + 1);
381  else
382    setproctitle("%s", title);
383}
384
385fd_set *
386mkfdset()
387{
388  return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
389}
390
391void
392zerofdset(fd_set *s)
393{
394  memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
395}
396
397void
398Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
399{
400  int i, n, pos;
401
402  *buf = '\0';
403  for (pos = i = 0; i < argc; i++) {
404    n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
405    if (n < 0) {
406      buf[pos] = '\0';
407      break;
408    }
409    if ((pos += n) >= sz)
410      break;
411  }
412}
413