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
8 * Author: Rémi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
29 #include <vlc_common.h>
31 #include <stddef.h> /* size_t */
32 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
33 #include <stdlib.h> /* malloc(), free(), strtoul() */
37 #include <sys/types.h>
38 #include <vlc_network.h>
44 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
45 char *host, int hostlen, int *portnum, int flags )
47 char psz_servbuf[6], *psz_serv;
50 flags |= NI_NUMERICSERV;
53 psz_serv = psz_servbuf;
54 i_servlen = sizeof( psz_servbuf );
62 i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
65 *portnum = atoi( psz_serv );
72 * Resolves a host name to a list of socket addresses (like getaddrinfo()).
74 * @param p_this a VLC object
75 * @param node host name to resolve (encoded as UTF-8), or NULL
76 * @param i_port port number for the socket addresses
77 * @param p_hints parameters (see getaddrinfo() manual page)
78 * @param res pointer set to the resulting chained list.
79 * @return 0 on success, a getaddrinfo() error otherwise.
80 * On failure, *res is undefined. On success, it must be freed with
83 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
84 int i_port, const struct addrinfo *p_hints,
85 struct addrinfo **res )
87 struct addrinfo hints;
88 char psz_buf[NI_MAXHOST], psz_service[6];
91 * In VLC, we always use port number as integer rather than strings
92 * for historical reasons (and portability).
94 if( ( i_port > 65535 ) || ( i_port < 0 ) )
96 msg_Err( p_this, "invalid port number %d specified", i_port );
100 /* cannot overflow */
101 snprintf( psz_service, 6, "%d", i_port );
103 /* Check if we have to force ipv4 or ipv6 */
104 memset (&hints, 0, sizeof (hints));
107 const int safe_flags =
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".
130 hints.ai_flags = p_hints->ai_flags & safe_flags;
133 /* We only ever use port *numbers* */
134 hints.ai_flags |= AI_NUMERICSERV;
138 * - accept "" as NULL
139 * - ignore square brackets
145 size_t len = strlen (node + 1);
146 if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
149 memcpy (psz_buf, node + 1, len - 1);
150 psz_buf[len - 1] = '\0';
161 * Winsock tries to resolve numerical IPv4 addresses as AAAA
162 * and IPv6 addresses as A... There comes the bug-to-bug fix.
164 if ((hints.ai_flags & AI_NUMERICHOST) == 0)
166 hints.ai_flags |= AI_NUMERICHOST;
167 ret = getaddrinfo (node, psz_service, &hints, res);
170 hints.ai_flags &= ~AI_NUMERICHOST;
174 /* Run-time I18n Domain Names support */
175 hints.ai_flags |= AI_IDN;
176 ret = getaddrinfo (node, psz_service, &hints, res);
177 if (ret != EAI_BADFLAGS)
179 /* IDN not available: disable and retry without it */
180 hints.ai_flags &= ~AI_IDN;
182 ret = getaddrinfo (node, psz_service, &hints, res);
184 #if defined(AI_IDN) || defined(WIN32)