00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef VLC_NETWORK_H
00028 # define VLC_NETWORK_H
00029
00030
00031
00032
00033
00034
00035 #if defined( WIN32 )
00036 # if !defined(UNDER_CE)
00037 # define _NO_OLDNAMES 1
00038 # include <io.h>
00039 # endif
00040 # include <winsock2.h>
00041 # include <ws2tcpip.h>
00042 # define ENETUNREACH WSAENETUNREACH
00043 # define net_errno (WSAGetLastError())
00044 extern const char *net_strerror( int val );
00045
00046 struct iovec
00047 {
00048 void *iov_base;
00049 size_t iov_len;
00050 };
00051
00052 struct msghdr
00053 {
00054 void *msg_name;
00055 size_t msg_namelen;
00056 struct iovec *msg_iov;
00057 size_t msg_iovlen;
00058 void *msg_control;
00059 size_t msg_controllen;
00060 int msg_flags;
00061 };
00062
00063 # ifndef IPV6_V6ONLY
00064 # define IPV6_V6ONLY 27
00065 # endif
00066 #else
00067 # include <sys/socket.h>
00068 # ifdef HAVE_NETINET_IN_H
00069 # include <netinet/in.h>
00070 # endif
00071 # ifdef HAVE_ARPA_INET_H
00072 # include <arpa/inet.h>
00073 # elif defined( SYS_BEOS )
00074 # include <net/netdb.h>
00075 # endif
00076 # include <netdb.h>
00077 # define net_errno errno
00078 #endif
00079
00080 int vlc_socket (int, int, int, bool nonblock) LIBVLC_USED;
00081
00082 struct sockaddr;
00083 VLC_EXPORT( int, vlc_accept, ( int, struct sockaddr *, socklen_t *, bool ) LIBVLC_USED );
00084
00085 # ifdef __cplusplus
00086 extern "C" {
00087 # endif
00088
00089
00090 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
00091
00092 VLC_EXPORT( int, net_Connect, (vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol) );
00093 #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e)
00094
00095 VLC_EXPORT( int *, net_Listen, (vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol) );
00096
00097 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
00098 SOCK_STREAM, IPPROTO_TCP)
00099
00100 static inline int net_ConnectTCP (vlc_object_t *obj, const char *host, int port)
00101 {
00102 return net_Connect (obj, host, port, SOCK_STREAM, IPPROTO_TCP);
00103 }
00104 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
00105
00106 VLC_EXPORT( int, net_AcceptSingle, (vlc_object_t *obj, int lfd) );
00107
00108 VLC_EXPORT( int, net_Accept, ( vlc_object_t *, int * ) );
00109 #define net_Accept(a, b) \
00110 net_Accept(VLC_OBJECT(a), b)
00111
00112 VLC_EXPORT( int, net_ConnectDgram, ( vlc_object_t *p_this, const char *psz_host, int i_port, int hlim, int proto ) );
00113 #define net_ConnectDgram(a, b, c, d, e ) \
00114 net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
00115
00116 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, int port, int hlim)
00117 {
00118 return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
00119 }
00120
00121 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 ) );
00122 #define net_OpenDgram( a, b, c, d, e, g, h ) \
00123 net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g, h)
00124
00125 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, int port)
00126 {
00127 return net_OpenDgram (obj, host, port, NULL, 0, 0, IPPROTO_UDP);
00128 }
00129
00130 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
00131
00132 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
00133 socklen_t addrlen);
00134
00135 VLC_EXPORT( int, net_SetCSCov, ( int fd, int sendcov, int recvcov ) );
00136
00137
00138 struct virtual_socket_t
00139 {
00140 void *p_sys;
00141 int (*pf_recv) ( void *, void *, int );
00142 int (*pf_send) ( void *, const void *, int );
00143 };
00144
00145 VLC_EXPORT( ssize_t, net_Read, ( vlc_object_t *p_this, int fd, const v_socket_t *, void *p_data, size_t i_data, bool b_retry ) );
00146 #define net_Read(a,b,c,d,e,f) net_Read(VLC_OBJECT(a),b,c,d,e,f)
00147 VLC_EXPORT( ssize_t, net_Write, ( vlc_object_t *p_this, int fd, const v_socket_t *, const void *p_data, size_t i_data ) );
00148 #define net_Write(a,b,c,d,e) net_Write(VLC_OBJECT(a),b,c,d,e)
00149 VLC_EXPORT( char *, net_Gets, ( vlc_object_t *p_this, int fd, const v_socket_t * ) );
00150 #define net_Gets(a,b,c) net_Gets(VLC_OBJECT(a),b,c)
00151
00152
00153 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 ) );
00154 #define net_Printf(o,fd,vs,...) net_Printf(VLC_OBJECT(o),fd,vs, __VA_ARGS__)
00155 VLC_EXPORT( ssize_t, net_vaPrintf, ( vlc_object_t *p_this, int fd, const v_socket_t *, const char *psz_fmt, va_list args ) );
00156 #define net_vaPrintf(a,b,c,d,e) net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
00157
00158 VLC_EXPORT (int, vlc_inet_pton, (int af, const char *src, void *dst) );
00159 VLC_EXPORT (const char *, vlc_inet_ntop, (int af, const void *src,
00160 char *dst, socklen_t cnt) );
00161 struct pollfd;
00162 VLC_EXPORT (int, vlc_poll, (struct pollfd *fds, unsigned nfds, int timeout));
00163
00164
00165 #ifdef WIN32
00166
00167 # define SHUT_RD SD_RECEIVE
00168 # define SHUT_WR SD_SEND
00169 # define SHUT_RDWR SD_BOTH
00170 # define net_Close( fd ) closesocket ((SOCKET)fd)
00171 #else
00172 #ifdef HAVE_UNISTD_H
00173 #include <unistd.h>
00174 #endif
00175 # define net_Close( fd ) (void)close (fd)
00176 #endif
00177
00178
00179
00180
00181 # ifndef EAI_BADFLAGS
00182 # define EAI_BADFLAGS -1
00183 # endif
00184 # ifndef EAI_NONAME
00185 # define EAI_NONAME -2
00186 # endif
00187 # ifndef EAI_AGAIN
00188 # define EAI_AGAIN -3
00189 # endif
00190 # ifndef EAI_FAIL
00191 # define EAI_FAIL -4
00192 # endif
00193 # ifndef EAI_NODATA
00194 # define EAI_NODATA -5
00195 # endif
00196 # ifndef EAI_FAMILY
00197 # define EAI_FAMILY -6
00198 # endif
00199 # ifndef EAI_SOCKTYPE
00200 # define EAI_SOCKTYPE -7
00201 # endif
00202 # ifndef EAI_SERVICE
00203 # define EAI_SERVICE -8
00204 # endif
00205 # ifndef EAI_ADDRFAMILY
00206 # define EAI_ADDRFAMILY -9
00207 # endif
00208 # ifndef EAI_MEMORY
00209 # define EAI_MEMORY -10
00210 # endif
00211 #ifndef EAI_OVERFLOW
00212 # define EAI_OVERFLOW -11
00213 #endif
00214 # ifndef EAI_SYSTEM
00215 # define EAI_SYSTEM -12
00216 # endif
00217
00218
00219 # ifndef NI_MAXHOST
00220 # define NI_MAXHOST 1025
00221 # define NI_MAXSERV 32
00222 # endif
00223 # define NI_MAXNUMERICHOST 64
00224
00225 #ifndef AI_NUMERICSERV
00226 # define AI_NUMERICSERV 0
00227 #endif
00228
00229 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
00230 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
00231
00232
00233 static inline bool
00234 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
00235 {
00236 switch (addr->sa_family)
00237 {
00238 #ifdef IN_MULTICAST
00239 case AF_INET:
00240 {
00241 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
00242 if ((size_t)len < sizeof (*v4))
00243 return false;
00244 return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
00245 }
00246 #endif
00247
00248 #ifdef IN6_IS_ADDR_MULTICAST
00249 case AF_INET6:
00250 {
00251 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
00252 if ((size_t)len < sizeof (*v6))
00253 return false;
00254 return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
00255 }
00256 #endif
00257 }
00258
00259 return false;
00260 }
00261
00262
00263 static inline int net_GetSockAddress( int fd, char *address, int *port )
00264 {
00265 struct sockaddr_storage addr;
00266 socklen_t addrlen = sizeof( addr );
00267
00268 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
00269 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00270 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00271 ? VLC_EGENERIC : 0;
00272 }
00273
00274 static inline int net_GetPeerAddress( int fd, char *address, int *port )
00275 {
00276 struct sockaddr_storage addr;
00277 socklen_t addrlen = sizeof( addr );
00278
00279 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
00280 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00281 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00282 ? VLC_EGENERIC : 0;
00283 }
00284
00285 static inline uint16_t net_GetPort (const struct sockaddr *addr)
00286 {
00287 switch (addr->sa_family)
00288 {
00289 #ifdef AF_INET6
00290 case AF_INET6:
00291 return ((const struct sockaddr_in6 *)addr)->sin6_port;
00292 #endif
00293 case AF_INET:
00294 return ((const struct sockaddr_in *)addr)->sin_port;
00295 }
00296 return 0;
00297 }
00298
00299 static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
00300 {
00301 switch (addr->sa_family)
00302 {
00303 #ifdef AF_INET6
00304 case AF_INET6:
00305 ((struct sockaddr_in6 *)addr)->sin6_port = port;
00306 break;
00307 #endif
00308 case AF_INET:
00309 ((struct sockaddr_in *)addr)->sin_port = port;
00310 break;
00311 }
00312 }
00313 # ifdef __cplusplus
00314 }
00315 # endif
00316
00317 #endif