defs.c revision 44279
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 *	$Id: defs.c,v 1.17 1998/06/27 14:18:05 brian Exp $
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 <stdlib.h>
38#include <string.h>
39#include <sys/errno.h>
40#if !defined(__FreeBSD__) || __FreeBSD__ < 3
41#include <time.h>
42#endif
43#include <unistd.h>
44
45#include "defs.h"
46
47void
48randinit()
49{
50#if __FreeBSD__ >= 3
51  static int initdone;		/* srandomdev() call is only required once */
52
53  if (!initdone) {
54    initdone = 1;
55    srandomdev();
56  }
57#else
58  srandom((time(NULL)^getpid())+random());
59#endif
60}
61
62ssize_t
63fullread(int fd, void *v, size_t n)
64{
65  size_t got, total;
66
67  for (total = 0; total < n; total += got)
68    switch ((got = read(fd, (char *)v + total, n - total))) {
69      case 0:
70        return total;
71      case -1:
72        if (errno == EINTR)
73          got = 0;
74        else
75          return -1;
76    }
77  return total;
78}
79
80static struct {
81  int mode;
82  const char *name;
83} modes[] = {
84  { PHYS_INTERACTIVE, "interactive" },
85  { PHYS_AUTO, "auto" },
86  { PHYS_DIRECT, "direct" },
87  { PHYS_DEDICATED, "dedicated" },
88  { PHYS_DDIAL, "ddial" },
89  { PHYS_BACKGROUND, "background" },
90  { PHYS_ALL, "*" },
91  { 0, 0 }
92};
93
94const char *
95mode2Nam(int mode)
96{
97  int m;
98
99  for (m = 0; modes[m].mode; m++)
100    if (modes[m].mode == mode)
101      return modes[m].name;
102
103  return "unknown";
104}
105
106int
107Nam2mode(const char *name)
108{
109  int m, got, len;
110
111  len = strlen(name);
112  got = -1;
113  for (m = 0; modes[m].mode; m++)
114    if (!strncasecmp(name, modes[m].name, len)) {
115      if (modes[m].name[len] == '\0')
116	return modes[m].mode;
117      if (got != -1)
118        return 0;
119      got = m;
120    }
121
122  return got == -1 ? 0 : modes[got].mode;
123}
124
125struct in_addr
126GetIpAddr(const char *cp)
127{
128  struct in_addr ipaddr;
129
130  if (!strcasecmp(cp, "default"))
131    ipaddr.s_addr = INADDR_ANY;
132  else if (inet_aton(cp, &ipaddr) == 0) {
133    const char *ptr;
134
135    /* Any illegal characters ? */
136    for (ptr = cp; *ptr != '\0'; ptr++)
137      if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
138        break;
139
140    if (*ptr == '\0') {
141      struct hostent *hp;
142
143      hp = gethostbyname(cp);
144      if (hp && hp->h_addrtype == AF_INET)
145        memcpy(&ipaddr, hp->h_addr, hp->h_length);
146      else
147        ipaddr.s_addr = INADDR_NONE;
148    } else
149      ipaddr.s_addr = INADDR_NONE;
150  }
151
152  return ipaddr;
153}
154