2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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().
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>
55 static struct gni_afd
{
62 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
63 offsetof(struct sockaddr_in6
, sin6_addr
)},
65 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
66 offsetof(struct sockaddr_in
, sin_addr
)},
76 #define ENI_NOSOCKET 0
77 #define ENI_NOSERVNAME 1
78 #define ENI_NOHOSTNAME 2
84 /* forward declaration to make gcc happy */
85 int getnameinfo
Py_PROTO((const struct sockaddr
*, size_t, char *, size_t,
86 char *, size_t, int));
89 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
90 const struct sockaddr
*sa
;
98 struct gni_afd
*gni_afd
;
115 #ifdef HAVE_SOCKADDR_SA_LEN
117 if (len
!= salen
) return ENI_SALEN
;
122 family
= sa
->sa_family
;
123 for (i
= 0; gni_afdl
[i
].a_af
; i
++)
124 if (gni_afdl
[i
].a_af
== family
) {
125 gni_afd
= &gni_afdl
[i
];
131 if (len
!= gni_afd
->a_socklen
) return ENI_SALEN
;
133 port
= ((struct gni_sockinet
*)sa
)->si_port
; /* network byte order */
134 addr
= (char *)sa
+ gni_afd
->a_off
;
136 if (serv
== NULL
|| servlen
== 0) {
137 /* what we should do? */
138 } else if (flags
& NI_NUMERICSERV
) {
139 sprintf(numserv
, "%d", ntohs(port
));
140 if (strlen(numserv
) > servlen
)
142 strcpy(serv
, numserv
);
144 sp
= getservbyport(port
, (flags
& NI_DGRAM
) ? "udp" : "tcp");
146 if (strlen(sp
->s_name
) > servlen
)
148 strcpy(serv
, sp
->s_name
);
150 return ENI_NOSERVNAME
;
153 switch (sa
->sa_family
) {
155 v4a
= ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
156 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
157 flags
|= NI_NUMERICHOST
;
158 v4a
>>= IN_CLASSA_NSHIFT
;
159 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
160 flags
|= NI_NUMERICHOST
;
164 pfx
= ((struct sockaddr_in6
*)sa
)->sin6_addr
.s6_addr8
[0];
165 if (pfx
== 0 || pfx
== 0xfe || pfx
== 0xff)
166 flags
|= NI_NUMERICHOST
;
170 if (host
== NULL
|| hostlen
== 0) {
171 /* what should we do? */
172 } else if (flags
& NI_NUMERICHOST
) {
173 if (inet_ntop(gni_afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
176 if (strlen(numaddr
) > hostlen
)
178 strcpy(host
, numaddr
);
181 hp
= getipnodebyaddr(addr
, gni_afd
->a_addrlen
, gni_afd
->a_af
, &h_error
);
183 hp
= gethostbyaddr(addr
, gni_afd
->a_addrlen
, gni_afd
->a_af
);
188 if (flags
& NI_NOFQDN
) {
189 p
= strchr(hp
->h_name
, '.');
192 if (strlen(hp
->h_name
) > hostlen
) {
198 strcpy(host
, hp
->h_name
);
203 if (flags
& NI_NAMEREQD
)
204 return ENI_NOHOSTNAME
;
205 if (inet_ntop(gni_afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
207 return ENI_NOHOSTNAME
;
208 if (strlen(numaddr
) > hostlen
)
210 strcpy(host
, numaddr
);