access: linsys: clear some warnings
[vlc.git] / include / vlc_network.h
blob37469c250f53cf864c45d6aa4666bfc54ad91cd2
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, unsigned 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 VLC_API int net_ConnectTCP (vlc_object_t *obj, const char *host, int port);
133 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
136 * Accepts an new connection on a set of listening sockets.
138 * If there are no pending connections, this function will wait.
140 * @note If the thread needs to handle events other than incoming connections,
141 * you need to use poll() and net_AcceptSingle() instead.
143 * @deprecated This function exists for backward compatibility.
144 * Use vlc_accept() or vlc_accept_i11e() in new code.
146 * @param obj VLC object for logging and object kill signal
147 * @param fds listening socket set
148 * @return -1 on error (may be transient error due to network issues),
149 * a new socket descriptor on success.
151 VLC_API int net_Accept(vlc_object_t *obj, int *fds);
152 #define net_Accept(a, b) \
153 net_Accept(VLC_OBJECT(a), b)
155 VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, unsigned i_port, int hlim, int proto );
156 #define net_ConnectDgram(a, b, c, d, e ) \
157 net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
159 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, unsigned port, int hlim)
161 return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
164 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 );
165 #define net_OpenDgram( a, b, c, d, e, g ) \
166 net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g)
168 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, unsigned port)
170 return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
173 VLC_API void net_ListenClose( int *fd );
175 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
178 * Reads data from a socket.
180 * This blocks until all requested data is received
181 * or the end of the stream is reached.
183 * This function is a cancellation point.
184 * @return -1 on error, or the number of bytes of read.
186 VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data );
187 #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d)
190 * Writes data to a socket.
192 * This blocks until all data is written or an error occurs.
194 * This function is a cancellation point.
196 * @return the total number of bytes written, or -1 if an error occurs
197 * before any data is written.
199 VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data );
200 #define net_Write(a,b,c,d) net_Write(VLC_OBJECT(a),b,c,d)
202 VLC_API int vlc_close(int);
204 /** @} */
206 /* Portable network names/addresses resolution layer */
208 #define NI_MAXNUMERICHOST 64
210 #ifndef AI_NUMERICSERV
211 # define AI_NUMERICSERV 0
212 #endif
213 #ifndef AI_IDN
214 # define AI_IDN 0 /* GNU/libc extension */
215 #endif
217 #ifdef _WIN32
218 # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
219 # undef gai_strerror
220 # define gai_strerror gai_strerrorA
221 # endif
222 #endif
224 VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
225 VLC_API int vlc_getaddrinfo (const char *, unsigned,
226 const struct addrinfo *, struct addrinfo **);
227 VLC_API int vlc_getaddrinfo_i11e(const char *, unsigned,
228 const struct addrinfo *, struct addrinfo **);
230 static inline bool
231 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
233 switch (addr->sa_family)
235 #ifdef IN_MULTICAST
236 case AF_INET:
238 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
239 if ((size_t)len < sizeof (*v4))
240 return false;
241 return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
243 #endif
245 #ifdef IN6_IS_ADDR_MULTICAST
246 case AF_INET6:
248 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
249 if ((size_t)len < sizeof (*v6))
250 return false;
251 return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
253 #endif
256 return false;
260 static inline int net_GetSockAddress( int fd, char *address, int *port )
262 struct sockaddr_storage addr;
263 socklen_t addrlen = sizeof( addr );
265 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
266 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
267 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
268 ? VLC_EGENERIC : 0;
271 static inline int net_GetPeerAddress( int fd, char *address, int *port )
273 struct sockaddr_storage addr;
274 socklen_t addrlen = sizeof( addr );
276 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
277 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
278 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
279 ? VLC_EGENERIC : 0;
282 VLC_API char *vlc_getProxyUrl(const char *);
284 # ifdef __cplusplus
286 # endif
288 /** @} */
290 #endif