]> git.sesse.net Git - vlc/blobdiff - src/stream_output/sap.c
Basic unit test for URI decoding
[vlc] / src / stream_output / sap.c
index 7a0f1441d76fb891a1edaa6b6404ed7f3ae92f86..4a63b198bd44e892b22c093367ff4f7d25f57518 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * sap.c : SAP announce handler
  *****************************************************************************
- * Copyright (C) 2002-2005 VideoLAN
+ * Copyright (C) 2002-2005 the VideoLAN team
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
@@ -19,7 +19,7 @@
  *
  * 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.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <vlc/sout.h>
 
 #include "network.h"
-#if defined( WIN32 ) || defined( UNDER_CE )
-#   if defined(UNDER_CE) && defined(sockaddr_storage)
-#       undef sockaddr_storage
-#   endif
-#   include <winsock2.h>
-#   include <ws2tcpip.h>
-#else
-#   include <netdb.h>
-#endif
 #include "charset.h"
 
 /* SAP is always on that port */
@@ -65,7 +56,7 @@
 struct sap_address_t
 {
     char *psz_address;
-    int i_port;
+    char psz_machine[NI_MAXNUMERICHOST];
     int i_rfd; /* Read socket */
     int i_wfd; /* Write socket */
 
@@ -83,19 +74,19 @@ struct sap_address_t
  *****************************************************************************/
 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 );
 
 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 );
-static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local );
 
 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
 
@@ -109,7 +100,6 @@ static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local );
 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
 {
     sap_handler_t *p_sap;
-    char *psz_charset;
 
     p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
 
@@ -121,14 +111,6 @@ sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
 
     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;
 
@@ -141,7 +123,7 @@ sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
     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;
     };
@@ -187,9 +169,6 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
         FREE( p_address );
     }
 
-    if( p_sap->iconvHandle != (vlc_iconv_t)(-1) )
-        vlc_iconv_close( p_sap->iconvHandle );
-
     /* Free the structure */
     vlc_object_destroy( p_sap );
 }
@@ -252,131 +231,147 @@ static void RunThread( vlc_object_t *p_this)
 
 /* 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, b_ipv6;
+    int i_header_size, i;
+    char *psz_head, psz_addr[NI_MAXNUMERICHOST];
+    vlc_bool_t b_ipv6 = VLC_FALSE;
     sap_session_t *p_sap_session;
     mtime_t i_hash;
+    struct addrinfo hints, *res;
+    struct sockaddr_storage addr;
 
     vlc_mutex_lock( &p_sap->object_lock );
 
-    /* If needed, build the SDP */
-    if( !p_session->psz_sdp )
+    if( p_session->psz_uri == NULL )
     {
-        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, "*FIXME* unexpected NULL URI for SAP announce" );
+        msg_Err( p_sap, "This should not happen. VLC needs fixing." );
+        return VLC_EGENERIC;
     }
 
-    if( p_method->psz_address == NULL )
-    {
-        /* Determine SAP multicast address automatically */
-        char psz_buf[NI_MAXHOST], *ptr;
-        const char *psz_addr;
-        struct addrinfo hints, *res;
+    /* Determine SAP multicast address automatically */
+    memset( &hints, 0, sizeof( hints ) );
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_flags = AI_NUMERICHOST;
 
-        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;
-        }
+    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;
+    }
 
-        /* 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;
-
-        i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, NULL,
-                             &hints, &res );
-        if( i == 0 )
-            i = vlc_getnameinfo( (vlc_object_t *)p_sap, res->ai_addr,
-                                 res->ai_addrlen, psz_buf, sizeof( psz_buf ),
-                                 NULL, 0, NI_NUMERICHOST );
-        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;
-        }
+    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;
+    }
 
-        /* Remove interface specification if present */
-        ptr = strchr( psz_buf, '%' );
-        if( ptr != NULL )
-            *ptr = '\0';
+    memcpy( &addr, res->ai_addr, res->ai_addrlen );
 
-        if( strchr( psz_buf, ':' ) != NULL )
+    switch( 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] ) )
-            {
-                /* 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;
-            }
+            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 ) )
+                 /* 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 = VLC_TRUE;
+            break;
         }
-        else
-        {
-            b_ipv6 = VLC_FALSE;
+#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
+            /* 239.255.0.0/16 => 239.255.255.255 */
+            if ((ipv4 & 0xffff0000) == 0xefff0000)
+                ipv4 =  0xefffffff;
             else
-            if( memcmp( psz_buf, "239.255.", 8 ) == 0 )
-                psz_addr = "239.255.255.255";
+            /* 239.192.0.0/14 => 239.195.255.255 */
+            if ((ipv4 & 0xfffc0000) == 0xefc00000)
+                ipv4 =  0xefc3ffff;
             else
-            if( ( memcmp( psz_buf, "239.19", 6 ) == 0 )
-             && ( ( psz_buf[6] >= '2' ) && ( psz_buf[6] <= '5' ) ) )
-                psz_addr = "239.195.255.255";
+            if ((ipv4 & 0xff000000) == 0xef000000)
+                ipv4 = 0;
             else
-                /* assume global scope */
-                psz_addr = "224.2.127.254";
+            /* other addresses => 224.2.127.254 */
+                ipv4 = 0xe0027ffe;
+
+            if( ipv4 == 0 )
+            {
+                msg_Err( p_sap, "Out-of-scope multicast address "
+                        "not supported by SAP: %s", p_session->psz_uri );
+                vlc_mutex_unlock( &p_sap->object_lock );
+                vlc_freeaddrinfo( res );
+                return VLC_EGENERIC;
+            }
+
+            ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
+            break;
         }
 
-        p_method->psz_address = strdup( psz_addr );
+        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,
+                         psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
+    vlc_freeaddrinfo( res );
+
+    if( i )
+    {
+        vlc_mutex_unlock( &p_sap->object_lock );
+        msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
+        return VLC_EGENERIC;
     }
-    else
-        b_ipv6 == (strchr( p_method->psz_address, ':' ) != NULL);
 
-    msg_Dbg( p_sap, "using SAP address: %s", p_method->psz_address);
+    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_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) );
@@ -385,17 +380,27 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
             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 );
+        p_address->psz_address = strdup( psz_addr );
+        p_address->i_wfd = net_ConnectUDP( 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';
+        }
 
         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_OpenUDP( p_sap, psz_addr, SAP_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;
@@ -424,66 +429,61 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
         p_sap_session->p_address = p_address;
     }
 
+
     /* Build the SAP Headers */
-    i_header_size = ( b_ipv6 ? 20 : 8 ) + strlen( psz_type ) + 1;
+    i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
-    if( ! psz_head )
+    if( psz_head == NULL )
     {
         msg_Err( p_sap, "out of memory" );
         return VLC_ENOMEM;
     }
 
-    psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */
+    /* 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 )
     {
-        /* 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 );
+        inet_pton( AF_INET6, /* can't fail */
+                   p_sap_session->p_address->psz_machine,
+                   psz_head + 4 );
     }
     else
+#endif
+    {
+        inet_pton( AF_INET, /* can't fail */
+                   p_sap_session->p_address->psz_machine,
+                   psz_head + 4 );
+    }
+
+    memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
+
+    /* If needed, build the SDP */
+    if( p_session->psz_sdp == 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 );
+        p_session->psz_sdp = SDPGenerate( p_sap, p_session,
+                                          p_sap_session->p_address );
+        if( p_session->psz_sdp == NULL )
+        {
+            vlc_mutex_unlock( &p_sap->object_lock );
+            return VLC_ENOMEM;
+        }
     }
 
+    p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
+    p_sap_session->i_last = 0;
+
     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 = (char *)malloc( sizeof(char)*
-                                              p_sap_session->i_length );
+    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 );
@@ -497,7 +497,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
                  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 */
@@ -537,7 +537,7 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
      * 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 );
 
@@ -560,16 +560,16 @@ static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
     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, 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
@@ -582,15 +582,18 @@ static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
     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 )
 {
     int64_t i_sdp_id = mdate();
     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
-    char *psz_group, *psz_name, psz_uribuf[48], *psz_uri;
+    char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
+         *psz_sdp;
     char ipv;
 
-    psz_group = convert_to_utf8( p_sap, p_session->psz_group );
-    psz_name = convert_to_utf8( p_sap, p_session->psz_name );
+    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 */
@@ -613,58 +616,37 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
         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,
+    if( asprintf( &psz_sdp,
                             "v=0\r\n"
-                            "o=- "I64Fd" %d IN IP4 127.0.0.1\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 udp %d\r\n"
+                            "m=video %d %s %d\r\n"
                             "a=tool:"PACKAGE_STRING"\r\n"
-                            "a=type:broadcast\r\n",
+                            "a=type:broadcast\r\n"
+                            "%s%s%s",
                             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 );
-    }
+                            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 )
+        return NULL;
 
-    msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
-                    p_session->psz_sdp );
-    return VLC_SUCCESS;
+    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];
+    uint8_t buffer[SAP_MAX_BUFFER];
     int i_tot = 0;
     mtime_t i_temp;
     int i_rate;
@@ -705,10 +687,8 @@ static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
         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;
@@ -718,34 +698,3 @@ static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
 
     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 )
-    {
-        msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local );
-        return strdup( psz_local );
-    }
-    *psz_out = '\0';
-    return psz_unicode;
-}