]>
Commit | Line | Data |
---|---|---|
76ebf6c4 AC |
1 | /* |
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 | * - RFC2553 says that we should raise error on short buffer. X/Open says | |
34 | * we need to truncate the result. We obey RFC2553 (and X/Open should be | |
35 | * modified). ipngwg rough consensus seems to follow RFC2553. | |
36 | * - What is "local" in NI_FQDN? | |
37 | * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. | |
38 | * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if | |
39 | * sin6_scope_id is filled - standardization status? | |
40 | * XXX breaks backward compat for code that expects no scopeid. | |
41 | * beware on merge. | |
42 | */ | |
43 | ||
44 | #ifdef _WIN32 | |
2dea53a2 | 45 | #include <rb_lib.h> |
76ebf6c4 AC |
46 | #include "getaddrinfo.h" |
47 | #include "getnameinfo.h" | |
48 | ||
76ebf6c4 AC |
49 | static const struct afd { |
50 | int a_af; | |
51 | int a_addrlen; | |
52 | rb_socklen_t a_socklen; | |
53 | int a_off; | |
54 | } afdl [] = { | |
55 | #ifdef IPV6 | |
56 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), | |
57 | offsetof(struct sockaddr_in6, sin6_addr)}, | |
58 | #endif | |
59 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), | |
60 | offsetof(struct sockaddr_in, sin_addr)}, | |
61 | {0, 0, 0, 0}, | |
62 | }; | |
63 | ||
64 | struct sockinet | |
65 | { | |
66 | unsigned char si_len; | |
67 | unsigned char si_family; | |
68 | unsigned short si_port; | |
69 | }; | |
70 | ||
71 | #ifdef IPV6 | |
72 | static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, | |
73 | size_t, int); | |
74 | #endif | |
75 | ||
76 | int | |
77 | rb_getnameinfo(const struct sockaddr *sa, rb_socklen_t salen, char *host, | |
78 | size_t hostlen, char *serv, size_t servlen, int flags) | |
79 | { | |
80 | const struct afd *afd; | |
81 | struct servent *sp; | |
82 | unsigned short port; | |
83 | int family, i; | |
84 | const char *addr; | |
85 | uint32_t v4a; | |
86 | char numserv[512]; | |
87 | char numaddr[512]; | |
88 | ||
89 | if (sa == NULL) | |
90 | return EAI_FAIL; | |
91 | ||
92 | /* if (sa->sa_len != salen) | |
93 | return EAI_FAIL; | |
94 | */ | |
95 | family = sa->sa_family; | |
96 | for (i = 0; afdl[i].a_af; i++) | |
97 | if (afdl[i].a_af == family) { | |
98 | afd = &afdl[i]; | |
99 | goto found; | |
100 | } | |
101 | return EAI_FAMILY; | |
102 | ||
103 | found: | |
104 | if (salen != afd->a_socklen) | |
105 | return EAI_FAIL; | |
106 | ||
107 | /* network byte order */ | |
108 | port = ((const struct sockinet *)sa)->si_port; | |
109 | addr = (const char *)sa + afd->a_off; | |
110 | ||
111 | if (serv == NULL || servlen == 0) { | |
112 | /* | |
113 | * do nothing in this case. | |
114 | * in case you are wondering if "&&" is more correct than | |
115 | * "||" here: rfc2553bis-03 says that serv == NULL OR | |
116 | * servlen == 0 means that the caller does not want the result. | |
117 | */ | |
118 | } else { | |
119 | if (flags & NI_NUMERICSERV) | |
120 | sp = NULL; | |
121 | else { | |
122 | sp = getservbyport(port, | |
123 | (flags & NI_DGRAM) ? "udp" : "tcp"); | |
124 | } | |
125 | if (sp) { | |
126 | if (strlen(sp->s_name) + 1 > servlen) | |
127 | return EAI_MEMORY; | |
128 | rb_strlcpy(serv, sp->s_name, servlen); | |
129 | } else { | |
130 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | |
131 | if (strlen(numserv) + 1 > servlen) | |
132 | return EAI_MEMORY; | |
133 | rb_strlcpy(serv, numserv, servlen); | |
134 | } | |
135 | } | |
136 | ||
137 | switch (sa->sa_family) { | |
138 | case AF_INET: | |
139 | v4a = (uint32_t) | |
140 | ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr); | |
141 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) | |
142 | flags |= NI_NUMERICHOST; | |
143 | v4a >>= IN_CLASSA_NSHIFT; | |
144 | if (v4a == 0) | |
4094d2fa | 145 | flags |= NI_NUMERICHOST; |
76ebf6c4 AC |
146 | break; |
147 | #ifdef IPV6 | |
148 | case AF_INET6: | |
149 | { | |
150 | const struct sockaddr_in6 *sin6; | |
151 | sin6 = (const struct sockaddr_in6 *)sa; | |
152 | switch (sin6->sin6_addr.s6_addr[0]) { | |
153 | case 0x00: | |
154 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | |
155 | ; | |
156 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | |
157 | ; | |
158 | else | |
159 | flags |= NI_NUMERICHOST; | |
160 | break; | |
161 | default: | |
162 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | |
163 | flags |= NI_NUMERICHOST; | |
164 | } | |
165 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | |
166 | flags |= NI_NUMERICHOST; | |
167 | break; | |
168 | } | |
169 | } | |
170 | break; | |
171 | #endif | |
172 | } | |
173 | if (host == NULL || hostlen == 0) { | |
174 | /* | |
175 | * do nothing in this case. | |
176 | * in case you are wondering if "&&" is more correct than | |
177 | * "||" here: rfc2553bis-03 says that host == NULL or | |
178 | * hostlen == 0 means that the caller does not want the result. | |
179 | */ | |
180 | } else if (flags & NI_NUMERICHOST) { | |
181 | size_t numaddrlen; | |
182 | ||
183 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | |
184 | if (flags & NI_NAMEREQD) | |
185 | return EAI_NONAME; | |
186 | ||
187 | switch(afd->a_af) { | |
188 | #ifdef IPV6 | |
189 | case AF_INET6: | |
190 | { | |
191 | int error; | |
192 | ||
193 | if ((error = ip6_parsenumeric(sa, addr, host, | |
194 | hostlen, flags)) != 0) | |
195 | return(error); | |
196 | break; | |
197 | } | |
198 | #endif | |
199 | default: | |
200 | if (rb_inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | |
201 | == NULL) | |
202 | return EAI_SYSTEM; | |
203 | numaddrlen = strlen(numaddr); | |
204 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | |
205 | return EAI_MEMORY; | |
206 | rb_strlcpy(host, numaddr, hostlen); | |
207 | break; | |
208 | } | |
209 | } | |
210 | return(0); | |
211 | } | |
212 | ||
213 | #ifdef IPV6 | |
214 | static int | |
215 | ip6_parsenumeric(const struct sockaddr *sa, const char *addr, | |
216 | char *host, size_t hostlen, int flags) | |
217 | { | |
218 | size_t numaddrlen; | |
219 | char numaddr[512]; | |
220 | ||
221 | if (rb_inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) | |
222 | return(EAI_SYSTEM); | |
223 | ||
224 | numaddrlen = strlen(numaddr); | |
225 | ||
226 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | |
227 | return(EAI_MEMORY); | |
228 | ||
229 | if (*numaddr == ':') | |
230 | { | |
231 | *host = '0'; | |
232 | rb_strlcpy(host+1, numaddr, hostlen-1); | |
233 | } | |
234 | else | |
235 | rb_strlcpy(host, numaddr, hostlen); | |
236 | ||
237 | return(0); | |
238 | } | |
239 | #endif | |
240 | #endif |