VLC  4.0.0-dev
vlc_network.h
Go to the documentation of this file.
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 #else
52 # include <sys/socket.h>
53 # include <netinet/in.h>
54 # include <netdb.h>
55 # define net_errno errno
56 # define net_Close(fd) ((void)vlc_close(fd))
57 #endif
58 
59 /**
60  * Creates a socket file descriptor.
61  *
62  * This function creates a socket, similar to the standard socket() function.
63  * However, the new file descriptor has the close-on-exec flag set atomically,
64  * so as to avoid leaking the descriptor to child processes.
65  *
66  * The non-blocking flag can also optionally be set.
67  *
68  * @param pf protocol family
69  * @param type socket type
70  * @param proto network protocol
71  * @param nonblock true to create a non-blocking socket
72  * @return a new file descriptor or -1 on error
73  */
74 VLC_API int vlc_socket(int pf, int type, int proto, bool nonblock) VLC_USED;
75 
76 /**
77  * Creates a pair of socket file descriptors.
78  *
79  * This function creates a pair of sockets that are mutually connected,
80  * much like the standard socketpair() function. However, the new file
81  * descriptors have the close-on-exec flag set atomically.
82  * See also vlc_socket().
83  *
84  * @param pf protocol family
85  * @param type socket type
86  * @param proto network protocol
87  * @param nonblock true to create non-blocking sockets
88  * @retval 0 on success
89  * @retval -1 on failure
90  */
91 VLC_API int vlc_socketpair(int pf, int type, int proto, int fds[2],
92  bool nonblock);
93 
94 struct sockaddr;
95 
96 /**
97  * Accepts an inbound connection request on a listening socket.
98  *
99  * This function creates a connected socket from a listening socket, much like
100  * the standard accept() function. However, the new file descriptor has the
101  * close-on-exec flag set atomically. See also vlc_socket().
102  *
103  * @param lfd listening socket file descriptor
104  * @param addr pointer to the peer address or NULL [OUT]
105  * @param alen pointer to the length of the peer address or NULL [OUT]
106  * @param nonblock whether to put the new socket in non-blocking mode
107  * @return a new file descriptor or -1 on error
108  */
109 VLC_API int vlc_accept(int lfd, struct sockaddr *addr, socklen_t *alen,
110  bool nonblock) VLC_USED;
111 
112 /**
113  * Sends data.
114  *
115  * Like @c send(), this function sends raw data to the peer of a
116  * connection-mode socket, or to the predefined peer of a connection-less
117  * socket.
118  * Unlike @c send(), this function never triggers a signal; if the peer hung
119  * up, it returns an error.
120  *
121  * @param fd socket to send data through
122  * @param buf start address of data
123  * @param buflen byte size of data
124  * @param flags socket send flags (see @c send() documentation)
125  * @return number of bytes actually sent, or -1 on error (@c errno is set)
126  */
127 VLC_API ssize_t vlc_send(int fd, const void *buf, size_t buflen, int flags);
128 
129 /**
130  * Sends data to a peer.
131  *
132  * This function operates like @c sendto() with the exception that it never
133  * triggers a signal.
134  *
135  * This function mainly exists for the sakes of completeness and consistency:
136  * - To send data on a connection-mode socket, using \ref vlc_send() is
137  * simpler.
138  * - To send data on a connection-less socket, @c sendto() and/or @c send() can
139  * be used directly.
140  *
141  * @param fd socket to send data through
142  * @param buf start address of data
143  * @param buflen byte size of data
144  * @param flags socket send flags (see @c send() documentation)
145  * @param dst destination address (ignored for connection-mode sockets)
146  * @param dstlen byte size of destination address
147  * @return number of bytes actually sent, or -1 on error (@c errno is set)
148  */
149 VLC_API ssize_t vlc_sendto(int fd, const void *buf, size_t buflen, int flags,
150  const struct sockaddr *dst, socklen_t dstlen);
151 
152 /**
153  * Sends a socket message.
154  *
155  * Like @c sendmsg(), this function sends a message through a socket.
156  * Unlike @c sendmsg(), this function never triggers a signal; if the peer hung
157  * up, it returns an error.
158  *
159  * @param fd socket to send data through
160  * @param msg message to send (see @c sendmsg() documentation)
161  * @param flags socket send flags (see @c sendmsg() documentation)
162  * @return number of bytes actually sent, or -1 on error (@c errno is set)
163  */
164 VLC_API ssize_t vlc_sendmsg(int fd, const struct msghdr *msg, int flags);
165 
166 # ifdef __cplusplus
167 extern "C" {
168 # endif
169 
170 /* Portable networking layer communication */
171 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
172 
173 VLC_API int net_Connect(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
174 #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e)
175 
176 VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, unsigned i_port, int socktype, int protocol);
177 
178 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
179  SOCK_STREAM, IPPROTO_TCP)
180 
181 VLC_API int net_ConnectTCP (vlc_object_t *obj, const char *host, int port);
182 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
183 
184 /**
185  * Accepts an new connection on a set of listening sockets.
186  *
187  * If there are no pending connections, this function will wait.
188  *
189  * @note If the thread needs to handle events other than incoming connections,
190  * you need to use poll() and net_AcceptSingle() instead.
191  *
192  * @deprecated This function exists for backward compatibility.
193  * Use vlc_accept() or vlc_accept_i11e() in new code.
194  *
195  * @param obj VLC object for logging and object kill signal
196  * @param fds listening socket set
197  * @return -1 on error (may be transient error due to network issues),
198  * a new socket descriptor on success.
199  */
200 VLC_API int net_Accept(vlc_object_t *obj, int *fds);
201 #define net_Accept(a, b) \
202  net_Accept(VLC_OBJECT(a), b)
203 
204 VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, unsigned i_port, int hlim, int proto );
205 #define net_ConnectDgram(a, b, c, d, e ) \
206  net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
207 
208 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, unsigned port, int hlim)
209 {
210  return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
211 }
212 
213 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 );
214 #define net_OpenDgram( a, b, c, d, e, g ) \
215  net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g)
216 
217 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, unsigned port)
218 {
219  return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
220 }
221 
222 VLC_API void net_ListenClose( int *fd );
223 
224 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
225 
226 /**
227  * Reads data from a socket.
228  *
229  * This blocks until all requested data is received
230  * or the end of the stream is reached.
231  *
232  * This function is a cancellation point.
233  * @return -1 on error, or the number of bytes of read.
234  */
235 VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data );
236 #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d)
237 
238 /**
239  * Writes data to a socket.
240  *
241  * This blocks until all data is written or an error occurs.
242  *
243  * This function is a cancellation point.
244  *
245  * @return the total number of bytes written, or -1 if an error occurs
246  * before any data is written.
247  */
248 VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data );
249 #define net_Write(a,b,c,d) net_Write(VLC_OBJECT(a),b,c,d)
250 
251 VLC_API int vlc_close(int);
252 
253 /** @} */
254 
255 #ifdef _WIN32
256 static inline int vlc_getsockopt(int s, int level, int name,
257  void *val, socklen_t *len)
258 {
259  return getsockopt(s, level, name, (char *)val, len);
260 }
261 #define getsockopt vlc_getsockopt
262 
263 static inline int vlc_setsockopt(int s, int level, int name,
264  const void *val, socklen_t len)
265 {
266  return setsockopt(s, level, name, (const char *)val, len);
267 }
268 #define setsockopt vlc_setsockopt
269 #endif
270 
271 /* Portable network names/addresses resolution layer */
272 
273 #define NI_MAXNUMERICHOST 64
274 
275 #ifndef AI_NUMERICSERV
276 # define AI_NUMERICSERV 0
277 #endif
278 #ifndef AI_IDN
279 # define AI_IDN 0 /* GNU/libc extension */
280 #endif
281 
282 #ifdef _WIN32
283 # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
284 # undef gai_strerror
285 # define gai_strerror gai_strerrorA
286 # endif
287 #endif
288 
289 VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
290 VLC_API int vlc_getaddrinfo (const char *, unsigned,
291  const struct addrinfo *, struct addrinfo **);
292 VLC_API int vlc_getaddrinfo_i11e(const char *, unsigned,
293  const struct addrinfo *, struct addrinfo **);
294 
295 static inline bool
296 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
297 {
298  switch (addr->sa_family)
299  {
300 #ifdef IN_MULTICAST
301  case AF_INET:
302  {
303  const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
304  if ((size_t)len < sizeof (*v4))
305  return false;
306  return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
307  }
308 #endif
309 
310 #ifdef IN6_IS_ADDR_MULTICAST
311  case AF_INET6:
312  {
313  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
314  if ((size_t)len < sizeof (*v6))
315  return false;
316  return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
317  }
318 #endif
319  }
320 
321  return false;
322 }
323 
324 
325 static inline int net_GetSockAddress( int fd, char *address, int *port )
326 {
327  struct sockaddr_storage addr;
328  socklen_t addrlen = sizeof( addr );
329 
330  return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
331  || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
332  NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
333  ? VLC_EGENERIC : 0;
334 }
335 
336 static inline int net_GetPeerAddress( int fd, char *address, int *port )
337 {
338  struct sockaddr_storage addr;
339  socklen_t addrlen = sizeof( addr );
340 
341  return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
342  || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
343  NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
344  ? VLC_EGENERIC : 0;
345 }
346 
347 VLC_API char *vlc_getProxyUrl(const char *);
348 
349 # ifdef __cplusplus
350 }
351 # endif
352 
353 /** @} */
354 
355 #endif
vlc_sendto
VLC_EXPORT ssize_t vlc_sendto(int fd, const void *buf, size_t buflen, int flags, const struct sockaddr *dst, socklen_t dstlen)
Sends data to a peer.
Definition: filesystem.c:368
VLC_API
#define VLC_API
Definition: fourcc_gen.c:31
vlc_getaddrinfo
VLC_EXPORT int vlc_getaddrinfo(const char *, unsigned, const struct addrinfo *, struct addrinfo **)
Resolves a host name to a list of socket addresses (like getaddrinfo()).
Definition: getaddrinfo.c:77
vlc_common.h
vlc_sendmsg
VLC_EXPORT ssize_t vlc_sendmsg(int fd, const struct msghdr *msg, int flags)
Sends a socket message.
Definition: filesystem.c:374
net_SockAddrIsMulticast
static bool net_SockAddrIsMulticast(const struct sockaddr *addr, socklen_t len)
Definition: vlc_network.h:297
net_Connect
#define net_Connect(a, b, c, d, e)
Definition: vlc_network.h:175
vlc_socketpair
VLC_EXPORT int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
Creates a pair of socket file descriptors.
Definition: filesystem.c:335
vlc_getProxyUrl
VLC_EXPORT char * vlc_getProxyUrl(const char *)
Determines the network proxy server to use (if any).
Definition: specific.c:342
VLC_EGENERIC
#define VLC_EGENERIC
Unspecified error.
Definition: vlc_common.h:474
net_ListenUDP1
static int net_ListenUDP1(vlc_object_t *obj, const char *host, unsigned port)
Definition: vlc_network.h:218
net_Socket
int net_Socket(vlc_object_t *obj, int family, int socktype, int proto)
Definition: io.c:65
net_OpenDgram
#define net_OpenDgram(a, b, c, d, e, g)
Definition: vlc_network.h:215
vlc_getaddrinfo_i11e
VLC_EXPORT int vlc_getaddrinfo_i11e(const char *, unsigned, const struct addrinfo *, struct addrinfo **)
Definition: getaddrinfo.c:39
net_ListenClose
VLC_EXPORT void net_ListenClose(int *fd)
Definition: io.c:296
vlc_socket
VLC_EXPORT int vlc_socket(int pf, int type, int proto, bool nonblock)
Creates a socket file descriptor.
Definition: filesystem.c:327
vlc_close
VLC_EXPORT int vlc_close(int)
Closes a file descriptor.
Definition: filesystem.c:91
net_ConnectTCP
#define net_ConnectTCP(a, b, c)
Definition: vlc_network.h:183
vlc_object_t
VLC object common members.
Definition: vlc_objects.h:43
net_ConnectDgram
#define net_ConnectDgram(a, b, c, d, e)
Definition: vlc_network.h:206
VLC_USED
#define VLC_USED
Definition: fourcc_gen.c:32
net_GetSockAddress
static int net_GetSockAddress(int fd, char *address, int *port)
Definition: vlc_network.h:326
name
const char name[16]
Definition: httpd.c:1268
net_SetCSCov
VLC_EXPORT int net_SetCSCov(int fd, int sendcov, int recvcov)
net_SetCSCov: Sets the send and receive checksum coverage of a socket:
Definition: udp.c:692
net_Read
#define net_Read(a, b, c, d)
Definition: vlc_network.h:237
net_GetPeerAddress
static int net_GetPeerAddress(int fd, char *address, int *port)
Definition: vlc_network.h:337
net_Write
#define net_Write(a, b, c, d)
Definition: vlc_network.h:250
net_Accept
#define net_Accept(a, b)
Definition: vlc_network.h:202
vlc_accept
VLC_EXPORT int vlc_accept(int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
Accepts an inbound connection request on a listening socket.
Definition: filesystem.c:345
net_Listen
VLC_EXPORT int * net_Listen(vlc_object_t *p_this, const char *psz_host, unsigned i_port, int socktype, int protocol)
Definition: io.c:214
net_ConnectUDP
static int net_ConnectUDP(vlc_object_t *obj, const char *host, unsigned port, int hlim)
Definition: vlc_network.h:209
vlc_getnameinfo
VLC_EXPORT int vlc_getnameinfo(const struct sockaddr *, int, char *, int, int *, int)
Definition: getaddrinfo.c:39
NI_MAXNUMERICHOST
#define NI_MAXNUMERICHOST
Definition: vlc_network.h:274
vlc_send
VLC_EXPORT ssize_t vlc_send(int fd, const void *buf, size_t buflen, int flags)
Sends data.
Definition: filesystem.c:363
i_port
uint16_t i_port
Definition: keystore.c:216