From 197c65992068ef0f3135a48c695a7f4950202f0f Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 19 Dec 2017 21:20:51 +0100 Subject: [PATCH] libnetwork: re-introduce gethostbyname_r and gethostbyaddr_r These are needed by existing openjdk package. Let's keep them until we get the OpenJDK build working again and can remove the requirement. --- src/kits/network/Jamfile | 1 + src/kits/network/gethostbyname.c | 125 +++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/kits/network/gethostbyname.c diff --git a/src/kits/network/Jamfile b/src/kits/network/Jamfile index 3c73433821..7260cdf9a8 100644 --- a/src/kits/network/Jamfile +++ b/src/kits/network/Jamfile @@ -19,6 +19,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { SharedLibrary $(libnetwork) : init.cpp interfaces.cpp + gethostbyname.c getifaddrs.cpp socket.cpp r5_compatibility.cpp diff --git a/src/kits/network/gethostbyname.c b/src/kits/network/gethostbyname.c new file mode 100644 index 0000000000..30c96839d8 --- /dev/null +++ b/src/kits/network/gethostbyname.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1998-1999 by Internet Software Consortium. + * + * 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. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. + */ + + +#include +#include +#include +#include + + +#define ALIGN(p) (((uintptr_t)(p) + (sizeof(long) - 1)) & ~(sizeof(long) - 1)) + + +struct hostent *gethostbyname_r(const char *, struct hostent *, + char *, int, int *); + +struct hostent * +gethostbyaddr_r(const char *addr, int len, int type, + struct hostent *hptr, char *buf, int buflen, int *h_errnop); + + +static struct hostent * +copy_hostent(struct hostent *he, struct hostent *hptr, char *buf, int buflen) { + char *cp; + char **ptr; + int i, n; + int nptr, len; + + /* Find out the amount of space required to store the answer. */ + nptr = 2; /*%< NULL ptrs */ + len = (char *)ALIGN(buf) - buf; + for (i = 0; he->h_addr_list[i]; i++, nptr++) { + len += he->h_length; + } + for (i = 0; he->h_aliases[i]; i++, nptr++) { + len += strlen(he->h_aliases[i]) + 1; + } + len += strlen(he->h_name) + 1; + len += nptr * sizeof(char*); + + if (len > buflen) { + errno = ERANGE; + return 0; + } + + /* copy address size and type */ + hptr->h_addrtype = he->h_addrtype; + n = hptr->h_length = he->h_length; + + ptr = (char **)ALIGN(buf); + cp = (char *)ALIGN(buf) + nptr * sizeof(char *); + + /* copy address list */ + hptr->h_addr_list = ptr; + for (i = 0; he->h_addr_list[i]; i++ , ptr++) { + memcpy(cp, he->h_addr_list[i], n); + hptr->h_addr_list[i] = cp; + cp += n; + } + hptr->h_addr_list[i] = NULL; + ptr++; + + /* copy official name */ + n = strlen(he->h_name) + 1; + strcpy(cp, he->h_name); + hptr->h_name = cp; + cp += n; + + /* copy aliases */ + hptr->h_aliases = ptr; + for (i = 0 ; he->h_aliases[i]; i++) { + n = strlen(he->h_aliases[i]) + 1; + strcpy(cp, he->h_aliases[i]); + hptr->h_aliases[i] = cp; + cp += n; + } + hptr->h_aliases[i] = NULL; + + return hptr; +} + + +struct hostent * +gethostbyname_r(const char *name, struct hostent *hptr, + char *buf, int buflen, int *h_errnop) +{ + struct hostent *he = gethostbyname(name); + + *h_errnop = h_errno; + + if (he == NULL) + return NULL; + + return copy_hostent(he, hptr, buf, buflen); +} + + +struct hostent * +gethostbyaddr_r(const char *addr, int len, int type, + struct hostent *hptr, char *buf, int buflen, int *h_errnop) +{ + struct hostent *he = gethostbyaddr(addr, len, type); + + *h_errnop = h_errno; + + if (he == NULL) + return NULL; + + return copy_hostent(he, hptr, buf, buflen); +} +