HaikuDepot: notify work status from main window
[haiku.git] / src / kits / network / gethostbyname.c
blob30c96839d8febb533dcea42f8a9c3cd5967d77e9
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1998-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <errno.h>
20 #include <netdb.h>
21 #include <stdint.h>
22 #include <string.h>
25 #define ALIGN(p) (((uintptr_t)(p) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
28 struct hostent *gethostbyname_r(const char *, struct hostent *,
29 char *, int, int *);
31 struct hostent *
32 gethostbyaddr_r(const char *addr, int len, int type,
33 struct hostent *hptr, char *buf, int buflen, int *h_errnop);
36 static struct hostent *
37 copy_hostent(struct hostent *he, struct hostent *hptr, char *buf, int buflen) {
38 char *cp;
39 char **ptr;
40 int i, n;
41 int nptr, len;
43 /* Find out the amount of space required to store the answer. */
44 nptr = 2; /*%< NULL ptrs */
45 len = (char *)ALIGN(buf) - buf;
46 for (i = 0; he->h_addr_list[i]; i++, nptr++) {
47 len += he->h_length;
49 for (i = 0; he->h_aliases[i]; i++, nptr++) {
50 len += strlen(he->h_aliases[i]) + 1;
52 len += strlen(he->h_name) + 1;
53 len += nptr * sizeof(char*);
55 if (len > buflen) {
56 errno = ERANGE;
57 return 0;
60 /* copy address size and type */
61 hptr->h_addrtype = he->h_addrtype;
62 n = hptr->h_length = he->h_length;
64 ptr = (char **)ALIGN(buf);
65 cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
67 /* copy address list */
68 hptr->h_addr_list = ptr;
69 for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
70 memcpy(cp, he->h_addr_list[i], n);
71 hptr->h_addr_list[i] = cp;
72 cp += n;
74 hptr->h_addr_list[i] = NULL;
75 ptr++;
77 /* copy official name */
78 n = strlen(he->h_name) + 1;
79 strcpy(cp, he->h_name);
80 hptr->h_name = cp;
81 cp += n;
83 /* copy aliases */
84 hptr->h_aliases = ptr;
85 for (i = 0 ; he->h_aliases[i]; i++) {
86 n = strlen(he->h_aliases[i]) + 1;
87 strcpy(cp, he->h_aliases[i]);
88 hptr->h_aliases[i] = cp;
89 cp += n;
91 hptr->h_aliases[i] = NULL;
93 return hptr;
97 struct hostent *
98 gethostbyname_r(const char *name, struct hostent *hptr,
99 char *buf, int buflen, int *h_errnop)
101 struct hostent *he = gethostbyname(name);
103 *h_errnop = h_errno;
105 if (he == NULL)
106 return NULL;
108 return copy_hostent(he, hptr, buf, buflen);
112 struct hostent *
113 gethostbyaddr_r(const char *addr, int len, int type,
114 struct hostent *hptr, char *buf, int buflen, int *h_errnop)
116 struct hostent *he = gethostbyaddr(addr, len, type);
118 *h_errnop = h_errno;
120 if (he == NULL)
121 return NULL;
123 return copy_hostent(he, hptr, buf, buflen);