1 /*****************************************************************************
2 * vlc_network.h: interface to communicate with network plug-ins
3 *****************************************************************************
4 * Copyright (C) 2002-2005 VLC authors and VideoLAN
5 * Copyright © 2006-2007 Rémi Denis-Courmont
6 * $Id: 010454a01c09730b342d9603d2dc1770361057d2 $
7 *
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
10 * Rémi Denis-Courmont
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
26
27 #ifndef VLC_NETWORK_H
28 # define VLC_NETWORK_H
29
30 /**
31 * \ingroup os
32 * \defgroup net Networking
33 * @{
34 * \file
35 * Definitions for sockets and low-level networking
36 * \defgroup sockets Internet sockets
37 * @{
38 */
39
40 #include <sys/types.h>
41 #include <unistd.h>
42
43 #if defined( _WIN32 )
44 # define _NO_OLDNAMES 1
45 # include <winsock2.h>
46 # include <ws2tcpip.h>
47 # define net_errno (WSAGetLastError())
48 # define net_Close(fd) ((void)closesocket((SOCKET)fd))
49 # ifndef IPV6_V6ONLY
50 # define IPV6_V6ONLY 27
51 # endif
52 #else
53 # include <sys/socket.h>
54 # include <netinet/in.h>
55 # include <netdb.h>
56 # define net_errno errno
57 # define net_Close(fd) ((void)vlc_close(fd))
58 #endif
59
60 #ifndef MSG_NOSIGNAL
61 # define MSG_NOSIGNAL 0
62 #endif
63
64 /**
65 * Creates a socket file descriptor.
66 *
67 * This function creates a socket, similar to the standard socket() function.
68 * However, the new file descriptor has the close-on-exec flag set atomically,
69 * so as to avoid leaking the descriptor to child processes.
70 *
71 * The non-blocking flag can also optionally be set.
72 *
73 * @param pf protocol family
74 * @param type socket type
75 * @param proto network protocol
76 * @param nonblock true to create a non-blocking socket
77 * @return a new file descriptor or -1 on error
78 */
79 VLC_API int vlc_socket(int pf, int type, int proto, bool nonblock) VLC_USED;
80
81 /**
82 * Creates a pair of socket file descriptors.
83 *
84 * This function creates a pair of sockets that are mutually connected,
85 * much like the standard socketpair() function. However, the new file
86 * descriptors have the close-on-exec flag set atomically.
87 * See also vlc_socket().
88 *
89 * @param pf protocol family
90 * @param type socket type
91 * @param proto network protocol
92 * @param nonblock true to create non-blocking sockets
93 * @retval 0 on success
94 * @retval -1 on failure
95 */
96 VLC_API int vlc_socketpair(int pf, int type, int proto, int fds[2],
97 bool nonblock);
98
99 struct sockaddr;
100
101 /**
102 * Accepts an inbound connection request on a listening socket.
103 *
104 * This function creates a connected socket from a listening socket, much like
105 * the standard accept() function. However, the new file descriptor has the
106 * close-on-exec flag set atomically. See also vlc_socket().
107 *
108 * @param lfd listening socket file descriptor
109 * @param addr pointer to the peer address or NULL [OUT]
110 * @param alen pointer to the length of the peer address or NULL [OUT]
111 * @param nonblock whether to put the new socket in non-blocking mode
112 * @return a new file descriptor or -1 on error
113 */
114 VLC_API int vlc_accept(int lfd, struct sockaddr *addr, socklen_t *alen,
115 bool nonblock) VLC_USED;
116
117 # ifdef __cplusplus
118 extern "C" {
119 # endif
120
121 /* Portable networking layer communication */
122 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
123
124 VLC_API int net_Connect(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
125 #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e)
126
127 VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
128
129 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
130 SOCK_STREAM, IPPROTO_TCP)
131
net_ConnectTCP(vlc_object_t * obj,const char * host,int port)132 static inline int net_ConnectTCP (vlc_object_t *obj, const char *host, int port)
133 {
134 return net_Connect (obj, host, port, SOCK_STREAM, IPPROTO_TCP);
135 }
136 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
137
138 VLC_API int net_AcceptSingle(vlc_object_t *obj, int lfd);
139
140 VLC_API int net_Accept( vlc_object_t *, int * );
141 #define net_Accept(a, b) \
142 net_Accept(VLC_OBJECT(a), b)
143
144 VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port, int hlim, int proto );
145 #define net_ConnectDgram(a, b, c, d, e ) \
146 net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
147
net_ConnectUDP(vlc_object_t * obj,const char * host,int port,int hlim)148 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, int port, int hlim)
149 {
150 return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
151 }
152
153 VLC_API int net_OpenDgram( vlc_object_t *p_this, const char *psz_bind, int i_bind, const char *psz_server, int i_server, int proto );
154 #define net_OpenDgram( a, b, c, d, e, g ) \
155 net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g)
156
net_ListenUDP1(vlc_object_t * obj,const char * host,int port)157 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, int port)
158 {
159 return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
160 }
161
162 VLC_API void net_ListenClose( int *fd );
163
164 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
165 socklen_t addrlen);
166
167 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
168
169 VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data );
170 #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d)
171 VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data );
172 #define net_Write(a,b,c,d) net_Write(VLC_OBJECT(a),b,c,d)
173 VLC_API char * net_Gets( vlc_object_t *p_this, int fd );
174 #define net_Gets(a,b) net_Gets(VLC_OBJECT(a),b)
175
176
177 VLC_API ssize_t net_Printf( vlc_object_t *p_this, int fd, const char *psz_fmt, ... ) VLC_FORMAT( 3, 4 );
178 #define net_Printf(o,fd,...) net_Printf(VLC_OBJECT(o),fd, __VA_ARGS__)
179 VLC_API ssize_t net_vaPrintf( vlc_object_t *p_this, int fd, const char *psz_fmt, va_list args );
180 #define net_vaPrintf(a,b,c,d) net_vaPrintf(VLC_OBJECT(a),b,c,d)
181
182 VLC_API int vlc_close(int);
183
184 /** @} */
185
186 /* Portable network names/addresses resolution layer */
187
188 #define NI_MAXNUMERICHOST 64
189
190 #ifndef AI_NUMERICSERV
191 # define AI_NUMERICSERV 0
192 #endif
193 #ifndef AI_IDN
194 # define AI_IDN 0 /* GNU/libc extension */
195 #endif
196
197 #ifdef _WIN32
198 # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
199 # undef gai_strerror
200 # define gai_strerror gai_strerrorA
201 # endif
202 #endif
203
204 VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
205 VLC_API int vlc_getaddrinfo (const char *, unsigned,
206 const struct addrinfo *, struct addrinfo **);
207 VLC_API int vlc_getaddrinfo_i11e(const char *, unsigned,
208 const struct addrinfo *, struct addrinfo **);
209
210 static inline bool
net_SockAddrIsMulticast(const struct sockaddr * addr,socklen_t len)211 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
212 {
213 switch (addr->sa_family)
214 {
215 #ifdef IN_MULTICAST
216 case AF_INET:
217 {
218 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
219 if ((size_t)len < sizeof (*v4))
220 return false;
221 return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
222 }
223 #endif
224
225 #ifdef IN6_IS_ADDR_MULTICAST
226 case AF_INET6:
227 {
228 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
229 if ((size_t)len < sizeof (*v6))
230 return false;
231 return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
232 }
233 #endif
234 }
235
236 return false;
237 }
238
239
net_GetSockAddress(int fd,char * address,int * port)240 static inline int net_GetSockAddress( int fd, char *address, int *port )
241 {
242 struct sockaddr_storage addr;
243 socklen_t addrlen = sizeof( addr );
244
245 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
246 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
247 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
248 ? VLC_EGENERIC : 0;
249 }
250
net_GetPeerAddress(int fd,char * address,int * port)251 static inline int net_GetPeerAddress( int fd, char *address, int *port )
252 {
253 struct sockaddr_storage addr;
254 socklen_t addrlen = sizeof( addr );
255
256 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
257 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
258 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
259 ? VLC_EGENERIC : 0;
260 }
261
net_GetPort(const struct sockaddr * addr)262 static inline uint16_t net_GetPort (const struct sockaddr *addr)
263 {
264 switch (addr->sa_family)
265 {
266 #ifdef AF_INET6
267 case AF_INET6:
268 return ((const struct sockaddr_in6 *)addr)->sin6_port;
269 #endif
270 case AF_INET:
271 return ((const struct sockaddr_in *)addr)->sin_port;
272 }
273 return 0;
274 }
275
net_SetPort(struct sockaddr * addr,uint16_t port)276 static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
277 {
278 switch (addr->sa_family)
279 {
280 #ifdef AF_INET6
281 case AF_INET6:
282 ((struct sockaddr_in6 *)addr)->sin6_port = port;
283 break;
284 #endif
285 case AF_INET:
286 ((struct sockaddr_in *)addr)->sin_port = port;
287 break;
288 }
289 }
290
291 VLC_API char *vlc_getProxyUrl(const char *);
292
293 # ifdef __cplusplus
294 }
295 # endif
296
297 /** @} */
298
299 #endif
300