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