diff options
Diffstat (limited to 'src/lib/libc/net/getnetnamadr.c')
| -rw-r--r-- | src/lib/libc/net/getnetnamadr.c | 384 |
1 files changed, 384 insertions, 0 deletions
diff --git a/src/lib/libc/net/getnetnamadr.c b/src/lib/libc/net/getnetnamadr.c new file mode 100644 index 0000000000..7b770f1ce7 --- /dev/null +++ b/src/lib/libc/net/getnetnamadr.c | |||
| @@ -0,0 +1,384 @@ | |||
| 1 | /* $OpenBSD: getnetnamadr.c,v 1.26 2005/08/06 20:30:03 espie Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1997, Jason Downs. All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions | ||
| 8 | * are met: | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * | ||
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS | ||
| 16 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, | ||
| 19 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 25 | * SUCH DAMAGE. | ||
| 26 | */ | ||
| 27 | /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro | ||
| 28 | * Dep. Matematica Universidade de Coimbra, Portugal, Europe | ||
| 29 | * | ||
| 30 | * Permission to use, copy, modify, and distribute this software for any | ||
| 31 | * purpose with or without fee is hereby granted, provided that the above | ||
| 32 | * copyright notice and this permission notice appear in all copies. | ||
| 33 | */ | ||
| 34 | /* | ||
| 35 | * Copyright (c) 1983, 1993 | ||
| 36 | * The Regents of the University of California. All rights reserved. | ||
| 37 | * | ||
| 38 | * Redistribution and use in source and binary forms, with or without | ||
| 39 | * modification, are permitted provided that the following conditions | ||
| 40 | * are met: | ||
| 41 | * 1. Redistributions of source code must retain the above copyright | ||
| 42 | * notice, this list of conditions and the following disclaimer. | ||
| 43 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 44 | * notice, this list of conditions and the following disclaimer in the | ||
| 45 | * documentation and/or other materials provided with the distribution. | ||
| 46 | * 3. Neither the name of the University nor the names of its contributors | ||
| 47 | * may be used to endorse or promote products derived from this software | ||
| 48 | * without specific prior written permission. | ||
| 49 | * | ||
| 50 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 51 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 52 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 53 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 54 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 55 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 56 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 57 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 58 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 59 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 60 | * SUCH DAMAGE. | ||
| 61 | */ | ||
| 62 | |||
| 63 | #include <sys/types.h> | ||
| 64 | #include <sys/param.h> | ||
| 65 | #include <sys/socket.h> | ||
| 66 | #include <netinet/in.h> | ||
| 67 | #include <arpa/inet.h> | ||
| 68 | #include <arpa/nameser.h> | ||
| 69 | |||
| 70 | #include <stdio.h> | ||
| 71 | #include <netdb.h> | ||
| 72 | #include <resolv.h> | ||
| 73 | #include <ctype.h> | ||
| 74 | #include <errno.h> | ||
| 75 | #include <string.h> | ||
| 76 | #include <stdlib.h> | ||
| 77 | |||
| 78 | #include "thread_private.h" | ||
| 79 | |||
| 80 | extern int h_errno; | ||
| 81 | |||
| 82 | struct netent *_getnetbyaddr(in_addr_t net, int type); | ||
| 83 | struct netent *_getnetbyname(const char *name); | ||
| 84 | |||
| 85 | int _hokchar(const char *); | ||
| 86 | |||
| 87 | #define BYADDR 0 | ||
| 88 | #define BYNAME 1 | ||
| 89 | #define MAXALIASES 35 | ||
| 90 | |||
| 91 | #define MAXPACKET (64*1024) | ||
| 92 | |||
| 93 | typedef union { | ||
| 94 | HEADER hdr; | ||
| 95 | u_char buf[MAXPACKET]; | ||
| 96 | } querybuf; | ||
| 97 | |||
| 98 | typedef union { | ||
| 99 | long al; | ||
| 100 | char ac; | ||
| 101 | } align; | ||
| 102 | |||
| 103 | static struct netent * | ||
| 104 | getnetanswer(querybuf *answer, int anslen, int net_i) | ||
| 105 | { | ||
| 106 | |||
| 107 | HEADER *hp; | ||
| 108 | u_char *cp; | ||
| 109 | int n; | ||
| 110 | u_char *eom; | ||
| 111 | int type, class, ancount, qdcount, haveanswer, i, nchar; | ||
| 112 | char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; | ||
| 113 | char *in, *st, *pauxt, *bp, **ap, *ep; | ||
| 114 | char *paux1 = &aux1[0], *paux2 = &aux2[0]; | ||
| 115 | static struct netent net_entry; | ||
| 116 | static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1]; | ||
| 117 | |||
| 118 | /* | ||
| 119 | * find first satisfactory answer | ||
| 120 | * | ||
| 121 | * answer --> +------------+ ( MESSAGE ) | ||
| 122 | * | Header | | ||
| 123 | * +------------+ | ||
| 124 | * | Question | the question for the name server | ||
| 125 | * +------------+ | ||
| 126 | * | Answer | RRs answering the question | ||
| 127 | * +------------+ | ||
| 128 | * | Authority | RRs pointing toward an authority | ||
| 129 | * | Additional | RRs holding additional information | ||
| 130 | * +------------+ | ||
| 131 | */ | ||
| 132 | eom = answer->buf + anslen; | ||
| 133 | hp = &answer->hdr; | ||
| 134 | ancount = ntohs(hp->ancount); /* #/records in the answer section */ | ||
| 135 | qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ | ||
| 136 | bp = netbuf; | ||
| 137 | ep = netbuf + sizeof(netbuf); | ||
| 138 | cp = answer->buf + HFIXEDSZ; | ||
| 139 | if (!qdcount) { | ||
| 140 | if (hp->aa) | ||
| 141 | h_errno = HOST_NOT_FOUND; | ||
| 142 | else | ||
| 143 | h_errno = TRY_AGAIN; | ||
| 144 | return (NULL); | ||
| 145 | } | ||
| 146 | while (qdcount-- > 0) { | ||
| 147 | n = __dn_skipname(cp, eom); | ||
| 148 | if (n < 0 || (cp + n + QFIXEDSZ) > eom) { | ||
| 149 | h_errno = NO_RECOVERY; | ||
| 150 | return(NULL); | ||
| 151 | } | ||
| 152 | cp += n + QFIXEDSZ; | ||
| 153 | } | ||
| 154 | ap = net_aliases; | ||
| 155 | *ap = NULL; | ||
| 156 | net_entry.n_aliases = net_aliases; | ||
| 157 | haveanswer = 0; | ||
| 158 | while (--ancount >= 0 && cp < eom) { | ||
| 159 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
| 160 | #ifdef USE_RESOLV_NAME_OK | ||
| 161 | if ((n < 0) || !res_dnok(bp)) | ||
| 162 | #else | ||
| 163 | if ((n < 0) || !_hokchar(bp)) | ||
| 164 | #endif | ||
| 165 | break; | ||
| 166 | cp += n; | ||
| 167 | ans[0] = '\0'; | ||
| 168 | strlcpy(&ans[0], bp, sizeof ans); | ||
| 169 | GETSHORT(type, cp); | ||
| 170 | GETSHORT(class, cp); | ||
| 171 | cp += INT32SZ; /* TTL */ | ||
| 172 | GETSHORT(n, cp); | ||
| 173 | if (class == C_IN && type == T_PTR) { | ||
| 174 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
| 175 | #ifdef USE_RESOLV_NAME_OK | ||
| 176 | if ((n < 0) || !res_hnok(bp)) | ||
| 177 | #else | ||
| 178 | if ((n < 0) || !_hokchar(bp)) | ||
| 179 | #endif | ||
| 180 | { | ||
| 181 | cp += n; | ||
| 182 | return (NULL); | ||
| 183 | } | ||
| 184 | cp += n; | ||
| 185 | if ((ap + 2) < &net_aliases[MAXALIASES]) { | ||
| 186 | *ap++ = bp; | ||
| 187 | bp += strlen(bp) + 1; | ||
| 188 | net_entry.n_addrtype = | ||
| 189 | (class == C_IN) ? AF_INET : AF_UNSPEC; | ||
| 190 | haveanswer++; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | if (haveanswer) { | ||
| 195 | *ap = NULL; | ||
| 196 | switch (net_i) { | ||
| 197 | case BYADDR: | ||
| 198 | net_entry.n_name = *net_entry.n_aliases; | ||
| 199 | net_entry.n_net = 0L; | ||
| 200 | break; | ||
| 201 | case BYNAME: | ||
| 202 | ap = net_entry.n_aliases; | ||
| 203 | next_alias: | ||
| 204 | in = *ap++; | ||
| 205 | if (in == NULL) { | ||
| 206 | h_errno = HOST_NOT_FOUND; | ||
| 207 | return (NULL); | ||
| 208 | } | ||
| 209 | net_entry.n_name = ans; | ||
| 210 | aux2[0] = '\0'; | ||
| 211 | for (i = 0; i < 4; i++) { | ||
| 212 | for (st = in, nchar = 0; | ||
| 213 | isdigit((unsigned char)*st); | ||
| 214 | st++, nchar++) | ||
| 215 | ; | ||
| 216 | if (*st != '.' || nchar == 0 || nchar > 3) | ||
| 217 | goto next_alias; | ||
| 218 | if (i != 0) | ||
| 219 | nchar++; | ||
| 220 | strlcpy(paux1, in, nchar+1); | ||
| 221 | strlcat(paux1, paux2, MAXHOSTNAMELEN); | ||
| 222 | pauxt = paux2; | ||
| 223 | paux2 = paux1; | ||
| 224 | paux1 = pauxt; | ||
| 225 | in = ++st; | ||
| 226 | } | ||
| 227 | if (strcasecmp(in, "IN-ADDR.ARPA") != 0) | ||
| 228 | goto next_alias; | ||
| 229 | net_entry.n_net = inet_network(paux2); | ||
| 230 | break; | ||
| 231 | } | ||
| 232 | net_entry.n_aliases++; | ||
| 233 | return (&net_entry); | ||
| 234 | } | ||
| 235 | h_errno = TRY_AGAIN; | ||
| 236 | return (NULL); | ||
| 237 | } | ||
| 238 | |||
| 239 | struct netent * | ||
| 240 | getnetbyaddr(in_addr_t net, int net_type) | ||
| 241 | { | ||
| 242 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
| 243 | unsigned int netbr[4]; | ||
| 244 | int nn, anslen; | ||
| 245 | querybuf *buf; | ||
| 246 | char qbuf[MAXDNAME]; | ||
| 247 | in_addr_t net2; | ||
| 248 | struct netent *net_entry = NULL; | ||
| 249 | char lookups[MAXDNSLUS]; | ||
| 250 | int i; | ||
| 251 | |||
| 252 | if (_res_init(0) == -1) | ||
| 253 | return(_getnetbyaddr(net, net_type)); | ||
| 254 | |||
| 255 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
| 256 | if (lookups[0] == '\0') | ||
| 257 | strlcpy(lookups, "bf", sizeof lookups); | ||
| 258 | |||
| 259 | for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { | ||
| 260 | switch (lookups[i]) { | ||
| 261 | #ifdef YP | ||
| 262 | case 'y': | ||
| 263 | /* There is no YP support. */ | ||
| 264 | break; | ||
| 265 | #endif /* YP */ | ||
| 266 | case 'b': | ||
| 267 | if (net_type != AF_INET) | ||
| 268 | break; /* DNS only supports AF_INET? */ | ||
| 269 | |||
| 270 | for (nn = 4, net2 = net; net2; net2 >>= 8) | ||
| 271 | netbr[--nn] = net2 & 0xff; | ||
| 272 | switch (nn) { | ||
| 273 | case 3: /* Class A */ | ||
| 274 | snprintf(qbuf, sizeof(qbuf), | ||
| 275 | "0.0.0.%u.in-addr.arpa", netbr[3]); | ||
| 276 | break; | ||
| 277 | case 2: /* Class B */ | ||
| 278 | snprintf(qbuf, sizeof(qbuf), | ||
| 279 | "0.0.%u.%u.in-addr.arpa", | ||
| 280 | netbr[3], netbr[2]); | ||
| 281 | break; | ||
| 282 | case 1: /* Class C */ | ||
| 283 | snprintf(qbuf, sizeof(qbuf), | ||
| 284 | "0.%u.%u.%u.in-addr.arpa", | ||
| 285 | netbr[3], netbr[2], netbr[1]); | ||
| 286 | break; | ||
| 287 | case 0: /* Class D - E */ | ||
| 288 | snprintf(qbuf, sizeof(qbuf), | ||
| 289 | "%u.%u.%u.%u.in-addr.arpa", | ||
| 290 | netbr[3], netbr[2], netbr[1], netbr[0]); | ||
| 291 | break; | ||
| 292 | } | ||
| 293 | buf = malloc(sizeof(*buf)); | ||
| 294 | if (buf == NULL) | ||
| 295 | break; | ||
| 296 | anslen = res_query(qbuf, C_IN, T_PTR, buf->buf, | ||
| 297 | sizeof(buf->buf)); | ||
| 298 | if (anslen < 0) { | ||
| 299 | free(buf); | ||
| 300 | #ifdef DEBUG | ||
| 301 | if (_resp->options & RES_DEBUG) | ||
| 302 | printf("res_query failed\n"); | ||
| 303 | #endif | ||
| 304 | break; | ||
| 305 | } | ||
| 306 | net_entry = getnetanswer(buf, anslen, BYADDR); | ||
| 307 | free(buf); | ||
| 308 | if (net_entry != NULL) { | ||
| 309 | unsigned u_net = net; /* maybe net should be unsigned ? */ | ||
| 310 | |||
| 311 | /* Strip trailing zeros */ | ||
| 312 | while ((u_net & 0xff) == 0 && u_net != 0) | ||
| 313 | u_net >>= 8; | ||
| 314 | net_entry->n_net = u_net; | ||
| 315 | return (net_entry); | ||
| 316 | } | ||
| 317 | break; | ||
| 318 | case 'f': | ||
| 319 | net_entry = _getnetbyaddr(net, net_type); | ||
| 320 | if (net_entry != NULL) | ||
| 321 | return (net_entry); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | /* Nothing matched. */ | ||
| 326 | return (NULL); | ||
| 327 | } | ||
| 328 | |||
| 329 | struct netent * | ||
| 330 | getnetbyname(const char *net) | ||
| 331 | { | ||
| 332 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
| 333 | int anslen; | ||
| 334 | querybuf *buf; | ||
| 335 | char qbuf[MAXDNAME]; | ||
| 336 | struct netent *net_entry = NULL; | ||
| 337 | char lookups[MAXDNSLUS]; | ||
| 338 | int i; | ||
| 339 | |||
| 340 | if (_res_init(0) == -1) | ||
| 341 | return (_getnetbyname(net)); | ||
| 342 | |||
| 343 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
| 344 | if (lookups[0] == '\0') | ||
| 345 | strlcpy(lookups, "bf", sizeof lookups); | ||
| 346 | |||
| 347 | for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { | ||
| 348 | switch (lookups[i]) { | ||
| 349 | #ifdef YP | ||
| 350 | case 'y': | ||
| 351 | /* There is no YP support. */ | ||
| 352 | break; | ||
| 353 | #endif /* YP */ | ||
| 354 | case 'b': | ||
| 355 | strlcpy(qbuf, net, sizeof qbuf); | ||
| 356 | buf = malloc(sizeof(*buf)); | ||
| 357 | if (buf == NULL) | ||
| 358 | break; | ||
| 359 | anslen = res_search(qbuf, C_IN, T_PTR, buf->buf, | ||
| 360 | sizeof(buf->buf)); | ||
| 361 | if (anslen < 0) { | ||
| 362 | free(buf); | ||
| 363 | #ifdef DEBUG | ||
| 364 | if (_resp->options & RES_DEBUG) | ||
| 365 | printf("res_query failed\n"); | ||
| 366 | #endif | ||
| 367 | break; | ||
| 368 | } | ||
| 369 | net_entry = getnetanswer(buf, anslen, BYNAME); | ||
| 370 | free(buf); | ||
| 371 | if (net_entry != NULL) | ||
| 372 | return (net_entry); | ||
| 373 | break; | ||
| 374 | case 'f': | ||
| 375 | net_entry = _getnetbyname(net); | ||
| 376 | if (net_entry != NULL) | ||
| 377 | return (net_entry); | ||
| 378 | break; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | /* Nothing matched. */ | ||
| 383 | return (NULL); | ||
| 384 | } | ||
