Commit e2af7970 authored by Gildas Bazin's avatar Gildas Bazin

* modules/misc/network/ipv4.c: a bit of cleanup.
parent 0d15ca7b
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* ipv4.c: IPv4 network abstraction layer * ipv4.c: IPv4 network abstraction layer
***************************************************************************** *****************************************************************************
* Copyright (C) 2001, 2002 VideoLAN * Copyright (C) 2001, 2002 VideoLAN
* $Id: ipv4.c,v 1.17 2003/03/24 17:15:30 gbazin Exp $ * $Id: ipv4.c,v 1.18 2003/04/21 16:22:43 gbazin Exp $
* *
* Authors: Christophe Massiot <massiot@via.ecp.fr> * Authors: Christophe Massiot <massiot@via.ecp.fr>
* Mathias Kretschmer <mathias@research.att.com> * Mathias Kretschmer <mathias@research.att.com>
...@@ -51,9 +51,7 @@ ...@@ -51,9 +51,7 @@
#elif defined( WIN32 ) #elif defined( WIN32 )
# include <winsock2.h> # include <winsock2.h>
# include <ws2tcpip.h> # include <ws2tcpip.h>
# ifndef IN_MULTICAST # define close closesocket
# define IN_MULTICAST(a) IN_CLASSD(a)
# endif
#else #else
# include <netdb.h> /* hostent ... */ # include <netdb.h> /* hostent ... */
# include <sys/socket.h> # include <sys/socket.h>
...@@ -71,6 +69,9 @@ ...@@ -71,6 +69,9 @@
#ifndef INADDR_NONE #ifndef INADDR_NONE
# define INADDR_NONE 0xFFFFFFFF # define INADDR_NONE 0xFFFFFFFF
#endif #endif
#ifndef IN_MULTICAST
# define IN_MULTICAST(a) IN_CLASSD(a)
#endif
/***************************************************************************** /*****************************************************************************
* Local prototypes * Local prototypes
...@@ -146,9 +147,6 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -146,9 +147,6 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
int i_bind_port = p_socket->i_bind_port; int i_bind_port = p_socket->i_bind_port;
char * psz_server_addr = p_socket->psz_server_addr; char * psz_server_addr = p_socket->psz_server_addr;
int i_server_port = p_socket->i_server_port; int i_server_port = p_socket->i_server_port;
#if defined( WIN32 ) && !defined( UNDER_CE )
char * psz_bind_win32; /* WIN32 multicast kludge */
#endif
int i_handle, i_opt; int i_handle, i_opt;
socklen_t i_opt_size; socklen_t i_opt_size;
...@@ -177,11 +175,7 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -177,11 +175,7 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
#else #else
msg_Err( p_this, "cannot configure socket (SO_REUSEADDR)" ); msg_Err( p_this, "cannot configure socket (SO_REUSEADDR)" );
#endif #endif
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
...@@ -232,26 +226,16 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -232,26 +226,16 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
/* Build the local socket */ /* Build the local socket */
#if defined( WIN32 ) && !defined( UNDER_CE ) #if defined( WIN32 ) && !defined( UNDER_CE )
/* Under Win32 and for the multicast, we bind on INADDR_ANY, /* Under Win32 and for multicasting, we bind to INADDR_ANY,
* so let's call BuildAddr with "" instead of psz_bind_addr */ * so let's call BuildAddr with "" instead of psz_bind_addr */
psz_bind_win32 = psz_bind_addr ; if( BuildAddr( &sock, IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) ?
"" : psz_bind_addr, i_bind_port ) == -1 )
/* Check if this is a multicast socket */
if (IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
{
psz_bind_win32 = "";
}
if ( BuildAddr( &sock, psz_bind_win32, i_bind_port ) == -1 )
#else #else
if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 ) if( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
#endif #endif
{ {
msg_Dbg( p_this, "could not build local address" ); msg_Dbg( p_this, "could not build local address" );
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
...@@ -263,14 +247,23 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -263,14 +247,23 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
#else #else
msg_Err( p_this, "cannot bind socket" ); msg_Err( p_this, "cannot bind socket" );
#endif #endif
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
#if defined( WIN32 ) && !defined( UNDER_CE )
/* Restore the sock struct so we can spare a few #ifdef WIN32 later on */
if( IN_MULTICAST( ntohl( inet_addr(psz_bind_addr) ) ) )
{
if ( BuildAddr( &sock, psz_bind_addr, i_bind_port ) == -1 )
{
msg_Dbg( p_this, "could not build local address" );
close( i_handle );
return( -1 );
}
}
#endif
/* Allow broadcast reception if we bound on INADDR_ANY */ /* Allow broadcast reception if we bound on INADDR_ANY */
if( !*psz_bind_addr ) if( !*psz_bind_addr )
{ {
...@@ -293,25 +286,15 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -293,25 +286,15 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
#if !defined( UNDER_CE ) && !defined( SYS_BEOS ) #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
/* Join the multicast group if the socket is a multicast address */ /* Join the multicast group if the socket is a multicast address */
#ifndef IN_MULTICAST
# define IN_MULTICAST(a) IN_CLASSD(a)
#endif
#ifndef WIN32
if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) ) if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
{ {
struct ip_mreq imr; struct ip_mreq imr;
/* Determine interface to be used for multicast */
char * psz_if_addr = config_GetPsz( p_this, "iface-addr" ); char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr; imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
#else if( psz_if_addr != NULL && *psz_if_addr
if( IN_MULTICAST( ntohl(inet_addr(psz_bind_addr) ) ) ) && inet_addr(psz_if_addr) != INADDR_NONE )
{
struct ip_mreq imr;
char * psz_if_addr = config_GetPsz( p_this, "iface-addr" );
imr.imr_multiaddr.s_addr = inet_addr(psz_bind_addr);
#endif
if ( psz_if_addr != NULL && *psz_if_addr
&& inet_addr(psz_if_addr) != INADDR_NONE )
{ {
imr.imr_interface.s_addr = inet_addr(psz_if_addr); imr.imr_interface.s_addr = inet_addr(psz_if_addr);
} }
...@@ -319,8 +302,9 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -319,8 +302,9 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
{ {
imr.imr_interface.s_addr = INADDR_ANY; imr.imr_interface.s_addr = INADDR_ANY;
} }
if ( psz_if_addr != NULL ) free( psz_if_addr ); if( psz_if_addr != NULL ) free( psz_if_addr );
/* Join Multicast group */
if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP, if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char*)&imr, sizeof(struct ip_mreq) ) == -1 ) (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
{ {
...@@ -330,11 +314,7 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -330,11 +314,7 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
#else #else
msg_Warn( p_this, "failed to join IP multicast group" ); msg_Warn( p_this, "failed to join IP multicast group" );
#endif #endif
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
} }
...@@ -346,11 +326,7 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -346,11 +326,7 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 ) if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
{ {
msg_Err( p_this, "cannot build remote address" ); msg_Err( p_this, "cannot build remote address" );
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
...@@ -363,40 +339,26 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -363,40 +339,26 @@ static int OpenUDP( vlc_object_t * p_this, network_socket_t * p_socket )
#else #else
msg_Err( p_this, "cannot connect socket" ); msg_Err( p_this, "cannot connect socket" );
#endif #endif
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
#if !defined( UNDER_CE ) && !defined( SYS_BEOS ) #if !defined( UNDER_CE ) && !defined( SYS_BEOS )
#ifndef WIN32
if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
#else
if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) ) if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) )
#endif
{ {
/* set the time-to-live */ /* set the time-to-live */
int ttl = config_GetInt( p_this, "ttl" ); int ttl = config_GetInt( p_this, "ttl" );
if( ttl < 1 ) if( ttl < 1 ) ttl = 1;
ttl = 1;
if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL, if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL,
(void *) &ttl, sizeof( ttl ) ) < 0 ) (void *) &ttl, sizeof( ttl ) ) < 0 )
{ {
#ifdef HAVE_ERRNO_H #ifdef HAVE_ERRNO_H
msg_Warn( p_this, "failed to set ttl (%s)", msg_Warn( p_this, "failed to set ttl (%s)", strerror(errno) );
strerror(errno) );
#else #else
msg_Warn( p_this, "failed to set ttl" ); msg_Warn( p_this, "failed to set ttl" );
#endif #endif
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
} }
...@@ -445,11 +407,7 @@ static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -445,11 +407,7 @@ static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 ) if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 )
{ {
msg_Dbg( p_this, "could not build local address" ); msg_Dbg( p_this, "could not build local address" );
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
...@@ -462,11 +420,7 @@ static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket ) ...@@ -462,11 +420,7 @@ static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket )
#else #else
msg_Err( p_this, "cannot connect socket" ); msg_Err( p_this, "cannot connect socket" );
#endif #endif
#if defined( WIN32 ) || defined( UNDER_CE )
closesocket( i_handle );
#else
close( i_handle ); close( i_handle );
#endif
return( -1 ); return( -1 );
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* ipv6.c: IPv6 network abstraction layer * ipv6.c: IPv6 network abstraction layer
***************************************************************************** *****************************************************************************
* Copyright (C) 2002 VideoLAN * Copyright (C) 2002 VideoLAN
* $Id: ipv6.c,v 1.8 2003/02/07 23:36:55 marcari Exp $ * $Id: ipv6.c,v 1.9 2003/04/21 16:22:43 gbazin Exp $
* *
* Authors: Alexis Guillard <alexis.guillard@bt.com> * Authors: Alexis Guillard <alexis.guillard@bt.com>
* Christophe Massiot <massiot@via.ecp.fr> * Christophe Massiot <massiot@via.ecp.fr>
...@@ -65,6 +65,7 @@ static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}}; ...@@ -65,6 +65,7 @@ static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
#ifndef IPV6_JOIN_GROUP #ifndef IPV6_JOIN_GROUP
# define IPV6_JOIN_GROUP 20 # define IPV6_JOIN_GROUP 20
#endif #endif
# define close closesocket
#endif #endif
/***************************************************************************** /*****************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment