Commit 87d72409 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Fix socket errors handling.

Fallback to IPv4 with option --ipv6 if IPv6 fails
parent d16c1ff9
...@@ -893,14 +893,13 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -893,14 +893,13 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
/* resolv */ /* resolv */
#ifdef HAVE_GETADDRINFO #ifdef HAVE_GETADDRINFO
{ {
vlc_value_t val; vlc_value_t val;
char psz_port[6]; char psz_port[6];
struct addrinfo hints; struct addrinfo hints;
memset( &hints, 0, sizeof( hints ) );
#if 0 #if 0
memset( &hints, 0, sizeof( hints ) );
/* Check if we have force ipv4 or ipv6 */ /* Check if we have force ipv4 or ipv6 */
var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
var_Get( p_this, "ipv4", &val ); var_Get( p_this, "ipv4", &val );
...@@ -915,12 +914,12 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -915,12 +914,12 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
* *
*/ */
hints.ai_family = PF_INET; hints.ai_family = PF_INET;
#endif
var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
var_Get( p_this, "ipv6", &val ); var_Get( p_this, "ipv6", &val );
if( val.b_bool ) if( val.b_bool )
hints.ai_family = PF_INET6; hints.ai_family = 0; // try IPv6, then IPv4
#endif
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
...@@ -1052,7 +1051,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -1052,7 +1051,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
/* create the listening socket */ /* create the listening socket */
fd = socket( res->ai_family, res->ai_socktype, res->ai_protocol ); fd = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
if( fd < 0 ) if( fd == -1 )
continue; continue;
/* reuse socket */ /* reuse socket */
...@@ -1069,7 +1068,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -1069,7 +1068,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
if( bind( fd, res->ai_addr, res->ai_addrlen ) ) if( bind( fd, res->ai_addr, res->ai_addrlen ) )
{ {
msg_Err( p_this, "cannot bind socket" ); msg_Err( p_this, "cannot bind socket" );
continue; goto socket_error;
} }
/* set to non-blocking */ /* set to non-blocking */
#if defined( WIN32 ) || defined( UNDER_CE ) #if defined( WIN32 ) || defined( UNDER_CE )
...@@ -1078,7 +1077,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -1078,7 +1077,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 ) if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
{ {
msg_Err( p_this, "cannot set socket to non-blocking mode" ); msg_Err( p_this, "cannot set socket to non-blocking mode" );
continue; goto socket_error;
} }
} }
#else #else
...@@ -1087,12 +1086,12 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -1087,12 +1086,12 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 ) if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
{ {
msg_Err( p_this, "cannot F_GETFL socket" ); msg_Err( p_this, "cannot F_GETFL socket" );
continue; goto socket_error;
} }
if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 ) if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
{ {
msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" ); msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
continue; goto socket_error;
} }
} }
#endif #endif
...@@ -1100,8 +1099,15 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port ) ...@@ -1100,8 +1099,15 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
if( listen( fd, LISTEN_BACKLOG ) < 0 ) if( listen( fd, LISTEN_BACKLOG ) < 0 )
{ {
msg_Err( p_this, "cannot listen socket" ); msg_Err( p_this, "cannot listen socket" );
continue; close( fd );
fd = -1;
} }
continue;
socket_error:
close( fd );
fd = -1;
} }
freeaddrinfo( res ); freeaddrinfo( res );
......
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