util.c revision 1.2 1 1.2 dyoung /* $NetBSD: util.c,v 1.2 2008/05/06 16:09:18 dyoung Exp $ */
2 1.2 dyoung
3 1.2 dyoung /*-
4 1.2 dyoung * Copyright (c)2008 David Young. All rights reserved.
5 1.2 dyoung *
6 1.2 dyoung * Redistribution and use in source and binary forms, with or without
7 1.2 dyoung * modification, are permitted provided that the following conditions
8 1.2 dyoung * are met:
9 1.2 dyoung * 1. Redistributions of source code must retain the above copyright
10 1.2 dyoung * notice, this list of conditions and the following disclaimer.
11 1.2 dyoung * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 dyoung * notice, this list of conditions and the following disclaimer in the
13 1.2 dyoung * documentation and/or other materials provided with the distribution.
14 1.2 dyoung *
15 1.2 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.2 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.2 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.2 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.2 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.2 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.2 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.2 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.2 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.2 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.2 dyoung * SUCH DAMAGE.
26 1.2 dyoung */
27 1.2 dyoung
28 1.1 dyoung #include <ctype.h>
29 1.1 dyoung #include <err.h>
30 1.1 dyoung #include <errno.h>
31 1.1 dyoung #include <stddef.h>
32 1.1 dyoung #include <stdio.h>
33 1.1 dyoung #include <stdlib.h>
34 1.1 dyoung #include <string.h>
35 1.1 dyoung #include <unistd.h>
36 1.1 dyoung
37 1.1 dyoung #include <sys/socket.h>
38 1.1 dyoung #include <netinet/in.h> /* XXX */
39 1.1 dyoung
40 1.1 dyoung #include "util.h"
41 1.1 dyoung
42 1.1 dyoung int
43 1.1 dyoung getsock(int naf)
44 1.1 dyoung {
45 1.1 dyoung static int oaf = -1, s;
46 1.1 dyoung
47 1.1 dyoung if (oaf == naf || (oaf != -1 && naf == AF_UNSPEC))
48 1.1 dyoung return s;
49 1.1 dyoung
50 1.1 dyoung if (oaf != -1)
51 1.1 dyoung close(s);
52 1.1 dyoung
53 1.1 dyoung if (naf == AF_UNSPEC)
54 1.1 dyoung naf = AF_INET;
55 1.1 dyoung
56 1.1 dyoung s = socket(naf, SOCK_DGRAM, 0);
57 1.1 dyoung if (s == -1)
58 1.1 dyoung oaf = -1;
59 1.1 dyoung else
60 1.1 dyoung oaf = naf;
61 1.1 dyoung return s;
62 1.1 dyoung }
63 1.1 dyoung
64 1.1 dyoung const char *
65 1.1 dyoung get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
66 1.1 dyoung {
67 1.1 dyoung int len;
68 1.1 dyoung bool hexstr;
69 1.1 dyoung u_int8_t *p;
70 1.1 dyoung
71 1.1 dyoung len = *lenp;
72 1.1 dyoung p = buf;
73 1.1 dyoung hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
74 1.1 dyoung if (hexstr)
75 1.1 dyoung val += 2;
76 1.1 dyoung for (;;) {
77 1.1 dyoung if (*val == '\0')
78 1.1 dyoung break;
79 1.1 dyoung if (sep != NULL && strchr(sep, *val) != NULL) {
80 1.1 dyoung val++;
81 1.1 dyoung break;
82 1.1 dyoung }
83 1.1 dyoung if (hexstr) {
84 1.1 dyoung if (!isxdigit((u_char)val[0]) ||
85 1.1 dyoung !isxdigit((u_char)val[1])) {
86 1.1 dyoung warnx("bad hexadecimal digits");
87 1.1 dyoung return NULL;
88 1.1 dyoung }
89 1.1 dyoung }
90 1.1 dyoung if (p > buf + len) {
91 1.1 dyoung if (hexstr)
92 1.1 dyoung warnx("hexadecimal digits too long");
93 1.1 dyoung else
94 1.1 dyoung warnx("strings too long");
95 1.1 dyoung return NULL;
96 1.1 dyoung }
97 1.1 dyoung if (hexstr) {
98 1.1 dyoung #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
99 1.1 dyoung *p++ = (tohex((u_char)val[0]) << 4) |
100 1.1 dyoung tohex((u_char)val[1]);
101 1.1 dyoung #undef tohex
102 1.1 dyoung val += 2;
103 1.1 dyoung } else
104 1.1 dyoung *p++ = *val++;
105 1.1 dyoung }
106 1.1 dyoung len = p - buf;
107 1.1 dyoung if (len < *lenp)
108 1.1 dyoung memset(p, 0, *lenp - len);
109 1.1 dyoung *lenp = len;
110 1.1 dyoung return val;
111 1.1 dyoung }
112 1.1 dyoung
113 1.1 dyoung void
114 1.1 dyoung print_string(const u_int8_t *buf, int len)
115 1.1 dyoung {
116 1.1 dyoung int i;
117 1.1 dyoung bool hasspc;
118 1.1 dyoung
119 1.1 dyoung i = 0;
120 1.1 dyoung hasspc = false;
121 1.1 dyoung if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
122 1.1 dyoung for (; i < len; i++) {
123 1.1 dyoung if (!isprint(buf[i]))
124 1.1 dyoung break;
125 1.1 dyoung if (isspace(buf[i]))
126 1.1 dyoung hasspc = true;
127 1.1 dyoung }
128 1.1 dyoung }
129 1.1 dyoung if (i == len) {
130 1.1 dyoung if (hasspc || len == 0)
131 1.1 dyoung printf("\"%.*s\"", len, buf);
132 1.1 dyoung else
133 1.1 dyoung printf("%.*s", len, buf);
134 1.1 dyoung } else {
135 1.1 dyoung printf("0x");
136 1.1 dyoung for (i = 0; i < len; i++)
137 1.1 dyoung printf("%02x", buf[i]);
138 1.1 dyoung }
139 1.1 dyoung }
140 1.1 dyoung
141 1.1 dyoung struct paddr_prefix *
142 1.1 dyoung prefixlen_to_mask(int af, int plen)
143 1.1 dyoung {
144 1.1 dyoung union {
145 1.1 dyoung struct sockaddr sa;
146 1.1 dyoung struct sockaddr_in sin;
147 1.1 dyoung struct sockaddr_in6 sin6;
148 1.1 dyoung } u;
149 1.1 dyoung struct paddr_prefix *pfx;
150 1.1 dyoung size_t addrlen;
151 1.1 dyoung uint8_t *addr;
152 1.1 dyoung int nbit;
153 1.1 dyoung
154 1.1 dyoung memset(&u, 0, sizeof(u));
155 1.1 dyoung
156 1.1 dyoung switch (af) {
157 1.1 dyoung case AF_INET:
158 1.1 dyoung addrlen = sizeof(u.sin.sin_addr);
159 1.1 dyoung addr = (uint8_t *)&u.sin.sin_addr;
160 1.1 dyoung u.sa.sa_len = sizeof(u.sin);
161 1.1 dyoung break;
162 1.1 dyoung case AF_INET6:
163 1.1 dyoung addrlen = sizeof(u.sin6.sin6_addr);
164 1.1 dyoung addr = (uint8_t *)&u.sin6.sin6_addr;
165 1.1 dyoung u.sa.sa_len = sizeof(u.sin6);
166 1.1 dyoung break;
167 1.1 dyoung default:
168 1.1 dyoung errno = EINVAL;
169 1.1 dyoung return NULL;
170 1.1 dyoung }
171 1.1 dyoung u.sa.sa_family = af;
172 1.1 dyoung
173 1.1 dyoung if (plen < 0 || plen > addrlen * NBBY) {
174 1.1 dyoung errno = EINVAL;
175 1.1 dyoung return NULL;
176 1.1 dyoung }
177 1.1 dyoung
178 1.1 dyoung memset(addr, 0xff, (plen + NBBY - 1) / NBBY);
179 1.1 dyoung
180 1.1 dyoung nbit = plen % NBBY;
181 1.1 dyoung if (nbit != 0)
182 1.1 dyoung addr[plen / NBBY] &= ~((uint8_t)0xff >> nbit);
183 1.1 dyoung pfx = malloc(offsetof(struct paddr_prefix, pfx_addr) + u.sa.sa_len);
184 1.1 dyoung if (pfx == NULL)
185 1.1 dyoung return NULL;
186 1.1 dyoung pfx->pfx_len = plen;
187 1.1 dyoung memcpy(&pfx->pfx_addr, &u.sa, u.sa.sa_len);
188 1.1 dyoung
189 1.1 dyoung return pfx;
190 1.1 dyoung }
191