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>
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
;
46 flags
|= NI_NUMERICSERV
;
49 psz_serv
= psz_servbuf
;
50 i_servlen
= sizeof( psz_servbuf
);
58 i_val
= getnameinfo(sa
, salen
, host
, hostlen
, psz_serv
, i_servlen
, flags
);
61 *portnum
= atoi( psz_serv
);
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
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).
92 snprintf (portbuf
, sizeof (portbuf
), "%u", port
);
100 * - accept the empty string as unspecified host (i.e. NULL)
101 * - ignore square brackets (for IPv6 numerals)
107 size_t len
= strlen (node
+ 1);
108 if ((len
<= sizeof (hostbuf
)) && (node
[len
] == ']'))
111 memcpy (hostbuf
, node
+ 1, len
- 1);
112 hostbuf
[len
- 1] = '\0';
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
);