demux: adaptive: handle obsolete http header line folding
[vlc.git] / src / network / getaddrinfo.c
blob90dbdda9744a66e05475bfeb0c1ccabdea764b1d
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>
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>
37 #include <sys/types.h>
38 #include <vlc_network.h>
40 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
41 char *host, int hostlen, int *portnum, int flags )
43 char psz_servbuf[6], *psz_serv;
44 int i_servlen, i_val;
46 flags |= NI_NUMERICSERV;
47 if( portnum != NULL )
49 psz_serv = psz_servbuf;
50 i_servlen = sizeof( psz_servbuf );
52 else
54 psz_serv = NULL;
55 i_servlen = 0;
58 i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
60 if( portnum != NULL )
61 *portnum = atoi( psz_serv );
63 return i_val;
67 /**
68 * Resolves a host name to a list of socket addresses (like getaddrinfo()).
70 * @param node host name to resolve (encoded as UTF-8), or NULL
71 * @param i_port port number for the socket addresses
72 * @param p_hints parameters (see getaddrinfo() manual page)
73 * @param res pointer set to the resulting chained list.
74 * @return 0 on success, a getaddrinfo() error otherwise.
75 * On failure, *res is undefined. On success, it must be freed with
76 * freeaddrinfo().
78 int vlc_getaddrinfo (const char *node, unsigned port,
79 const struct addrinfo *hints, struct addrinfo **res)
81 char hostbuf[NI_MAXHOST], portbuf[6], *servname;
84 * In VLC, we always use port number as integer rather than strings
85 * for historical reasons (and portability).
87 if (port != 0)
89 if (port > 65535)
90 return EAI_SERVICE;
91 /* cannot overflow */
92 snprintf (portbuf, sizeof (portbuf), "%u", port);
93 servname = portbuf;
95 else
96 servname = NULL;
99 * VLC extensions :
100 * - accept the empty string as unspecified host (i.e. NULL)
101 * - ignore square brackets (for IPv6 numerals)
103 if (node != NULL)
105 if (node[0] == '[')
107 size_t len = strlen (node + 1);
108 if ((len <= sizeof (hostbuf)) && (node[len] == ']'))
110 assert (len > 0);
111 memcpy (hostbuf, node + 1, len - 1);
112 hostbuf[len - 1] = '\0';
113 node = hostbuf;
116 if (node[0] == '\0')
117 node = NULL;
120 return getaddrinfo (node, servname, hints, res);
123 #if defined (_WIN32) || defined (__OS2__) \
124 || defined (__ANDROID__) || defined (__APPLE__) \
125 || defined (__native_client__)
126 #warning vlc_getaddr_info_i11e() not implemented!
127 int vlc_getaddrinfo_i11e(const char *node, unsigned port,
128 const struct addrinfo *hints, struct addrinfo **res)
130 return vlc_getaddrinfo(node, port, hints, res);
132 #endif