/src/vlc/include/vlc_network.h
Line | Count | Source |
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 | | * |
7 | | * Authors: Christophe Massiot <massiot@via.ecp.fr> |
8 | | * Laurent Aimar <fenrir@via.ecp.fr> |
9 | | * Rémi Denis-Courmont |
10 | | * |
11 | | * This program is free software; you can redistribute it and/or modify it |
12 | | * under the terms of the GNU Lesser General Public License as published by |
13 | | * the Free Software Foundation; either version 2.1 of the License, or |
14 | | * (at your option) any later version. |
15 | | * |
16 | | * This program is distributed in the hope that it will be useful, |
17 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | | * GNU Lesser General Public License for more details. |
20 | | * |
21 | | * You should have received a copy of the GNU Lesser General Public License |
22 | | * along with this program; if not, write to the Free Software Foundation, |
23 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
24 | | *****************************************************************************/ |
25 | | |
26 | | #ifndef VLC_NETWORK_H |
27 | | # define VLC_NETWORK_H |
28 | | |
29 | | /** |
30 | | * \ingroup os |
31 | | * \defgroup net Networking |
32 | | * @{ |
33 | | * \file |
34 | | * Definitions for sockets and low-level networking |
35 | | * \defgroup sockets Internet sockets |
36 | | * @{ |
37 | | */ |
38 | | |
39 | | #include <sys/types.h> |
40 | | #include <unistd.h> |
41 | | |
42 | | #if defined( _WIN32 ) |
43 | | # define _NO_OLDNAMES 1 |
44 | | # include <winsock2.h> |
45 | | # include <ws2tcpip.h> |
46 | | # define net_errno (WSAGetLastError()) |
47 | | # define net_Close(fd) ((void)closesocket((SOCKET)fd)) |
48 | | # ifndef IPV6_V6ONLY |
49 | | # define IPV6_V6ONLY 27 |
50 | | # endif |
51 | | # if !defined(SHUT_RDWR) |
52 | | # define SHUT_RDWR (SD_BOTH) |
53 | | # define SHUT_WR (SD_SEND) |
54 | | # define SHUT_RD (SD_RECEIVE) |
55 | | # endif |
56 | | #else |
57 | | # include <sys/socket.h> |
58 | | # include <netinet/in.h> |
59 | | # include <netdb.h> |
60 | 0 | # define net_errno errno |
61 | 0 | # define net_Close(fd) ((void)vlc_close(fd)) |
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 fds the output array storing the file descriptor pair |
93 | | * @param nonblock true to create non-blocking sockets |
94 | | * @retval 0 on success |
95 | | * @retval -1 on failure |
96 | | */ |
97 | | VLC_API int vlc_socketpair(int pf, int type, int proto, int fds[2], |
98 | | bool nonblock); |
99 | | |
100 | | struct sockaddr; |
101 | | |
102 | | /** |
103 | | * Accepts an inbound connection request on a listening socket. |
104 | | * |
105 | | * This function creates a connected socket from a listening socket, much like |
106 | | * the standard accept() function. However, the new file descriptor has the |
107 | | * close-on-exec flag set atomically. See also vlc_socket(). |
108 | | * |
109 | | * @param lfd listening socket file descriptor |
110 | | * @param addr pointer to the peer address or NULL [OUT] |
111 | | * @param alen pointer to the length of the peer address or NULL [OUT] |
112 | | * @param nonblock whether to put the new socket in non-blocking mode |
113 | | * @return a new file descriptor or -1 on error |
114 | | */ |
115 | | VLC_API int vlc_accept(int lfd, struct sockaddr *addr, socklen_t *alen, |
116 | | bool nonblock) VLC_USED; |
117 | | |
118 | | /** |
119 | | * Sends data. |
120 | | * |
121 | | * Like @c send(), this function sends raw data to the peer of a |
122 | | * connection-mode socket, or to the predefined peer of a connection-less |
123 | | * socket. |
124 | | * Unlike @c send(), this function never triggers a signal; if the peer hung |
125 | | * up, it returns an error. |
126 | | * |
127 | | * @param fd socket to send data through |
128 | | * @param buf start address of data |
129 | | * @param buflen byte size of data |
130 | | * @param flags socket send flags (see @c send() documentation) |
131 | | * @return number of bytes actually sent, or -1 on error (@c errno is set) |
132 | | */ |
133 | | VLC_API ssize_t vlc_send(int fd, const void *buf, size_t buflen, int flags); |
134 | | |
135 | | /** |
136 | | * Sends data to a peer. |
137 | | * |
138 | | * This function operates like @c sendto() with the exception that it never |
139 | | * triggers a signal. |
140 | | * |
141 | | * This function mainly exists for the sakes of completeness and consistency: |
142 | | * - To send data on a connection-mode socket, using \ref vlc_send() is |
143 | | * simpler. |
144 | | * - To send data on a connection-less socket, @c sendto() and/or @c send() can |
145 | | * be used directly. |
146 | | * |
147 | | * @param fd socket to send data through |
148 | | * @param buf start address of data |
149 | | * @param buflen byte size of data |
150 | | * @param flags socket send flags (see @c send() documentation) |
151 | | * @param dst destination address (ignored for connection-mode sockets) |
152 | | * @param dstlen byte size of destination address |
153 | | * @return number of bytes actually sent, or -1 on error (@c errno is set) |
154 | | */ |
155 | | VLC_API ssize_t vlc_sendto(int fd, const void *buf, size_t buflen, int flags, |
156 | | const struct sockaddr *dst, socklen_t dstlen); |
157 | | |
158 | | /** |
159 | | * Sends a socket message. |
160 | | * |
161 | | * Like @c sendmsg(), this function sends a message through a socket. |
162 | | * Unlike @c sendmsg(), this function never triggers a signal; if the peer hung |
163 | | * up, it returns an error. |
164 | | * |
165 | | * @param fd socket to send data through |
166 | | * @param msg message to send (see @c sendmsg() documentation) |
167 | | * @param flags socket send flags (see @c sendmsg() documentation) |
168 | | * @return number of bytes actually sent, or -1 on error (@c errno is set) |
169 | | */ |
170 | | VLC_API ssize_t vlc_sendmsg(int fd, const struct msghdr *msg, int flags); |
171 | | |
172 | | # ifdef __cplusplus |
173 | | extern "C" { |
174 | | # endif |
175 | | |
176 | | /* Portable networking layer communication */ |
177 | | int net_Socket (vlc_object_t *obj, int family, int socktype, int proto); |
178 | | |
179 | | VLC_API int net_Connect(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol); |
180 | | #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e) |
181 | | |
182 | | VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, unsigned i_port, int socktype, int protocol); |
183 | | |
184 | | #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \ |
185 | | SOCK_STREAM, IPPROTO_TCP) |
186 | | |
187 | | /** |
188 | | * Accepts an new connection on a set of listening sockets. |
189 | | * |
190 | | * If there are no pending connections, this function will wait. |
191 | | * |
192 | | * @note If the thread needs to handle events other than incoming connections, |
193 | | * you need to use poll() and net_AcceptSingle() instead. |
194 | | * |
195 | | * @deprecated This function exists for backward compatibility. |
196 | | * Use vlc_accept() or vlc_accept_i11e() in new code. |
197 | | * |
198 | | * @param obj VLC object for logging and object kill signal |
199 | | * @param fds listening socket set |
200 | | * @return -1 on error (may be transient error due to network issues), |
201 | | * a new socket descriptor on success. |
202 | | */ |
203 | | VLC_API int net_Accept(vlc_object_t *obj, int *fds); |
204 | | #define net_Accept(a, b) \ |
205 | | net_Accept(VLC_OBJECT(a), b) |
206 | | |
207 | | VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, unsigned i_port, int hlim, int proto ); |
208 | | #define net_ConnectDgram(a, b, c, d, e ) \ |
209 | | net_ConnectDgram(VLC_OBJECT(a), b, c, d, e) |
210 | | |
211 | | static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, unsigned port, int hlim) |
212 | 0 | { |
213 | 0 | return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP); |
214 | 0 | } Unexecuted instantiation: sap.c:net_ConnectUDP Unexecuted instantiation: decoder.c:net_ConnectUDP Unexecuted instantiation: input.c:net_ConnectUDP Unexecuted instantiation: resource.c:net_ConnectUDP Unexecuted instantiation: udp.c:net_ConnectUDP Unexecuted instantiation: interrupt.c:net_ConnectUDP Unexecuted instantiation: filesystem.c:net_ConnectUDP Unexecuted instantiation: process.c:net_ConnectUDP Unexecuted instantiation: stream_output.c:net_ConnectUDP Unexecuted instantiation: es_out.c:net_ConnectUDP Unexecuted instantiation: getaddrinfo.c:net_ConnectUDP Unexecuted instantiation: io.c:net_ConnectUDP |
215 | | |
216 | | VLC_API int net_OpenDgram( vlc_object_t *p_this, const char *psz_bind, unsigned i_bind, const char *psz_server, unsigned i_server, int proto ); |
217 | | #define net_OpenDgram( a, b, c, d, e, g ) \ |
218 | 0 | net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g) |
219 | | |
220 | | static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, unsigned port) |
221 | 0 | { |
222 | 0 | return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP); |
223 | 0 | } Unexecuted instantiation: sap.c:net_ListenUDP1 Unexecuted instantiation: decoder.c:net_ListenUDP1 Unexecuted instantiation: input.c:net_ListenUDP1 Unexecuted instantiation: resource.c:net_ListenUDP1 Unexecuted instantiation: udp.c:net_ListenUDP1 Unexecuted instantiation: interrupt.c:net_ListenUDP1 Unexecuted instantiation: filesystem.c:net_ListenUDP1 Unexecuted instantiation: process.c:net_ListenUDP1 Unexecuted instantiation: stream_output.c:net_ListenUDP1 Unexecuted instantiation: es_out.c:net_ListenUDP1 Unexecuted instantiation: getaddrinfo.c:net_ListenUDP1 Unexecuted instantiation: io.c:net_ListenUDP1 |
224 | | |
225 | | VLC_API void net_ListenClose( int *fd ); |
226 | | |
227 | | VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov ); |
228 | | |
229 | | /** |
230 | | * Reads data from a socket. |
231 | | * |
232 | | * This blocks until all requested data is received |
233 | | * or the end of the stream is reached. |
234 | | * |
235 | | * This function is a cancellation point. |
236 | | * @return -1 on error, or the number of bytes of read. |
237 | | */ |
238 | | VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data ); |
239 | | #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d) |
240 | | |
241 | | /** |
242 | | * Writes data to a socket. |
243 | | * |
244 | | * This blocks until all data is written or an error occurs. |
245 | | * |
246 | | * This function is a cancellation point. |
247 | | * |
248 | | * @return the total number of bytes written, or -1 if an error occurs |
249 | | * before any data is written. |
250 | | */ |
251 | | VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data ); |
252 | | #define net_Write(a,b,c,d) net_Write(VLC_OBJECT(a),b,c,d) |
253 | | |
254 | | VLC_API int vlc_close(int); |
255 | | |
256 | | /** @} */ |
257 | | |
258 | | #ifdef _WIN32 |
259 | | static inline int vlc_getsockopt(int s, int level, int name, |
260 | | void *val, socklen_t *len) |
261 | | { |
262 | | return getsockopt(s, level, name, (char *)val, len); |
263 | | } |
264 | | #define getsockopt vlc_getsockopt |
265 | | |
266 | | static inline int vlc_setsockopt(int s, int level, int name, |
267 | | const void *val, socklen_t len) |
268 | | { |
269 | | return setsockopt(s, level, name, (const char *)val, len); |
270 | | } |
271 | | #define setsockopt vlc_setsockopt |
272 | | #endif |
273 | | |
274 | | /* Portable network names/addresses resolution layer */ |
275 | | |
276 | | #define NI_MAXNUMERICHOST 64 |
277 | | |
278 | | #ifndef AI_NUMERICSERV |
279 | | # define AI_NUMERICSERV 0 |
280 | | #endif |
281 | | #ifndef AI_IDN |
282 | | # define AI_IDN 0 /* GNU/libc extension */ |
283 | | #endif |
284 | | |
285 | | #ifdef _WIN32 |
286 | | # if defined(UNICODE) |
287 | | # undef gai_strerror |
288 | | # define gai_strerror gai_strerrorA |
289 | | # endif |
290 | | #endif |
291 | | |
292 | | /* union of the various sockaddr structures often used together */ |
293 | | typedef union { |
294 | | struct sockaddr sa; |
295 | | struct sockaddr_storage ss; |
296 | | struct sockaddr_in sin; |
297 | | #ifdef AF_INET6 |
298 | | struct sockaddr_in6 sin6; |
299 | | #endif |
300 | | } vlc_sockaddr; |
301 | | |
302 | | VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int ); |
303 | | |
304 | | /** |
305 | | * Resolves a host name to a list of socket addresses (like getaddrinfo()). |
306 | | * |
307 | | * @param node host name to resolve (encoded as UTF-8), or NULL |
308 | | * @param i_port port number for the socket addresses |
309 | | * @param p_hints parameters (see getaddrinfo() manual page) |
310 | | * @param res pointer set to the resulting chained list. |
311 | | * @return 0 on success, a getaddrinfo() error otherwise. |
312 | | * On failure, *res is undefined. On success, it must be freed with |
313 | | * freeaddrinfo(). |
314 | | */ |
315 | | VLC_API int vlc_getaddrinfo (const char *node, unsigned i_port, |
316 | | const struct addrinfo *p_hints, |
317 | | struct addrinfo **res); |
318 | | VLC_API int vlc_getaddrinfo_i11e(const char *, unsigned, |
319 | | const struct addrinfo *, struct addrinfo **); |
320 | | |
321 | | static inline bool |
322 | | net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len) |
323 | 0 | { |
324 | 0 | switch (addr->sa_family) |
325 | 0 | { |
326 | 0 | #ifdef IN_MULTICAST |
327 | 0 | case AF_INET: |
328 | 0 | { |
329 | 0 | const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
330 | 0 | if ((size_t)len < sizeof (*v4)) |
331 | 0 | return false; |
332 | 0 | return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0; |
333 | 0 | } |
334 | 0 | #endif |
335 | | |
336 | 0 | #ifdef IN6_IS_ADDR_MULTICAST |
337 | 0 | case AF_INET6: |
338 | 0 | { |
339 | 0 | const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
340 | 0 | if ((size_t)len < sizeof (*v6)) |
341 | 0 | return false; |
342 | 0 | return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0; |
343 | 0 | } |
344 | 0 | #endif |
345 | 0 | } |
346 | | |
347 | 0 | return false; |
348 | 0 | } Unexecuted instantiation: sap.c:net_SockAddrIsMulticast Unexecuted instantiation: decoder.c:net_SockAddrIsMulticast Unexecuted instantiation: input.c:net_SockAddrIsMulticast Unexecuted instantiation: resource.c:net_SockAddrIsMulticast Unexecuted instantiation: udp.c:net_SockAddrIsMulticast Unexecuted instantiation: interrupt.c:net_SockAddrIsMulticast Unexecuted instantiation: filesystem.c:net_SockAddrIsMulticast Unexecuted instantiation: process.c:net_SockAddrIsMulticast Unexecuted instantiation: stream_output.c:net_SockAddrIsMulticast Unexecuted instantiation: es_out.c:net_SockAddrIsMulticast Unexecuted instantiation: getaddrinfo.c:net_SockAddrIsMulticast Unexecuted instantiation: io.c:net_SockAddrIsMulticast |
349 | | |
350 | | |
351 | | static inline int net_GetSockAddress( int fd, char *address, int *port ) |
352 | 0 | { |
353 | 0 | vlc_sockaddr addr; |
354 | 0 | socklen_t addrlen = sizeof( addr.ss ); |
355 | 0 |
|
356 | 0 | return getsockname( fd, &addr.sa, &addrlen ) |
357 | 0 | || vlc_getnameinfo( &addr.sa, addrlen, address, |
358 | 0 | NI_MAXNUMERICHOST, port, NI_NUMERICHOST ) |
359 | 0 | ? VLC_EGENERIC : 0; |
360 | 0 | } Unexecuted instantiation: sap.c:net_GetSockAddress Unexecuted instantiation: decoder.c:net_GetSockAddress Unexecuted instantiation: input.c:net_GetSockAddress Unexecuted instantiation: resource.c:net_GetSockAddress Unexecuted instantiation: udp.c:net_GetSockAddress Unexecuted instantiation: interrupt.c:net_GetSockAddress Unexecuted instantiation: filesystem.c:net_GetSockAddress Unexecuted instantiation: process.c:net_GetSockAddress Unexecuted instantiation: stream_output.c:net_GetSockAddress Unexecuted instantiation: es_out.c:net_GetSockAddress Unexecuted instantiation: getaddrinfo.c:net_GetSockAddress Unexecuted instantiation: io.c:net_GetSockAddress |
361 | | |
362 | | static inline int net_GetPeerAddress( int fd, char *address, int *port ) |
363 | 0 | { |
364 | 0 | vlc_sockaddr addr; |
365 | 0 | socklen_t addrlen = sizeof( addr.ss ); |
366 | 0 |
|
367 | 0 | return getpeername( fd, &addr.sa, &addrlen ) |
368 | 0 | || vlc_getnameinfo( &addr.sa, addrlen, address, |
369 | 0 | NI_MAXNUMERICHOST, port, NI_NUMERICHOST ) |
370 | 0 | ? VLC_EGENERIC : 0; |
371 | 0 | } Unexecuted instantiation: sap.c:net_GetPeerAddress Unexecuted instantiation: decoder.c:net_GetPeerAddress Unexecuted instantiation: input.c:net_GetPeerAddress Unexecuted instantiation: resource.c:net_GetPeerAddress Unexecuted instantiation: udp.c:net_GetPeerAddress Unexecuted instantiation: interrupt.c:net_GetPeerAddress Unexecuted instantiation: filesystem.c:net_GetPeerAddress Unexecuted instantiation: process.c:net_GetPeerAddress Unexecuted instantiation: stream_output.c:net_GetPeerAddress Unexecuted instantiation: es_out.c:net_GetPeerAddress Unexecuted instantiation: getaddrinfo.c:net_GetPeerAddress Unexecuted instantiation: io.c:net_GetPeerAddress |
372 | | |
373 | | /** |
374 | | * Determines the network proxy server to use (if any). |
375 | | * @param url absolute URL for which to get the proxy server |
376 | | * @return proxy URL, NULL if no proxy or error |
377 | | */ |
378 | | VLC_API char *vlc_getProxyUrl(const char *); |
379 | | |
380 | | # ifdef __cplusplus |
381 | | } |
382 | | # endif |
383 | | |
384 | | /** @} */ |
385 | | |
386 | | #endif |