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/types.h>
00068 # include <unistd.h>
00069 # include <sys/socket.h>
00070 # include <netinet/in.h>
00071 # include <netdb.h>
00072 # define net_errno errno
00073 #endif
00074
00075 #if defined( __SYMBIAN32__ )
00076 # undef AF_INET6
00077 # undef IN6_IS_ADDR_MULTICAST
00078 # undef IPV6_V6ONLY
00079 # undef IPV6_MULTICAST_HOPS
00080 # undef IPV6_MULTICAST_IF
00081 # undef IPV6_TCLASS
00082 # undef IPV6_JOIN_GROUP
00083 #endif
00084
00085 int vlc_socket (int, int, int, bool nonblock) VLC_USED;
00086
00087 struct sockaddr;
00088 VLC_API int vlc_accept( int, struct sockaddr *, socklen_t *, bool ) VLC_USED;
00089
00090 # ifdef __cplusplus
00091 extern "C" {
00092 # endif
00093
00094
00095 int net_Socket (vlc_object_t *obj, int family, int socktype, int proto);
00096
00097 VLC_API int net_Connect(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
00098 #define net_Connect(a, b, c, d, e) net_Connect(VLC_OBJECT(a), b, c, d, e)
00099
00100 VLC_API int * net_Listen(vlc_object_t *p_this, const char *psz_host, int i_port, int socktype, int protocol);
00101
00102 #define net_ListenTCP(a, b, c) net_Listen(VLC_OBJECT(a), b, c, \
00103 SOCK_STREAM, IPPROTO_TCP)
00104
00105 static inline int net_ConnectTCP (vlc_object_t *obj, const char *host, int port)
00106 {
00107 return net_Connect (obj, host, port, SOCK_STREAM, IPPROTO_TCP);
00108 }
00109 #define net_ConnectTCP(a, b, c) net_ConnectTCP(VLC_OBJECT(a), b, c)
00110
00111 VLC_API int net_AcceptSingle(vlc_object_t *obj, int lfd);
00112
00113 VLC_API int net_Accept( vlc_object_t *, int * );
00114 #define net_Accept(a, b) \
00115 net_Accept(VLC_OBJECT(a), b)
00116
00117 VLC_API int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port, int hlim, int proto );
00118 #define net_ConnectDgram(a, b, c, d, e ) \
00119 net_ConnectDgram(VLC_OBJECT(a), b, c, d, e)
00120
00121 static inline int net_ConnectUDP (vlc_object_t *obj, const char *host, int port, int hlim)
00122 {
00123 return net_ConnectDgram (obj, host, port, hlim, IPPROTO_UDP);
00124 }
00125
00126 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 );
00127 #define net_OpenDgram( a, b, c, d, e, g ) \
00128 net_OpenDgram(VLC_OBJECT(a), b, c, d, e, g)
00129
00130 static inline int net_ListenUDP1 (vlc_object_t *obj, const char *host, int port)
00131 {
00132 return net_OpenDgram (obj, host, port, NULL, 0, IPPROTO_UDP);
00133 }
00134
00135 VLC_API void net_ListenClose( int *fd );
00136
00137 int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
00138 socklen_t addrlen);
00139
00140 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
00141
00142
00143 struct virtual_socket_t
00144 {
00145 void *p_sys;
00146 int (*pf_recv) ( void *, void *, size_t );
00147 int (*pf_send) ( void *, const void *, size_t );
00148 };
00149
00150 VLC_API 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 );
00151 #define net_Read(a,b,c,d,e,f) net_Read(VLC_OBJECT(a),b,c,d,e,f)
00152 VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const v_socket_t *, const void *p_data, size_t i_data );
00153 #define net_Write(a,b,c,d,e) net_Write(VLC_OBJECT(a),b,c,d,e)
00154 VLC_API char * net_Gets( vlc_object_t *p_this, int fd, const v_socket_t * );
00155 #define net_Gets(a,b,c) net_Gets(VLC_OBJECT(a),b,c)
00156
00157
00158 VLC_API ssize_t net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *, const char *psz_fmt, ... ) VLC_FORMAT( 4, 5 );
00159 #define net_Printf(o,fd,vs,...) net_Printf(VLC_OBJECT(o),fd,vs, __VA_ARGS__)
00160 VLC_API ssize_t net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *, const char *psz_fmt, va_list args );
00161 #define net_vaPrintf(a,b,c,d,e) net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
00162
00163 #ifdef WIN32
00164
00165 # define SHUT_RD SD_RECEIVE
00166 # define SHUT_WR SD_SEND
00167 # define SHUT_RDWR SD_BOTH
00168 # define net_Close( fd ) closesocket ((SOCKET)fd)
00169 #else
00170 # ifdef __OS2__
00171 # define SHUT_RD 0
00172 # define SHUT_WR 1
00173 # define SHUT_RDWR 2
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 #ifdef __OS2__
00230 # ifndef NI_NUMERICHOST
00231 # define NI_NUMERICHOST 0x01
00232 # define NI_NUMERICSERV 0x02
00233 # define NI_NOFQDN 0x04
00234 # define NI_NAMEREQD 0x08
00235 # define NI_DGRAM 0x10
00236 # endif
00237
00238 struct addrinfo
00239 {
00240 int ai_flags;
00241 int ai_family;
00242 int ai_socktype;
00243 int ai_protocol;
00244 size_t ai_addrlen;
00245 struct sockaddr *ai_addr;
00246 char *ai_canonname;
00247 struct addrinfo *ai_next;
00248 };
00249
00250 # define AI_PASSIVE 1
00251 # define AI_CANONNAME 2
00252 # define AI_NUMERICHOST 4
00253
00254 VLC_API const char *gai_strerror( int errnum );
00255
00256 VLC_API int getaddrinfo ( const char *, const char *,
00257 const struct addrinfo *, struct addrinfo ** );
00258 VLC_API void freeaddrinfo( struct addrinfo * );
00259 VLC_API int getnameinfo ( const struct sockaddr *, socklen_t,
00260 char *, int, char *, int, int );
00261 #endif
00262
00263 VLC_API int vlc_getnameinfo( const struct sockaddr *, int, char *, int, int *, int );
00264 VLC_API int vlc_getaddrinfo( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** );
00265
00266
00267 #ifdef __OS2__
00268
00269 struct in6_addr
00270 {
00271 uint8_t s6_addr[16];
00272 };
00273
00274 struct sockaddr_in6
00275 {
00276 uint8_t sin6_len;
00277 uint8_t sin6_family;
00278 uint16_t sin6_port;
00279 uint32_t sin6_flowinfo;
00280 struct in6_addr sin6_addr;
00281 uint32_t sin6_scope_id;
00282 };
00283
00284 # define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
00285
00286 static const struct in6_addr in6addr_any =
00287 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
00288 #endif
00289
00290 static inline bool
00291 net_SockAddrIsMulticast (const struct sockaddr *addr, socklen_t len)
00292 {
00293 switch (addr->sa_family)
00294 {
00295 #ifdef IN_MULTICAST
00296 case AF_INET:
00297 {
00298 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
00299 if ((size_t)len < sizeof (*v4))
00300 return false;
00301 return IN_MULTICAST (ntohl (v4->sin_addr.s_addr)) != 0;
00302 }
00303 #endif
00304
00305 #ifdef IN6_IS_ADDR_MULTICAST
00306 case AF_INET6:
00307 {
00308 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
00309 if ((size_t)len < sizeof (*v6))
00310 return false;
00311 return IN6_IS_ADDR_MULTICAST (&v6->sin6_addr) != 0;
00312 }
00313 #endif
00314 }
00315
00316 return false;
00317 }
00318
00319
00320 static inline int net_GetSockAddress( int fd, char *address, int *port )
00321 {
00322 struct sockaddr_storage addr;
00323 socklen_t addrlen = sizeof( addr );
00324
00325 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
00326 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00327 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00328 ? VLC_EGENERIC : 0;
00329 }
00330
00331 static inline int net_GetPeerAddress( int fd, char *address, int *port )
00332 {
00333 struct sockaddr_storage addr;
00334 socklen_t addrlen = sizeof( addr );
00335
00336 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
00337 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00338 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00339 ? VLC_EGENERIC : 0;
00340 }
00341
00342 static inline uint16_t net_GetPort (const struct sockaddr *addr)
00343 {
00344 switch (addr->sa_family)
00345 {
00346 #ifdef AF_INET6
00347 case AF_INET6:
00348 return ((const struct sockaddr_in6 *)addr)->sin6_port;
00349 #endif
00350 case AF_INET:
00351 return ((const struct sockaddr_in *)addr)->sin_port;
00352 }
00353 return 0;
00354 }
00355
00356 static inline void net_SetPort (struct sockaddr *addr, uint16_t port)
00357 {
00358 switch (addr->sa_family)
00359 {
00360 #ifdef AF_INET6
00361 case AF_INET6:
00362 ((struct sockaddr_in6 *)addr)->sin6_port = port;
00363 break;
00364 #endif
00365 case AF_INET:
00366 ((struct sockaddr_in *)addr)->sin_port = port;
00367 break;
00368 }
00369 }
00370 # ifdef __cplusplus
00371 }
00372 # endif
00373
00374 #endif