| 1 | n/a | /* |
|---|
| 2 | n/a | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
|---|
| 3 | n/a | * All rights reserved. |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | n/a | * modification, are permitted provided that the following conditions |
|---|
| 7 | n/a | * are met: |
|---|
| 8 | n/a | * 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | n/a | * notice, this list of conditions and the following disclaimer. |
|---|
| 10 | n/a | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 11 | n/a | * notice, this list of conditions and the following disclaimer in the |
|---|
| 12 | n/a | * documentation and/or other materials provided with the distribution. |
|---|
| 13 | n/a | * 3. Neither the name of the project nor the names of its contributors |
|---|
| 14 | n/a | * may be used to endorse or promote products derived from this software |
|---|
| 15 | n/a | * without specific prior written permission. |
|---|
| 16 | n/a | * |
|---|
| 17 | n/a | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 18 | n/a | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 19 | n/a | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 20 | n/a | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
|---|
| 21 | n/a | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 | n/a | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 23 | n/a | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 24 | n/a | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 25 | n/a | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 26 | n/a | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 27 | n/a | * SUCH DAMAGE. |
|---|
| 28 | n/a | */ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | /* |
|---|
| 31 | n/a | * Issues to be discussed: |
|---|
| 32 | n/a | * - Thread safe-ness must be checked |
|---|
| 33 | n/a | * - Return values. There seems to be no standard for return value (RFC2133) |
|---|
| 34 | n/a | * but INRIA implementation returns EAI_xxx defined for getaddrinfo(). |
|---|
| 35 | n/a | */ |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | #if 0 |
|---|
| 38 | n/a | #include <sys/types.h> |
|---|
| 39 | n/a | #include <sys/socket.h> |
|---|
| 40 | n/a | #include <netinet/in.h> |
|---|
| 41 | n/a | #include <arpa/inet.h> |
|---|
| 42 | n/a | #include <arpa/nameser.h> |
|---|
| 43 | n/a | #include <netdb.h> |
|---|
| 44 | n/a | #include <resolv.h> |
|---|
| 45 | n/a | #include <string.h> |
|---|
| 46 | n/a | #include <stddef.h> |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | #include "addrinfo.h" |
|---|
| 49 | n/a | #endif |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | #define SUCCESS 0 |
|---|
| 52 | n/a | #define YES 1 |
|---|
| 53 | n/a | #define NO 0 |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | static struct gni_afd { |
|---|
| 56 | n/a | int a_af; |
|---|
| 57 | n/a | int a_addrlen; |
|---|
| 58 | n/a | int a_socklen; |
|---|
| 59 | n/a | int a_off; |
|---|
| 60 | n/a | } gni_afdl [] = { |
|---|
| 61 | n/a | #ifdef ENABLE_IPV6 |
|---|
| 62 | n/a | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), |
|---|
| 63 | n/a | offsetof(struct sockaddr_in6, sin6_addr)}, |
|---|
| 64 | n/a | #endif |
|---|
| 65 | n/a | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), |
|---|
| 66 | n/a | offsetof(struct sockaddr_in, sin_addr)}, |
|---|
| 67 | n/a | {0, 0, 0}, |
|---|
| 68 | n/a | }; |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | struct gni_sockinet { |
|---|
| 71 | n/a | u_char si_len; |
|---|
| 72 | n/a | u_char si_family; |
|---|
| 73 | n/a | u_short si_port; |
|---|
| 74 | n/a | }; |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | #define ENI_NOSOCKET 0 |
|---|
| 77 | n/a | #define ENI_NOSERVNAME 1 |
|---|
| 78 | n/a | #define ENI_NOHOSTNAME 2 |
|---|
| 79 | n/a | #define ENI_MEMORY 3 |
|---|
| 80 | n/a | #define ENI_SYSTEM 4 |
|---|
| 81 | n/a | #define ENI_FAMILY 5 |
|---|
| 82 | n/a | #define ENI_SALEN 6 |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | /* forward declaration to make gcc happy */ |
|---|
| 85 | n/a | int getnameinfo(const struct sockaddr *, size_t, char *, size_t, |
|---|
| 86 | n/a | char *, size_t, int); |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | int |
|---|
| 89 | n/a | getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) |
|---|
| 90 | n/a | const struct sockaddr *sa; |
|---|
| 91 | n/a | size_t salen; |
|---|
| 92 | n/a | char *host; |
|---|
| 93 | n/a | size_t hostlen; |
|---|
| 94 | n/a | char *serv; |
|---|
| 95 | n/a | size_t servlen; |
|---|
| 96 | n/a | int flags; |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | struct gni_afd *gni_afd; |
|---|
| 99 | n/a | struct servent *sp; |
|---|
| 100 | n/a | struct hostent *hp; |
|---|
| 101 | n/a | u_short port; |
|---|
| 102 | n/a | int family, len, i; |
|---|
| 103 | n/a | char *addr, *p; |
|---|
| 104 | n/a | u_long v4a; |
|---|
| 105 | n/a | #ifdef ENABLE_IPV6 |
|---|
| 106 | n/a | u_char pfx; |
|---|
| 107 | n/a | #endif |
|---|
| 108 | n/a | int h_error; |
|---|
| 109 | n/a | char numserv[512]; |
|---|
| 110 | n/a | char numaddr[512]; |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | if (sa == NULL) |
|---|
| 113 | n/a | return ENI_NOSOCKET; |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
|---|
| 116 | n/a | len = sa->sa_len; |
|---|
| 117 | n/a | if (len != salen) return ENI_SALEN; |
|---|
| 118 | n/a | #else |
|---|
| 119 | n/a | len = salen; |
|---|
| 120 | n/a | #endif |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | family = sa->sa_family; |
|---|
| 123 | n/a | for (i = 0; gni_afdl[i].a_af; i++) |
|---|
| 124 | n/a | if (gni_afdl[i].a_af == family) { |
|---|
| 125 | n/a | gni_afd = &gni_afdl[i]; |
|---|
| 126 | n/a | goto found; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | return ENI_FAMILY; |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | found: |
|---|
| 131 | n/a | if (len != gni_afd->a_socklen) return ENI_SALEN; |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | port = ((struct gni_sockinet *)sa)->si_port; /* network byte order */ |
|---|
| 134 | n/a | addr = (char *)sa + gni_afd->a_off; |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | if (serv == NULL || servlen == 0) { |
|---|
| 137 | n/a | /* what we should do? */ |
|---|
| 138 | n/a | } else if (flags & NI_NUMERICSERV) { |
|---|
| 139 | n/a | sprintf(numserv, "%d", ntohs(port)); |
|---|
| 140 | n/a | if (strlen(numserv) > servlen) |
|---|
| 141 | n/a | return ENI_MEMORY; |
|---|
| 142 | n/a | strcpy(serv, numserv); |
|---|
| 143 | n/a | } else { |
|---|
| 144 | n/a | sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp"); |
|---|
| 145 | n/a | if (sp) { |
|---|
| 146 | n/a | if (strlen(sp->s_name) > servlen) |
|---|
| 147 | n/a | return ENI_MEMORY; |
|---|
| 148 | n/a | strcpy(serv, sp->s_name); |
|---|
| 149 | n/a | } else |
|---|
| 150 | n/a | return ENI_NOSERVNAME; |
|---|
| 151 | n/a | } |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | switch (sa->sa_family) { |
|---|
| 154 | n/a | case AF_INET: |
|---|
| 155 | n/a | v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr; |
|---|
| 156 | n/a | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) |
|---|
| 157 | n/a | flags |= NI_NUMERICHOST; |
|---|
| 158 | n/a | v4a >>= IN_CLASSA_NSHIFT; |
|---|
| 159 | n/a | if (v4a == 0 || v4a == IN_LOOPBACKNET) |
|---|
| 160 | n/a | flags |= NI_NUMERICHOST; |
|---|
| 161 | n/a | break; |
|---|
| 162 | n/a | #ifdef ENABLE_IPV6 |
|---|
| 163 | n/a | case AF_INET6: |
|---|
| 164 | n/a | pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0]; |
|---|
| 165 | n/a | if (pfx == 0 || pfx == 0xfe || pfx == 0xff) |
|---|
| 166 | n/a | flags |= NI_NUMERICHOST; |
|---|
| 167 | n/a | break; |
|---|
| 168 | n/a | #endif |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | if (host == NULL || hostlen == 0) { |
|---|
| 171 | n/a | /* what should we do? */ |
|---|
| 172 | n/a | } else if (flags & NI_NUMERICHOST) { |
|---|
| 173 | n/a | if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr)) |
|---|
| 174 | n/a | == NULL) |
|---|
| 175 | n/a | return ENI_SYSTEM; |
|---|
| 176 | n/a | if (strlen(numaddr) > hostlen) |
|---|
| 177 | n/a | return ENI_MEMORY; |
|---|
| 178 | n/a | strcpy(host, numaddr); |
|---|
| 179 | n/a | } else { |
|---|
| 180 | n/a | #ifdef ENABLE_IPV6 |
|---|
| 181 | n/a | hp = getipnodebyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af, &h_error); |
|---|
| 182 | n/a | #else |
|---|
| 183 | n/a | hp = gethostbyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af); |
|---|
| 184 | n/a | h_error = h_errno; |
|---|
| 185 | n/a | #endif |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | if (hp) { |
|---|
| 188 | n/a | if (flags & NI_NOFQDN) { |
|---|
| 189 | n/a | p = strchr(hp->h_name, '.'); |
|---|
| 190 | n/a | if (p) *p = '\0'; |
|---|
| 191 | n/a | } |
|---|
| 192 | n/a | if (strlen(hp->h_name) > hostlen) { |
|---|
| 193 | n/a | #ifdef ENABLE_IPV6 |
|---|
| 194 | n/a | freehostent(hp); |
|---|
| 195 | n/a | #endif |
|---|
| 196 | n/a | return ENI_MEMORY; |
|---|
| 197 | n/a | } |
|---|
| 198 | n/a | strcpy(host, hp->h_name); |
|---|
| 199 | n/a | #ifdef ENABLE_IPV6 |
|---|
| 200 | n/a | freehostent(hp); |
|---|
| 201 | n/a | #endif |
|---|
| 202 | n/a | } else { |
|---|
| 203 | n/a | if (flags & NI_NAMEREQD) |
|---|
| 204 | n/a | return ENI_NOHOSTNAME; |
|---|
| 205 | n/a | if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr)) |
|---|
| 206 | n/a | == NULL) |
|---|
| 207 | n/a | return ENI_NOHOSTNAME; |
|---|
| 208 | n/a | if (strlen(numaddr) > hostlen) |
|---|
| 209 | n/a | return ENI_MEMORY; |
|---|
| 210 | n/a | strcpy(host, numaddr); |
|---|
| 211 | n/a | } |
|---|
| 212 | n/a | } |
|---|
| 213 | n/a | return SUCCESS; |
|---|
| 214 | n/a | } |
|---|