blob: f7985c9acde0b24cf0ceb996f4c99e6038ed5848 [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.
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004 *
Martin v. Löwis01dfdb32001-06-23 16:30:13 +00005 * 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.
Antoine Pitrouc83ea132010-05-09 14:46:46 +000016 *
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000017 * 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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000056 int a_af;
57 int a_addrlen;
58 int a_socklen;
59 int a_off;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000060} gni_afdl [] = {
Martin v. Löwis44ddbde2001-12-02 10:15:37 +000061#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
63 offsetof(struct sockaddr_in6, sin6_addr)},
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000064#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
66 offsetof(struct sockaddr_in, sin_addr)},
67 {0, 0, 0},
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000068};
69
70struct gni_sockinet {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000071 u_char si_len;
72 u_char si_family;
73 u_short si_port;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000074};
75
Antoine Pitrouc83ea132010-05-09 14:46:46 +000076#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
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000083
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +000084/* forward declaration to make gcc happy */
85int getnameinfo Py_PROTO((const struct sockaddr *, size_t, char *, size_t,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 char *, size_t, int));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +000087
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000088int
89getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 const struct sockaddr *sa;
91 size_t salen;
92 char *host;
93 size_t hostlen;
94 char *serv;
95 size_t servlen;
96 int flags;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +000097{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 struct gni_afd *gni_afd;
99 struct servent *sp;
100 struct hostent *hp;
101 u_short port;
102 int family, len, i;
103 char *addr, *p;
104 u_long v4a;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000105#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000106 u_char pfx;
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000107#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 int h_error;
109 char numserv[512];
110 char numaddr[512];
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000111
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 if (sa == NULL)
113 return ENI_NOSOCKET;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000114
115#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 len = sa->sa_len;
117 if (len != salen) return ENI_SALEN;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000118#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 len = salen;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000120#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121
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];
126 goto found;
127 }
128 return ENI_FAMILY;
129
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000130 found:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 if (len != gni_afd->a_socklen) return ENI_SALEN;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000132
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 port = ((struct gni_sockinet *)sa)->si_port; /* network byte order */
134 addr = (char *)sa + gni_afd->a_off;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000135
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 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)
141 return ENI_MEMORY;
142 strcpy(serv, numserv);
143 } else {
144 sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
145 if (sp) {
146 if (strlen(sp->s_name) > servlen)
147 return ENI_MEMORY;
148 strcpy(serv, sp->s_name);
149 } else
150 return ENI_NOSERVNAME;
151 }
152
153 switch (sa->sa_family) {
154 case AF_INET:
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;
161 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000162#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 case AF_INET6:
Antoine Pitrou1fa9f7b2012-08-02 20:37:12 +0200164 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000165 if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
166 flags |= NI_NUMERICHOST;
167 break;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000168#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000169 }
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))
174 == NULL)
175 return ENI_SYSTEM;
176 if (strlen(numaddr) > hostlen)
177 return ENI_MEMORY;
178 strcpy(host, numaddr);
179 } else {
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000180#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 hp = getipnodebyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af, &h_error);
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000182#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000183 hp = gethostbyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af);
184 h_error = h_errno;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000185#endif
186
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 if (hp) {
188 if (flags & NI_NOFQDN) {
189 p = strchr(hp->h_name, '.');
190 if (p) *p = '\0';
191 }
192 if (strlen(hp->h_name) > hostlen) {
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000193#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000194 freehostent(hp);
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000195#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 return ENI_MEMORY;
197 }
198 strcpy(host, hp->h_name);
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000199#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 freehostent(hp);
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000201#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 } else {
203 if (flags & NI_NAMEREQD)
204 return ENI_NOHOSTNAME;
205 if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr))
206 == NULL)
207 return ENI_NOHOSTNAME;
208 if (strlen(numaddr) > hostlen)
209 return ENI_MEMORY;
210 strcpy(host, numaddr);
211 }
212 }
213 return SUCCESS;
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000214}