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
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 *****************************************************************************/
28 # define VLC_NETWORK_H
32 * \defgroup net Networking
35 * Definitions for sockets and low-level networking
36 * \defgroup sockets Internet sockets
40 #include <sys/types.h>
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))
50 # define IPV6_V6ONLY 27
53 # include <sys/socket.h>
54 # include <netinet/in.h>
56 # define net_errno errno
57 # define net_Close(fd) ((void)vlc_close(fd))
61 # define MSG_NOSIGNAL 0
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
;
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],
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
;
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 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
, unsigned 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
, unsigned 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
, unsigned i_bind
, const char *psz_server
, unsigned 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
, unsigned 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
,
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);
186 /* Portable network names/addresses resolution layer */
188 #define NI_MAXNUMERICHOST 64
190 #ifndef AI_NUMERICSERV
191 # define AI_NUMERICSERV 0
194 # define AI_IDN 0 /* GNU/libc extension */
198 # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
200 # define gai_strerror gai_strerrorA
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
**);
211 net_SockAddrIsMulticast (const struct sockaddr
*addr
, socklen_t len
)
213 switch (addr
->sa_family
)
218 const struct sockaddr_in
*v4
= (const struct sockaddr_in
*)addr
;
219 if ((size_t)len
< sizeof (*v4
))
221 return IN_MULTICAST (ntohl (v4
->sin_addr
.s_addr
)) != 0;
225 #ifdef IN6_IS_ADDR_MULTICAST
228 const struct sockaddr_in6
*v6
= (const struct sockaddr_in6
*)addr
;
229 if ((size_t)len
< sizeof (*v6
))
231 return IN6_IS_ADDR_MULTICAST (&v6
->sin6_addr
) != 0;
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
)
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
)
262 static inline uint16_t net_GetPort (const struct sockaddr
*addr
)
264 switch (addr
->sa_family
)
268 return ((const struct sockaddr_in6
*)addr
)->sin6_port
;
271 return ((const struct sockaddr_in
*)addr
)->sin_port
;
276 static inline void net_SetPort (struct sockaddr
*addr
, uint16_t port
)
278 switch (addr
->sa_family
)
282 ((struct sockaddr_in6
*)addr
)->sin6_port
= port
;
286 ((struct sockaddr_in
*)addr
)->sin_port
= port
;
291 VLC_API
char *vlc_getProxyUrl(const char *);