qml: restore the focus on the last album when navigating back to the album view
[vlc.git] / include / vlc_network.h
blob88dcfefbca339d1cb6acf0fda0dbe6d146567665
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
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
26 #ifndef VLC_NETWORK_H
27 # define VLC_NETWORK_H
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 * @{
39 #include <sys/types.h>
40 #include <unistd.h>
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
59 #ifndef MSG_NOSIGNAL
60 # define MSG_NOSIGNAL 0
61 #endif
63 /**
64 * Creates a socket file descriptor.
66 * This function creates a socket, similar to the standard socket() function.
67 * However, the new file descriptor has the close-on-exec flag set atomically,
68 * so as to avoid leaking the descriptor to child processes.
70 * The non-blocking flag can also optionally be set.
72 * @param pf protocol family
73 * @param type socket type
74 * @param proto network protocol
75 * @param nonblock true to create a non-blocking socket
76 * @return a new file descriptor or -1 on error
78 VLC_API int vlc_socket(int pf, int type, int proto, bool nonblock) VLC_USED;
80 /**
81 * Creates a pair of socket file descriptors.
83 * This function creates a pair of sockets that are mutually connected,
84 * much like the standard socketpair() function. However, the new file
85 * descriptors have the close-on-exec flag set atomically.
86 * See also vlc_socket().
88 * @param pf protocol family
89 * @param type socket type
90 * @param proto network protocol
91 * @param nonblock true to create non-blocking sockets
92 * @retval 0 on success
93 * @retval -1 on failure
95 VLC_API int vlc_socketpair(int pf, int type, int proto, int fds[2],
96 bool nonblock);
98 struct sockaddr;
101 * Accepts an inbound connection request on a listening socket.
103 * This function creates a connected socket from a listening socket, much like
104 * the standard accept() function. However, the new file descriptor has the
105 * close-on-exec flag set atomically. See also vlc_socket().
107 * @param lfd listening socket file descriptor
108 * @param addr pointer to the peer address or NULL [OUT]
109 * @param alen pointer to the length of the peer address or NULL [OUT]
110 * @param nonblock whether to put the new socket in non-blocking mode
111 * @return a new file descriptor or -1 on error
113 VLC_API int vlc_accept(int lfd, struct sockaddr *addr, socklen_t *alen,
114 bool nonblock) VLC_USED;
116 # ifdef __cplusplus
117 extern "C" {
118 # endif
120 /* Portable networking layer communication */
121 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
123 VLC_API int net_Connect(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
124 #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e)
126 VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, unsigned i_port, int socktype, int protocol);
128 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
129 SOCK_STREAM, IPPROTO_TCP)
131 VLC_API int net_ConnectTCP (vlc_object_t *obj, const char *host, int port);
132 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
135 * Accepts an new connection on a set of listening sockets.
137 * If there are no pending connections, this function will wait.
139 * @note If the thread needs to handle events other than incoming connections,
140 * you need to use poll() and net_AcceptSingle() instead.
142 * @deprecated This function exists for backward compatibility.
143 * Use vlc_accept() or vlc_accept_i11e() in new code.
145 * @param obj VLC object for logging and object kill signal
146 * @param fds listening socket set
147 * @return -1 on error (may be transient error due to network issues),
148 * a new socket descriptor on success.
150 VLC_API int net_Accept(vlc_object_t *obj, int *fds);
151 #define net_Accept(a, b) \
152 net_Accept(VLC_OBJECT(a), b)
154 VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, unsigned i_port, int hlim, int proto );
155 #define net_ConnectDgram(a, b, c, d, e ) \
156 net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
158 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, unsigned port, int hlim)
160 return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
163 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 );
164 #define net_OpenDgram( a, b, c, d, e, g ) \
165 net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g)
167 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, unsigned port)
169 return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
172 VLC_API void net_ListenClose( int *fd );
174 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
177 * Reads data from a socket.
179 * This blocks until all requested data is received
180 * or the end of the stream is reached.
182 * This function is a cancellation point.
183 * @return -1 on error, or the number of bytes of read.
185 VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data );
186 #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d)
189 * Writes data to a socket.
191 * This blocks until all data is written or an error occurs.
193 * This function is a cancellation point.
195 * @return the total number of bytes written, or -1 if an error occurs
196 * before any data is written.
198 VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data );
199 #define net_Write(a,b,c,d) net_Write(VLC_OBJECT(a),b,c,d)
201 VLC_API int vlc_close(int);
203 /** @} */
205 #ifdef _WIN32
206 static inline int vlc_getsockopt(int s, int level, int name,
207 void *val, socklen_t *len)
209 return getsockopt(s, level, name, (char *)val, len);
211 #define getsockopt vlc_getsockopt
213 static inline int vlc_setsockopt(int s, int level, int name,
214 const void *val, socklen_t len)
216 return setsockopt(s, level, name, (const char *)val, len);
218 #define setsockopt vlc_setsockopt
219 #endif
221 /* Portable network names/addresses resolution layer */
223 #define NI_MAXNUMERICHOST 64
225 #ifndef AI_NUMERICSERV
226 # define AI_NUMERICSERV 0
227 #endif
228 #ifndef AI_IDN
229 # define AI_IDN 0 /* GNU/libc extension */
230 #endif
232 #ifdef _WIN32
233 # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
234 # undef gai_strerror
235 # define gai_strerror gai_strerrorA
236 # endif
237 #endif
239 VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
240 VLC_API int vlc_getaddrinfo (const char *, unsigned,
241 const struct addrinfo *, struct addrinfo **);
242 VLC_API int vlc_getaddrinfo_i11e(const char *, unsigned,
243 const struct addrinfo *, struct addrinfo **);
245 static inline bool
246 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
248 switch (addr->sa_family)
250 #ifdef IN_MULTICAST
251 case AF_INET:
253 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
254 if ((size_t)len < sizeof (*v4))
255 return false;
256 return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
258 #endif
260 #ifdef IN6_IS_ADDR_MULTICAST
261 case AF_INET6:
263 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
264 if ((size_t)len < sizeof (*v6))
265 return false;
266 return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
268 #endif
271 return false;
275 static inline int net_GetSockAddress( int fd, char *address, int *port )
277 struct sockaddr_storage addr;
278 socklen_t addrlen = sizeof( addr );
280 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
281 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
282 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
283 ? VLC_EGENERIC : 0;
286 static inline int net_GetPeerAddress( int fd, char *address, int *port )
288 struct sockaddr_storage addr;
289 socklen_t addrlen = sizeof( addr );
291 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
292 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
293 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
294 ? VLC_EGENERIC : 0;
297 VLC_API char *vlc_getProxyUrl(const char *);
299 # ifdef __cplusplus
301 # endif
303 /** @} */
305 #endif