]> git.sesse.net Git - vlc/blobdiff - src/stream_output/sap.c
Restore some kind of SAP support
[vlc] / src / stream_output / sap.c
index 5a5f6535a895b55478eacea083c2bc7f14304568..573b88d34df740785caed7a61b113ca5eb38b899 100644 (file)
 /*****************************************************************************
  * 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 "charset.h"
+#include "stream_output.h"
 
 /* SAP is always on that port */
 #define SAP_PORT 9875
@@ -56,7 +59,8 @@
 struct sap_address_t
 {
     char *psz_address;
-    char psz_machine[NI_MAXNUMERICHOST];
+    struct sockaddr_storage orig;
+    socklen_t origlen;
     int i_rfd; /* Read socket */
     int i_wfd; /* Write socket */
 
@@ -69,6 +73,18 @@ struct sap_address_t
     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
  *****************************************************************************/
@@ -76,7 +92,7 @@ static void RunThread( vlc_object_t *p_this);
 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
 static char *SDPGenerate( sap_handler_t *p_sap,
                           const session_descriptor_t *p_session,
-                          const sap_address_t *p_addr );
+                          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 );
@@ -88,8 +104,6 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
 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
@@ -146,17 +160,16 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
     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 );
@@ -166,7 +179,7 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
             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 */
@@ -233,51 +246,27 @@ static void RunThread( vlc_object_t *p_this)
 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
                              session_descriptor_t *p_session )
 {
-    int i_header_size, i;
-    char *psz_head, psz_addr[NI_MAXNUMERICHOST];
-    vlc_bool_t b_ipv6 = VLC_FALSE;
+    int i;
+    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 addrinfo hints, *res;
     struct sockaddr_storage addr;
+    socklen_t addrlen;
 
     vlc_mutex_lock( &p_sap->object_lock );
-
-    if( p_session->psz_uri == NULL )
+    addrlen = p_session->addrlen;
+    if ((addrlen == 0) || (addrlen > sizeof (addr)))
     {
         vlc_mutex_unlock( &p_sap->object_lock );
-        msg_Err( p_sap, "*FIXME* unexpected NULL URI for SAP announce" );
-        msg_Err( p_sap, "This should not happen. VLC needs fixing." );
+        msg_Err( p_sap, "No/invalid address specified for SAP announce" );
         return VLC_EGENERIC;
     }
 
     /* Determine SAP multicast address automatically */
-    memset( &hints, 0, sizeof( hints ) );
-    hints.ai_socktype = SOCK_DGRAM;
-    hints.ai_flags = AI_NUMERICHOST;
-
-    i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
-                         &hints, &res );
-    if( i )
-    {
-        vlc_mutex_unlock( &p_sap->object_lock );
-        msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",
-                 p_session->psz_uri, vlc_gai_strerror( i ) );
-        return VLC_EGENERIC;
-    }
-
-    if( (unsigned)res->ai_addrlen > sizeof( addr ) )
-    {
-        vlc_mutex_unlock( &p_sap->object_lock );
-        vlc_freeaddrinfo( res );
-        msg_Err( p_sap, "Unsupported address family of size %d > %u",
-                 res->ai_addrlen, (unsigned) sizeof( addr ) );
-        return VLC_EGENERIC;
-    }
-
-    memcpy( &addr, res->ai_addr, res->ai_addrlen );
+    memcpy (&addr, &p_session->addr, addrlen);
 
-    switch( addr.ss_family )
+    switch( p_session->addr.ss_family )
     {
 #if defined (HAVE_INET_PTON) || defined (WIN32)
         case AF_INET6:
@@ -288,8 +277,13 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
             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 ) )
-                 /* force flags to zero, preserve 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
                 /* Unicast IPv6 - assume global scope */
                 memcpy( a6->s6_addr, "\xff\x0e", 2 );
@@ -321,14 +315,18 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
                 ipv4 = 0;
             else
             /* other addresses => 224.2.127.254 */
+            {
+                /* SSM: 232.0.0.0/8 */
+                b_ssm = (ipv4 >> 24) == 232;
                 ipv4 = 0xe0027ffe;
+            }
 
+            /* FIXME FIXME FIXME */
             if( ipv4 == 0 )
             {
                 msg_Err( p_sap, "Out-of-scope multicast address "
-                        "not supported by SAP: %s", p_session->psz_uri );
+                         "not supported by SAP" );
                 vlc_mutex_unlock( &p_sap->object_lock );
-                vlc_freeaddrinfo( res );
                 return VLC_EGENERIC;
             }
 
@@ -338,15 +336,13 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
 
         default:
             vlc_mutex_unlock( &p_sap->object_lock );
-            vlc_freeaddrinfo( res );
             msg_Err( p_sap, "Address family %d not supported by SAP",
                      addr.ss_family );
             return VLC_EGENERIC;
     }
 
-    i = vlc_getnameinfo( (struct sockaddr *)&addr, res->ai_addrlen,
+    i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
                          psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
-    vlc_freeaddrinfo( res );
 
     if( i )
     {
@@ -359,6 +355,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
 
     /* XXX: Check for dupes */
     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
+    p_sap_session->p_sd = p_session;
     p_sap_session->p_address = NULL;
 
     /* Add the address to the buffer */
@@ -377,28 +374,22 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
                                     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( psz_addr );
-        p_address->i_wfd = net_ConnectUDP( p_sap, psz_addr, SAP_PORT, 255 );
+        p_address->i_wfd = net_ConnectUDP( VLC_OBJECT(p_sap), psz_addr, SAP_PORT, 255 );
         if( p_address->i_wfd != -1 )
         {
-            char *ptr;
-
             net_StopRecv( p_address->i_wfd );
-            net_GetSockAddress( p_address->i_wfd, p_address->psz_machine,
-                                NULL );
-
-            /* removes scope if present */
-            ptr = strchr( p_address->psz_machine, '%' );
-            if( ptr != NULL )
-                *ptr = '\0';
+            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, psz_addr, SAP_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;
@@ -429,46 +420,33 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
         p_sap_session->p_address = p_address;
     }
 
+    if (p_session->origlen == 0)
+        memcpy (&p_session->orig, &p_sap_session->p_address->orig,
+                p_session->origlen = p_sap_session->p_address->origlen);
 
-    /* Build the SAP Headers */
-    i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
-    psz_head = (char *) malloc( i_header_size * sizeof( char ) );
-    if( psz_head == NULL )
+    size_t headsize = 20;
+    switch (p_session->orig.ss_family)
     {
-        msg_Err( p_sap, "out of memory" );
-        return VLC_ENOMEM;
-    }
-
-    /* SAPv1, not encrypted, not compressed */
-    psz_head[0] = b_ipv6 ? 0x30 : 0x20;
-    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 */
-
-#if defined (HAVE_INET_PTON) || defined (WIN32)
-    if( b_ipv6 )
-    {
-        inet_pton( AF_INET6, /* can't fail */
-                   p_sap_session->p_address->psz_machine,
-                   psz_head + 4 );
-    }
-    else
+#ifdef AF_INET6
+        case AF_INET6:
+            headsize += 16;
+            break;
 #endif
-    {
-        inet_pton( AF_INET, /* can't fail */
-                   p_sap_session->p_address->psz_machine,
-                   psz_head + 4 );
+        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;
     }
 
-    memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
-
     /* If needed, build the SDP */
     if( p_session->psz_sdp == NULL )
     {
         p_session->psz_sdp = SDPGenerate( p_sap, p_session,
-                                          p_sap_session->p_address );
+                                          p_sap_session->p_address, b_ssm );
         if( p_session->psz_sdp == NULL )
         {
             vlc_mutex_unlock( &p_sap->object_lock );
@@ -476,21 +454,57 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
         }
     }
 
-    p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
     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;
+    }
+
+    /* Build the SAP Headers */
+    uint8_t *psz_head = p_sap_session->psz_data;
 
-    psz_head[ i_header_size-1 ] = '\0';
-    p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
+    /* SAPv1, not encrypted, not compressed */
+    psz_head[0] = 0x20;
+    psz_head[1] = 0x00; /* No authentification length */
 
-    p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
-                                                 p_sap_session->i_length );
+    i_hash = mdate();
+    psz_head[2] = i_hash >> 8; /* Msg id hash */
+    psz_head[3] = i_hash;      /* Msg id hash 2 */
 
-    /* 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) );
+    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;
+        }
 
-    free( psz_head );
+    }
+
+    memcpy (psz_head + headsize, "application/sdp", 16);
+    headsize += 16;
+
+    /* Build the final message */
+    strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
 
     /* Enqueue the announce */
     INSERT_ELEM( p_sap->pp_sessions,
@@ -500,9 +514,6 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
     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;
@@ -515,20 +526,20 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
     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_session->p_sap->psz_sdp );
-            FREE( p_session->p_sap->psz_data );
-            free( p_session->p_sap );
+            free( p_mysession->psz_data );
+            free( p_mysession );
             break;
         }
     }
@@ -584,57 +595,46 @@ static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
 
 static char *SDPGenerate( sap_handler_t *p_sap,
                           const session_descriptor_t *p_session,
-                          const sap_address_t *p_addr )
+                          const sap_address_t *p_addr, vlc_bool_t b_ssm )
 {
-    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,
-         *psz_sdp;
-    char ipv;
+    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,
+        (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;
 
-    /* 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;
-
-        strlcpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
-        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 */
-    if( asprintf( &psz_sdp,
-                            "v=0\r\n"
-                            "o=- "I64Fd" %d IN IP%c %s\r\n"
-                            "s=%s\r\n"
-                            "t=0 0\r\n"
-                            "c=IN IP%c %s/%d\r\n"
-                            "m=video %d %s %d\r\n"
-                            "a=tool:"PACKAGE_STRING"\r\n"
-                            "a=type:broadcast\r\n"
-                            "%s%s%s",
-                            i_sdp_id, i_sdp_version,
-                            ipv, p_addr->psz_machine,
-                            psz_name, ipv, psz_uri,
-                            /* FIXME: 1 is IPv4 default TTL, not that of IPv6 */
-                            p_session->i_ttl ?: (config_GetInt( p_sap, "ttl" ) ?: 1),
-                            p_session->i_port, 
-                            p_session->b_rtp ? "RTP/AVP" : "udp",
-                            p_session->i_payload,
-                            psz_group ? "a=x-plgroup:" : "",
-                            psz_group ? psz_group : "", psz_group ? "\r\n" : "" ) == -1 )
+    char *plgroup;
+    if ((psz_group == NULL)
+     || (asprintf (&plgroup, "a=x-plgroup:%s\r\n", psz_group) == -1))
+        plgroup = NULL;
+
+    char *sfilter;
+#if 0
+    if ((!b_ssm)
+     || (asprintf (&sfilter, "a=source-filter: incl IN IP%c * %s\r\n",
+                   ipv, p_addr->psz_machine) == -1))
+#else
+# warning FIXME: repair Source Specific Multicast
+#endif
+        sfilter = NULL;
+
+    int res = asprintf (&psz_sdp, "%s" "%s" "%s"
+                        "m=video %d %s\r\n",
+                        head,
+                        plgroup ?: "",
+                        sfilter ?: "",
+                        net_GetPort ((const struct sockaddr *)&p_session->addr),
+                        p_session->sdpformat);
+    free (plgroup);
+    free (sfilter);
+
+    if (res == -1)
         return NULL;
 
     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
@@ -661,7 +661,7 @@ static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
         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 );
+    } while( i_read > 0 );
 
     i_temp = mdate();