ยปCore Development>Code coverage>Modules/getnameinfo.c

Python code coverage for Modules/getnameinfo.c

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