X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fstream_output%2Fsap.c;h=e71906114db4bb0aeca777063a9414205f0317a6;hb=d20c8d6613bfcd2363edfe267bc8456f40fd30bd;hp=46f4daf8ef1031d936da59a53710b3ad11cf817f;hpb=d20e734b043aef614e340e0ff5da2d8b19afaa3e;p=vlc diff --git a/src/stream_output/sap.c b/src/stream_output/sap.c index 46f4daf8ef..e71906114d 100644 --- a/src/stream_output/sap.c +++ b/src/stream_output/sap.c @@ -1,7 +1,7 @@ /***************************************************************************** * sap.c : SAP announce handler ***************************************************************************** - * Copyright (C) 2002-2005 the VideoLAN team + * Copyright (C) 2002-2008 the VideoLAN team * $Id$ * * Authors: Clément Stenac @@ -19,179 +19,152 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** * Preamble *****************************************************************************/ -#include /* free() */ -#include /* sprintf() */ -#include /* strerror() */ -#include /* tolower(), isxdigit() */ -#include -#include - -#include "network.h" -#if defined( WIN32 ) || defined( UNDER_CE ) -# if defined(UNDER_CE) && defined(sockaddr_storage) -# undef sockaddr_storage -# endif -# include -# include -#else -# include +#ifdef HAVE_CONFIG_H +# include "config.h" #endif -#include "charset.h" -/* SAP is always on that port */ -#define SAP_PORT 9875 +#include -#define DEFAULT_PORT "1234" +#include /* free() */ +#include /* sprintf() */ +#include +#include /* tolower(), isxdigit() */ +#include -#undef EXTRA_DEBUG +#include +#include -/* SAP Specific structures */ +#include "stream_output.h" +#include "libvlc.h" -/* 100ms */ -#define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ)) -#define SAP_MAX_BUFFER 65534 -#define MIN_INTERVAL 2 -#define MAX_INTERVAL 300 +/* SAP is always on that port */ +#define IPPORT_SAP 9875 + +/* A SAP session descriptor, enqueued in the SAP handler queue */ +typedef struct sap_session_t +{ + struct sap_session_t *next; + const session_descriptor_t *p_sd; + size_t length; + uint8_t data[]; +} sap_session_t; /* A SAP announce address. For each of these, we run the * control flow algorithm */ -struct sap_address_t +typedef struct sap_address_t { - char *psz_address; - int i_port; - int i_rfd; /* Read socket */ - int i_wfd; /* Write socket */ - - /* Used for flow control */ - mtime_t t1; - vlc_bool_t b_enabled; - vlc_bool_t b_ready; - int i_interval; - int i_buff; - int i_limit; -}; + struct sap_address_t *next; -/***************************************************************************** - * Local prototypes - *****************************************************************************/ -static void RunThread( vlc_object_t *p_this); -static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ); -static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session ); + vlc_thread_t thread; + vlc_mutex_t lock; + vlc_cond_t wait; -static int announce_SendSAPAnnounce( sap_handler_t *p_sap, - sap_session_t *p_session ); + char group[NI_MAXNUMERICHOST]; + struct sockaddr_storage orig; + socklen_t origlen; + int fd; + unsigned interval; + unsigned session_count; + sap_session_t *first; +} sap_address_t; -static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, - session_descriptor_t *p_session, - announce_method_t *p_method ); +/* The SAP handler, running in a separate thread */ +struct sap_handler_t +{ + VLC_COMMON_MEMBERS -static int announce_SAPAnnounceDel( sap_handler_t *p_sap, - session_descriptor_t *p_session ); -static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local ); + vlc_mutex_t lock; + sap_address_t *first; +}; -#define FREE( p ) if( p ) { free( p ); (p) = NULL; } +#define SAP_MAX_BUFFER 65534 +#define MIN_INTERVAL 2 +#define MAX_INTERVAL 300 +/***************************************************************************** + * Local prototypes + *****************************************************************************/ +static void *RunThread (void *); /** * Create the SAP handler * - * \param p_announce the parent announce_handler + * \param p_announce a VLC object * \return the newly created SAP handler or NULL on error */ -sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce ) +sap_handler_t *SAP_Create (vlc_object_t *p_announce) { sap_handler_t *p_sap; - char *psz_charset; - - p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) ); - if( !p_sap ) - { - msg_Err( p_announce, "out of memory" ); + p_sap = vlc_custom_create (p_announce, sizeof (*p_sap), + VLC_OBJECT_GENERIC, "sap sender"); + if (p_sap == NULL) return NULL; - } - - vlc_mutex_init( p_sap, &p_sap->object_lock ); - - vlc_current_charset( &psz_charset ); - p_sap->iconvHandle = vlc_iconv_open( "UTF-8", psz_charset ); - free( psz_charset ); - if( p_sap->iconvHandle == (vlc_iconv_t)(-1) ) - { - msg_Warn( p_sap, "Unable to do requested conversion" ); - } - - p_sap->pf_add = announce_SAPAnnounceAdd; - p_sap->pf_del = announce_SAPAnnounceDel; - p_sap->i_sessions = 0; - p_sap->i_addresses = 0; - p_sap->i_current_session = 0; - - p_sap->b_control = config_GetInt( p_sap, "sap-flow-control"); - - if( vlc_thread_create( p_sap, "sap handler", RunThread, - VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) ) - { - msg_Dbg( p_announce, "Unable to spawn SAP handler thread"); - free( p_sap ); - return NULL; - }; - msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions); + vlc_mutex_init (&p_sap->lock); + p_sap->first = NULL; return p_sap; } -/** - * Destroy the SAP handler - * \param p_this the SAP Handler to destroy - * \return nothing - */ -void announce_SAPHandlerDestroy( sap_handler_t *p_sap ) +void SAP_Destroy (sap_handler_t *p_sap) { - int i; + assert (p_sap->first == NULL); + vlc_mutex_destroy (&p_sap->lock); + vlc_object_release (p_sap); +} - vlc_mutex_destroy( &p_sap->object_lock ); +static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group) +{ + int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255); + if (fd == -1) + return NULL; - /* Free the remaining sessions */ - for( i = 0 ; i< p_sap->i_sessions ; i++) + sap_address_t *addr = malloc (sizeof (*addr)); + if (addr == NULL) { - sap_session_t *p_session = p_sap->pp_sessions[i]; - FREE( p_session->psz_sdp ); - FREE( p_session->psz_data ); - REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i ); - FREE( p_session ); + net_Close (fd); + return NULL; } - /* Free the remaining addresses */ - for( i = 0 ; i< p_sap->i_addresses ; i++) + strlcpy (addr->group, group, sizeof (addr->group)); + addr->fd = fd; + addr->origlen = sizeof (addr->orig); + getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen); + + addr->interval = var_CreateGetInteger (obj, "sap-interval"); + vlc_mutex_init (&addr->lock); + vlc_cond_init (&addr->wait); + addr->session_count = 0; + addr->first = NULL; + + if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW)) { - sap_address_t *p_address = p_sap->pp_addresses[i]; - FREE( p_address->psz_address ); - if( p_address->i_rfd > -1 ) - { - net_Close( p_address->i_rfd ); - } - if( p_address->i_wfd > -1 && p_sap->b_control ) - { - net_Close( p_address->i_wfd ); - } - REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i ); - FREE( p_address ); + msg_Err (obj, "unable to spawn SAP announce thread"); + net_Close (fd); + free (addr); + return NULL; } + return addr; +} - if( p_sap->iconvHandle != (vlc_iconv_t)(-1) ) - vlc_iconv_close( p_sap->iconvHandle ); - - /* Free the structure */ - vlc_object_destroy( p_sap ); +static void AddressDestroy (sap_address_t *addr) +{ + assert (addr->first == NULL); + + vlc_cancel (addr->thread); + vlc_join (addr->thread, NULL); + vlc_cond_destroy (&addr->wait); + vlc_mutex_destroy (&addr->lock); + net_Close (addr->fd); + free (addr); } /** @@ -199,558 +172,293 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap ) * \param p_this the SAP Handler object * \return nothing */ -static void RunThread( vlc_object_t *p_this) +static void *RunThread (void *self) { - sap_handler_t *p_sap = (sap_handler_t*)p_this; - sap_session_t *p_session; + sap_address_t *addr = self; - while( !p_sap->b_die ) + vlc_mutex_lock (&addr->lock); + mutex_cleanup_push (&addr->lock); + + for (;;) { - int i; + sap_session_t *p_session; + mtime_t deadline; - /* If needed, get the rate info */ - if( p_sap->b_control == VLC_TRUE ) - { - for( i = 0 ; i< p_sap->i_addresses ; i++) - { - if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE ) - { - CalculateRate( p_sap, p_sap->pp_addresses[i] ); - } - } - } + while (addr->first == NULL) + vlc_cond_wait (&addr->wait, &addr->lock); - /* Find the session to announce */ - vlc_mutex_lock( &p_sap->object_lock ); - if( p_sap->i_sessions > p_sap->i_current_session + 1) - { - p_sap->i_current_session++; - } - else if( p_sap->i_sessions > 0) - { - p_sap->i_current_session = 0; - } - else - { - vlc_mutex_unlock( &p_sap->object_lock ); - msleep( SAP_IDLE ); - continue; - } - p_session = p_sap->pp_sessions[p_sap->i_current_session]; - vlc_mutex_unlock( &p_sap->object_lock ); + assert (addr->session_count > 0); - /* And announce it */ - if( p_session->p_address->b_enabled == VLC_TRUE && - p_session->p_address->b_ready == VLC_TRUE ) + deadline = mdate (); + for (p_session = addr->first; p_session; p_session = p_session->next) { - announce_SendSAPAnnounce( p_sap, p_session ); - } + send (addr->fd, p_session->data, p_session->length, 0); + deadline += addr->interval * CLOCK_FREQ / addr->session_count; - msleep( SAP_IDLE ); + if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0) + break; /* list may have changed! */ + } } + + vlc_cleanup_pop (); + assert (0); } -/* Add a SAP announce */ -static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, - session_descriptor_t *p_session, - announce_method_t *p_method ) +/** + * Add a SAP announce + */ +int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session) { int i; - char *psz_type = "application/sdp"; - int i_header_size; - char *psz_head; - vlc_bool_t b_found = VLC_FALSE, b_ipv6 = VLC_FALSE; + char psz_addr[NI_MAXNUMERICHOST]; + bool b_ipv6 = false, b_ssm = false; sap_session_t *p_sap_session; mtime_t i_hash; + struct sockaddr_storage addr; + socklen_t addrlen; - vlc_mutex_lock( &p_sap->object_lock ); - - /* If needed, build the SDP */ - if( !p_session->psz_sdp ) + addrlen = p_session->addrlen; + if ((addrlen == 0) || (addrlen > sizeof (addr))) { - if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS ) - { - vlc_mutex_unlock( &p_sap->object_lock ); - return VLC_EGENERIC; - } + msg_Err( p_sap, "No/invalid address specified for SAP announce" ); + return VLC_EGENERIC; } - if( p_method->psz_address == NULL ) - { - /* Determine SAP multicast address automatically */ - char psz_buf[NI_MAXNUMERICHOST], *ptr; - const char *psz_addr; - struct addrinfo hints, *res; - - if( p_session->psz_uri == NULL ) - { - msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" ); - msg_Err( p_sap, "This should not happen. VLC needs fixing." ); - vlc_mutex_unlock( &p_sap->object_lock ); - return VLC_EGENERIC; - } - - /* Canonicalize IP address (e.g. 224.00.010.1 => 224.0.10.1) */ - memset( &hints, 0, sizeof( hints ) ); - hints.ai_socktype = SOCK_DGRAM; - hints.ai_flags = AI_NUMERICHOST; + /* Determine SAP multicast address automatically */ + memcpy (&addr, &p_session->addr, addrlen); - i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0, - &hints, &res ); - if( i == 0 ) - { - i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf, - sizeof( psz_buf ), NULL, NI_NUMERICHOST ); - vlc_freeaddrinfo( res ); - } - - if( i ) - { - msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s", - p_session->psz_uri, vlc_gai_strerror( i ) ); - vlc_mutex_unlock( &p_sap->object_lock ); - return VLC_EGENERIC; - } - - /* Remove interface specification if present */ - ptr = strchr( psz_buf, '%' ); - if( ptr != NULL ) - *ptr = '\0'; - - if( strchr( psz_buf, ':' ) != NULL ) + switch( p_session->addr.ss_family ) + { +#if defined (HAVE_INET_PTON) || defined (WIN32) + case AF_INET6: { - b_ipv6 = VLC_TRUE; - /* See RFC3513 for list of valid IPv6 scopes */ - if( ( tolower( psz_buf[0] ) == 'f' ) - && ( tolower( psz_buf[1] ) == 'f' ) - && isxdigit( psz_buf[2] ) && isxdigit( psz_buf[3] ) ) + struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr; + + memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 ); + if( IN6_IS_ADDR_MULTICAST( a6 ) ) { - /* Multicast IPv6 */ - psz_buf[2] = '0'; /* force flags to zero */ - /* keep scope in psz_addr[3] */ - memcpy( &psz_buf[4], "::2:7ffe", sizeof( "::2:7ffe" ) ); - psz_addr = psz_buf; + /* SSM <=> ff3x::/32 */ + b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000; + + /* force flags to zero, preserve scope */ + a6->s6_addr[1] &= 0xf; } else /* Unicast IPv6 - assume global scope */ - psz_addr = "ff0e::2:7ffe"; + memcpy( a6->s6_addr, "\xff\x0e", 2 ); + + b_ipv6 = true; + break; } - else +#endif + + case AF_INET: { /* See RFC2365 for IPv4 scopes */ - if( memcmp( psz_buf, "224.0.0.", 8 ) == 0 ) - psz_addr = "224.0.0.255"; + uint32_t ipv4; + + ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr ); + /* 224.0.0.0/24 => 224.0.0.255 */ + if ((ipv4 & 0xffffff00) == 0xe0000000) + ipv4 = 0xe00000ff; else - if( memcmp( psz_buf, "239.255.", 8 ) == 0 ) - psz_addr = "239.255.255.255"; + /* 239.255.0.0/16 => 239.255.255.255 */ + if ((ipv4 & 0xffff0000) == 0xefff0000) + ipv4 = 0xefffffff; else - if( ( memcmp( psz_buf, "239.19", 6 ) == 0 ) - && ( ( psz_buf[6] >= '2' ) && ( psz_buf[6] <= '5' ) ) ) - psz_addr = "239.195.255.255"; + /* 239.192.0.0/14 => 239.195.255.255 */ + if ((ipv4 & 0xfffc0000) == 0xefc00000) + ipv4 = 0xefc3ffff; else - /* assume global scope */ - psz_addr = "224.2.127.254"; - } + if ((ipv4 & 0xff000000) == 0xef000000) + ipv4 = 0; + else + /* other addresses => 224.2.127.254 */ + { + /* SSM: 232.0.0.0/8 */ + b_ssm = (ipv4 >> 24) == 232; + ipv4 = 0xe0027ffe; + } - p_method->psz_address = strdup( psz_addr ); - } - else - b_ipv6 = (strchr( p_method->psz_address, ':' ) != NULL); + if( ipv4 == 0 ) + { + msg_Err( p_sap, "Out-of-scope multicast address " + "not supported by SAP" ); + return VLC_EGENERIC; + } - msg_Dbg( p_sap, "using SAP address: %s", p_method->psz_address); + ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 ); + break; + } - /* XXX: Check for dupes */ - p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t)); + default: + msg_Err( p_sap, "Address family %d not supported by SAP", + addr.ss_family ); + return VLC_EGENERIC; + } - p_sap_session->psz_sdp = strdup( p_session->psz_sdp ); - p_sap_session->i_last = 0; + i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, + psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST ); - /* Add the address to the buffer */ - for( i = 0; i< p_sap->i_addresses; i++) + if( i ) { - if( !strcmp( p_method->psz_address, - p_sap->pp_addresses[i]->psz_address ) ) - { - p_sap_session->p_address = p_sap->pp_addresses[i]; - b_found = VLC_TRUE; - break; - } + msg_Err( p_sap, "%s", vlc_gai_strerror( i ) ); + return VLC_EGENERIC; } - if( b_found == VLC_FALSE ) - { - sap_address_t *p_address = (sap_address_t *) - malloc( sizeof(sap_address_t) ); - if( !p_address ) - { - msg_Err( p_sap, "out of memory" ); - return VLC_ENOMEM; - } - p_address->psz_address = strdup( p_method->psz_address ); - p_address->i_port = 9875; - p_address->i_wfd = net_OpenUDP( p_sap, "", 0, - p_address->psz_address, - p_address->i_port ); - if( p_address->i_wfd != -1 ) - net_StopRecv( p_address->i_wfd ); - - if( p_sap->b_control == VLC_TRUE ) - { - p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address, - p_address->i_port, - "", 0 ); - if( p_address->i_rfd != -1 ) - net_StopSend( p_address->i_rfd ); - p_address->i_buff = 0; - p_address->b_enabled = VLC_TRUE; - p_address->b_ready = VLC_FALSE; - p_address->i_limit = 10000; /* 10000 bps */ - p_address->t1 = 0; - } - else - { - p_address->b_enabled = VLC_TRUE; - p_address->b_ready = VLC_TRUE; - p_address->i_interval = config_GetInt( p_sap,"sap-interval"); - p_address->i_rfd = -1; - } - if( p_address->i_wfd == -1 || (p_address->i_rfd == -1 - && p_sap->b_control ) ) - { - msg_Warn( p_sap, "disabling address" ); - p_address->b_enabled = VLC_FALSE; - } + /* Find/create SAP address thread */ + msg_Dbg( p_sap, "using SAP address: %s", psz_addr); - INSERT_ELEM( p_sap->pp_addresses, - p_sap->i_addresses, - p_sap->i_addresses, - p_address ); - p_sap_session->p_address = p_address; - } + vlc_mutex_lock (&p_sap->lock); + sap_address_t *sap_addr; + for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next) + if (!strcmp (psz_addr, sap_addr->group)) + break; - /* Build the SAP Headers */ - i_header_size = ( b_ipv6 ? 20 : 8 ) + strlen( psz_type ) + 1; - psz_head = (char *) malloc( i_header_size * sizeof( char ) ); - if( ! psz_head ) + if (sap_addr == NULL) { - msg_Err( p_sap, "out of memory" ); - return VLC_ENOMEM; + sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr); + if (sap_addr == NULL) + { + vlc_mutex_unlock (&p_sap->lock); + return VLC_EGENERIC; + } + sap_addr->next = p_sap->first; + p_sap->first = sap_addr; } + /* Switch locks. + * NEVER take the global SAP lock when holding a SAP thread lock! */ + vlc_mutex_lock (&sap_addr->lock); + vlc_mutex_unlock (&p_sap->lock); - psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */ - psz_head[1] = 0x00; /* No authentification length */ - - i_hash = mdate(); - psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */ - psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */ + memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen); + p_session->origlen = sap_addr->origlen; - if( b_ipv6 ) + size_t headsize = 20, length; + switch (p_session->orig.ss_family) { - /* in_addr_t ip_server = inet_addr( ip ); */ - psz_head[0] |= 0x10; /* Set IPv6 */ - - psz_head[4] = 0x01; /* Source IP FIXME: we should get the real address */ - psz_head[5] = 0x02; /* idem */ - psz_head[6] = 0x03; /* idem */ - psz_head[7] = 0x04; /* idem */ - - psz_head[8] = 0x01; /* Source IP FIXME: we should get the real address */ - psz_head[9] = 0x02; /* idem */ - psz_head[10] = 0x03; /* idem */ - psz_head[11] = 0x04; /* idem */ - - psz_head[12] = 0x01; /* Source IP FIXME: we should get the real address */ - psz_head[13] = 0x02; /* idem */ - psz_head[14] = 0x03; /* idem */ - psz_head[15] = 0x04; /* idem */ - - psz_head[16] = 0x01; /* Source IP FIXME: we should get the real address */ - psz_head[17] = 0x02; /* idem */ - psz_head[18] = 0x03; /* idem */ - psz_head[19] = 0x04; /* idem */ - - strncpy( psz_head + 20, psz_type, 15 ); +#ifdef AF_INET6 + case AF_INET6: + headsize += 16; + break; +#endif + case AF_INET: + headsize += 4; + break; + default: + assert (0); } - else + + /* XXX: Check for dupes */ + length = headsize + strlen (p_session->psz_sdp); + p_sap_session = malloc (sizeof (*p_sap_session) + length + 1); + if (p_sap_session == NULL) { - /* in_addr_t ip_server = inet_addr( ip) */ - /* Source IP FIXME: we should get the real address */ - psz_head[4] = 0x01; /* ip_server */ - psz_head[5] = 0x02; /* ip_server>>8 */ - psz_head[6] = 0x03; /* ip_server>>16 */ - psz_head[7] = 0x04; /* ip_server>>24 */ - - strncpy( psz_head + 8, psz_type, 15 ); + vlc_mutex_unlock (&sap_addr->lock); + return VLC_EGENERIC; /* NOTE: we should destroy the thread if left unused */ } + p_sap_session->next = sap_addr->first; + sap_addr->first = p_sap_session; + p_sap_session->p_sd = p_session; + p_sap_session->length = length; - psz_head[ i_header_size-1 ] = '\0'; - p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp); - - p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)* - p_sap_session->i_length ); - - /* Build the final message */ - memcpy( p_sap_session->psz_data, psz_head, i_header_size ); - memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp, - strlen( p_sap_session->psz_sdp) ); - - free( psz_head ); - - /* Enqueue the announce */ - INSERT_ELEM( p_sap->pp_sessions, - p_sap->i_sessions, - p_sap->i_sessions, - p_sap_session ); - msg_Dbg( p_sap,"Addresses: %i Sessions: %i", - p_sap->i_addresses,p_sap->i_sessions); - - /* Remember the SAP session for later deletion */ - p_session->p_sap = p_sap_session; - - vlc_mutex_unlock( &p_sap->object_lock ); - - return VLC_SUCCESS; -} + /* Build the SAP Headers */ + uint8_t *psz_head = p_sap_session->data; -/* Remove a SAP Announce */ -static int announce_SAPAnnounceDel( sap_handler_t *p_sap, - session_descriptor_t *p_session ) -{ - int i; - vlc_mutex_lock( &p_sap->object_lock ); + /* SAPv1, not encrypted, not compressed */ + psz_head[0] = 0x20; + psz_head[1] = 0x00; /* No authentification length */ - msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap); + i_hash = mdate(); + psz_head[2] = i_hash >> 8; /* Msg id hash */ + psz_head[3] = i_hash; /* Msg id hash 2 */ - /* Dequeue the announce */ - for( i = 0; i< p_sap->i_sessions; i++) + headsize = 4; + switch (p_session->orig.ss_family) { - if( p_session->p_sap == p_sap->pp_sessions[i] ) +#ifdef AF_INET6 + case AF_INET6: { - REMOVE_ELEM( p_sap->pp_sessions, - p_sap->i_sessions, - i ); - - FREE( p_session->p_sap->psz_sdp ); - FREE( p_session->p_sap->psz_data ); - free( p_session->p_sap ); + struct in6_addr *a6 = + &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr; + memcpy (psz_head + headsize, a6, 16); + psz_head[0] |= 0x10; /* IPv6 flag */ + headsize += 16; break; } - } - - /* XXX: Dequeue the address too if it is not used anymore - * TODO: - address refcount - - send a SAP deletion packet */ - - msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions ); - - vlc_mutex_unlock( &p_sap->object_lock ); - - return VLC_SUCCESS; -} - -static int announce_SendSAPAnnounce( sap_handler_t *p_sap, - sap_session_t *p_session ) -{ - int i_ret; - - /* This announce has never been sent yet */ - if( p_session->i_last == 0 ) - { - p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000; - p_session->i_last = 1; - return VLC_SUCCESS; - } - - if( p_session->i_next < mdate() ) - { -#ifdef EXTRA_DEBUG - msg_Dbg( p_sap, "Sending announce"); #endif - i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL, - p_session->psz_data, - p_session->i_length ); - if( i_ret != p_session->i_length ) + case AF_INET: { - msg_Warn( p_sap, "SAP send failed on address %s (%i %i)", - p_session->p_address->psz_address, - i_ret, p_session->i_length ); + uint32_t ipv4 = + (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr); + memcpy (psz_head + headsize, &ipv4, 4); + headsize += 4; + break; } - p_session->i_last = p_session->i_next; - p_session->i_next = p_session->i_last - + p_session->p_address->i_interval*1000000; - } - else - { - return VLC_SUCCESS; - } - return VLC_SUCCESS; -} -static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session ) -{ - int64_t i_sdp_id = mdate(); - int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff); - char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri; - char ipv; - - psz_group = convert_to_utf8( p_sap, p_session->psz_group ); - psz_name = convert_to_utf8( p_sap, p_session->psz_name ); - - /* FIXME: really check that psz_uri is a real IP address - * FIXME: make a common function to obtain a canonical IP address */ - ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4'; - if( *p_session->psz_uri == '[' ) - { - char *ptr; - - strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) ); - psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0'; - ptr = strchr( psz_uribuf, '%' ); - if( ptr != NULL) - *ptr = '\0'; - ptr = strchr( psz_uribuf, ']' ); - if( ptr != NULL) - *ptr = '\0'; - psz_uri = psz_uribuf; - } - else - psz_uri = p_session->psz_uri; - - /* see the lists in modules/stream_out/rtp.c for compliance stuff */ - p_session->psz_sdp = (char *)malloc( - sizeof("v=0\r\n" - "o=- 45383436098 45398 IN IP4 127.0.0.1\r\n" /* FIXME */ - "s=\r\n" - "t=0 0\r\n" - "c=IN IP4 /\r\n" - "m=video udp\r\n" - "a=tool:"PACKAGE_STRING"\r\n" - "a=type:broadcast\r\n") - + strlen( psz_name ) - + strlen( psz_uri ) + 300 - + ( psz_group ? strlen( psz_group ) : 0 ) ); - - if( p_session->psz_sdp == NULL || psz_name == NULL ) - { - msg_Err( p_sap, "out of memory" ); - FREE( psz_name ); - FREE( psz_group ); - return VLC_ENOMEM; } - sprintf( p_session->psz_sdp, - "v=0\r\n" - "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n" - "s=%s\r\n" - "t=0 0\r\n" - "c=IN IP%c %s/%d\r\n" - "m=video %d udp %d\r\n" - "a=tool:"PACKAGE_STRING"\r\n" - "a=type:broadcast\r\n", - i_sdp_id, i_sdp_version, - psz_name, ipv, - psz_uri, p_session->i_ttl, - p_session->i_port, p_session->i_payload ); - free( psz_name ); - - if( psz_group ) - { - sprintf( p_session->psz_sdp, "%sa=x-plgroup:%s\r\n", - p_session->psz_sdp, psz_group ); - free( psz_group ); - } + memcpy (psz_head + headsize, "application/sdp", 16); + headsize += 16; + + /* Build the final message */ + strcpy( (char *)psz_head + headsize, p_session->psz_sdp); - msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp), - p_session->psz_sdp ); + sap_addr->session_count++; + vlc_cond_signal (&sap_addr->wait); + vlc_mutex_unlock (&sap_addr->lock); return VLC_SUCCESS; } -static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ) +/** + * Remove a SAP Announce + */ +void SAP_Del (sap_handler_t *p_sap, const session_descriptor_t *p_session) { - int i_read; - uint8_t buffer[SAP_MAX_BUFFER]; - int i_tot = 0; - mtime_t i_temp; - int i_rate; - - if( p_address->t1 == 0 ) - { - p_address->t1 = mdate(); - return VLC_SUCCESS; - } - do - { - /* Might be too slow if we have huge data */ - i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer, - SAP_MAX_BUFFER, 0 ); - i_tot += i_read; - } while( i_read > 0 && i_tot < SAP_MAX_BUFFER ); + vlc_mutex_lock (&p_sap->lock); - i_temp = mdate(); + /* TODO: give a handle back in SAP_Add, and use that... */ + sap_address_t *addr, **paddr; + sap_session_t *session, **psession; - /* We calculate the rate every 5 seconds */ - if( i_temp - p_address->t1 < 5000000 ) + paddr = &p_sap->first; + for (addr = p_sap->first; addr; addr = addr->next) { - p_address->i_buff += i_tot; - return VLC_SUCCESS; + psession = &addr->first; + vlc_mutex_lock (&addr->lock); + for (session = addr->first; session; session = session->next) + { + if (session->p_sd == p_session) + goto found; + psession = &session->next; + } + vlc_mutex_unlock (&addr->lock); + paddr = &addr->next; } + assert (0); - /* Bits/second */ - i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) / - (i_temp - p_address->t1 )); - - p_address->i_limit = 10000; +found: + *psession = session->next; - p_address->i_interval = ((1000*i_rate / p_address->i_limit) * - (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL; + if (addr->first == NULL) + /* Last session for this address -> unlink the address */ + *paddr = addr->next; + vlc_mutex_unlock (&p_sap->lock); - if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 ) + if (addr->first == NULL) { - p_address->i_interval = MAX_INTERVAL; + /* Last session for this address -> unlink the address */ + vlc_mutex_unlock (&addr->lock); + AddressDestroy (addr); } -#ifdef EXTRA_DEBUG - msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s", - p_address->psz_address,p_address->i_port, - i_rate, - p_address->i_interval ); -#endif - - p_address->b_ready = VLC_TRUE; - - p_address->t1 = i_temp; - p_address->i_buff = 0; - - return VLC_SUCCESS; -} - - -static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local ) -{ - char *psz_unicode, *psz_in, *psz_out; - size_t ret, i_in, i_out; - - if( psz_local == NULL ) - return NULL; - if ( p_this->iconvHandle == (vlc_iconv_t)(-1) ) - return strdup( psz_local ); - - psz_in = psz_local; - i_in = strlen( psz_local ); - - i_out = 6 * i_in; - psz_unicode = malloc( i_out + 1 ); - if( psz_unicode == NULL ) - return strdup( psz_local ); - psz_out = psz_unicode; - - ret = vlc_iconv( p_this->iconvHandle, - &psz_in, &i_in, &psz_out, &i_out); - if( ret == (size_t)(-1) || i_in ) + else { - msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local ); - return strdup( psz_local ); + addr->session_count--; + vlc_cond_signal (&addr->wait); + vlc_mutex_unlock (&addr->lock); } - *psz_out = '\0'; - return psz_unicode; + + free (session); }