VLC  3.0.21
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  * $Id$
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 
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 
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 
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 #ifdef _WIN32
187 static inline int vlc_getsockopt(int s, int level, int name,
188  void *val, socklen_t *len)
189 {
190  return getsockopt(s, level, name, (char *)val, len);
191 }
192 #define getsockopt vlc_getsockopt
193 
194 static inline int vlc_setsockopt(int s, int level, int name,
195  const void *val, socklen_t len)
196 {
197  return setsockopt(s, level, name, (const char *)val, len);
198 }
199 #define setsockopt vlc_setsockopt
200 #endif
201 
202 /* Portable network names/addresses resolution layer */
203 
204 #define NI_MAXNUMERICHOST 64
205 
206 #ifndef AI_NUMERICSERV
207 # define AI_NUMERICSERV 0
208 #endif
209 #ifndef AI_IDN
210 # define AI_IDN 0 /* GNU/libc extension */
211 #endif
212 
213 #ifdef _WIN32
214 # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
215 # undef gai_strerror
216 # define gai_strerror gai_strerrorA
217 # endif
218 #endif
219 
220 VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
221 VLC_API int vlc_getaddrinfo (const char *, unsigned,
222  const struct addrinfo *, struct addrinfo **);
223 VLC_API int vlc_getaddrinfo_i11e(const char *, unsigned,
224  const struct addrinfo *, struct addrinfo **);
225 
226 static inline bool
227 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
228 {
229  switch (addr->sa_family)
230  {
231 #ifdef IN_MULTICAST
232  case AF_INET:
233  {
234  const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
235  if ((size_t)len < sizeof (*v4))
236  return false;
237  return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
238  }
239 #endif
240 
241 #ifdef IN6_IS_ADDR_MULTICAST
242  case AF_INET6:
243  {
244  const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
245  if ((size_t)len < sizeof (*v6))
246  return false;
247  return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
248  }
249 #endif
250  }
251 
252  return false;
253 }
254 
255 
256 static inline int net_GetSockAddress( int fd, char *address, int *port )
257 {
258  struct sockaddr_storage addr;
259  socklen_t addrlen = sizeof( addr );
260 
261  return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
262  || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
263  NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
264  ? VLC_EGENERIC : 0;
265 }
266 
267 static inline int net_GetPeerAddress( int fd, char *address, int *port )
268 {
269  struct sockaddr_storage addr;
270  socklen_t addrlen = sizeof( addr );
271 
272  return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
273  || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
274  NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
275  ? VLC_EGENERIC : 0;
276 }
277 
278 static inline uint16_t net_GetPort (const struct sockaddr *addr)
279 {
280  switch (addr->sa_family)
281  {
282 #ifdef AF_INET6
283  case AF_INET6:
284  return ((const struct sockaddr_in6 *)addr)->sin6_port;
285 #endif
286  case AF_INET:
287  return ((const struct sockaddr_in *)addr)->sin_port;
288  }
289  return 0;
290 }
291 
292 static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
293 {
294  switch (addr->sa_family)
295  {
296 #ifdef AF_INET6
297  case AF_INET6:
298  ((struct sockaddr_in6 *)addr)->sin6_port = port;
299  break;
300 #endif
301  case AF_INET:
302  ((struct sockaddr_in *)addr)->sin_port = port;
303  break;
304  }
305 }
306 
307 VLC_API char *vlc_getProxyUrl(const char *);
308 
309 # ifdef __cplusplus
310 }
311 # endif
312 
313 /** @} */
314 
315 #endif
VLC_FORMAT
#define VLC_FORMAT(x, y)
Definition: vlc_common.h:100
net_SetCSCov
int net_SetCSCov(int fd, int sendcov, int recvcov)
net_SetCSCov: Sets the send and receive checksum coverage of a socket:
Definition: udp.c:684
VLC_API
#define VLC_API
Definition: fourcc_gen.c:30
vlc_getnameinfo
int vlc_getnameinfo(const struct sockaddr *, int, char *, int, int *, int)
Definition: getaddrinfo.c:39
net_Printf
#define net_Printf(o, fd,...)
Definition: vlc_network.h:178
vlc_common.h
net_Gets
#define net_Gets(a, b)
Definition: vlc_network.h:174
net_SockAddrIsMulticast
static bool net_SockAddrIsMulticast(const struct sockaddr *addr, socklen_t len)
Definition: vlc_network.h:227
net_vaPrintf
#define net_vaPrintf(a, b, c, d)
Definition: vlc_network.h:180
net_Connect
#define net_Connect(a, b, c, d, e)
Definition: vlc_network.h:125
net_Listen
int * net_Listen(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol)
Definition: io.c:104
vlc_getProxyUrl
char * vlc_getProxyUrl(const char *)
Determines the network proxy server to use (if any).
Definition: specific.c:315
VLC_EGENERIC
#define VLC_EGENERIC
Unspecified error.
Definition: vlc_common.h:354
vlc_getaddrinfo_i11e
int vlc_getaddrinfo_i11e(const char *, unsigned, const struct addrinfo *, struct addrinfo **)
Definition: getaddrinfo.c:38
net_ConnectUDP
static int net_ConnectUDP(vlc_object_t *obj, const char *host, int port, int hlim)
Definition: vlc_network.h:148
vlc_getaddrinfo
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
net_Subscribe
int net_Subscribe(vlc_object_t *obj, int fd, const struct sockaddr *addr, socklen_t addrlen)
Definition: udp.c:393
net_Socket
int net_Socket(vlc_object_t *obj, int family, int socktype, int proto)
Definition: io.c:53
net_GetPort
static uint16_t net_GetPort(const struct sockaddr *addr)
Definition: vlc_network.h:278
net_OpenDgram
#define net_OpenDgram(a, b, c, d, e, g)
Definition: vlc_network.h:154
net_ConnectTCP
#define net_ConnectTCP(a, b, c)
Definition: vlc_network.h:136
vlc_object_t
The main vlc_object_t structure.
Definition: vlc_objects.h:39
net_ConnectDgram
#define net_ConnectDgram(a, b, c, d, e)
Definition: vlc_network.h:145
VLC_USED
#define VLC_USED
Definition: fourcc_gen.c:31
net_GetSockAddress
static int net_GetSockAddress(int fd, char *address, int *port)
Definition: vlc_network.h:256
name
const char name[16]
Definition: httpd.c:1251
vlc_close
int vlc_close(int)
Closes a file descriptor.
Definition: filesystem.c:88
net_ListenUDP1
static int net_ListenUDP1(vlc_object_t *obj, const char *host, int port)
Definition: vlc_network.h:157
net_Read
#define net_Read(a, b, c, d)
Definition: vlc_network.h:170
net_GetPeerAddress
static int net_GetPeerAddress(int fd, char *address, int *port)
Definition: vlc_network.h:267
net_ListenClose
void net_ListenClose(int *fd)
Definition: tcp.c:527
net_Write
#define net_Write(a, b, c, d)
Definition: vlc_network.h:172
net_Accept
#define net_Accept(a, b)
Definition: vlc_network.h:141
vlc_socketpair
int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
Creates a pair of socket file descriptors.
Definition: filesystem.c:323
vlc_accept
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:333
NI_MAXNUMERICHOST
#define NI_MAXNUMERICHOST
Definition: vlc_network.h:204
net_SetPort
static void net_SetPort(struct sockaddr *addr, uint16_t port)
Definition: vlc_network.h:292
vlc_socket
int vlc_socket(int pf, int type, int proto, bool nonblock)
Creates a socket file descriptor.
Definition: filesystem.c:315
i_port
uint16_t i_port
Definition: keystore.c:210
net_AcceptSingle
int net_AcceptSingle(vlc_object_t *obj, int lfd)
Definition: tcp.c:239