]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Modules/getnameinfo.c
2 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available under
4 the terms and conditions of the BSD License that accompanies this distribution.
5 The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php.
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the project nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Issues to be discussed:
40 * - Thread safe-ness must be checked
41 * - Return values. There seems to be no standard for return value (RFC2133)
42 * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <arpa/nameser.h>
63 static struct gni_afd
{
70 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
71 offsetof(struct sockaddr_in6
, sin6_addr
)},
73 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
74 offsetof(struct sockaddr_in
, sin_addr
)},
84 #define ENI_NOSOCKET 0
85 #define ENI_NOSERVNAME 1
86 #define ENI_NOHOSTNAME 2
92 /* forward declaration to make gcc happy */
93 int getnameinfo
Py_PROTO((const struct sockaddr
*, size_t, char *, size_t,
94 char *, size_t, int));
97 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
98 const struct sockaddr
*sa
;
106 struct gni_afd
*gni_afd
;
123 #ifdef HAVE_SOCKADDR_SA_LEN
125 if (len
!= salen
) return ENI_SALEN
;
130 family
= sa
->sa_family
;
131 for (i
= 0; gni_afdl
[i
].a_af
; i
++)
132 if (gni_afdl
[i
].a_af
== family
) {
133 gni_afd
= &gni_afdl
[i
];
139 if (len
!= gni_afd
->a_socklen
) return ENI_SALEN
;
141 port
= ((struct gni_sockinet
*)sa
)->si_port
; /* network byte order */
142 addr
= (char *)sa
+ gni_afd
->a_off
;
144 if (serv
== NULL
|| servlen
== 0) {
145 /* what we should do? */
146 } else if (flags
& NI_NUMERICSERV
) {
147 sprintf(numserv
, "%d", ntohs(port
));
148 if (strlen(numserv
) > servlen
)
150 strcpy(serv
, numserv
);
152 sp
= getservbyport(port
, (flags
& NI_DGRAM
) ? "udp" : "tcp");
154 if (strlen(sp
->s_name
) > servlen
)
156 strcpy(serv
, sp
->s_name
);
158 return ENI_NOSERVNAME
;
161 switch (sa
->sa_family
) {
163 v4a
= ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
164 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
165 flags
|= NI_NUMERICHOST
;
166 v4a
>>= IN_CLASSA_NSHIFT
;
167 if (v4a
== 0 || v4a
== IN_LOOPBACKNET
)
168 flags
|= NI_NUMERICHOST
;
172 pfx
= ((struct sockaddr_in6
*)sa
)->sin6_addr
.s6_addr8
[0];
173 if (pfx
== 0 || pfx
== 0xfe || pfx
== 0xff)
174 flags
|= NI_NUMERICHOST
;
178 if (host
== NULL
|| hostlen
== 0) {
179 /* what should we do? */
180 } else if (flags
& NI_NUMERICHOST
) {
181 if (inet_ntop(gni_afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
184 if (strlen(numaddr
) > hostlen
)
186 strcpy(host
, numaddr
);
189 hp
= getipnodebyaddr(addr
, gni_afd
->a_addrlen
, gni_afd
->a_af
, &h_error
);
191 hp
= gethostbyaddr(addr
, gni_afd
->a_addrlen
, gni_afd
->a_af
);
196 if (flags
& NI_NOFQDN
) {
197 p
= strchr(hp
->h_name
, '.');
200 if (strlen(hp
->h_name
) > hostlen
) {
206 strcpy(host
, hp
->h_name
);
211 if (flags
& NI_NAMEREQD
)
212 return ENI_NOHOSTNAME
;
213 if (inet_ntop(gni_afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
215 return ENI_NOHOSTNAME
;
216 if (strlen(numaddr
) > hostlen
)
218 strcpy(host
, numaddr
);