]> git.sesse.net Git - vlc/blobdiff - modules/services_discovery/sap.c
* Do not take and release the structure lock for each element of the list while
[vlc] / modules / services_discovery / sap.c
index fe639004c5bc7864771aca27aa6e6930e63d29ab..b4850490d202bd85d5620c5edead3d1a458c976e 100644 (file)
 /*****************************************************************************
  * Includes
  *****************************************************************************/
+#define _GNU_SOURCE
 #include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
-#include <vlc/intf.h>
+#include <vlc_playlist.h>
+#include <vlc_demux.h>
 
-#include <network.h>
-#include <charset.h>
+#include <vlc_network.h>
+#include <vlc_charset.h>
 
 #include <ctype.h>
 #include <errno.h>
@@ -224,7 +225,6 @@ struct services_discovery_sys_t
     /* playlist node */
     playlist_item_t *p_node_cat;
     playlist_item_t *p_node_one;
-    playlist_t *p_playlist;
 
     /* Table of announces */
     int i_announces;
@@ -255,26 +255,18 @@ struct demux_sys_t
 
 /* Main parsing functions */
     static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp );
-    static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read );
-    static sdp_t *  ParseSDP( vlc_object_t *p_sd, char* psz_sdp );
+    static int ParseSAP( services_discovery_t *p_sd, const uint8_t *p_buffer, size_t i_read );
+    static sdp_t *ParseSDP (vlc_object_t *p_sd, const char *psz_sdp);
     static sap_announce_t *CreateAnnounce( services_discovery_t *, uint16_t, sdp_t * );
     static int RemoveAnnounce( services_discovery_t *p_sd, sap_announce_t *p_announce );
 
-/* Cache */
-    static void CacheLoad( services_discovery_t *p_sd );
-    static void CacheSave( services_discovery_t *p_sd );
 /* Helper functions */
     static char *GetAttribute( sdp_t *p_sdp, const char *psz_search );
     static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 );
-    static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port );
-#ifdef HAVE_ZLIB_H
-    static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );
-#endif
+    static int InitSocket( services_discovery_t *p_sd, const char *psz_address, int i_port );
+    static int Decompress( const unsigned char *psz_src, unsigned char **_dst, int i_len );
     static void FreeSDP( sdp_t *p_sdp );
 
-
-#define FREE( p ) \
-    if( p ) { free( p ); (p) = NULL; }
 /*****************************************************************************
  * Open: initialize and create stuff
  *****************************************************************************/
@@ -284,9 +276,6 @@ static int Open( vlc_object_t *p_this )
     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)
                                 malloc( sizeof( services_discovery_sys_t ) );
 
-    playlist_view_t     *p_view;
-    vlc_value_t         val;
-
     p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" );
 
     p_sd->pf_run = Run;
@@ -310,18 +299,12 @@ static int Open( vlc_object_t *p_this )
             ? VLC_TRUE : VLC_FALSE;
 
     /* Create our playlist node */
-    p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
-                                                       VLC_OBJECT_PLAYLIST,
-                                                       FIND_ANYWHERE );
-    if( !p_sys->p_playlist )
-    {
-        msg_Warn( p_sd, "unable to find playlist, cancelling SAP listening");
-        return VLC_EGENERIC;
-    }
-
-    playlist_NodesCreateForSD( p_sys->p_playlist, _("SAP sessions"),
-                               &p_sys->p_node_cat, &p_sys->p_node_one );
+    pl_Yield( p_sd );
 
+    playlist_NodesPairCreate( pl_Get( p_sd ), _("SAP sessions"),
+                              &p_sys->p_node_cat, &p_sys->p_node_one,
+                              VLC_TRUE );
+    p_sys->p_node_cat->p_input->b_prefers_tree = VLC_TRUE;
     p_sys->i_announces = 0;
     p_sys->pp_announces = NULL;
 
@@ -414,12 +397,12 @@ static int OpenDemux( vlc_object_t *p_this )
     p_demux->pf_control = Control;
     p_demux->pf_demux = Demux;
 
-    free( psz_sdp );
+    FREENULL( psz_sdp );
     return VLC_SUCCESS;
 
 error:
-    free( psz_sdp );
-    if( p_sdp ) FreeSDP( p_sdp );
+    FREENULL( psz_sdp );
+    if( p_sdp ) FreeSDP( p_sdp ); p_sdp = NULL;
     stream_Seek( p_demux->s, 0 );
     return VLC_EGENERIC;
 }
@@ -438,7 +421,7 @@ static void Close( vlc_object_t *p_this )
     {
         net_Close( p_sys->pi_fd[i] );
     }
-    FREE( p_sys->pi_fd );
+    FREENULL( p_sys->pi_fd );
 
 #if 0
     if( config_GetInt( p_sd, "sap-cache" ) )
@@ -451,17 +434,13 @@ static void Close( vlc_object_t *p_this )
     {
         RemoveAnnounce( p_sd, p_sys->pp_announces[i] );
     }
-    FREE( p_sys->pp_announces );
-
-    if( p_sys->p_playlist )
-    {
-        playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE,
-                             VLC_TRUE );
-        playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE,
-                             VLC_TRUE );
-        vlc_object_release( p_sys->p_playlist );
-    }
+    FREENULL( p_sys->pp_announces );
 
+    playlist_NodeDelete( pl_Get(p_sd), p_sys->p_node_cat, VLC_TRUE,
+                         VLC_TRUE );
+    playlist_NodeDelete( pl_Get(p_sd), p_sys->p_node_one, VLC_TRUE,
+                         VLC_TRUE );
+    pl_Release( p_sd );
     free( p_sys );
 }
 
@@ -470,7 +449,12 @@ static void Close( vlc_object_t *p_this )
  *****************************************************************************/
 static void CloseDemux( vlc_object_t *p_this )
 {
-
+    demux_t *p_demux = (demux_t *)p_this;
+    if( p_demux->p_sys )
+    {
+        if( p_demux->p_sys->p_sdp ) { FreeSDP( p_demux->p_sys->p_sdp ); p_demux->p_sys->p_sdp = NULL; }
+        free( p_demux->p_sys );
+    }
 }
 
 /*****************************************************************************
@@ -514,6 +498,7 @@ static void Run( services_discovery_t *p_sd )
     if( psz_addr && *psz_addr )
     {
         InitSocket( p_sd, psz_addr, SAP_PORT );
+        free( psz_addr );
     }
 
     if( p_sd->p_sys->i_fd == 0 )
@@ -539,18 +524,7 @@ static void Run( services_discovery_t *p_sd )
 
             if( mdate() - p_sd->p_sys->pp_announces[i]->i_last > i_timeout )
             {
-                struct sap_announce_t *p_announce;
-                p_announce = p_sd->p_sys->pp_announces[i];
-
-                /* Remove the playlist item */
-                playlist_LockDeleteAllFromInput( p_sd->p_sys->p_playlist,
-                                                 p_announce->i_input_id );
-
-                /* Remove the sap_announce from the array */
-                REMOVE_ELEM( p_sd->p_sys->pp_announces,
-                           p_sd->p_sys->i_announces, i );
-
-                free( p_announce );
+                RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i] );
             }
         }
 
@@ -578,17 +552,38 @@ static void Run( services_discovery_t *p_sd )
 static int Demux( demux_t *p_demux )
 {
     sdp_t *p_sdp = p_demux->p_sys->p_sdp;
-    playlist_t *p_playlist;
+    input_thread_t *p_input;
+    input_item_t *p_parent_input;
 
-    p_playlist = (playlist_t *)vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
-                                               FIND_ANYWHERE );
+    playlist_t *p_playlist = pl_Yield( p_demux );
+    p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT,
+                                                 FIND_PARENT );
+    assert( p_input );
+    if( !p_input )
+    {
+        msg_Err( p_demux, "parent input could not be found" );
+        return VLC_EGENERIC;
+    }
+
+    p_parent_input = input_GetItem(p_input);
 
-    /* TODO FIXME !! Add at the correct place */
-    playlist_PlaylistAdd( p_playlist, p_sdp->psz_uri, p_sdp->psz_sessionname,
-                          PLAYLIST_APPEND, PLAYLIST_END );
+    vlc_mutex_lock( &p_parent_input->lock );
+    FREENULL( p_parent_input->psz_uri );
+    p_parent_input->psz_uri = strdup( p_sdp->psz_uri );
+    FREENULL( p_parent_input->psz_name );
+    p_parent_input->psz_name = strdup( EnsureUTF8( p_sdp->psz_sessionname ) );
+    p_parent_input->i_type = ITEM_TYPE_NET;
 
+    if( p_playlist->status.p_item &&
+             p_playlist->status.p_item->p_input == p_parent_input )
+    {
+        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
+                          p_playlist->status.p_node, p_playlist->status.p_item );
+    }
+
+    vlc_mutex_unlock( &p_parent_input->lock );
+    vlc_object_release( p_input );
     vlc_object_release( p_playlist );
-    if( p_sdp ) FreeSDP( p_sdp );
 
     return VLC_SUCCESS;
 }
@@ -603,44 +598,37 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
  **************************************************************/
 
 /* i_read is at least > 6 */
-static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
+static int ParseSAP( services_discovery_t *p_sd, const uint8_t *buf,
+                     size_t len )
 {
-    int                 i_version, i_address_type, i_hash, i;
-    char                *psz_sdp, *psz_foo, *psz_initial_sdp;
+    int i;
+    const char          *psz_sdp;
+    const uint8_t *end = buf + len;
     sdp_t               *p_sdp;
-    vlc_bool_t          b_compressed;
-    vlc_bool_t          b_need_delete = VLC_FALSE;
 
-    /* First, check the sap announce is correct */
-    i_version = p_buffer[0] >> 5;
-    if( i_version != 1 )
-    {
-       msg_Dbg( p_sd, "strange sap version %d found", i_version );
-    }
+    assert (buf[len] == '\0');
 
-    i_address_type = p_buffer[0] & 0x10;
+    if (len < 4)
+        return VLC_EGENERIC;
 
-    if( (p_buffer[0] & 0x08) != 0 )
-    {
-        msg_Dbg( p_sd, "reserved bit incorrectly set" );
+    uint8_t flags = buf[0];
+
+    /* First, check the sap announce is correct */
+    if ((flags >> 5) != 1)
         return VLC_EGENERIC;
-    }
 
-    if( (p_buffer[0] & 0x04) != 0 )
-    {
-        msg_Dbg( p_sd, "session deletion packet" );
-        b_need_delete = VLC_TRUE;
-    }
+    vlc_bool_t b_ipv6 = (flags & 0x10) != 0;
+    vlc_bool_t b_need_delete = (flags & 0x04) != 0;
 
-    if( p_buffer[0] & 0x02  )
+    if (flags & 0x02)
     {
         msg_Dbg( p_sd, "encrypted packet, unsupported" );
         return VLC_EGENERIC;
     }
 
-    b_compressed = p_buffer[0] & 0x01;
+    vlc_bool_t b_compressed = (flags & 0x01) != 0;
 
-    i_hash = ( p_buffer[2] << 8 ) + p_buffer[3];
+    uint16_t i_hash = U16_AT (buf + 2);
 
     if( p_sd->p_sys->b_strict && i_hash == 0 )
     {
@@ -648,80 +636,54 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
         return VLC_EGENERIC;
     }
 
-    psz_sdp  = (char *)p_buffer + 4;
-    psz_initial_sdp = psz_sdp;
+    // Skips source address and auth data
+    buf += 4 + (b_ipv6 ? 16 : 4) + buf[1];
+    if (buf > end)
+        return VLC_EGENERIC;
 
-    if( i_address_type == 0 ) /* ipv4 source address */
-    {
-        psz_sdp += 4;
-        if( i_read <= 9 )
-        {
-            msg_Warn( p_sd, "too short SAP packet" );
-            return VLC_EGENERIC;
-        }
-    }
-    else /* ipv6 source address */
+    uint8_t *decomp = NULL;
+    if( b_compressed )
     {
-        psz_sdp += 16;
-        if( i_read <= 21 )
+        int newsize = Decompress (buf, &decomp, end - buf);
+        if (newsize < 0)
         {
-            msg_Warn( p_sd, "too short SAP packet" );
+            msg_Warn( p_sd, "decompression of sap packet failed" );
             return VLC_EGENERIC;
         }
-    }
 
-    if( b_compressed )
-    {
-#ifdef HAVE_ZLIB_H
-        uint8_t *p_decompressed_buffer = NULL;
-        int      i_decompressed_size;
+        decomp = realloc (decomp, newsize + 1);
+        decomp[newsize] = '\0';
 
-        i_decompressed_size = Decompress( (uint8_t *)psz_sdp,
-                   &p_decompressed_buffer, i_read - ( psz_sdp - (char *)p_buffer ) );
-        if( i_decompressed_size > 0 && i_decompressed_size < MAX_SAP_BUFFER )
-        {
-            memcpy( psz_sdp, p_decompressed_buffer, i_decompressed_size );
-            psz_sdp[i_decompressed_size] = '\0';
-            free( p_decompressed_buffer );
-        }
-#else
-        msg_Warn( p_sd, "ignoring compressed sap packet" );
-        return VLC_EGENERIC;
-#endif
+        psz_sdp = (const char *)decomp;
+        len = newsize;
     }
-
-    /* Add the size of authentification info */
-    if( i_read < p_buffer[1] + (psz_sdp - psz_initial_sdp ) )
+    else
     {
-        msg_Warn( p_sd, "too short SAP packet\n");
-        return VLC_EGENERIC;
+        psz_sdp = (const char *)buf;
+        len = end - buf;
     }
-    psz_sdp += p_buffer[1];
-    psz_foo = psz_sdp;
+
+    /* len is a strlen here here. both buf and decomp are len+1 where the 1 should be a \0 */
+    assert( psz_sdp[len] == '\0');
 
     /* Skip payload type */
-    /* Handle announces without \0 between SAP and SDP */
-    while( *psz_sdp != '\0' && ( psz_sdp[0] != 'v' && psz_sdp[1] != '=' ) )
+    /* SAPv1 has implicit "application/sdp" payload type: first line is v=0 */
+    if (strncmp (psz_sdp, "v=0", 3))
     {
-        if( psz_sdp - psz_initial_sdp >= i_read - 5 )
+        size_t clen = strlen (psz_sdp) + 1;
+
+        if (strcmp (psz_sdp, "application/sdp"))
         {
-            msg_Warn( p_sd, "empty SDP ?");
+            msg_Dbg (p_sd, "unsupported content type: %s", psz_sdp);
+            return VLC_EGENERIC;
         }
-        psz_sdp++;
-    }
 
-    if( *psz_sdp == '\0' )
-    {
-        psz_sdp++;
-    }
-    if( ( psz_sdp != psz_foo ) && strcasecmp( psz_foo, "application/sdp" ) )
-    {
-        msg_Dbg( p_sd, "unhandled content type: %s", psz_foo );
-    }
-    if( ( psz_sdp - (char *)p_buffer ) >= i_read )
-    {
-        msg_Warn( p_sd, "package without content" );
-        return VLC_EGENERIC;
+        // skips content type
+        if (len <= clen)
+            return VLC_EGENERIC;
+
+        len -= clen;
+        psz_sdp += clen;
     }
 
     /* Parse SDP info */
@@ -760,14 +722,13 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
             if( b_need_delete )
             {
                 RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i]);
-                return VLC_SUCCESS;
             }
             else
             {
                 p_sd->p_sys->pp_announces[i]->i_last = mdate();
-                FreeSDP( p_sdp );
-                return VLC_SUCCESS;
             }
+            FreeSDP( p_sdp ); p_sdp = NULL;
+            return VLC_SUCCESS;
         }
     }
     /* Add item */
@@ -778,6 +739,7 @@ static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
 
     CreateAnnounce( p_sd, i_hash, p_sdp );
 
+    FREENULL (decomp);
     return VLC_SUCCESS;
 }
 
@@ -813,16 +775,16 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
     }
 
     if( p_sys->b_timeshift )
-        vlc_input_item_AddOption( p_input, ":access-filter=timeshift" );
+        input_ItemAddOption( p_input, ":access-filter=timeshift" );
 
     psz_value = GetAttribute( p_sap->p_sdp, "tool" );
     if( psz_value != NULL )
     {
-        vlc_input_item_AddInfo( p_input, _("Session"),_("Tool"), psz_value );
+        input_ItemAddInfo( p_input, _("Session"),_("Tool"), psz_value );
     }
     if( strcmp( p_sdp->psz_username, "-" ) )
     {
-        vlc_input_item_AddInfo( p_input, _("Session"),
+        input_ItemAddInfo( p_input, _("Session"),
                                 _("User"), p_sdp->psz_username );
     }
 
@@ -839,8 +801,8 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
 
         if( p_child == NULL )
         {
-            p_child = playlist_NodeCreate( p_sys->p_playlist, psz_value,
-                                           p_sys->p_node_cat );
+            p_child = playlist_NodeCreate( pl_Get( p_sd ), psz_value,
+                                           p_sys->p_node_cat, 0 );
             p_child->i_flags &= ~PLAYLIST_SKIP_FLAG;
         }
     }
@@ -849,13 +811,13 @@ sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
         p_child = p_sys->p_node_cat;
     }
 
-    p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input, p_child,
+    p_item = playlist_NodeAddInput( pl_Get( p_sd ), p_input, p_child,
                                     PLAYLIST_APPEND, PLAYLIST_END );
     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
     p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
     p_sap->i_item_id_cat = p_item->i_id;
 
-    p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
+    p_item = playlist_NodeAddInput( pl_Get( p_sd ), p_input,
                         p_sys->p_node_one, PLAYLIST_APPEND, PLAYLIST_END );
     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
     p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
@@ -885,72 +847,45 @@ static char *GetAttribute( sdp_t *p_sdp, const char *psz_search )
 /* Fill p_sdp->psz_uri */
 static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
 {
-    char *psz_eof;
-    char *psz_parse;
-    char *psz_uri = NULL;
+    char *psz_eof = NULL;
+    char *psz_parse = NULL;
+    char psz_uri[1026];
     char *psz_proto = NULL;
-    char psz_source[256];
     int i_port = 0;
 
     /* Parse c= field */
     if( p_sdp->psz_connection )
     {
-        psz_parse = p_sdp->psz_connection;
-
-        psz_eof = strchr( psz_parse, ' ' );
-
-        if( psz_eof )
+        char hostname[1024];
+        int ipv;
+
+        /*
+         * NOTE: we ignore the TTL parameter on-purpose, as some SAP
+         * advertisers don't include it (and it is utterly useless).
+         */
+        if (sscanf (p_sdp->psz_connection, "IN IP%d %1023[^/]", &ipv,
+                    hostname) != 2)
         {
-            *psz_eof = '\0';
-            psz_parse = psz_eof + 1;
-        }
-        else
-        {
-            msg_Warn( p_obj, "unable to parse c field (1)");
+            msg_Warn (p_obj, "unable to parse c field: \"%s\"",
+                      p_sdp->psz_connection);
             return VLC_EGENERIC;
         }
 
-        psz_eof = strchr( psz_parse, ' ' );
-
-        if( psz_eof )
-        {
-            *psz_eof = '\0';
-            if( !strncmp( psz_parse, "IP4", 3 ) )
-            {
-                p_sdp->i_in = 4;
-            }
-            else if( !strncmp( psz_parse, "IP6", 3 ) )
-            {
-                p_sdp->i_in = 6;
-            }
-            else
-            {
-                p_sdp->i_in = 0;
-            }
-            psz_parse = psz_eof + 1;
-        }
-        else
+        switch (ipv)
         {
-            msg_Warn( p_obj, "unable to parse c field (2)");
-            return VLC_EGENERIC;
-        }
-
-        psz_eof = strchr( psz_parse, '/' );
+            case 4:
+            case 6:
+                break;
 
-        if( psz_eof )
-        {
-            *psz_eof = '\0';
-        }
-        else
-        {
-            msg_Dbg( p_obj, "incorrect c field, %s", p_sdp->psz_connection );
-        }
-        if( p_sdp->i_in == 6 && ( isxdigit( *psz_parse ) || *psz_parse == ':' ) )
-        {
-            asprintf( &psz_uri, "[%s]", psz_parse );
+            default:
+                msg_Warn (p_obj, "unknown IP version %d", ipv);
+                return VLC_EGENERIC;
         }
-        else psz_uri = strdup( psz_parse );
 
+        if (strchr (hostname, ':') != NULL)
+            sprintf (psz_uri, "[%s]", hostname);
+        else
+            strcpy (psz_uri, hostname);
     }
 
     /* Parse m= field */
@@ -964,11 +899,16 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         {
             *psz_eof = '\0';
 
-            if( strncmp( psz_parse, "audio", 5 )  &&
-                strncmp( psz_parse, "video", 5 ) )
+            /*
+             * That's ugly. We should go through every media, and make sure
+             * at least one of them is audio or video. In the mean time, I
+             * need to accept data too.
+             */
+            if( strncmp( psz_parse, "audio", 5 )
+             && strncmp( psz_parse, "video", 5 )
+             && strncmp( psz_parse, "data", 4 ) )
             {
-                msg_Warn( p_obj, "unhandled media type -%s-", psz_parse );
-                FREE( psz_uri );
+                msg_Warn( p_obj, "unhandled media type \"%s\"", psz_parse );
                 return VLC_EGENERIC;
             }
 
@@ -977,7 +917,6 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         else
         {
             msg_Warn( p_obj, "unable to parse m field (1)");
-            FREE( psz_uri );
             return VLC_EGENERIC;
         }
 
@@ -1000,7 +939,6 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         else
         {
             msg_Warn( p_obj, "unable to parse m field (2)");
-            FREE( psz_uri );
             return VLC_EGENERIC;
         }
 
@@ -1034,8 +972,6 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
         psz_proto = strdup( "udp" );
     }
 
-    /* FIXME: HTTP support */
-
     if( i_port == 0 )
     {
         i_port = 1234;
@@ -1043,15 +979,24 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
 
     /* handle SSM case */
     psz_parse = GetAttribute( p_sdp, "source-filter" );
-    psz_source[0] = '\0';
+    char psz_source[258] = "";
+    if (psz_parse != NULL)
+    {
+        char psz_source_ip[256];
 
-    if( psz_parse ) sscanf( psz_parse, " incl IN IP%*s %*s %255s ", psz_source);
+        if (sscanf (psz_parse, " incl IN IP%*c %*s %255s ", psz_source_ip) == 1)
+        {
+            if (strchr (psz_source_ip, ':') != NULL)
+                sprintf (psz_source, "[%s]", psz_source_ip);
+            else
+                strcpy (psz_source, psz_source_ip);
+        }
+    }
 
     asprintf( &p_sdp->psz_uri, "%s://%s@%s:%i", psz_proto, psz_source,
               psz_uri, i_port );
 
-    FREE( psz_uri );
-    FREE( psz_proto );
+    FREENULL( psz_proto );
     return VLC_SUCCESS;
 }
 
@@ -1060,7 +1005,7 @@ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
  * *********************************************************************
  * Validate SDP and parse all fields
  ***********************************************************************/
-static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
+static sdp_t *ParseSDP (vlc_object_t *p_obj, const char *psz_sdp)
 {
     sdp_t *p_sdp;
     vlc_bool_t b_invalid = VLC_FALSE;
@@ -1080,6 +1025,9 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
     if( p_sdp == NULL )
         return NULL;
 
+    /* init to 0 */
+    memset( p_sdp, 0, sizeof( sdp_t ) );
+
     p_sdp->psz_sdp = strdup( psz_sdp );
     if( p_sdp->psz_sdp == NULL )
     {
@@ -1087,23 +1035,12 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
         return NULL;
     }
 
-    p_sdp->psz_sessionname = NULL;
-    p_sdp->psz_media       = NULL;
-    p_sdp->psz_connection  = NULL;
-    p_sdp->psz_uri         = NULL;
-    p_sdp->psz_address     = NULL;
-    p_sdp->psz_address_type= NULL;
-
-    p_sdp->i_media         = 0;
-    p_sdp->i_attributes    = 0;
-    p_sdp->pp_attributes   = NULL;
-
     while( *psz_sdp != '\0' && b_end == VLC_FALSE  )
     {
-        char *psz_eol;
-        char *psz_eof;
-        char *psz_parse;
-        char *psz_sess_id;
+        char *psz_eol = NULL;
+        char *psz_eof = NULL;
+        char *psz_parse = NULL;
+        char *psz_sess_id = NULL;
 
         while( *psz_sdp == '\r' || *psz_sdp == '\n' ||
                *psz_sdp == ' ' || *psz_sdp == '\t' )
@@ -1113,7 +1050,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
 
         if( ( psz_eol = strchr( psz_sdp, '\n' ) ) == NULL )
         {
-            psz_eol = psz_sdp + strlen( psz_sdp );
+            psz_eol = (char *)psz_sdp + strlen( psz_sdp );
             b_end = VLC_TRUE;
         }
         if( psz_eol > psz_sdp && *( psz_eol - 1 ) == '\r' )
@@ -1131,7 +1068,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
         if( psz_sdp[1] != '=' )
         {
             msg_Warn( p_obj, "invalid packet" ) ;
-            FreeSDP( p_sdp );
+            FreeSDP( p_sdp ); p_sdp = NULL;
             return NULL;
         }
 
@@ -1169,16 +1106,16 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
                 psz_parse = psz_eof + 1; i_field++;
 
 
-                psz_parse = &psz_sdp[2];
+                psz_parse = (char *)&psz_sdp[2];
                 GET_FIELD( p_sdp->psz_username );
                 GET_FIELD( psz_sess_id );
 
                 p_sdp->i_session_id = atoll( psz_sess_id );
 
-                FREE( psz_sess_id );
+                FREENULL( psz_sess_id );
 
                 GET_FIELD( psz_sess_id );
-                FREE( psz_sess_id );
+                FREENULL( psz_sess_id );
 
                 GET_FIELD( p_sdp->psz_network_type );
                 GET_FIELD( p_sdp->psz_address_type );
@@ -1230,7 +1167,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
 
             case( 'c' ):
             {
-                if( p_sdp->i_media > 1 )
+                if( p_sdp->psz_connection != NULL ) // FIXME
                     break;
 
                 p_sdp->psz_connection = strdup( &psz_sdp[2] );
@@ -1243,7 +1180,7 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
 
         if( b_invalid )
         {
-            FreeSDP( p_sdp );
+            FreeSDP( p_sdp ); p_sdp = NULL;
             return NULL;
         }
 
@@ -1253,44 +1190,34 @@ static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
     return p_sdp;
 }
 
-static int InitSocket( services_discovery_t *p_sd, char *psz_address,
+static int InitSocket( services_discovery_t *p_sd, const char *psz_address,
                        int i_port )
 {
-    int i_fd = net_OpenUDP( p_sd, psz_address, i_port, NULL, 0 );
-
-    if( i_fd != -1 )
-    {
-        net_StopSend( i_fd );
-        INSERT_ELEM(  p_sd->p_sys->pi_fd, p_sd->p_sys->i_fd,
-                      p_sd->p_sys->i_fd, i_fd );
-        return VLC_SUCCESS;
-    }
+    int i_fd = net_ListenUDP1 ((vlc_object_t *)p_sd, psz_address, i_port);
+    if (i_fd == -1)
+        return VLC_EGENERIC;
 
-    return VLC_EGENERIC;
+    net_StopSend( i_fd );
+    INSERT_ELEM (p_sd->p_sys->pi_fd, p_sd->p_sys->i_fd,
+                 p_sd->p_sys->i_fd, i_fd);
+    return VLC_SUCCESS;
 }
 
-#ifdef HAVE_ZLIB_H
-static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len )
+static int Decompress( const unsigned char *psz_src, unsigned char **_dst, int i_len )
 {
-    int i_result, i_dstsize, n;
-    unsigned char *psz_dst;
+#ifdef HAVE_ZLIB_H
+    int i_result, i_dstsize, n = 0;
+    unsigned char *psz_dst = NULL;
     z_stream d_stream;
 
-    d_stream.zalloc = (alloc_func)0;
-    d_stream.zfree = (free_func)0;
-    d_stream.opaque = (voidpf)0;
+    memset (&d_stream, 0, sizeof (d_stream));
 
     i_result = inflateInit(&d_stream);
     if( i_result != Z_OK )
-    {
-        printf( "inflateInit() failed. Result: %d\n", i_result );
         return( -1 );
-    }
+
     d_stream.next_in = (Bytef *)psz_src;
     d_stream.avail_in = i_len;
-    n = 0;
-
-    psz_dst = NULL;
 
     do
     {
@@ -1302,7 +1229,7 @@ static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len )
         i_result = inflate(&d_stream, Z_NO_FLUSH);
         if( ( i_result != Z_OK ) && ( i_result != Z_STREAM_END ) )
         {
-            printf( "Zlib decompression failed. Result: %d\n", i_result );
+            inflateEnd( &d_stream );
             return( -1 );
         }
     }
@@ -1315,33 +1242,38 @@ static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len )
     *_dst = (unsigned char *)realloc( psz_dst, i_dstsize );
 
     return i_dstsize;
-}
+#else
+    (void)psz_src;
+    (void)_dst;
+    (void)i_len;
+    return -1;
 #endif
+}
 
 
 static void FreeSDP( sdp_t *p_sdp )
 {
     int i;
-    FREE( p_sdp->psz_sdp );
-    FREE( p_sdp->psz_sessionname );
-    FREE( p_sdp->psz_connection );
-    FREE( p_sdp->psz_media );
-    FREE( p_sdp->psz_uri );
-    FREE( p_sdp->psz_username );
-    FREE( p_sdp->psz_network_type );
+    FREENULL( p_sdp->psz_sdp );
+    FREENULL( p_sdp->psz_sessionname );
+    FREENULL( p_sdp->psz_connection );
+    FREENULL( p_sdp->psz_media );
+    FREENULL( p_sdp->psz_uri );
+    FREENULL( p_sdp->psz_username );
+    FREENULL( p_sdp->psz_network_type );
 
-    FREE( p_sdp->psz_address );
-    FREE( p_sdp->psz_address_type );
+    FREENULL( p_sdp->psz_address );
+    FREENULL( p_sdp->psz_address_type );
 
     for( i= p_sdp->i_attributes - 1; i >= 0 ; i-- )
     {
         struct attribute_t *p_attr = p_sdp->pp_attributes[i];
-        FREE( p_sdp->pp_attributes[i]->psz_field );
-        FREE( p_sdp->pp_attributes[i]->psz_value );
+        FREENULL( p_sdp->pp_attributes[i]->psz_field );
+        FREENULL( p_sdp->pp_attributes[i]->psz_value );
         REMOVE_ELEM( p_sdp->pp_attributes, p_sdp->i_attributes, i);
-        FREE( p_attr );
+        FREENULL( p_attr );
     }
-    free( p_sdp );
+    FREENULL( p_sdp );
 }
 
 static int RemoveAnnounce( services_discovery_t *p_sd,
@@ -1349,14 +1281,15 @@ static int RemoveAnnounce( services_discovery_t *p_sd,
 {
     int i;
 
-    if( p_announce->p_sdp ) FreeSDP( p_announce->p_sdp );
-
-    if( p_announce->i_input_id > -1 )
+    if( p_announce->p_sdp )
     {
-        playlist_LockDeleteAllFromInput(
-                        p_sd->p_sys->p_playlist, p_announce->i_input_id );
+        FreeSDP( p_announce->p_sdp );
+        p_announce->p_sdp = NULL;
     }
 
+    if( p_announce->i_input_id > -1 )
+        playlist_DeleteFromInput( pl_Get(p_sd), p_announce->i_input_id, VLC_FALSE );
+
     for( i = 0; i< p_sd->p_sys->i_announces; i++)
     {
         if( p_sd->p_sys->pp_announces[i] == p_announce )
@@ -1399,14 +1332,3 @@ static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 )
         return VLC_FALSE;
     }
 }
-
-
-static void CacheLoad( services_discovery_t *p_sd )
-{
-    msg_Warn( p_sd, "cache not implemented") ;
-}
-
-static void CacheSave( services_discovery_t *p_sd )
-{
-    msg_Warn( p_sd, "cache not implemented") ;
-}