vout: win32: Remove screensaver inhnibition code
[vlc.git] / include / vlc_network.h
blob184c23acae9ddf2413b3df600523280a8f43975b
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$
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
10 * Rémi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
27 #ifndef VLC_NETWORK_H
28 # define VLC_NETWORK_H
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 * @{
40 #include <sys/types.h>
41 #include <unistd.h>
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
60 #ifndef MSG_NOSIGNAL
61 # define MSG_NOSIGNAL 0
62 #endif
64 /**
65 * Creates a socket file descriptor.
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.
71 * The non-blocking flag can also optionally be set.
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
79 VLC_API int vlc_socket(int pf, int type, int proto, bool nonblock) VLC_USED;
81 /**
82 * Creates a pair of socket file descriptors.
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().
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
96 VLC_API int vlc_socketpair(int pf, int type, int proto, int fds[2],
97 bool nonblock);
99 struct sockaddr;
102 * Accepts an inbound connection request on a listening socket.
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().
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
114 VLC_API int vlc_accept(int lfd, struct sockaddr *addr, socklen_t *alen,
115 bool nonblock) VLC_USED;
117 # ifdef __cplusplus
118 extern "C" {
119 # endif
121 /* Portable networking layer communication */
122 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
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)
127 VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
129 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
130 SOCK_STREAM, IPPROTO_TCP)
132 static inline int net_ConnectTCP (vlc_object_t *obj, const char *host, int port)
134 return net_Connect (obj, host, port, SOCK_STREAM, IPPROTO_TCP);
136 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
138 VLC_API int net_AcceptSingle(vlc_object_t *obj, int lfd);
140 VLC_API int net_Accept( vlc_object_t *, int * );
141 #define net_Accept(a, b) \
142 net_Accept(VLC_OBJECT(a), b)
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)
148 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, int port, int hlim)
150 return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
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)
157 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, int port)
159 return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
162 VLC_API void net_ListenClose( int *fd );
164 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
165 socklen_t addrlen);
167 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
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)
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)
182 VLC_API int vlc_close(int);
184 /** @} */
186 /* Portable network names/addresses resolution layer */
188 #define NI_MAXNUMERICHOST 64
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
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
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 **);
210 static inline bool
211 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
213 switch (addr->sa_family)
215 #ifdef IN_MULTICAST
216 case AF_INET:
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;
223 #endif
225 #ifdef IN6_IS_ADDR_MULTICAST
226 case AF_INET6:
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;
233 #endif
236 return false;
240 static inline int net_GetSockAddress( int fd, char *address, int *port )
242 struct sockaddr_storage addr;
243 socklen_t addrlen = sizeof( addr );
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;
251 static inline int net_GetPeerAddress( int fd, char *address, int *port )
253 struct sockaddr_storage addr;
254 socklen_t addrlen = sizeof( addr );
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;
262 static inline uint16_t net_GetPort (const struct sockaddr *addr)
264 switch (addr->sa_family)
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;
273 return 0;
276 static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
278 switch (addr->sa_family)
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;
291 VLC_API char *vlc_getProxyUrl(const char *);
293 # ifdef __cplusplus
295 # endif
297 /** @} */
299 #endif