]> git.sesse.net Git - vlc/blob - src/network/getaddrinfo.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / network / getaddrinfo.c
1 /*****************************************************************************
2  * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * Copyright (C) 2002-2007 Rémi Denis-Courmont
6  * $Id$
7  *
8  * Author: Rémi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
31
32 #include <stddef.h> /* size_t */
33 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
34 #include <stdlib.h> /* malloc(), free(), strtoul() */
35 #include <errno.h>
36 #include <assert.h>
37
38 #include <sys/types.h>
39 #include <vlc_network.h>
40
41 #ifndef AF_UNSPEC
42 #   define AF_UNSPEC   0
43 #endif
44
45 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
46                      char *host, int hostlen, int *portnum, int flags )
47 {
48     char psz_servbuf[6], *psz_serv;
49     int i_servlen, i_val;
50
51     flags |= NI_NUMERICSERV;
52     if( portnum != NULL )
53     {
54         psz_serv = psz_servbuf;
55         i_servlen = sizeof( psz_servbuf );
56     }
57     else
58     {
59         psz_serv = NULL;
60         i_servlen = 0;
61     }
62
63     i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
64
65     if( portnum != NULL )
66         *portnum = atoi( psz_serv );
67
68     return i_val;
69 }
70
71
72 /**
73  * Resolves a host name to a list of socket addresses (like getaddrinfo()).
74  *
75  * @param p_this a VLC object
76  * @param node host name to resolve (encoded as UTF-8), or NULL
77  * @param i_port port number for the socket addresses
78  * @param p_hints parameters (see getaddrinfo() manual page)
79  * @param res pointer set to the resulting chained list.
80  * @return 0 on success, a getaddrinfo() error otherwise.
81  * On failure, *res is undefined. On success, it must be freed with
82  * freeaddrinfo().
83  */
84 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
85                      int i_port, const struct addrinfo *p_hints,
86                      struct addrinfo **res )
87 {
88     struct addrinfo hints;
89     char psz_buf[NI_MAXHOST], psz_service[6];
90
91     /*
92      * In VLC, we always use port number as integer rather than strings
93      * for historical reasons (and portability).
94      */
95     if( ( i_port > 65535 ) || ( i_port < 0 ) )
96     {
97         msg_Err( p_this, "invalid port number %d specified", i_port );
98         return EAI_SERVICE;
99     }
100
101     /* cannot overflow */
102     snprintf( psz_service, 6, "%d", i_port );
103
104     /* Check if we have to force ipv4 or ipv6 */
105     memset (&hints, 0, sizeof (hints));
106     if (p_hints != NULL)
107     {
108         const int safe_flags =
109             AI_PASSIVE |
110             AI_CANONNAME |
111             AI_NUMERICHOST |
112             AI_NUMERICSERV |
113 #ifdef AI_ALL
114             AI_ALL |
115 #endif
116 #ifdef AI_ADDRCONFIG
117             AI_ADDRCONFIG |
118 #endif
119 #ifdef AI_V4MAPPED
120             AI_V4MAPPED |
121 #endif
122             0;
123
124         hints.ai_family = p_hints->ai_family;
125         hints.ai_socktype = p_hints->ai_socktype;
126         hints.ai_protocol = p_hints->ai_protocol;
127         /* Unfortunately, some flags chang the layout of struct addrinfo, so
128          * they cannot be copied blindly from p_hints to &hints. Therefore, we
129          * only copy flags that we know for sure are "safe".
130          */
131         hints.ai_flags = p_hints->ai_flags & safe_flags;
132     }
133
134     /* We only ever use port *numbers* */
135     hints.ai_flags |= AI_NUMERICSERV;
136
137     if( hints.ai_family == AF_UNSPEC )
138     {
139 #ifdef AF_INET6
140         if (var_CreateGetBool (p_this, "ipv6"))
141             hints.ai_family = AF_INET6;
142         else
143 #endif
144         if (var_CreateGetBool (p_this, "ipv4"))
145             hints.ai_family = AF_INET;
146     }
147
148     /*
149      * VLC extensions :
150      * - accept "" as NULL
151      * - ignore square brackets
152      */
153     if (node != NULL)
154     {
155         if (node[0] == '[')
156         {
157             size_t len = strlen (node + 1);
158             if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
159             {
160                 assert (len > 0);
161                 memcpy (psz_buf, node + 1, len - 1);
162                 psz_buf[len - 1] = '\0';
163                 node = psz_buf;
164             }
165         }
166         if (node[0] == '\0')
167             node = NULL;
168     }
169
170     int ret;
171     node = ToLocale (node);
172 #ifdef WIN32
173     /*
174      * Winsock tries to resolve numerical IPv4 addresses as AAAA
175      * and IPv6 addresses as A... There comes the bug-to-bug fix.
176      */
177     if ((hints.ai_flags & AI_NUMERICHOST) == 0)
178     {
179         hints.ai_flags |= AI_NUMERICHOST;
180         ret = getaddrinfo (node, psz_service, &hints, res);
181         if (ret == 0)
182             goto out;
183         hints.ai_flags &= ~AI_NUMERICHOST;
184     }
185 #endif
186 #ifdef AI_IDN
187     /* Run-time I18n Domain Names support */
188     hints.ai_flags |= AI_IDN;
189     ret = getaddrinfo (node, psz_service, &hints, res);
190     if (ret != EAI_BADFLAGS)
191         goto out;
192     /* IDN not available: disable and retry without it */
193     hints.ai_flags &= ~AI_IDN;
194 #endif
195     ret = getaddrinfo (node, psz_service, &hints, res);
196
197 #if defined(AI_IDN) || defined(WIN32)
198 out:
199 #endif
200     LocaleFree (node);
201     return ret;
202 }
203
204
205 /**
206  * inet_pton() replacement
207  */
208 int vlc_inet_pton (int af, const char *src, void *dst)
209 {
210 #ifndef HAVE_INET_PTON
211     /* Windows Vista has inet_pton(), but not XP. */
212     /* We have a pretty good example of abstraction inversion here... */
213     struct addrinfo hints = {
214         .ai_family = af,
215         .ai_socktype = SOCK_DGRAM, /* make sure we have... */
216         .ai_protocol = IPPROTO_UDP, /* ...only one response */
217         .ai_flags = AI_NUMERICHOST,
218     }, *res;
219
220     if (getaddrinfo (src, NULL, &hints, &res))
221         return 0;
222
223     const void *data;
224     size_t len;
225
226     switch (af)
227     {
228         case AF_INET:
229             data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr;
230             len = sizeof (struct in_addr);
231             break;
232 #ifdef AF_INET6
233         case AF_INET6:
234             data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
235             len = sizeof (struct in6_addr);
236             break;
237 #endif
238         default:
239             freeaddrinfo (res);
240             return -1;
241     }
242     memcpy (dst, data, len);
243     freeaddrinfo (res);
244     return 1;
245 #else /* HAVE_INET_PTON */
246     return inet_pton( af, src, dst );
247 #endif /* HAVE_INET_PTON */
248 }
249
250 /**
251  * inet_ntop() replacement
252  */
253 const char *vlc_inet_ntop (int af, const void *src, char *dst, socklen_t cnt)
254 {
255 #ifndef HAVE_INET_NTOP
256     int ret = EAI_FAMILY;
257
258     switch (af)
259     {
260 #ifdef AF_INET6
261         case AF_INET6:
262             {
263                 struct sockaddr_in6 addr;
264                 memset (&addr, 0, sizeof(addr));
265                 addr.sin6_family = AF_INET6;
266                 addr.sin6_addr = *(struct in6_addr *)src;
267                 ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
268                                    dst, cnt, NULL, 0, NI_NUMERICHOST);
269             }
270
271 #endif
272         case AF_INET:
273             {
274                 struct sockaddr_in addr;
275                 memset(&addr, 0, sizeof(addr));
276                 addr.sin_family = AF_INET;
277                 addr.sin_addr = *(struct in_addr *)src;
278                 ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
279                                    dst, cnt, NULL, 0, NI_NUMERICHOST);
280             }
281     }
282     return (ret == 0) ? dst : NULL;
283 #else /* HAVE_INET_NTOP */
284     return inet_ntop( af, src, dst, cnt );
285 #endif /* HAVE_INET_NTOP */
286 }
287