/*****************************************************************************
* httpd.c
*****************************************************************************
- * Copyright (C) 2004 VideoLAN
+ * Copyright (C) 2004-2005 VideoLAN
* $Id$
*
* Authors: Laurent Aimar <fenrir@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
#include <stdlib.h>
#include <vlc/vlc.h>
+#ifdef ENABLE_HTTPD
+
#include "vlc_httpd.h"
+#include "network.h"
+#include "vlc_tls.h"
+#include <string.h>
#include <errno.h>
+
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
-#include <fcntl.h>
+
+#ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
#if defined( UNDER_CE )
# include <winsock.h>
#elif defined( WIN32 )
# include <winsock2.h>
# include <ws2tcpip.h>
-# ifndef IN_MULTICAST
-# define IN_MULTICAST(a) IN_CLASSD(a)
-# endif
#else
# include <netdb.h> /* hostent ... */
# include <sys/socket.h>
# endif
#endif
+#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
+# define PF_INET AF_INET /* BeOS */
+#endif
+
#if 0
typedef struct httpd_t httpd_t;
void httpd_UrlDelete( httpd_url_t * );
-void httpd_ClientModeStream( httpd_client_t *cl );
-void httpd_ClientModeBidir( httpd_client_t *cl );
+void httpd_ClientModeStream( httpd_client_t *cl );
+void httpd_ClientModeBidir( httpd_client_t *cl );
+static void httpd_ClientClean( httpd_client_t *cl );
/* High level */
typedef struct httpd_file_t httpd_file_t;
};
#endif
+static void httpd_ClientClean( httpd_client_t *cl );
+
/* each host run in his own thread */
struct httpd_host_t
{
int i_ref;
/* address/port and socket for listening at connections */
- struct sockaddr_in sock;
- int fd;
+ struct sockaddr_storage sock;
+ int i_sock_size;
+ int fd;
vlc_mutex_t lock;
int i_client;
httpd_client_t **client;
+
+ /* TLS data */
+ tls_server_t *p_tls;
};
struct httpd_url_t
HTTPD_CLIENT_WAITING,
HTTPD_CLIENT_DEAD,
+
+ HTTPD_CLIENT_TLS_HS_IN,
+ HTTPD_CLIENT_TLS_HS_OUT
};
/* mode */
enum
int i_ref;
- struct sockaddr_in sock;
+ struct sockaddr_storage sock;
+ int i_sock_size;
int fd;
int i_mode;
/* */
httpd_message_t query; /* client -> httpd */
httpd_message_t answer; /* httpd -> client */
+
+ /* TLS data */
+ tls_session_t *p_tls;
};
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".png", "image/png" },
+ { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
/* media mime */
{ ".avi", "video/avi" },
/* Check that */
psz_args = query->p_body;
}
- file->pf_fill( file->p_sys, file, psz_args, &answer->p_body, &answer->i_body );
+ file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
+ &answer->i_body );
}
/* We respect client request */
if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
{
- httpd_MsgAdd( answer, "Connection", httpd_MsgGet( &cl->query, "Connection" ) );
+ httpd_MsgAdd( answer, "Connection",
+ httpd_MsgGet( &cl->query, "Connection" ) );
}
httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
{
httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
- if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user, psz_password ) ) == NULL )
+ if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
+ psz_password ) ) == NULL )
{
free( file );
return NULL;
file->pf_fill = pf_fill;
file->p_sys = p_sys;
- httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack, (httpd_callback_sys_t*)file );
- httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack, (httpd_callback_sys_t*)file );
- httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack, (httpd_callback_sys_t*)file );
+ httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
+ (httpd_callback_sys_t*)file );
+ httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
+ (httpd_callback_sys_t*)file );
+ httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
+ (httpd_callback_sys_t*)file );
return file;
}
-void httpd_FileDelete( httpd_file_t *file )
+void httpd_FileDelete( httpd_file_t *file )
{
httpd_UrlDelete( file->url );
char *psz_dst;
};
-static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
+static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
+ httpd_client_t *cl, httpd_message_t *answer,
+ httpd_message_t *query )
{
httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
uint8_t *p;
return VLC_SUCCESS;
}
-httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst, char *psz_url_src )
+httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
+ char *psz_url_src )
{
httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
- if( ( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) == NULL )
+ if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) )
{
free( rdir );
return NULL;
}
rdir->psz_dst = strdup( psz_url_dst );
- /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
- httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
- httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
- httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
- httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack, (httpd_callback_sys_t*)rdir );
+ /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
+ httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
+ (httpd_callback_sys_t*)rdir );
+ httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
+ (httpd_callback_sys_t*)rdir );
+ httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
+ (httpd_callback_sys_t*)rdir );
+ httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
+ (httpd_callback_sys_t*)rdir );
return rdir;
}
-void httpd_RedirectDelete( httpd_redirect_t *rdir )
+void httpd_RedirectDelete( httpd_redirect_t *rdir )
{
httpd_UrlDelete( rdir->url );
free( rdir->psz_dst );
int64_t i_buffer_last_pos; /* a new connection will start with that */
};
-static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
+static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
+ httpd_client_t *cl, httpd_message_t *answer,
+ httpd_message_t *query )
{
httpd_stream_t *stream = (httpd_stream_t*)p_sys;
int64_t i_write;
int i_pos;
- /* fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n", answer->i_body_offset ); */
+#if 0
+ fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
+ answer->i_body_offset );
+#endif
if( answer->i_body_offset >= stream->i_buffer_pos )
{
/* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
return VLC_EGENERIC; /* wait, no data available */
}
- if( answer->i_body_offset + stream->i_buffer_size < stream->i_buffer_pos )
+ if( answer->i_body_offset + stream->i_buffer_size <
+ stream->i_buffer_pos )
{
/* this client isn't fast enough */
fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
return VLC_EGENERIC; /* wait, no data available */
}
+ /* Don't go past the end of the circular buffer */
+ i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
+
/* using HTTPD_MSG_ANSWER -> data available */
answer->i_proto = HTTPD_PROTO_HTTP;
answer->i_version= 0;
else
{
httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
+ answer->i_body_offset = 0;
}
if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
vlc_bool_t b_xplaystream = VLC_FALSE;
int i;
- httpd_MsgAdd( answer, "Content-type", "%s", "application/octet-stream" );
+ httpd_MsgAdd( answer, "Content-type", "%s",
+ "application/octet-stream" );
httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
httpd_MsgAdd( answer, "Pragma", "no-cache" );
httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
for( i = 0; i < query->i_name; i++ )
{
if( !strcasecmp( query->name[i], "Pragma" ) &&
- !strcasecmp( query->value[i], "xPlayStrm=1" ) )
+ strstr( query->value[i], "xPlayStrm=1" ) )
{
b_xplaystream = VLC_TRUE;
}
{
httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
- if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user, psz_password ) ) == NULL )
+ if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
+ psz_password ) ) == NULL )
{
free( stream );
return NULL;
stream->p_header = NULL;
stream->i_buffer_size = 5000000; /* 5 Mo per stream */
stream->p_buffer = malloc( stream->i_buffer_size );
- /* We set to 1, to make life simpler (this way i_body_offset can never be 0) */
+ /* We set to 1 to make life simpler
+ * (this way i_body_offset can never be 0) */
stream->i_buffer_pos = 1;
stream->i_buffer_last_pos = 1;
- httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
- httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
- httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack, (httpd_callback_sys_t*)stream );
+ httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
+ (httpd_callback_sys_t*)stream );
+ httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
+ (httpd_callback_sys_t*)stream );
+ httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
+ (httpd_callback_sys_t*)stream );
return stream;
}
-int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
+int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
{
vlc_mutex_lock( &stream->lock );
if( stream->p_header )
return VLC_SUCCESS;
}
-int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
+int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
{
int i_count;
int i_pos;
*****************************************************************************/
#define LISTEN_BACKLOG 100
-#if defined( WIN32 ) || defined( UNDER_CE )
-#define SOCKET_CLOSE(a) closesocket(a)
-#else
-#define SOCKET_CLOSE(a) close(a)
+#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 );
+
+#ifndef HAVE_GETADDRINFO
+struct httpd_addrinfo
+{
+ int ai_family;
+ int ai_socktype;
+ int ai_protocol;
+ /*int ai_flags;*/
+ struct sockaddr *ai_addr;
+ int ai_addrlen;
+ struct httpd_addrinfo *ai_next;
+};
+# define addrinfo httpd_addrinfo
+
static int BuildAddr( struct sockaddr_in * p_socket,
const char * psz_address, int i_port );
+#endif
/* create a new host */
-httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
+httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host,
+ int i_port )
+{
+ return httpd_TLSHostNew( p_this, psz_host, i_port, NULL );
+}
+
+httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, char *psz_host,
+ int i_port, tls_server_t *p_tls )
{
httpd_t *httpd;
- httpd_host_t *host;
+ httpd_host_t *host = NULL;
vlc_value_t lockval;
- struct sockaddr_in sock;
- int i;
+ int fd = -1;
+ struct addrinfo *res, *ptr;
/* resolv */
+#ifdef HAVE_GETADDRINFO
+ {
+ vlc_value_t val;
+ char psz_port[6];
+ struct addrinfo hints;
+ int check;
+
+ memset( &hints, 0, sizeof( hints ) );
+
+ /* 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;
+
+ var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
+ var_Get( p_this, "ipv6", &val );
+ if( val.b_bool )
+ hints.ai_family = PF_INET6;
+
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE;
+
+ if (*psz_host == '\0')
+ psz_host = NULL;
+
+ snprintf( psz_port, sizeof( psz_port ), "%d", i_port );
+ psz_port[sizeof( psz_port ) - 1] = '\0';
+
+ 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;
+ }
+ }
+
+#else
+ struct sockaddr_in sock;
+ struct httpd_addrinfo info;
+
+ info.ai_family = PF_INET;
+ info.ai_socktype = SOCK_STREAM;
+ info.ai_protocol = 0;
+ info.ai_addr = (struct sockaddr *)&sock;
+ info.ai_addrlen = sizeof( sock );
+ info.ai_next = NULL;
+
+ res = &info;
+
if( BuildAddr( &sock, psz_host, i_port ) )
{
msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
return NULL;
}
+# define freeaddrinfo( r ) (void)0;
+#endif
+
/* to be sure to avoid multiple creation */
var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
vlc_mutex_lock( lockval.p_address );
- if( ( httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE ) ) == NULL )
+ if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
{
msg_Info( p_this, "creating httpd" );
if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
{
vlc_mutex_unlock( lockval.p_address );
+ freeaddrinfo( res );
return NULL;
}
vlc_object_attach( httpd, p_this->p_vlc );
}
- /* verify if it already exist */
- for( i = 0; i < httpd->i_host; i++ )
+ for( ptr = res; (ptr != NULL) && (fd == -1); ptr = ptr->ai_next )
{
- if( httpd->host[i]->sock.sin_port == sock.sin_port &&
- ( httpd->host[i]->sock.sin_addr.s_addr == INADDR_ANY ||
- httpd->host[i]->sock.sin_addr.s_addr == sock.sin_addr.s_addr ) )
+ int i;
+
+ if( ((unsigned)ptr->ai_addrlen) > sizeof( struct sockaddr_storage ) )
{
+ msg_Dbg( p_this, "socket address too big" );
+ continue;
+ }
+
+ /* verify if it already exist */
+ for( i = 0; i < httpd->i_host; i++ )
+ {
+ if( GetAddrPort (&httpd->host[i]->sock) != i_port )
+ continue;
+
+ /* Cannot re-use host if it uses TLS/SSL */
+ if( httpd->host[i]->p_tls != NULL )
+ continue;
+
+#ifdef AF_INET6
+ if( httpd->host[i]->sock.ss_family == AF_INET6 )
+ {
+ const struct sockaddr_in6 *p_hsock, *p_sock;
+
+ p_hsock = (const struct sockaddr_in6 *)&httpd->host[i]->sock;
+ p_sock = (const struct sockaddr_in6 *)ptr->ai_addr;
+
+ if( memcmp( &p_hsock->sin6_addr, &in6addr_any,
+ sizeof( struct in6_addr ) ) &&
+ ( p_sock->sin6_family != AF_INET6 ||
+ memcmp( &p_hsock->sin6_addr, &p_sock->sin6_addr,
+ sizeof( struct in6_addr ) ) ) )
+ continue; /* does not match */
+ }
+ else if( ptr->ai_family == PF_INET6 )
+ continue;
+ else
+#endif
+ if( httpd->host[i]->sock.ss_family == AF_INET )
+ {
+ const struct sockaddr_in *p_hsock, *p_sock;
+
+ p_hsock = (const struct sockaddr_in *)&httpd->host[i]->sock;
+ p_sock = (const struct sockaddr_in *)ptr->ai_addr;
+
+ if( p_hsock->sin_addr.s_addr != INADDR_ANY &&
+ ( p_sock->sin_family != AF_INET ||
+ p_hsock->sin_addr.s_addr != p_sock->sin_addr.s_addr ) )
+ continue; /* does not match */
+ }
+ else if( ptr->ai_family == PF_INET )
+ continue;
+ else
+ {
+ msg_Dbg( p_this, "host with unknown address family" );
+ continue;
+ }
+
+ freeaddrinfo( res );
+
/* yep found */
host = httpd->host[i];
host->i_ref++;
msg_Dbg( p_this, "host already registered" );
return host;
}
- }
- /* create the new host */
- host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
- host->httpd = httpd;
- vlc_mutex_init( httpd, &host->lock );
- host->i_ref = 1;
- memcpy( &host->sock, &sock, sizeof( struct sockaddr_in ) );
- host->i_url = 0;
- host->url = NULL;
- host->i_client = 0;
- host->client = NULL;
- /* create the listening socket */
- if( ( host->fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
- {
- goto socket_error;
- }
- /* reuse socket */
- i = 1;
- if( setsockopt( host->fd, SOL_SOCKET, SO_REUSEADDR,
- (void *) &i, sizeof( i ) ) < 0 )
- {
- msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
- }
- /* bind it */
- if( bind( host->fd, (struct sockaddr *)&host->sock, sizeof( struct sockaddr_in ) ) < 0 )
- {
- msg_Err( p_this, "cannot bind socket" );
- goto socket_error;
- }
- /* set to non-blocking */
-#if defined( WIN32 ) || defined( UNDER_CE )
- {
- unsigned long i_dummy = 1;
- if( ioctlsocket( host->fd, FIONBIO, &i_dummy ) != 0 )
+ /* create the listening socket */
+ fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
+ if( fd == -1 )
+ continue;
+
+ /* reuse socket */
+ {
+ int dummy = 1;
+ if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
+ (void *)&dummy, sizeof( dummy ) ) < 0 )
+ {
+ msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
+ }
+ }
+
+ /* bind it */
+ if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
{
- msg_Err( p_this, "cannot set socket to non-blocking mode" );
+ msg_Err( p_this, "cannot bind socket" );
goto socket_error;
}
- }
+ /* set to non-blocking */
+#if defined( WIN32 ) || defined( UNDER_CE )
+ {
+ unsigned long i_dummy = 1;
+ if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
+ {
+ msg_Err( p_this, "cannot set socket to non-blocking mode" );
+ goto socket_error;
+ }
+ }
#else
- {
- unsigned int i_flags;
- if( ( i_flags = fcntl( host->fd, F_GETFL, 0 ) ) < 0 )
{
- msg_Err( p_this, "cannot F_GETFL socket" );
- goto socket_error;
+ unsigned int i_flags;
+ if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
+ {
+ msg_Err( p_this, "cannot F_GETFL socket" );
+ goto socket_error;
+ }
+ if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
+ {
+ msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
+ goto socket_error;
+ }
}
- if( fcntl( host->fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
+#endif
+ /* listen */
+ if( listen( fd, LISTEN_BACKLOG ) < 0 )
{
- msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
+ msg_Err( p_this, "cannot listen socket" );
goto socket_error;
}
+
+ break; // success
+
+socket_error:
+ net_Close( fd );
+ fd = -1;
}
-#endif
- /* listen */
- if( listen( host->fd, LISTEN_BACKLOG ) < 0 )
+
+
+ if( fd == -1 )
{
- msg_Err( p_this, "cannot listen socket" );
- goto socket_error;
+ freeaddrinfo( res );
+ goto error;
}
+ /* create the new host */
+ host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
+ host->httpd = httpd;
+ vlc_mutex_init( httpd, &host->lock );
+ host->i_ref = 1;
+ host->fd = fd;
+
+ memcpy( &host->sock, ptr->ai_addr, ptr->ai_addrlen );
+ host->i_sock_size = ptr->ai_addrlen;
+ host->i_url = 0;
+ host->url = NULL;
+ host->i_client = 0;
+ host->client = NULL;
+
+ freeaddrinfo( res );
+ host->p_tls = p_tls;
+
/* create the thread */
- if( vlc_thread_create( host, "httpd host thread",
- httpd_HostThread, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
+ if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
+ VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
{
msg_Err( p_this, "cannot spawn http host thread" );
- goto socket_error;
+ goto error;
}
/* now add it to httpd */
return host;
-socket_error:
+error:
vlc_mutex_unlock( lockval.p_address );
- if( host->fd > 0 )
+ if( fd != -1 )
+ net_Close( fd );
+
+ if( host != NULL )
{
- SOCKET_CLOSE( host->fd );
+ vlc_mutex_destroy( &host->lock );
+ vlc_object_destroy( host );
}
- vlc_mutex_destroy( &host->lock );
- vlc_object_destroy( host );
/* TODO destroy no more used httpd TODO */
vlc_object_release( httpd );
}
/* delete a host */
-void httpd_HostDelete( httpd_host_t *host )
+void httpd_HostDelete( httpd_host_t *host )
{
httpd_t *httpd = host->httpd;
vlc_value_t lockval;
{
httpd_client_t *cl = host->client[i];
msg_Warn( host, "client still connected" );
- SOCKET_CLOSE( cl->fd );
+ httpd_ClientClean( cl );
+ TAB_REMOVE( host->i_client, host->client, cl );
+ free( cl );
+ i--;
/* TODO */
}
- SOCKET_CLOSE( host->fd );
+ if( host->p_tls != NULL)
+ tls_ServerDelete( host->p_tls );
+ net_Close( host->fd );
vlc_mutex_destroy( &host->lock );
vlc_object_destroy( host );
}
/* register a new url */
-static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password, vlc_bool_t b_check )
+static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
+ char *psz_user, char *psz_password,
+ vlc_bool_t b_check )
{
httpd_url_t *url;
int i;
return url;
}
-httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password )
+httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
+ char *psz_user, char *psz_password )
{
- return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, VLC_FALSE );
+ return httpd_UrlNewPrivate( host, psz_url, psz_user,
+ psz_password, VLC_FALSE );
}
-httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url, char *psz_user, char *psz_password )
+httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
+ char *psz_user, char *psz_password )
{
- return httpd_UrlNewPrivate( host, psz_url, psz_user, psz_password, VLC_TRUE );
+ return httpd_UrlNewPrivate( host, psz_url, psz_user,
+ psz_password, VLC_TRUE );
}
/* register callback on a url */
-int httpd_UrlCatch( httpd_url_t *url, int i_msg,
- httpd_callback_t cb,
- httpd_callback_sys_t *p_sys )
+int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
+ httpd_callback_sys_t *p_sys )
{
vlc_mutex_lock( &url->lock );
url->catch[i_msg].cb = cb;
/* delete an url */
-void httpd_UrlDelete( httpd_url_t *url )
+void httpd_UrlDelete( httpd_url_t *url )
{
httpd_host_t *host = url->host;
int i;
{
/* TODO complete it */
msg_Warn( host, "force closing connections" );
- SOCKET_CLOSE( client->fd );
+ httpd_ClientClean( client );
TAB_REMOVE( host->i_client, host->client, client );
+ free( client );
i--;
}
}
+ free( url );
vlc_mutex_unlock( &host->lock );
}
{
cl->i_state = HTTPD_CLIENT_RECEIVING;
cl->i_activity_date = mdate();
- cl->i_activity_timeout = 10000000LL;
+ cl->i_activity_timeout = I64C(10000000);
cl->i_buffer_size = 10000;
cl->i_buffer = 0;
cl->p_buffer = malloc( cl->i_buffer_size );
httpd_MsgInit( &cl->query );
httpd_MsgInit( &cl->answer );
}
+
void httpd_ClientModeStream( httpd_client_t *cl )
{
cl->i_mode = HTTPD_CLIENT_STREAM;
}
+
void httpd_ClientModeBidir( httpd_client_t *cl )
{
cl->i_mode = HTTPD_CLIENT_BIDIR;
}
+char* httpd_ClientIP( httpd_client_t *cl )
+{
+#ifdef HAVE_GETNAMEINFO
+ char sz_ip[INET6_ADDRSTRLEN + 2];
+ int i;
+
+ if( (cl->sock.ss_family == AF_INET6) &&
+ IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
+ &cl->sock)->sin6_addr) )
+ {
+ /* If client is using IPv4 but server is using IPv6 */
+ struct sockaddr_in a;
+
+ memset( &a, 0, sizeof( a ) );
+ a.sin_family = AF_INET;
+ a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
+ a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
+ &cl->sock)->sin6_addr)[3];
+ i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
+ &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
+ }
+ else
+ i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
+ &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
+ NI_NUMERICHOST );
+
+ if( i != 0 )
+ /* FIXME: msg_Err */
+ return NULL;
+
+ if( strchr( &sz_ip[1], ':' ) != NULL )
+ {
+ *sz_ip = '[';
+ i = strlen( sz_ip );
+ sz_ip[i++] = ']';
+ sz_ip[i] = '\0';
+
+ return strdup( sz_ip );
+ }
+
+ return strdup( &sz_ip[1] );
+
+#else
+ /* FIXME not thread safe */
+ return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
+#endif
+}
+
static void httpd_ClientClean( httpd_client_t *cl )
{
- if( cl->fd > 0 )
+ if( cl->fd >= 0 )
{
- SOCKET_CLOSE( cl->fd );
+ if( cl->p_tls != NULL )
+ tls_ServerSessionClose( cl->p_tls );
+ net_Close( cl->fd );
+ cl->fd = -1;
}
httpd_MsgClean( &cl->answer );
if( cl->p_buffer )
{
free( cl->p_buffer );
+ cl->p_buffer = NULL;
}
}
-static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_in *sock )
+static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
+ int i_sock_size,
+ tls_session_t *p_tls )
{
httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
- /* set this new socket non-block */
-#if defined( WIN32 ) || defined( UNDER_CE )
- {
- unsigned long i_dummy = 1;
- ioctlsocket( fd, FIONBIO, &i_dummy );
- }
-#else
- fcntl( fd, F_SETFL, O_NONBLOCK );
-#endif
cl->i_ref = 0;
cl->fd = fd;
- cl->sock = *sock;
+ memcpy( &cl->sock, sock, sizeof( cl->sock ) );
+ cl->i_sock_size = i_sock_size;
cl->url = NULL;
+ cl->p_tls = p_tls;
httpd_ClientInit( cl );
return cl;
}
+
+static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
+{
+ tls_session_t *p_tls;
+
+ p_tls = cl->p_tls;
+ if( p_tls != NULL)
+ return tls_Recv( p_tls, p, i_len );
+
+ return recv( cl->fd, p, i_len, 0 );
+}
+
+
+static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
+{
+ tls_session_t *p_tls;
+
+ p_tls = cl->p_tls;
+ if( p_tls != NULL)
+ return tls_Send( p_tls, p, i_len );
+
+ return send( cl->fd, p, i_len, 0 );
+}
+
+
static void httpd_ClientRecv( httpd_client_t *cl )
{
int i_len;
if( cl->query.i_proto == HTTPD_PROTO_NONE )
{
/* enought to see if it's rtp over rtsp or RTSP/HTTP */
- i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 4 - cl->i_buffer, 0 );
-
+ i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
+ 4 - cl->i_buffer );
if( i_len > 0 )
{
cl->i_buffer += i_len;
cl->query.i_proto = HTTPD_PROTO_RTSP;
cl->query.i_type = HTTPD_MSG_ANSWER;
}
- else if( !strncmp( cl->p_buffer, "GET", 3 ) || !strncmp( cl->p_buffer, "HEAD", 4 ) || !strncmp( cl->p_buffer, "POST", 4 ) )
+ else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
+ !strncmp( cl->p_buffer, "HEAD", 4 ) ||
+ !strncmp( cl->p_buffer, "POST", 4 ) )
{
cl->query.i_proto = HTTPD_PROTO_HTTP;
cl->query.i_type = HTTPD_MSG_NONE;
else if( cl->query.i_body > 0 )
{
/* we are reading the body of a request or a channel */
- i_len = recv( cl->fd, &cl->query.p_body[cl->i_buffer], cl->query.i_body - cl->i_buffer, 0 );
+ i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
+ cl->query.i_body - cl->i_buffer );
if( i_len > 0 )
{
cl->i_buffer += i_len;
/* we are reading a header -> char by char */
for( ;; )
{
- i_len = recv( cl->fd, &cl->p_buffer[cl->i_buffer], 1, 0 );
+ i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
if( i_len <= 0 )
{
break;
if( cl->query.i_type == HTTPD_MSG_ANSWER )
{
- cl->query.i_status = strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
+ cl->query.i_status =
+ strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
while( *p == ' ' )
{
p++;
for( i = 0; msg_type[i].name != NULL; i++ )
{
- if( !strncmp( cl->p_buffer, msg_type[i].name, strlen( msg_type[i].name ) ) )
+ if( !strncmp( cl->p_buffer, msg_type[i].name,
+ strlen( msg_type[i].name ) ) )
{
p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
cl->query.i_type = msg_type[i].i_type;
}
cl->i_activity_date = mdate();
+ /* XXX: for QT I have to disable timeout. Try to find why */
+ if( cl->query.i_proto == HTTPD_PROTO_RTSP )
+ cl->i_activity_timeout = 0;
+
/* Debugging only */
if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
{
int i;
fprintf( stderr, "received new request\n" );
- fprintf( stderr, " - proto=%s\n", cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
+ fprintf( stderr, " - proto=%s\n",
+ cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
fprintf( stderr, " - version=%d\n", cl->query.i_version );
fprintf( stderr, " - msg=%d\n", cl->query.i_type );
if( cl->query.i_type == HTTPD_MSG_ANSWER )
{
- fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status, cl->query.psz_status );
+ fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
+ cl->query.psz_status );
}
else if( cl->query.i_type != HTTPD_MSG_NONE )
{
}
for( i = 0; i < cl->query.i_name; i++ )
{
- fprintf( stderr, " - option name='%s' value='%s'\n", cl->query.name[i], cl->query.value[i] );
+ fprintf( stderr, " - option name='%s' value='%s'\n",
+ cl->query.name[i], cl->query.value[i] );
}
}
}
+
static void httpd_ClientSend( httpd_client_t *cl )
{
int i;
strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
for( i = 0; i < cl->answer.i_name; i++ )
{
- i_size += strlen( cl->answer.name[i] ) + 2 + strlen( cl->answer.value[i] ) + 2;
+ i_size += strlen( cl->answer.name[i] ) + 2 +
+ strlen( cl->answer.value[i] ) + 2;
}
if( cl->i_buffer_size < i_size )
cl->answer.i_status, cl->answer.psz_status );
for( i = 0; i < cl->answer.i_name; i++ )
{
- p += sprintf( p, "%s: %s\r\n", cl->answer.name[i], cl->answer.value[i] );
+ p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
+ cl->answer.value[i] );
}
p += sprintf( p, "\r\n" );
fprintf( stderr, "%s", cl->p_buffer );
}
- i_len = send( cl->fd, &cl->p_buffer[cl->i_buffer], cl->i_buffer_size - cl->i_buffer, 0 );
+ i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
+ cl->i_buffer_size - cl->i_buffer );
if( i_len > 0 )
{
cl->i_activity_date = mdate();
if( cl->i_buffer >= cl->i_buffer_size )
{
- if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 && !cl->b_read_waiting )
+ if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
+ !cl->b_read_waiting )
{
/* catch more body data */
- int i_msg = cl->query.i_type;
+ int i_msg = cl->query.i_type;
+ int64_t i_offset = cl->answer.i_body_offset;
+
+ httpd_MsgClean( &cl->answer );
+ cl->answer.i_body_offset = i_offset;
+
cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
&cl->answer, &cl->query );
}
}
}
+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;
+
while( !host->b_die )
{
struct timeval timeout;
FD_SET( host->fd, &fds_read );
i_handle_max = host->fd;
+ /* prepare a new TLS session */
+ if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
+ p_tls = tls_ServerSessionPrepare( host->p_tls );
+
/* add all socket that should be read/write and close dead connection */
vlc_mutex_lock( &host->lock );
for( i_client = 0; i_client < host->i_client; i_client++ )
{
httpd_client_t *cl = host->client[i_client];
- if( cl->i_ref < 0 ||
- ( cl->i_ref == 0 &&
- ( cl->i_state == HTTPD_CLIENT_DEAD ||
- cl->i_activity_date + cl->i_activity_timeout < mdate() ) ) )
+ if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
+ ( cl->i_state == HTTPD_CLIENT_DEAD ||
+ ( cl->i_activity_timeout > 0 &&
+ cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
{
- msg_Dbg( host, "connection closed(%s)", inet_ntoa(cl->sock.sin_addr) );
+ char *ip;
- httpd_ClientClean( cl );
+ // FIXME: it sucks to allocate memory on the stack for debug
+ ip = httpd_ClientIP( cl );
+ msg_Dbg( host, "connection closed(%s)",
+ (ip != NULL) ? ip : "unknown" );
+ free( ip );
+ httpd_ClientClean( cl );
TAB_REMOVE( host->i_client, host->client, cl );
+ free( cl );
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 );
httpd_MsgInit( answer );
/* Handle what we received */
- if( cl->i_mode != HTTPD_CLIENT_BIDIR && ( i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL ) )
+ if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
+ (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
{
- /* we can only receive request from client when not in BIDIR mode */
+ /* we can only receive request from client when not
+ * in BIDIR mode */
cl->url = NULL;
cl->i_state = HTTPD_CLIENT_DEAD;
}
else if( i_msg == HTTPD_MSG_ANSWER )
{
- /* We are in BIDIR mode, trigger the callback and then check for new data */
+ /* We are in BIDIR mode, trigger the callback and then
+ * check for new data */
if( cl->url && cl->url->catch[i_msg].cb )
{
- cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, NULL, query );
+ cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
+ cl, NULL, query );
}
cl->i_state = HTTPD_CLIENT_WAITING;
}
else if( i_msg == HTTPD_MSG_CHANNEL )
{
- /* We are in BIDIR mode, trigger the callback and then check for new data */
+ /* We are in BIDIR mode, trigger the callback and then
+ * check for new data */
if( cl->url && cl->url->catch[i_msg].cb )
{
- cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, NULL, query );
+ cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
+ cl, NULL, query );
}
cl->i_state = HTTPD_CLIENT_WAITING;
}
i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
httpd_MsgAdd( answer, "Server", "VLC Server" );
- httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE" );
- httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
+ httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
+ "TEARDOWN, PLAY, PAUSE" );
+ httpd_MsgAdd( answer, "Content-Length", "%d",
+ answer->i_body );
- cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
+ cl->i_buffer = -1; /* Force the creation of the answer in
+ * httpd_ClientSend */
cl->i_state = HTTPD_CLIENT_SENDING;
}
else if( i_msg == HTTPD_MSG_NONE )
if( answer && ( *url->psz_user || *url->psz_password ) )
{
/* create the headers */
- char id[strlen(url->psz_user)+strlen(url->psz_password) + 2];
char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
- char auth[strlen(b64) +1];
+ char *auth;
+ char *id;
+
+ asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
+ auth = malloc( strlen(b64) + 1 );
- sprintf( id, "%s:%s", url->psz_user, url->psz_password );
if( !strncasecmp( b64, "BASIC", 5 ) )
{
b64 += 5;
httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
/* We fail for all url */
b_auth_failed = VLC_TRUE;
+ free( id );
+ free( auth );
break;
}
+
+ free( id );
+ free( auth );
}
if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
p += sprintf( p, "<title>Error 404</title>\n" );
p += sprintf( p, "</head>\n" );
p += sprintf( p, "<body>\n" );
- p += sprintf( p, "<h1><center> 404 Ressource not found(%s)</center></h1>\n", query->psz_url );
+ p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
p += sprintf( p, "<hr />\n" );
p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
p += sprintf( p, "</body>\n" );
}
else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
{
- if( cl->i_mode == HTTPD_CLIENT_FILE )
+ if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
{
cl->url = NULL;
if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
httpd_MsgClean( &cl->answer );
cl->answer.i_body_offset = i_offset;
+ free( cl->p_buffer );
+ cl->p_buffer = NULL;
+ cl->i_buffer = 0;
+ cl->i_buffer_size = 0;
+
cl->i_state = HTTPD_CLIENT_WAITING;
}
}
httpd_MsgInit( &cl->answer );
cl->answer.i_body_offset = i_offset;
- cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl, &cl->answer, &cl->query );
+ cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
+ &cl->answer, &cl->query );
if( cl->answer.i_type != HTTPD_MSG_NONE )
{
/* we have new data, so reenter send mode */
}
/* Special for BIDIR mode we also check reading */
- if( cl->i_mode == HTTPD_CLIENT_BIDIR && cl->i_state == HTTPD_CLIENT_SENDING )
+ if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
+ cl->i_state == HTTPD_CLIENT_SENDING )
{
FD_SET( cl->fd, &fds_read );
i_handle_max = __MAX( i_handle_max, cl->fd );
/* accept new connections */
if( FD_ISSET( host->fd, &fds_read ) )
{
- int i_sock_size = sizeof( struct sockaddr_in );
- struct sockaddr_in sock;
+ int i_sock_size = sizeof( struct sockaddr_storage );
+ struct sockaddr_storage sock;
int fd;
fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
- if( fd > 0 )
+ if( fd >= 0 )
{
- httpd_client_t *cl = httpd_ClientNew( fd, &sock );
+ int i_state = 0;
- vlc_mutex_lock( &host->lock );
- TAB_APPEND( host->i_client, host->client, cl );
- vlc_mutex_unlock( &host->lock );
+ /* set this new socket non-block */
+#if defined( WIN32 ) || defined( UNDER_CE )
+ {
+ unsigned long i_dummy = 1;
+ ioctlsocket( fd, FIONBIO, &i_dummy );
+ }
+#else
+ fcntl( fd, F_SETFL, O_NONBLOCK );
+#endif
+
+ if( p_tls != NULL)
+ {
+ switch ( tls_ServerSessionHandshake( p_tls, fd ) )
+ {
+ 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;
- msg_Dbg( host, "new connection (%s)", inet_ntoa(sock.sin_addr) );
+ case 2: /* missing output */
+ i_state = HTTPD_CLIENT_TLS_HS_OUT;
+ break;
+ }
+ }
+
+ if( fd >= 0 )
+ {
+ char *ip;
+ httpd_client_t *cl;
+
+ cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
+ p_tls = NULL;
+ vlc_mutex_lock( &host->lock );
+ 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)",
+ ip != NULL ? ip : "unknown" );
+ if( ip != NULL)
+ free( ip );
+ }
}
}
/* now try all others socket */
{
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 &&
+ if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
+ cl->i_state == HTTPD_CLIENT_SENDING &&
FD_ISSET( cl->fd, &fds_read ) )
{
cl->b_read_waiting = VLC_TRUE;
}
vlc_mutex_unlock( &host->lock );
}
-}
-
-
-
+ if( p_tls != NULL )
+ tls_ServerSessionClose( p_tls );
+}
+#ifndef HAVE_GETADDRINFO
static int BuildAddr( struct sockaddr_in * p_socket,
const char * psz_address, int i_port )
{
if( !inet_aton( psz_address, &p_socket->sin_addr ) )
#else
p_socket->sin_addr.s_addr = inet_addr( psz_address );
- if( p_socket->sin_addr.s_addr == INADDR_NONE )
+
+/* if( p_socket->sin_addr.s_addr == INADDR_NONE )*/
+ if( p_socket->sin_addr.s_addr == INADDR_BROADCAST )
#endif
{
/* We have a fqdn, try to find its address */
}
/* Copy the first address of the host in the socket address */
- memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
+ memcpy( &((struct sockaddr_in *)p_socket)->sin_addr, p_hostent->h_addr_list[0],
p_hostent->h_length );
}
}
return( 0 );
}
+#endif
+
+static int GetAddrPort( const struct sockaddr_storage *p_ss )
+{
+ int i_port = 0;
+
+ switch (p_ss->ss_family)
+ {
+#ifdef AF_INET6
+ case AF_INET6:
+ i_port = ((const struct sockaddr_in6 *)p_ss)->sin6_port;
+ break;
+#endif
+
+ case AF_INET:
+ i_port = ((const struct sockaddr_in *)p_ss)->sin_port;
+ break;
+
+ default:
+ return -1;
+ }
+
+ return ntohs( i_port );
+}
+
+#else /* ENABLE_HTTPD */
+
+/* We just define an empty wrapper */
+httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
+ tls_server_t *d )
+{
+ msg_Err( a, "HTTP daemon support is disabled" );
+ return 0;
+}
+httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
+{
+ msg_Err( a, "HTTP daemon support is disabled" );
+ return 0;
+}
+void httpd_HostDelete( httpd_host_t *a ){}
+httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
+httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
+ char *d ){ return 0; }
+int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
+ httpd_callback_sys_t *d ){ return 0; }
+void httpd_UrlDelete( httpd_url_t *a ){}
+
+char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
+void httpd_ClientModeStream( httpd_client_t *a ){}
+void httpd_ClientModeBidir( httpd_client_t *a ){}
+
+void httpd_FileDelete( httpd_file_t *a ){}
+httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
+ char *e, httpd_file_callback_t f,
+ httpd_file_sys_t *g ){ return 0; }
+
+void httpd_RedirectDelete( httpd_redirect_t *a ){}
+httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
+ char *b, char *c ){ return 0; }
+
+void httpd_StreamDelete( httpd_stream_t *a ){}
+int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
+int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
+httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
+ char *d, char *e ){ return 0; }
+
+void httpd_MsgInit ( httpd_message_t *a ){}
+void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
+char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
+void httpd_MsgClean( httpd_message_t *a ){}
+
+#endif /* ENABLE_HTTPD */