PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/include/vlc_network.h

https://gitlab.com/evilbinary/vlc
C Header | 348 lines | 254 code | 56 blank | 38 comment | 13 complexity | ec42ccb0d3527c6e12339a372634fb5d MD5 | raw 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: 8779ad29de4833d90b9bd34419c1c5c2ae2a1038 $
  7. *
  8. * Authors: Christophe Massiot <massiot@via.ecp.fr>
  9. * Laurent Aimar <fenrir@via.ecp.fr>
  10. * Rémi Denis-Courmont <rem # videolan.org>
  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. #ifndef VLC_NETWORK_H
  27. # define VLC_NETWORK_H
  28. /**
  29. * \ingroup file
  30. * \defgroup sockets Internet sockets
  31. * @{
  32. * \file
  33. * Definitions for sockets and low-level networking
  34. */
  35. #if defined( _WIN32 )
  36. # define _NO_OLDNAMES 1
  37. # include <io.h>
  38. # include <winsock2.h>
  39. # include <ws2tcpip.h>
  40. # define net_errno (WSAGetLastError())
  41. # ifndef IPV6_V6ONLY
  42. # define IPV6_V6ONLY 27
  43. # endif
  44. #else
  45. # include <sys/types.h>
  46. # include <unistd.h>
  47. # include <sys/socket.h>
  48. # include <netinet/in.h>
  49. # include <netdb.h>
  50. # define net_errno errno
  51. #endif
  52. #ifndef MSG_NOSIGNAL
  53. # define MSG_NOSIGNAL 0
  54. #endif
  55. VLC_API int vlc_socket (int, int, int, bool nonblock) VLC_USED;
  56. VLC_API int vlc_socketpair (int, int, int, int [2], bool nonblock);
  57. struct sockaddr;
  58. VLC_API int vlc_accept( int, struct sockaddr *, socklen_t *, bool ) VLC_USED;
  59. # ifdef __cplusplus
  60. extern "C" {
  61. # endif
  62. /* Portable networking layer communication */
  63. int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
  64. VLC_API int net_Connect(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
  65. #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e)
  66. VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
  67. #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
  68. SOCK_STREAM, IPPROTO_TCP)
  69. static inline int net_ConnectTCP (vlc_object_t *obj, const char *host, int port)
  70. {
  71. return net_Connect (obj, host, port, SOCK_STREAM, IPPROTO_TCP);
  72. }
  73. #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
  74. VLC_API int net_AcceptSingle(vlc_object_t *obj, int lfd);
  75. VLC_API int net_Accept( vlc_object_t *, int * );
  76. #define net_Accept(a, b) \
  77. net_Accept(VLC_OBJECT(a), b)
  78. VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port, int hlim, int proto );
  79. #define net_ConnectDgram(a, b, c, d, e ) \
  80. net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
  81. static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, int port, int hlim)
  82. {
  83. return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
  84. }
  85. 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 );
  86. #define net_OpenDgram( a, b, c, d, e, g ) \
  87. net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g)
  88. static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, int port)
  89. {
  90. return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
  91. }
  92. VLC_API void net_ListenClose( int *fd );
  93. int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
  94. socklen_t addrlen);
  95. VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
  96. VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data );
  97. #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d)
  98. VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data );
  99. #define net_Write(a,b,c,d) net_Write(VLC_OBJECT(a),b,c,d)
  100. VLC_API char * net_Gets( vlc_object_t *p_this, int fd );
  101. #define net_Gets(a,b) net_Gets(VLC_OBJECT(a),b)
  102. VLC_API ssize_t net_Printf( vlc_object_t *p_this, int fd, const char *psz_fmt, ... ) VLC_FORMAT( 3, 4 );
  103. #define net_Printf(o,fd,...) net_Printf(VLC_OBJECT(o),fd, __VA_ARGS__)
  104. VLC_API ssize_t net_vaPrintf( vlc_object_t *p_this, int fd, const char *psz_fmt, va_list args );
  105. #define net_vaPrintf(a,b,c,d) net_vaPrintf(VLC_OBJECT(a),b,c,d)
  106. #ifdef _WIN32
  107. /* Microsoft: same semantic, same value, different name... go figure */
  108. # define SHUT_RD SD_RECEIVE
  109. # define SHUT_WR SD_SEND
  110. # define SHUT_RDWR SD_BOTH
  111. # define net_Close( fd ) closesocket ((SOCKET)fd)
  112. #else
  113. # ifdef __OS2__
  114. # define SHUT_RD 0
  115. # define SHUT_WR 1
  116. # define SHUT_RDWR 2
  117. # endif
  118. VLC_API int vlc_close(int);
  119. # define net_Close( fd ) (void)vlc_close (fd)
  120. #endif
  121. /* Portable network names/addresses resolution layer */
  122. /* GAI error codes */
  123. # ifndef EAI_BADFLAGS
  124. # define EAI_BADFLAGS -1
  125. # endif
  126. # ifndef EAI_NONAME
  127. # define EAI_NONAME -2
  128. # endif
  129. # ifndef EAI_AGAIN
  130. # define EAI_AGAIN -3
  131. # endif
  132. # ifndef EAI_FAIL
  133. # define EAI_FAIL -4
  134. # endif
  135. # ifndef EAI_NODATA
  136. # define EAI_NODATA -5
  137. # endif
  138. # ifndef EAI_FAMILY
  139. # define EAI_FAMILY -6
  140. # endif
  141. # ifndef EAI_SOCKTYPE
  142. # define EAI_SOCKTYPE -7
  143. # endif
  144. # ifndef EAI_SERVICE
  145. # define EAI_SERVICE -8
  146. # endif
  147. # ifndef EAI_ADDRFAMILY
  148. # define EAI_ADDRFAMILY -9
  149. # endif
  150. # ifndef EAI_MEMORY
  151. # define EAI_MEMORY -10
  152. # endif
  153. #ifndef EAI_OVERFLOW
  154. # define EAI_OVERFLOW -11
  155. #endif
  156. # ifndef EAI_SYSTEM
  157. # define EAI_SYSTEM -12
  158. # endif
  159. # ifndef NI_MAXHOST
  160. # define NI_MAXHOST 1025
  161. # define NI_MAXSERV 32
  162. # endif
  163. # define NI_MAXNUMERICHOST 64
  164. #ifndef AI_NUMERICSERV
  165. # define AI_NUMERICSERV 0
  166. #endif
  167. #ifndef AI_IDN
  168. # define AI_IDN 0 /* GNU/libc extension */
  169. #endif
  170. #ifdef _WIN32
  171. # if !defined(WINAPI_FAMILY) || WINAPI_FAMILY != WINAPI_FAMILY_APP
  172. # undef gai_strerror
  173. # define gai_strerror gai_strerrorA
  174. # endif
  175. #endif
  176. #ifdef __OS2__
  177. # ifndef NI_NUMERICHOST
  178. # define NI_NUMERICHOST 0x01
  179. # define NI_NUMERICSERV 0x02
  180. # define NI_NOFQDN 0x04
  181. # define NI_NAMEREQD 0x08
  182. # define NI_DGRAM 0x10
  183. # endif
  184. # define AI_PASSIVE 1
  185. # define AI_CANONNAME 2
  186. # define AI_NUMERICHOST 4
  187. VLC_API const char *gai_strerror( int errnum );
  188. VLC_API int getaddrinfo ( const char *, const char *,
  189. const struct addrinfo *, struct addrinfo ** );
  190. VLC_API void freeaddrinfo( struct addrinfo * );
  191. VLC_API int getnameinfo ( const struct sockaddr *, socklen_t,
  192. char *, int, char *, int, int );
  193. #endif
  194. VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
  195. VLC_API int vlc_getaddrinfo (const char *, unsigned,
  196. const struct addrinfo *, struct addrinfo **);
  197. #ifdef __OS2__
  198. /* OS/2 does not support IPv6, yet. But declare these only for compilation */
  199. struct in6_addr
  200. {
  201. uint8_t s6_addr[16];
  202. };
  203. struct sockaddr_in6
  204. {
  205. uint8_t sin6_len;
  206. uint8_t sin6_family;
  207. uint16_t sin6_port;
  208. uint32_t sin6_flowinfo;
  209. struct in6_addr sin6_addr;
  210. uint32_t sin6_scope_id;
  211. };
  212. # define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
  213. static const struct in6_addr in6addr_any =
  214. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
  215. #endif
  216. static inline bool
  217. net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
  218. {
  219. switch (addr->sa_family)
  220. {
  221. #ifdef IN_MULTICAST
  222. case AF_INET:
  223. {
  224. const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
  225. if ((size_t)len < sizeof (*v4))
  226. return false;
  227. return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
  228. }
  229. #endif
  230. #ifdef IN6_IS_ADDR_MULTICAST
  231. case AF_INET6:
  232. {
  233. const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
  234. if ((size_t)len < sizeof (*v6))
  235. return false;
  236. return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
  237. }
  238. #endif
  239. }
  240. return false;
  241. }
  242. static inline int net_GetSockAddress( int fd, char *address, int *port )
  243. {
  244. struct sockaddr_storage addr;
  245. socklen_t addrlen = sizeof( addr );
  246. return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
  247. || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
  248. NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
  249. ? VLC_EGENERIC : 0;
  250. }
  251. static inline int net_GetPeerAddress( int fd, char *address, int *port )
  252. {
  253. struct sockaddr_storage addr;
  254. socklen_t addrlen = sizeof( addr );
  255. return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
  256. || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
  257. NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
  258. ? VLC_EGENERIC : 0;
  259. }
  260. static inline uint16_t net_GetPort (const struct sockaddr *addr)
  261. {
  262. switch (addr->sa_family)
  263. {
  264. #ifdef AF_INET6
  265. case AF_INET6:
  266. return ((const struct sockaddr_in6 *)addr)->sin6_port;
  267. #endif
  268. case AF_INET:
  269. return ((const struct sockaddr_in *)addr)->sin_port;
  270. }
  271. return 0;
  272. }
  273. static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
  274. {
  275. switch (addr->sa_family)
  276. {
  277. #ifdef AF_INET6
  278. case AF_INET6:
  279. ((struct sockaddr_in6 *)addr)->sin6_port = port;
  280. break;
  281. #endif
  282. case AF_INET:
  283. ((struct sockaddr_in *)addr)->sin_port = port;
  284. break;
  285. }
  286. }
  287. VLC_API char *vlc_getProxyUrl(const char *);
  288. # ifdef __cplusplus
  289. }
  290. # endif
  291. /** @} */
  292. #endif