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