/src/vlc/src/network/getaddrinfo.c
Line | Count | Source |
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 | | * |
7 | | * Author: Rémi Denis-Courmont |
8 | | * |
9 | | * This program is free software; you can redistribute it and/or modify it |
10 | | * under the terms of the GNU Lesser General Public License as published by |
11 | | * the Free Software Foundation; either version 2.1 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * This program is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program; if not, write to the Free Software Foundation, |
21 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
22 | | *****************************************************************************/ |
23 | | |
24 | | #ifdef HAVE_CONFIG_H |
25 | | # include "config.h" |
26 | | #endif |
27 | | |
28 | | #include <vlc_common.h> |
29 | | |
30 | | #include <stddef.h> /* size_t */ |
31 | | #include <string.h> /* strlen(), memcpy(), memset(), strchr() */ |
32 | | #include <stdlib.h> /* malloc(), free(), strtoul() */ |
33 | | #include <errno.h> |
34 | | #include <assert.h> |
35 | | |
36 | | #include <sys/types.h> |
37 | | #include <vlc_network.h> |
38 | | |
39 | | int vlc_getnameinfo( const struct sockaddr *sa, int salen, |
40 | | char *host, int hostlen, int *portnum, int flags ) |
41 | 0 | { |
42 | 0 | char psz_servbuf[6], *psz_serv; |
43 | 0 | int i_servlen, i_val; |
44 | |
|
45 | 0 | flags |= NI_NUMERICSERV; |
46 | 0 | if( portnum != NULL ) |
47 | 0 | { |
48 | 0 | psz_serv = psz_servbuf; |
49 | 0 | i_servlen = sizeof( psz_servbuf ); |
50 | 0 | } |
51 | 0 | else |
52 | 0 | { |
53 | 0 | psz_serv = NULL; |
54 | 0 | i_servlen = 0; |
55 | 0 | } |
56 | |
|
57 | 0 | i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags); |
58 | |
|
59 | 0 | if( portnum != NULL ) |
60 | 0 | *portnum = atoi( psz_serv ); |
61 | |
|
62 | 0 | return i_val; |
63 | 0 | } |
64 | | |
65 | | int vlc_getaddrinfo (const char *node, unsigned port, |
66 | | const struct addrinfo *hints, struct addrinfo **res) |
67 | 0 | { |
68 | 0 | char hostbuf[NI_MAXHOST], portbuf[6], *servname; |
69 | | |
70 | | /* |
71 | | * In VLC, we always use port number as integer rather than strings |
72 | | * for historical reasons (and portability). |
73 | | */ |
74 | 0 | if (port != 0) |
75 | 0 | { |
76 | 0 | if (port > 65535) |
77 | 0 | return EAI_SERVICE; |
78 | | /* cannot overflow */ |
79 | 0 | snprintf (portbuf, sizeof (portbuf), "%u", port); |
80 | 0 | servname = portbuf; |
81 | 0 | } |
82 | 0 | else |
83 | 0 | servname = NULL; |
84 | | |
85 | | /* |
86 | | * VLC extensions : |
87 | | * - accept the empty string as unspecified host (i.e. NULL) |
88 | | * - ignore square brackets (for IPv6 numerals) |
89 | | */ |
90 | 0 | if (node != NULL) |
91 | 0 | { |
92 | 0 | if (node[0] == '[') |
93 | 0 | { |
94 | 0 | size_t len = strlen (node + 1); |
95 | 0 | if ((len <= sizeof (hostbuf)) && (node[len] == ']')) |
96 | 0 | { |
97 | 0 | assert (len > 0); |
98 | 0 | memcpy (hostbuf, node + 1, len - 1); |
99 | 0 | hostbuf[len - 1] = '\0'; |
100 | 0 | node = hostbuf; |
101 | 0 | } |
102 | 0 | } |
103 | 0 | if (node[0] == '\0') |
104 | 0 | node = NULL; |
105 | 0 | } |
106 | |
|
107 | 0 | return getaddrinfo (node, servname, hints, res); |
108 | 0 | } |
109 | | |
110 | | #if defined (_WIN32) || defined (__OS2__) \ |
111 | | || defined (__ANDROID__) || defined (__APPLE__) |
112 | | #warning vlc_getaddrinfo_i11e() not implemented! |
113 | | int vlc_getaddrinfo_i11e(const char *node, unsigned port, |
114 | | const struct addrinfo *hints, struct addrinfo **res) |
115 | | { |
116 | | return vlc_getaddrinfo(node, port, hints, res); |
117 | | } |
118 | | #endif |