Fixed a crash caused by yadif deinterlacer on Windows XP
[vlc/solaris.git] / src / network / getaddrinfo.c
blob0d5cdf873792f36e0b29574145a777045c73535e
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$
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
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>
38 #include <sys/types.h>
39 #include <vlc_network.h>
41 #ifndef AF_UNSPEC
42 # define AF_UNSPEC 0
43 #endif
45 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
46 char *host, int hostlen, int *portnum, int flags )
48 char psz_servbuf[6], *psz_serv;
49 int i_servlen, i_val;
51 flags |= NI_NUMERICSERV;
52 if( portnum != NULL )
54 psz_serv = psz_servbuf;
55 i_servlen = sizeof( psz_servbuf );
57 else
59 psz_serv = NULL;
60 i_servlen = 0;
63 i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
65 if( portnum != NULL )
66 *portnum = atoi( psz_serv );
68 return i_val;
72 /**
73 * Resolves a host name to a list of socket addresses (like getaddrinfo()).
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().
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 )
88 struct addrinfo hints;
89 char psz_buf[NI_MAXHOST], psz_service[6];
92 * In VLC, we always use port number as integer rather than strings
93 * for historical reasons (and portability).
95 if( ( i_port > 65535 ) || ( i_port < 0 ) )
97 msg_Err( p_this, "invalid port number %d specified", i_port );
98 return EAI_SERVICE;
101 /* cannot overflow */
102 snprintf( psz_service, 6, "%d", i_port );
104 /* Check if we have to force ipv4 or ipv6 */
105 memset (&hints, 0, sizeof (hints));
106 if (p_hints != NULL)
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
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".
131 hints.ai_flags = p_hints->ai_flags & safe_flags;
134 /* We only ever use port *numbers* */
135 hints.ai_flags |= AI_NUMERICSERV;
138 * VLC extensions :
139 * - accept "" as NULL
140 * - ignore square brackets
142 if (node != NULL)
144 if (node[0] == '[')
146 size_t len = strlen (node + 1);
147 if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
149 assert (len > 0);
150 memcpy (psz_buf, node + 1, len - 1);
151 psz_buf[len - 1] = '\0';
152 node = psz_buf;
155 if (node[0] == '\0')
156 node = NULL;
159 int ret;
160 node = ToLocale (node);
161 #ifdef WIN32
163 * Winsock tries to resolve numerical IPv4 addresses as AAAA
164 * and IPv6 addresses as A... There comes the bug-to-bug fix.
166 if ((hints.ai_flags & AI_NUMERICHOST) == 0)
168 hints.ai_flags |= AI_NUMERICHOST;
169 ret = getaddrinfo (node, psz_service, &hints, res);
170 if (ret == 0)
171 goto out;
172 hints.ai_flags &= ~AI_NUMERICHOST;
174 #endif
175 #ifdef AI_IDN
176 /* Run-time I18n Domain Names support */
177 hints.ai_flags |= AI_IDN;
178 ret = getaddrinfo (node, psz_service, &hints, res);
179 if (ret != EAI_BADFLAGS)
180 goto out;
181 /* IDN not available: disable and retry without it */
182 hints.ai_flags &= ~AI_IDN;
183 #endif
184 ret = getaddrinfo (node, psz_service, &hints, res);
186 #if defined(AI_IDN) || defined(WIN32)
187 out:
188 #endif
189 LocaleFree (node);
190 return ret;