/*****************************************************************************
* httpd.c
*****************************************************************************
- * Copyright (C) 2004 VideoLAN
+ * Copyright (C) 2004-2005 VideoLAN
* $Id$
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
- * Remi Denis-Courmont <courmisch@via.ecp.fr>
+ * Remi Denis-Courmont <rem # videolan.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
# endif
#endif
-#if defined(WIN32)
+#if defined(WIN32) && !defined(UNDER_CE)
static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
+#elif defined(UNDER_CE) && defined(AF_INET6)
+# undef AF_INET6
#endif
#ifndef PF_INET
HTTPD_CLIENT_WAITING,
HTTPD_CLIENT_DEAD,
+
+ HTTPD_CLIENT_TLS_HS_IN,
+ HTTPD_CLIENT_TLS_HS_OUT
};
/* mode */
enum
*****************************************************************************/
#define LISTEN_BACKLOG 100
-#if defined(HAVE_GETNAMEINFO) && !defined(HAVE_GETADDRINFO)
-/*
- * For now, VLC's configure script does not check for getaddrinfo(),
- * but it should be present if getnameinfo() is (the opposite is untrue, with
- * Debian potato as an example)
- */
-# define HAVE_GETADDRINFO 1
-#endif
-
static void httpd_HostThread( httpd_host_t * );
static int GetAddrPort( const struct sockaddr_storage *p_ss );
vlc_value_t val;
char psz_port[6];
struct addrinfo hints;
+ int check;
memset( &hints, 0, sizeof( hints ) );
-#if 0
- /*
- * For now, keep IPv4 by default. That said, it should be safe to use
- * IPv6 by default *on the server side*, as, apart from NetBSD, most
- * systems accept IPv4 clients on IPv6 listening sockets.
- */
- hints.ai_family = PF_INET;
-#else
- hints.ai_family = 0;
-
/* Check if ipv4 or ipv6 were forced */
var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
var_Get( p_this, "ipv4", &val );
if( val.b_bool )
hints.ai_family = PF_INET;
-#endif
+
var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
var_Get( p_this, "ipv6", &val );
if( val.b_bool )
snprintf( psz_port, sizeof( psz_port ), "%d", i_port );
psz_port[sizeof( psz_port ) - 1] = '\0';
- if( getaddrinfo( psz_host, psz_port, &hints, &res ) )
+ check = getaddrinfo( psz_host, psz_port, &hints, &res );
+ if( check != 0 )
{
+#ifdef HAVE_GAI_STRERROR
+ msg_Err( p_this, "cannot resolve %s:%d : %s", psz_host, i_port,
+ gai_strerror( check ) );
+#else
msg_Err( p_this, "cannot resolve %s:%d", psz_host, i_port );
+#endif
return NULL;
}
}
continue;
/* Cannot re-use host if it uses TLS/SSL */
- if( &httpd->host[i]->p_tls != NULL )
+ if( httpd->host[i]->p_tls != NULL )
continue;
#ifdef AF_INET6
return host;
error:
+ if( httpd->i_host <= 0 )
+ {
+ vlc_object_release( httpd );
+ vlc_object_detach( httpd );
+ vlc_object_destroy( httpd );
+ }
vlc_mutex_unlock( lockval.p_address );
if( fd != -1 )
vlc_object_destroy( host );
}
- /* TODO destroy no more used httpd TODO */
- vlc_object_release( httpd );
return NULL;
}
var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
vlc_mutex_lock( lockval.p_address );
- vlc_object_release( httpd );
-
host->i_ref--;
if( host->i_ref > 0 )
{
if( httpd->i_host <= 0 )
{
msg_Info( httpd, "httpd doesn't reference any host, deleting" );
+ vlc_object_release( httpd );
vlc_object_detach( httpd );
vlc_object_destroy( httpd );
}
if( cl->fd >= 0 )
{
if( cl->p_tls != NULL )
- tls_SessionClose( cl->p_tls );
+ tls_ServerSessionClose( cl->p_tls );
net_Close( cl->fd );
cl->fd = -1;
}
if( cl->i_buffer >= 4 )
{
- fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
+ /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
/* detect type */
if( cl->p_buffer[0] == '$' )
{
p = NULL;
cl->query.i_type = HTTPD_MSG_NONE;
- fprintf( stderr, "received new request=%s\n", cl->p_buffer);
+ /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
for( i = 0; msg_type[i].name != NULL; i++ )
{
cl->i_activity_timeout = 0;
/* Debugging only */
- if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
+ /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
{
int i;
fprintf( stderr, " - option name='%s' value='%s'\n",
cl->query.name[i], cl->query.value[i] );
}
- }
+ }*/
}
cl->i_buffer = 0;
cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
- fprintf( stderr, "sending answer\n" );
- fprintf( stderr, "%s", cl->p_buffer );
+ /*fprintf( stderr, "sending answer\n" );
+ fprintf( stderr, "%s", cl->p_buffer );*/
}
i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
}
}
+static void httpd_ClientTlsHsIn( httpd_client_t *cl )
+{
+ switch( tls_SessionContinueHandshake( cl->p_tls ) )
+ {
+ case 0:
+ cl->i_state = HTTPD_CLIENT_RECEIVING;
+ break;
+
+ case -1:
+ cl->i_state = HTTPD_CLIENT_DEAD;
+ cl->p_tls = NULL;
+ break;
+
+ case 2:
+ cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
+ }
+}
+
+static void httpd_ClientTlsHsOut( httpd_client_t *cl )
+{
+ switch( tls_SessionContinueHandshake( cl->p_tls ) )
+ {
+ case 0:
+ cl->i_state = HTTPD_CLIENT_RECEIVING;
+ break;
+
+ case -1:
+ cl->i_state = HTTPD_CLIENT_DEAD;
+ cl->p_tls = NULL;
+ break;
+
+ case 1:
+ cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
+ break;
+ }
+}
+
static void httpd_HostThread( httpd_host_t *host )
{
tls_session_t *p_tls = NULL;
i_client--;
continue;
}
- else if( cl->i_state == HTTPD_CLIENT_RECEIVING )
+ else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
+ || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
{
FD_SET( cl->fd, &fds_read );
i_handle_max = __MAX( i_handle_max, cl->fd );
}
- else if( cl->i_state == HTTPD_CLIENT_SENDING )
+ else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
+ || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
{
FD_SET( cl->fd, &fds_write );
i_handle_max = __MAX( i_handle_max, cl->fd );
fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
if( fd >= 0 )
{
+ int i_state = 0;
+
/* set this new socket non-block */
#if defined( WIN32 ) || defined( UNDER_CE )
{
fcntl( fd, F_SETFL, O_NONBLOCK );
#endif
- /* FIXME: that MUST be non-blocking */
if( p_tls != NULL)
{
- p_tls = tls_SessionHandshake( p_tls, fd );
- if ( p_tls == NULL )
+ switch ( tls_ServerSessionHandshake( p_tls, fd ) )
{
- msg_Err( host, "Rejecting TLS connection" );
- net_Close( fd );
- fd = -1;
+ case -1:
+ msg_Err( host, "Rejecting TLS connection" );
+ net_Close( fd );
+ fd = -1;
+ p_tls = NULL;
+ break;
+
+ case 1: /* missing input - most likely */
+ i_state = HTTPD_CLIENT_TLS_HS_IN;
+ break;
+
+ case 2: /* missing output */
+ i_state = HTTPD_CLIENT_TLS_HS_OUT;
+ break;
}
}
TAB_APPEND( host->i_client, host->client, cl );
vlc_mutex_unlock( &host->lock );
-
+ if( i_state != 0 )
+ cl->i_state = i_state; // override state for TLS
+
// FIXME: it sucks to allocate memory for debug
ip = httpd_ClientIP( cl );
msg_Dbg( host, "new connection (%s)",
{
httpd_ClientSend( cl );
}
+ else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
+ {
+ httpd_ClientTlsHsIn( cl );
+ }
+ else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
+ {
+ httpd_ClientTlsHsOut( cl );
+ }
if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
cl->i_state == HTTPD_CLIENT_SENDING &&
}
vlc_mutex_unlock( &host->lock );
}
+
+ if( p_tls != NULL )
+ tls_ServerSessionClose( p_tls );
}
#ifndef HAVE_GETADDRINFO