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