]> git.sesse.net Git - vlc/blob - src/network/getaddrinfo.c
vlc_getaddrinfo: pass AI_NUMERICSERV explicitly where applicable
[vlc] / src / network / getaddrinfo.c
1 /*****************************************************************************
2  * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
3  *****************************************************************************
4  * Copyright (C) 2005 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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
31 #include <stddef.h> /* size_t */
32 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
33 #include <stdlib.h> /* malloc(), free(), strtoul() */
34 #include <errno.h>
35 #include <assert.h>
36
37 #include <sys/types.h>
38 #include <vlc_network.h>
39
40 #ifndef AF_UNSPEC
41 #   define AF_UNSPEC   0
42 #endif
43
44 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
45                      char *host, int hostlen, int *portnum, int flags )
46 {
47     char psz_servbuf[6], *psz_serv;
48     int i_servlen, i_val;
49
50     flags |= NI_NUMERICSERV;
51     if( portnum != NULL )
52     {
53         psz_serv = psz_servbuf;
54         i_servlen = sizeof( psz_servbuf );
55     }
56     else
57     {
58         psz_serv = NULL;
59         i_servlen = 0;
60     }
61
62     i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
63
64     if( portnum != NULL )
65         *portnum = atoi( psz_serv );
66
67     return i_val;
68 }
69
70
71 /**
72  * Resolves a host name to a list of socket addresses (like getaddrinfo()).
73  *
74  * @param node host name to resolve (encoded as UTF-8), or NULL
75  * @param i_port port number for the socket addresses
76  * @param p_hints parameters (see getaddrinfo() manual page)
77  * @param res pointer set to the resulting chained list.
78  * @return 0 on success, a getaddrinfo() error otherwise.
79  * On failure, *res is undefined. On success, it must be freed with
80  * freeaddrinfo().
81  */
82 int vlc_getaddrinfo (const char *node, unsigned port,
83                      const struct addrinfo *p_hints, struct addrinfo **res)
84 {
85     struct addrinfo hints;
86     char psz_buf[NI_MAXHOST], portbuf[6], *servname;
87
88     /*
89      * In VLC, we always use port number as integer rather than strings
90      * for historical reasons (and portability).
91      */
92     if (port != 0)
93     {
94         if (port > 65535)
95             return EAI_SERVICE;
96         /* cannot overflow */
97         snprintf (portbuf, sizeof (portbuf), "%u", port);
98         servname = portbuf;
99     }
100     else
101         servname = NULL;
102
103     /* Check if we have to force ipv4 or ipv6 */
104     memset (&hints, 0, sizeof (hints));
105     if (p_hints != NULL)
106     {
107         const int safe_flags =
108             AI_PASSIVE |
109             AI_CANONNAME |
110             AI_NUMERICHOST |
111             AI_NUMERICSERV |
112 #ifdef AI_ALL
113             AI_ALL |
114 #endif
115 #ifdef AI_ADDRCONFIG
116             AI_ADDRCONFIG |
117 #endif
118 #ifdef AI_V4MAPPED
119             AI_V4MAPPED |
120 #endif
121             0;
122
123         hints.ai_family = p_hints->ai_family;
124         hints.ai_socktype = p_hints->ai_socktype;
125         hints.ai_protocol = p_hints->ai_protocol;
126         /* Unfortunately, some flags chang the layout of struct addrinfo, so
127          * they cannot be copied blindly from p_hints to &hints. Therefore, we
128          * only copy flags that we know for sure are "safe".
129          */
130         hints.ai_flags = p_hints->ai_flags & safe_flags;
131     }
132
133     /*
134      * VLC extensions :
135      * - accept "" as NULL
136      * - ignore square brackets
137      */
138     if (node != NULL)
139     {
140         if (node[0] == '[')
141         {
142             size_t len = strlen (node + 1);
143             if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
144             {
145                 assert (len > 0);
146                 memcpy (psz_buf, node + 1, len - 1);
147                 psz_buf[len - 1] = '\0';
148                 node = psz_buf;
149             }
150         }
151         if (node[0] == '\0')
152             node = NULL;
153     }
154
155     int ret;
156 #ifdef WIN32
157     /*
158      * Winsock tries to resolve numerical IPv4 addresses as AAAA
159      * and IPv6 addresses as A... There comes the bug-to-bug fix.
160      */
161     if ((hints.ai_flags & AI_NUMERICHOST) == 0)
162     {
163         hints.ai_flags |= AI_NUMERICHOST;
164         ret = getaddrinfo (node, servname, &hints, res);
165         if (ret == 0)
166             goto out;
167         hints.ai_flags &= ~AI_NUMERICHOST;
168     }
169 #endif
170 #ifdef AI_IDN
171     /* Run-time I18n Domain Names support */
172     hints.ai_flags |= AI_IDN;
173     ret = getaddrinfo (node, servname, &hints, res);
174     if (ret != EAI_BADFLAGS)
175         goto out;
176     /* IDN not available: disable and retry without it */
177     hints.ai_flags &= ~AI_IDN;
178 #endif
179     ret = getaddrinfo (node, servname, &hints, res);
180
181 #if defined(AI_IDN) || defined(WIN32)
182 out:
183 #endif
184     return ret;
185 }