Hi, this summer, Marcus MERIGHI reported that gethostent(3) was completely destroyed during the switch to asr, i guess unintentionally.
Until somebody writes something better, i suggest to resurrect the last working version from the Attic, file gethostnamadr.c,v 1.73. I'm not attempting to do anything clever here, just putting back the old code into the new location (the old location can no longer be used because asr is now squatting on the filename), with minimal changes to the glue to make it work again. Probably, this code could be improved (code cleanup, YP integration, ...) but i'm not doing that here: One thing at a time. The Copyright notice states the year when this particular code was actually added to the CSRG SCCS repository by Kevin Dunlap (kjd). Between 1986 and the import into OpenBSD, no significant changes happened to these functions. In particular, no part of this came from BIND and no part is covered by DEC Copyright. After OpenBSD import, there were significant changes (in particular with respect to IPv6), but as those authors did not add their Copyright notices, i'm not doing that in this patch, either. OK? Ingo Index: asr/sethostent.c =================================================================== RCS file: /cvs/src/lib/libc/asr/sethostent.c,v retrieving revision 1.1 diff -u -p -r1.1 sethostent.c --- asr/sethostent.c 8 Sep 2012 11:08:21 -0000 1.1 +++ asr/sethostent.c 18 Oct 2014 22:59:10 -0000 @@ -1,41 +1,140 @@ -/* $OpenBSD: sethostent.c,v 1.1 2012/09/08 11:08:21 eric Exp $ */ +/* $OpenBSD: gethostnamadr.c,v 1.73 2009/11/18 07:43:22 guenther Exp $ */ /* - * Copyright (c) 2012 Eric Faurot <[email protected]> + * Copyright (c) 1986 + * The Regents of the University of California. All rights reserved. * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ -#include <sys/types.h> -#include <netinet/in.h> - +#include <sys/socket.h> +#include <arpa/inet.h> +#include <arpa/nameser.h> #include <netdb.h> -#include <resolv.h> +#include <stdio.h> +#include <string.h> + +#define MAXALIASES 35 +#define MAXADDRS 35 + +static char *h_addr_ptrs[MAXADDRS + 1]; +static struct hostent host; +static char *host_aliases[MAXALIASES]; +static char hostbuf[BUFSIZ+1]; +static union { + struct in_addr _host_in_addr; + u_char _host_addr[16]; /* IPv4 or IPv6 */ +} _host_addr_u; +#define host_addr _host_addr_u._host_addr +static FILE *hostf = NULL; -/* XXX these functions do nothing for now */ +extern int h_errno; void -sethostent(int stayopen) +sethostent(int dummy) { + if (hostf != NULL) + rewind(hostf); } void endhostent(void) { + if (hostf != NULL) { + fclose(hostf); + hostf = NULL; + } } struct hostent * gethostent(void) { - h_errno = NETDB_INTERNAL; - return (NULL); + char *p, *cp, **q; + int af; + size_t len; + + host.h_addrtype = AF_UNSPEC; + host.h_length = 0; + + if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { + h_errno = NETDB_INTERNAL; + return (NULL); + } + again: + if ((p = fgetln(hostf, &len)) == NULL) { + h_errno = HOST_NOT_FOUND; + return (NULL); + } + if (p[len-1] == '\n') + len--; + if (len >= sizeof(hostbuf) || len == 0) + goto again; + p = memcpy(hostbuf, p, len); + hostbuf[len] = '\0'; + if (*p == '#') + goto again; + if ((cp = strchr(p, '#'))) + *cp = '\0'; + if (!(cp = strpbrk(p, " \t"))) + goto again; + *cp++ = '\0'; + if (inet_pton(AF_INET6, p, host_addr) > 0) { + af = AF_INET6; + len = IN6ADDRSZ; + } else if (inet_pton(AF_INET, p, host_addr) > 0) { + af = AF_INET; + len = INADDRSZ; + } else { + goto again; + } + /* if this is not something we're looking for, skip it. */ + if (host.h_addrtype != AF_UNSPEC && host.h_addrtype != af) + goto again; + if (host.h_length != 0 && host.h_length != len) + goto again; + h_addr_ptrs[0] = (char *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_length = len; + host.h_addrtype = af; + while (*cp == ' ' || *cp == '\t') + cp++; + host.h_name = cp; + q = host.h_aliases = host_aliases; + if ((cp = strpbrk(cp, " \t"))) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + if ((cp = strpbrk(cp, " \t"))) + *cp++ = '\0'; + } + *q = NULL; + h_errno = NETDB_SUCCESS; + return (&host); }
