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