vlc_network.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_network.h: interface to communicate with network plug-ins
00003  *****************************************************************************
00004  * Copyright (C) 2002-2005 the VideoLAN team
00005  * Copyright © 2006-2007 Rémi Denis-Courmont
00006  * $Id$
00007  *
00008  * Authors: Christophe Massiot <massiot@via.ecp.fr>
00009  *          Laurent Aimar <fenrir@via.ecp.fr>
00010  *          Rémi Denis-Courmont <rem # videolan.org>
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00025  *****************************************************************************/
00026 
00027 #ifndef VLC_NETWORK_H
00028 # define VLC_NETWORK_H
00029 
00030 #if defined( WIN32 )
00031 #   if !defined(UNDER_CE)
00032 #       define _NO_OLDNAMES 1
00033 #       include <io.h>
00034 #   endif
00035 #   include <winsock2.h>
00036 #   include <ws2tcpip.h>
00037 #   define ENETUNREACH WSAENETUNREACH
00038 #   define net_errno (WSAGetLastError())
00039 extern const char *net_strerror( int val );
00040 
00041 struct iovec
00042 {
00043     void  *iov_base;
00044     size_t iov_len;
00045 };
00046 
00047 struct msghdr
00048 {
00049     void         *msg_name;
00050     size_t        msg_namelen;
00051     struct iovec *msg_iov;
00052     size_t        msg_iovlen;
00053     void         *msg_control;
00054     size_t        msg_controllen;
00055     int           msg_flags;
00056 };
00057 
00058 #define sendmsg vlc_sendmsg
00059 #define recvmsg vlc_recvmsg
00060 
00061 #   ifndef IPV6_V6ONLY
00062 #       define IPV6_V6ONLY 27
00063 #   endif
00064 #else
00065 #   include <sys/socket.h>
00066 #   ifdef HAVE_NETINET_IN_H
00067 #      include <netinet/in.h>
00068 #   endif
00069 #   ifdef HAVE_ARPA_INET_H
00070 #      include <arpa/inet.h>
00071 #   elif defined( SYS_BEOS )
00072 #      include <net/netdb.h>
00073 #   endif
00074 #   include <netdb.h>
00075 #   define net_errno errno
00076 #endif
00077 
00078 VLC_EXPORT( ssize_t, vlc_sendmsg, ( int, struct msghdr *, int ) );
00079 VLC_EXPORT( ssize_t, vlc_recvmsg, ( int, struct msghdr *, int ) );
00080 
00081 # ifdef __cplusplus
00082 extern "C" {
00083 # endif
00084 
00085 /* Portable networking layer communication */
00086 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
00087 int net_SetupSocket (int fd);
00088 
00089 #define net_Connect(a, b, c, d, e) __net_Connect(VLC_OBJECT(a), b, c, d, e)
00090 VLC_EXPORT( int, __net_Connect, (vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol) );
00091 
00092 VLC_EXPORT( int *, net_Listen, (vlc_object_t *p_this, const char *psz_host, int i_port, int protocol) );
00093 
00094 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, IPPROTO_TCP)
00095 #define net_ConnectTCP(a, b, c) __net_ConnectTCP(VLC_OBJECT(a), b, c)
00096 
00097 static inline int __net_ConnectTCP (vlc_object_t *obj, const char *host, int port)
00098 {
00099     return __net_Connect (obj, host, port, SOCK_STREAM, IPPROTO_TCP);
00100 }
00101 
00102 
00103 VLC_EXPORT( int, net_AcceptSingle, (vlc_object_t *obj, int lfd) );
00104 
00105 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
00106 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
00107 
00108 #define net_ConnectDgram(a, b, c, d, e ) __net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
00109 VLC_EXPORT( int, __net_ConnectDgram, ( vlc_object_t *p_this, const char *psz_host, int i_port, int hlim, int proto ) );
00110 
00111 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, int port, int hlim)
00112 {
00113     return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
00114 }
00115 
00116 #define net_OpenDgram( a, b, c, d, e, g, h ) __net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g, h)
00117 VLC_EXPORT( int, __net_OpenDgram, ( vlc_object_t *p_this, const char *psz_bind, int i_bind, const char *psz_server, int i_server, int family, int proto ) );
00118 
00119 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, int port)
00120 {
00121     return net_OpenDgram (obj, host, port, NULL, 0, 0, IPPROTO_UDP);
00122 }
00123 
00124 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
00125 
00126 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
00127                    socklen_t addrlen);
00128 
00129 VLC_EXPORT( int, net_SetCSCov, ( int fd, int sendcov, int recvcov ) );
00130 
00131 /* Functions to read from or write to the networking layer */
00132 struct virtual_socket_t
00133 {
00134     void *p_sys;
00135     int (*pf_recv) ( void *, void *, int );
00136     int (*pf_send) ( void *, const void *, int );
00137 };
00138 
00139 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
00140 VLC_EXPORT( ssize_t, __net_Read, ( vlc_object_t *p_this, int fd, const v_socket_t *, uint8_t *p_data, size_t i_data, bool b_retry ) );
00141 
00142 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
00143 VLC_EXPORT( ssize_t, __net_Write, ( vlc_object_t *p_this, int fd, const v_socket_t *, const uint8_t *p_data, size_t i_data ) );
00144 
00145 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
00146 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, const v_socket_t * ) );
00147 
00148 VLC_EXPORT( ssize_t, net_Printf, ( vlc_object_t *p_this, int fd, const v_socket_t *, const char *psz_fmt, ... ) LIBVLC_FORMAT( 4, 5 ) );
00149 
00150 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
00151 VLC_EXPORT( ssize_t, __net_vaPrintf, ( vlc_object_t *p_this, int fd, const v_socket_t *, const char *psz_fmt, va_list args ) );
00152 
00153 
00154 /* Don't go to an extra call layer if we have the symbol */
00155 #ifndef HAVE_INET_PTON
00156 #define inet_pton vlc_inet_pton
00157 #endif
00158 #ifndef HAVE_INET_NTOP
00159 #define inet_ntop vlc_inet_ntop
00160 #endif
00161 
00162 VLC_EXPORT (int, vlc_inet_pton, (int af, const char *src, void *dst) );
00163 VLC_EXPORT (const char *, vlc_inet_ntop, (int af, const void *src,
00164                                           char *dst, socklen_t cnt) );
00165 
00166 #ifndef HAVE_POLL
00167 enum
00168 {
00169     POLLIN=1,
00170     POLLOUT=2,
00171     POLLPRI=4,
00172     POLLERR=8,  // unsupported stub
00173     POLLHUP=16, // unsupported stub
00174     POLLNVAL=32 // unsupported stub
00175 };
00176 
00177 struct pollfd
00178 {
00179     int fd;
00180     int events;
00181     int revents;
00182 };
00183 # define poll(a, b, c) vlc_poll(a, b, c)
00184 #endif
00185 struct pollfd;
00186 VLC_EXPORT (int, vlc_poll, (struct pollfd *fds, unsigned nfds, int timeout));
00187 
00188 
00189 #ifdef WIN32
00190 /* Microsoft: same semantic, same value, different name... go figure */
00191 # define SHUT_RD SD_RECEIVE
00192 # define SHUT_WR SD_SEND
00193 # define SHUT_RDWR SD_BOTH
00194 # define net_Close( fd ) closesocket ((SOCKET)fd)
00195 #else
00196 #ifdef HAVE_UNISTD_H
00197 #include <unistd.h>
00198 #endif
00199 # define net_Close( fd ) (void)close (fd)
00200 #endif
00201 
00202 /* Portable network names/addresses resolution layer */
00203 
00204 /* GAI error codes */
00205 # ifndef EAI_BADFLAGS
00206 #  define EAI_BADFLAGS -1
00207 # endif
00208 # ifndef EAI_NONAME
00209 #  define EAI_NONAME -2
00210 # endif
00211 # ifndef EAI_AGAIN
00212 #  define EAI_AGAIN -3
00213 # endif
00214 # ifndef EAI_FAIL
00215 #  define EAI_FAIL -4
00216 # endif
00217 # ifndef EAI_NODATA
00218 #  define EAI_NODATA -5
00219 # endif
00220 # ifndef EAI_FAMILY
00221 #  define EAI_FAMILY -6
00222 # endif
00223 # ifndef EAI_SOCKTYPE
00224 #  define EAI_SOCKTYPE -7
00225 # endif
00226 # ifndef EAI_SERVICE
00227 #  define EAI_SERVICE -8
00228 # endif
00229 # ifndef EAI_ADDRFAMILY
00230 #  define EAI_ADDRFAMILY -9
00231 # endif
00232 # ifndef EAI_MEMORY
00233 #  define EAI_MEMORY -10
00234 # endif
00235 #ifndef EAI_OVERFLOW
00236 #  define EAI_OVERFLOW -11
00237 #endif
00238 # ifndef EAI_SYSTEM
00239 #  define EAI_SYSTEM -12
00240 # endif
00241 
00242 
00243 # ifndef NI_MAXHOST
00244 #  define NI_MAXHOST 1025
00245 #  define NI_MAXSERV 32
00246 # endif
00247 # define NI_MAXNUMERICHOST 64
00248 
00249 # ifndef NI_NUMERICHOST
00250 #  define NI_NUMERICHOST 0x01
00251 #  define NI_NUMERICSERV 0x02
00252 #  define NI_NOFQDN      0x04
00253 #  define NI_NAMEREQD    0x08
00254 #  define NI_DGRAM       0x10
00255 # endif
00256 
00257 # ifndef HAVE_STRUCT_ADDRINFO
00258 struct addrinfo
00259 {
00260     int ai_flags;
00261     int ai_family;
00262     int ai_socktype;
00263     int ai_protocol;
00264     size_t ai_addrlen;
00265     struct sockaddr *ai_addr;
00266     char *ai_canonname;
00267     struct addrinfo *ai_next;
00268 };
00269 #  define AI_PASSIVE     1
00270 #  define AI_CANONNAME   2
00271 #  define AI_NUMERICHOST 4
00272 # endif /* if !HAVE_STRUCT_ADDRINFO */
00273 
00274 #ifndef AI_NUMERICSERV
00275 # define AI_NUMERICSERV 0
00276 #endif
00277 
00278 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
00279 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
00280 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
00281 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
00282 
00283 
00284 static inline bool
00285 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
00286 {
00287     switch (addr->sa_family)
00288     {
00289 #ifdef IN_MULTICAST
00290         case AF_INET:
00291         {
00292             const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
00293             if ((size_t)len < sizeof (*v4))
00294                 return false;
00295             return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
00296         }
00297 #endif
00298 
00299 #ifdef IN6_IS_ADDR_MULTICAST
00300         case AF_INET6:
00301         {
00302             const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
00303             if ((size_t)len < sizeof (*v6))
00304                 return false;
00305             return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
00306         }
00307 #endif
00308     }
00309 
00310     return false;
00311 }
00312 
00313 
00314 static inline int net_GetSockAddress( int fd, char *address, int *port )
00315 {
00316     struct sockaddr_storage addr;
00317     socklen_t addrlen = sizeof( addr );
00318 
00319     return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
00320         || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00321                             NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00322         ? VLC_EGENERIC : 0;
00323 }
00324 
00325 static inline int net_GetPeerAddress( int fd, char *address, int *port )
00326 {
00327     struct sockaddr_storage addr;
00328     socklen_t addrlen = sizeof( addr );
00329 
00330     return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
00331         || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00332                             NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00333         ? VLC_EGENERIC : 0;
00334 }
00335 
00336 static inline uint16_t net_GetPort (const struct sockaddr *addr)
00337 {
00338     switch (addr->sa_family)
00339     {
00340 #ifdef AF_INET6
00341         case AF_INET6:
00342             return ((const struct sockaddr_in6 *)addr)->sin6_port;
00343 #endif
00344         case AF_INET:
00345             return ((const struct sockaddr_in *)addr)->sin_port;
00346     }
00347     return 0;
00348 }
00349 
00350 static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
00351 {
00352     switch (addr->sa_family)
00353     {
00354 #ifdef AF_INET6
00355         case AF_INET6:
00356             ((struct sockaddr_in6 *)addr)->sin6_port = port;
00357         break;
00358 #endif
00359         case AF_INET:
00360             ((struct sockaddr_in *)addr)->sin_port = port;
00361         break;
00362     }
00363 }
00364 # ifdef __cplusplus
00365 }
00366 # endif
00367 
00368 #endif

Generated on Wed Aug 13 08:02:37 2008 for VLC by  doxygen 1.5.1