getnameinfo.c revision 1.4 1 /* $NetBSD: getnameinfo.c,v 1.4 1999/08/22 12:54:02 kleink 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 "namespace.h"
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <arpa/nameser.h>
45 #include <netdb.h>
46 #include <resolv.h>
47 #include <string.h>
48 #include <stddef.h>
49
50 #define SUCCESS 0
51 #define ANY 0
52 #define YES 1
53 #define NO 0
54
55 static struct afd {
56 int a_af;
57 int a_addrlen;
58 int a_socklen;
59 int a_off;
60 } afdl [] = {
61 #ifdef INET6
62 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
63 offsetof(struct sockaddr_in6, sin6_addr)},
64 #endif
65 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
66 offsetof(struct sockaddr_in, sin_addr)},
67 {0, 0, 0},
68 };
69
70 struct sockinet {
71 u_char si_len;
72 u_char si_family;
73 u_short si_port;
74 };
75
76 #define ENI_NOSOCKET 0
77 #define ENI_NOSERVNAME 1
78 #define ENI_NOHOSTNAME 2
79 #define ENI_MEMORY 3
80 #define ENI_SYSTEM 4
81 #define ENI_FAMILY 5
82 #define ENI_SALEN 6
83
84 int
85 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
86 const struct sockaddr *sa;
87 size_t salen;
88 char *host;
89 size_t hostlen;
90 char *serv;
91 size_t servlen;
92 int flags;
93 {
94 struct afd *afd;
95 struct servent *sp;
96 struct hostent *hp;
97 u_short port;
98 int family, len, i;
99 char *addr, *p;
100 u_long v4a;
101 int h_error;
102 char numserv[512];
103 char numaddr[512];
104
105 if (sa == NULL)
106 return ENI_NOSOCKET;
107
108 len = sa->sa_len;
109 if (len != salen) return ENI_SALEN;
110
111 family = sa->sa_family;
112 for (i = 0; afdl[i].a_af; i++)
113 if (afdl[i].a_af == family) {
114 afd = &afdl[i];
115 goto found;
116 }
117 return ENI_FAMILY;
118
119 found:
120 if (len != afd->a_socklen) return ENI_SALEN;
121
122 port = ((struct sockinet *)sa)->si_port; /* network byte order */
123 addr = (char *)sa + afd->a_off;
124
125 if (serv == NULL || servlen == 0) {
126 /* what we should do? */
127 } else if (flags & NI_NUMERICSERV) {
128 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
129 if (strlen(numserv) > servlen)
130 return ENI_MEMORY;
131 strcpy(serv, numserv);
132 } else {
133 sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
134 if (sp) {
135 if (strlen(sp->s_name) > servlen)
136 return ENI_MEMORY;
137 strcpy(serv, sp->s_name);
138 } else
139 return ENI_NOSERVNAME;
140 }
141
142 switch (sa->sa_family) {
143 case AF_INET:
144 v4a = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
145 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
146 flags |= NI_NUMERICHOST;
147 v4a >>= IN_CLASSA_NSHIFT;
148 if (v4a == 0 || v4a == IN_LOOPBACKNET)
149 flags |= NI_NUMERICHOST;
150 break;
151 #ifdef INET6
152 case AF_INET6:
153 {
154 struct sockaddr_in6 *sin6;
155 sin6 = (struct sockaddr_in6 *)sa;
156 switch (sin6->sin6_addr.s6_addr8[0]) {
157 case 0x00:
158 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
159 ;
160 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
161 ;
162 else
163 flags |= NI_NUMERICHOST;
164 break;
165 default:
166 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
167 flags |= NI_NUMERICHOST;
168 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
169 flags |= NI_NUMERICHOST;
170 break;
171 }
172 }
173 break;
174 #endif
175 }
176 if (host == NULL || hostlen == 0) {
177 /* what should we do? */
178 } else if (flags & NI_NUMERICHOST) {
179 /* NUMERICHOST and NAMEREQD conflicts with each other */
180 if (flags & NI_NAMEREQD)
181 return ENI_NOHOSTNAME;
182 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
183 == NULL)
184 return ENI_SYSTEM;
185 if (strlen(numaddr) > hostlen)
186 return ENI_MEMORY;
187 strcpy(host, numaddr);
188 } else {
189 #ifdef USE_GETIPNODEBY
190 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
191 #else
192 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
193 h_error = h_errno;
194 #endif
195
196 if (hp) {
197 if (flags & NI_NOFQDN) {
198 p = strchr(hp->h_name, '.');
199 if (p) *p = '\0';
200 }
201 if (strlen(hp->h_name) > hostlen) {
202 #ifdef USE_GETIPNODEBY
203 freehostent(hp);
204 #endif
205 return ENI_MEMORY;
206 }
207 strcpy(host, hp->h_name);
208 #ifdef USE_GETIPNODEBY
209 freehostent(hp);
210 #endif
211 } else {
212 if (flags & NI_NAMEREQD)
213 return ENI_NOHOSTNAME;
214 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
215 == NULL)
216 return ENI_NOHOSTNAME;
217 if (strlen(numaddr) > hostlen)
218 return ENI_MEMORY;
219 strcpy(host, numaddr);
220 }
221 }
222 return SUCCESS;
223 }
224