/*****************************************************************************
* sap.c : SAP announce handler
*****************************************************************************
- * Copyright (C) 2002-2004 VideoLAN
- * $Id: sap.c 7307 2004-04-07 23:13:03Z fenrir $
+ * Copyright (C) 2002-2005 the VideoLAN team
+ * $Id$
*
- * Authors: Clément Stenac <zorglub@videolan.org>
+ * Authors: Clément Stenac <zorglub@videolan.org>
+ * Rémi 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
*
* 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 <vlc/vlc.h>
+
#include <stdlib.h> /* free() */
#include <stdio.h> /* sprintf() */
#include <string.h> /* strerror() */
+#include <ctype.h> /* tolower(), isxdigit() */
-#include <vlc/vlc.h>
-#include <vlc/sout.h>
+#include <vlc_sout.h>
+#include <vlc_network.h>
+#include <vlc_charset.h>
-#include <network.h>
+#include "stream_output.h"
-#define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
+/* SAP is always on that port */
#define SAP_PORT 9875
-#define SAP_IPV6_ADDR_1 "FF0"
-#define SAP_IPV6_ADDR_2 "::2:7FFE"
-
-#define DEFAULT_IPV6_SCOPE '8'
-
#define DEFAULT_PORT "1234"
#undef EXTRA_DEBUG
+/* SAP Specific structures */
+
+/* 100ms */
+#define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
+#define SAP_MAX_BUFFER 65534
+#define MIN_INTERVAL 2
+#define MAX_INTERVAL 300
+
+/* A SAP announce address. For each of these, we run the
+ * control flow algorithm */
+struct sap_address_t
+{
+ char *psz_address;
+ struct sockaddr_storage orig;
+ socklen_t origlen;
+ 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;
+};
+
+/* A SAP session descriptor, enqueued in the SAP handler queue */
+struct sap_session_t {
+ uint8_t *psz_data;
+ unsigned i_length;
+ sap_address_t *p_address;
+ session_descriptor_t *p_sd;
+
+ /* Last and next send */
+ mtime_t i_last;
+ mtime_t i_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 );
+static char *SDPGenerate( sap_handler_t *p_sap,
+ const session_descriptor_t *p_session,
+ const sap_address_t *p_addr, vlc_bool_t b_ssm );
static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
sap_session_t *p_session );
static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
- session_descriptor_t *p_session,
- announce_method_t *p_method );
+ session_descriptor_t *p_session );
static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
session_descriptor_t *p_session );
-#define FREE( p ) if( p ) { free( p ); (p) = NULL; }
-
/**
* Create the SAP handler
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");
+ msg_Dbg( p_announce, "unable to spawn SAP handler thread");
free( p_sap );
return NULL;
};
for( i = 0 ; i< p_sap->i_sessions ; i++)
{
sap_session_t *p_session = p_sap->pp_sessions[i];
- FREE( p_session->psz_sdp );
- FREE( p_session->psz_data );
+ FREENULL( p_session->psz_data );
REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
- FREE( p_session );
+ FREENULL( p_session );
}
/* Free the remaining addresses */
for( i = 0 ; i< p_sap->i_addresses ; i++)
{
sap_address_t *p_address = p_sap->pp_addresses[i];
- FREE( p_address->psz_address );
+ FREENULL( p_address->psz_address );
if( p_address->i_rfd > -1 )
{
net_Close( p_address->i_rfd );
net_Close( p_address->i_wfd );
}
REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
- FREE( p_address );
+ FREENULL( p_address );
}
/* Free the structure */
/* Add a SAP announce */
static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
- session_descriptor_t *p_session,
- announce_method_t *p_method )
+ 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;
+ char psz_addr[NI_MAXNUMERICHOST];
+ vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_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;
- }
+ vlc_mutex_unlock( &p_sap->object_lock );
+ msg_Err( p_sap, "No/invalid address specified for SAP announce" );
+ return VLC_EGENERIC;
}
- if( !p_method->psz_address )
+ /* Determine SAP multicast address automatically */
+ memcpy (&addr, &p_session->addr, addrlen);
+
+ switch( p_session->addr.ss_family )
{
- if( p_method->i_ip_version == 6 )
+#if defined (HAVE_INET_PTON) || defined (WIN32)
+ case AF_INET6:
{
- char sz_scope;
- if( p_method->psz_ipv6_scope != NULL )
+ /* See RFC3513 for list of valid IPv6 scopes */
+ 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 ) )
{
- sz_scope = *p_method->psz_ipv6_scope;
+ /* SSM <=> ff3x::/32 */
+ b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
+
+ /* force flags to zero, preserve scope */
+ a6->s6_addr[1] &= 0xf;
}
else
- {
- sz_scope = DEFAULT_IPV6_SCOPE;
- }
- p_method->psz_address = (char*)malloc( 30*sizeof(char ));
- sprintf( p_method->psz_address, "%s%c%s",
- SAP_IPV6_ADDR_1, sz_scope, SAP_IPV6_ADDR_2 );
+ /* Unicast IPv6 - assume global scope */
+ memcpy( a6->s6_addr, "\xff\x0e", 2 );
+
+ b_ipv6 = VLC_TRUE;
+ break;
}
- else
+#endif
+
+ case AF_INET:
{
- /* IPv4 */
- p_method->psz_address = (char*)malloc( 15*sizeof(char) );
- sprintf(p_method->psz_address, SAP_IPV4_ADDR );
+ /* See RFC2365 for IPv4 scopes */
+ 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
+ /* 239.255.0.0/16 => 239.255.255.255 */
+ if ((ipv4 & 0xffff0000) == 0xefff0000)
+ ipv4 = 0xefffffff;
+ else
+ /* 239.192.0.0/14 => 239.195.255.255 */
+ if ((ipv4 & 0xfffc0000) == 0xefc00000)
+ ipv4 = 0xefc3ffff;
+ else
+ 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;
+ }
+
+ if( ipv4 == 0 )
+ {
+ msg_Err( p_sap, "Out-of-scope multicast address "
+ "not supported by SAP" );
+ vlc_mutex_unlock( &p_sap->object_lock );
+ return VLC_EGENERIC;
+ }
+
+ ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
+ break;
}
+
+ default:
+ vlc_mutex_unlock( &p_sap->object_lock );
+ msg_Err( p_sap, "Address family %d not supported by SAP",
+ addr.ss_family );
+ return VLC_EGENERIC;
}
- msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address);
+
+ i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
+ psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
+
+ if( i )
+ {
+ vlc_mutex_unlock( &p_sap->object_lock );
+ msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
+ return VLC_EGENERIC;
+ }
+
+ msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
/* XXX: Check for dupes */
p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
-
- p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
- p_sap_session->i_last = 0;
+ p_sap_session->p_sd = p_session;
+ p_sap_session->p_address = NULL;
/* Add the address to the buffer */
- for( i = 0; i< p_sap->i_addresses; i++)
+ for( i = 0; i < p_sap->i_addresses; i++)
{
- if( !strcmp( p_method->psz_address,
- p_sap->pp_addresses[i]->psz_address ) )
+ if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
{
p_sap_session->p_address = p_sap->pp_addresses[i];
- b_found = VLC_TRUE;
break;
}
}
- if( b_found == VLC_FALSE )
+
+ if( p_sap_session->p_address == NULL )
{
sap_address_t *p_address = (sap_address_t *)
malloc( sizeof(sap_address_t) );
if( !p_address )
{
- msg_Err( p_sap, "out of memory" );
+ vlc_mutex_unlock( &p_sap->object_lock );
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 );
+ p_address->psz_address = strdup( psz_addr );
+ p_address->i_wfd = net_ConnectUDP( VLC_OBJECT(p_sap), psz_addr, SAP_PORT, 255 );
+ if( p_address->i_wfd != -1 )
+ {
+ net_StopRecv( p_address->i_wfd );
+ p_address->origlen = sizeof (p_address->orig);
+ getsockname (p_address->i_wfd, (struct sockaddr *)&p_address->orig,
+ &p_address->origlen);
+ }
if( p_sap->b_control == VLC_TRUE )
{
- p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
- p_address->i_port,
- "", 0 );
+ p_address->i_rfd = net_ListenUDP1( (vlc_object_t*)p_sap, psz_addr, SAP_PORT );
+ 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->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_session->p_address = p_address;
}
- /* Build the SAP Headers */
- i_header_size = 8 + strlen( psz_type ) + 1;
- psz_head = (char *) malloc( i_header_size * sizeof( char ) );
- if( ! psz_head )
+ if (p_session->origlen == 0)
+ memcpy (&p_session->orig, &p_sap_session->p_address->orig,
+ p_session->origlen = p_sap_session->p_address->origlen);
+
+ size_t headsize = 20;
+ switch (p_session->orig.ss_family)
+ {
+#ifdef AF_INET6
+ case AF_INET6:
+ headsize += 16;
+ break;
+#endif
+ case AF_INET:
+ headsize += 4;
+ break;
+ default:
+ msg_Err( p_sap, "Address family %d not supported by SAP",
+ addr.ss_family );
+ vlc_mutex_unlock( &p_sap->object_lock );
+ return VLC_EGENERIC;
+ }
+
+ /* If needed, build the SDP */
+ if( p_session->psz_sdp == NULL )
{
- msg_Err( p_sap, "out of memory" );
+ p_session->psz_sdp = SDPGenerate( p_sap, p_session,
+ p_sap_session->p_address, b_ssm );
+ if( p_session->psz_sdp == NULL )
+ {
+ vlc_mutex_unlock( &p_sap->object_lock );
+ return VLC_ENOMEM;
+ }
+ }
+
+ p_sap_session->i_last = 0;
+ p_sap_session->i_length = headsize + strlen (p_session->psz_sdp);
+ p_sap_session->psz_data = malloc (p_sap_session->i_length + 1);
+ if (p_sap_session->psz_data == NULL)
+ {
+ free (p_session->psz_sdp);
+ vlc_mutex_unlock( &p_sap->object_lock );
return VLC_ENOMEM;
}
- psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */
- psz_head[1] = 0x00; /* No authentification */
- psz_head[2] = 0x42; /* Msg id hash */
- psz_head[3] = 0x12; /* Msg id hash 2 */
+ /* Build the SAP Headers */
+ uint8_t *psz_head = p_sap_session->psz_data;
- 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 */
+ /* SAPv1, not encrypted, not compressed */
+ psz_head[0] = 0x20;
+ psz_head[1] = 0x00; /* No authentification length */
- strncpy( psz_head + 8, psz_type, 15 );
- psz_head[ i_header_size-1 ] = '\0';
- p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
+ i_hash = mdate();
+ psz_head[2] = i_hash >> 8; /* Msg id hash */
+ psz_head[3] = i_hash; /* Msg id hash 2 */
- p_sap_session->psz_data = (char *)malloc( sizeof(char)*
- p_sap_session->i_length );
+ headsize = 4;
+ switch (p_session->orig.ss_family)
+ {
+#ifdef AF_INET6
+ case AF_INET6:
+ {
+ 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;
+ }
+#endif
+ case AF_INET:
+ {
+ uint32_t ipv4 =
+ (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
+ memcpy (psz_head + headsize, &ipv4, 4);
+ headsize += 4;
+ break;
+ }
+
+ }
+
+ memcpy (psz_head + headsize, "application/sdp", 16);
+ headsize += 16;
/* 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) );
+ strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
/* 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",
+ msg_Dbg( p_sap,"%i addresses, %i sessions",
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;
int i;
vlc_mutex_lock( &p_sap->object_lock );
- msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
+ msg_Dbg( p_sap, "removing session %p from SAP", p_session);
/* Dequeue the announce */
for( i = 0; i< p_sap->i_sessions; i++)
{
- if( p_session->p_sap == p_sap->pp_sessions[i] )
+ if( p_session == p_sap->pp_sessions[i]->p_sd )
{
+ sap_session_t *p_mysession = p_sap->pp_sessions[i];
REMOVE_ELEM( p_sap->pp_sessions,
p_sap->i_sessions,
i );
+
+ free( p_mysession->psz_data );
+ free( p_mysession );
break;
}
}
- /* XXX: Dequeue the adress too if it is not used anymore
- * TODO: address refcount */
+ /* 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 );
+ msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
vlc_mutex_unlock( &p_sap->object_lock );
if( p_session->i_next < mdate() )
{
#ifdef EXTRA_DEBUG
- msg_Dbg( p_sap, "Sending announce");
+ msg_Dbg( p_sap, "sending announce");
#endif
- i_ret = net_Write( p_sap, p_session->p_address->i_wfd,
+ 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 )
+ if( i_ret != (int)p_session->i_length )
{
msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
- p_session->p_address->psz_address,
- i_ret, p_session->i_length );
+ p_session->p_address->psz_address,
+ i_ret, p_session->i_length );
}
p_session->i_last = p_session->i_next;
p_session->i_next = p_session->i_last
return VLC_SUCCESS;
}
-static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
+static char *SDPGenerate( sap_handler_t *p_sap,
+ const session_descriptor_t *p_session,
+ const sap_address_t *p_addr, vlc_bool_t b_ssm )
{
- p_session->psz_sdp = (char *)malloc(
- sizeof("v=0\n"
- "o=- 12 12 IN IP4 127.0.0.1\n" /* FIXME */
- "s=\n"
- "c=IN IP4 /\n"
- "m=video udp\n"
- "a=tool:VLC "VERSION"\n"
- "a=type:broadcast")
- + strlen( p_session->psz_name )
- + strlen( p_session->psz_uri ) + 300 );
- if( !p_session->psz_sdp )
- {
- msg_Err( p_sap, "out of memory" );
- return VLC_ENOMEM;
- }
- sprintf( p_session->psz_sdp,
- "v=0\n"
- "o=- 12 12 IN IP4 127.0.0.1\n"
- "s=%s\n"
- "c=IN IP4 %s/%d\n"
- "m=video %d udp %d\n"
- "a=tool:VLC "VERSION"\n"
- "a=type:broadcast\n",
- p_session->psz_name,
- p_session->psz_uri, p_session->i_ttl,
- p_session->i_port, p_session->i_payload );
- msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
- p_session->psz_sdp );
- return VLC_SUCCESS;
+ char *psz_group, *psz_name, *psz_sdp;
+
+ char *head = StartSDP (p_session->psz_name, p_session->description,
+ p_session->url, p_session->email, p_session->phone, b_ssm,
+ (const struct sockaddr *)&p_session->orig, p_session->origlen,
+ (const struct sockaddr *)&p_session->addr, p_session->addrlen);
+ if (head == NULL)
+ return NULL;
+
+ psz_group = p_session->psz_group;
+ psz_name = p_session->psz_name;
+
+ char *plgroup;
+ if ((psz_group == NULL)
+ || (asprintf (&plgroup, "a=x-plgroup:%s\r\n", psz_group) == -1))
+ plgroup = NULL;
+
+ const char *comedia = NULL;
+ if (!strncasecmp (p_session->sdpformat, "DCCP", 4)
+ || !strncasecmp (p_session->sdpformat, "TCP", 3))
+ comedia = "a=setup:passive\r\n"
+ "a=connection:new\r\n";
+
+ int res = asprintf (&psz_sdp, "%s" "%s" "%s"
+ "m=video %d %s\r\n",
+ head,
+ plgroup ?: "",
+ comedia ?: "",
+ ntohs (net_GetPort ((const struct sockaddr *)&p_session->addr)),
+ p_session->sdpformat);
+ free (plgroup);
+
+ if (res == -1)
+ return NULL;
+
+ msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
+ psz_sdp );
+ return psz_sdp;
}
static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
{
- int i_read;
- char buffer[SAP_MAX_BUFFER];
- int i_tot = 0;
+ uint8_t buffer[SAP_MAX_BUFFER];
+ ssize_t i_tot = 0;
mtime_t i_temp;
int i_rate;
p_address->t1 = mdate();
return VLC_SUCCESS;
}
- do
+ for (;;)
{
/* Might be too slow if we have huge data */
- i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, buffer,
- SAP_MAX_BUFFER, 0 );
+ ssize_t i_read = recv( p_address->i_rfd, buffer, SAP_MAX_BUFFER, 0 );
+ if (i_read == -1)
+ break;
i_tot += i_read;
- } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
+ }
i_temp = mdate();
p_address->i_interval = MAX_INTERVAL;
}
#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 );
+ msg_Dbg( p_sap,"%s:%i: rate=%i, interval = %i s",
+ p_address->psz_address,SAP_PORT, i_rate, p_address->i_interval );
#endif
p_address->b_ready = VLC_TRUE;