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