]> git.sesse.net Git - vlc/blob - modules/services_discovery/sap.c
Rename playlist_NodesCreateForSD to playlist_NodesPairCreate and document it
[vlc] / modules / services_discovery / sap.c
1 /*****************************************************************************
2  * sap.c :  SAP interface module
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/intf.h>
32
33 #include <network.h>
34 #include <charset.h>
35
36 #include <ctype.h>
37 #include <errno.h>
38
39 #ifdef HAVE_UNISTD_H
40 #    include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #    include <sys/time.h>
44 #endif
45
46 #ifdef HAVE_ZLIB_H
47 #   include <zlib.h>
48 #endif
49
50 /************************************************************************
51  * Macros and definitions
52  ************************************************************************/
53
54 #define MAX_LINE_LENGTH 256
55
56 /* SAP is always on that port */
57 #define SAP_PORT 9875
58 /* Global-scope SAP address */
59 #define SAP_V4_GLOBAL_ADDRESS   "224.2.127.254"
60 /* Organization-local SAP address */
61 #define SAP_V4_ORG_ADDRESS      "239.195.255.255"
62 /* Local (smallest non-link-local scope) SAP address */
63 #define SAP_V4_LOCAL_ADDRESS    "239.255.255.255"
64 /* Link-local SAP address */
65 #define SAP_V4_LINK_ADDRESS     "224.0.0.255"
66 #define ADD_SESSION 1
67
68 #define SAP_V6_1 "FF0"
69 /* Scope is inserted between them */
70 #define SAP_V6_2 "::2:7FFE"
71 /* See RFC3513 for list of valid scopes */
72 /* FIXME: find a way to listen to link-local scope */
73 static const char ipv6_scopes[] = "1456789ABCDE";
74
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 #define SAP_ADDR_TEXT N_( "SAP multicast address" )
80 #define SAP_ADDR_LONGTEXT N_( "The SAP module normally chooses itself the " \
81                               "right addresses to listen to. However, you " \
82                               "can specify a specific address." )
83 #define SAP_IPV4_TEXT N_( "IPv4 SAP" )
84 #define SAP_IPV4_LONGTEXT N_( \
85       "Listen to IPv4 announcements " \
86       "on the standard address." )
87 #define SAP_IPV6_TEXT N_( "IPv6 SAP" )
88 #define SAP_IPV6_LONGTEXT N_( \
89       "Listen to IPv6 announcements " \
90       "on the standard addresses." )
91 #define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )
92 #define SAP_SCOPE_LONGTEXT N_( \
93        "Scope for IPv6 announcements (default is 8)." )
94 #define SAP_TIMEOUT_TEXT N_( "SAP timeout (seconds)" )
95 #define SAP_TIMEOUT_LONGTEXT N_( \
96        "Delay after which SAP items get deleted if no new announcement " \
97        "is received." )
98 #define SAP_PARSE_TEXT N_( "Try to parse the announce" )
99 #define SAP_PARSE_LONGTEXT N_( \
100        "This enables actual parsing of the announces by the SAP module. " \
101        "Otherwise, all announcements are parsed by the \"livedotcom\" " \
102        "(RTP/RTSP) module." )
103 #define SAP_STRICT_TEXT N_( "SAP Strict mode" )
104 #define SAP_STRICT_LONGTEXT N_( \
105        "When this is set, the SAP parser will discard some non-compliant " \
106        "announcements." )
107 #define SAP_CACHE_TEXT N_("Use SAP cache")
108 #define SAP_CACHE_LONGTEXT N_( \
109        "This enables a SAP caching mechanism. " \
110        "This will result in lower SAP startup time, but you could end up " \
111        "with items corresponding to legacy streams." )
112 #define SAP_TIMESHIFT_TEXT N_("Allow timeshifting")
113 #define SAP_TIMESHIFT_LONGTEXT N_( "This automatically enables timeshifting " \
114         "for streams discovered through SAP announcements." )
115
116 /* Callbacks */
117     static int  Open ( vlc_object_t * );
118     static void Close( vlc_object_t * );
119     static int  OpenDemux ( vlc_object_t * );
120     static void CloseDemux ( vlc_object_t * );
121
122 vlc_module_begin();
123     set_shortname( _("SAP"));
124     set_description( _("SAP Announcements") );
125     set_category( CAT_PLAYLIST );
126     set_subcategory( SUBCAT_PLAYLIST_SD );
127
128     add_string( "sap-addr", NULL, NULL,
129                 SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
130     add_bool( "sap-ipv4", 1 , NULL,
131                SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE );
132     add_bool( "sap-ipv6", 1 , NULL,
133               SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE );
134     add_integer( "sap-timeout", 1800, NULL,
135                  SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE );
136     add_bool( "sap-parse", 1 , NULL,
137                SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE );
138     add_bool( "sap-strict", 0 , NULL,
139                SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE );
140 #if 0
141     add_bool( "sap-cache", 0 , NULL,
142                SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE );
143 #endif
144     add_bool( "sap-timeshift", 0 , NULL,
145               SAP_TIMESHIFT_TEXT,SAP_TIMESHIFT_LONGTEXT, VLC_TRUE );
146
147     set_capability( "services_discovery", 0 );
148     set_callbacks( Open, Close );
149
150     add_submodule();
151         set_description( _("SDP file parser for UDP") );
152         add_shortcut( "sdp" );
153         set_capability( "demux2", 51 );
154         set_callbacks( OpenDemux, CloseDemux );
155 vlc_module_end();
156
157
158 /*****************************************************************************
159  * Local structures
160  *****************************************************************************/
161
162 typedef struct sdp_t sdp_t;
163 typedef struct attribute_t attribute_t;
164 typedef struct sap_announce_t sap_announce_t;
165
166 /* The structure that contains sdp information */
167 struct  sdp_t
168 {
169     char *psz_sdp;
170
171     /* s= field */
172     char *psz_sessionname;
173
174     /* Raw m= and c= fields */
175     char *psz_connection;
176     char *psz_media;
177
178     /* o field */
179     char *psz_username;
180     char *psz_network_type;
181     char *psz_address_type;
182     char *psz_address;
183     int64_t i_session_id;
184
185     /* "computed" URI */
186     char *psz_uri;
187
188     int           i_in; /* IP version */
189
190     int           i_media;
191     int           i_media_type;
192
193     int           i_attributes;
194     attribute_t  **pp_attributes;
195 };
196
197 struct attribute_t
198 {
199     char *psz_field;
200     char *psz_value;
201 };
202
203 struct sap_announce_t
204 {
205     mtime_t i_last;
206
207     uint16_t    i_hash;
208     uint32_t    i_source[4];
209
210     /* SAP annnounces must only contain one SDP */
211     sdp_t       *p_sdp;
212
213     int i_input_id;
214     int i_item_id_cat;
215     int i_item_id_one;
216 };
217
218 struct services_discovery_sys_t
219 {
220     /* Socket descriptors */
221     int i_fd;
222     int *pi_fd;
223
224     /* playlist node */
225     playlist_item_t *p_node_cat;
226     playlist_item_t *p_node_one;
227     playlist_t *p_playlist;
228
229     /* Table of announces */
230     int i_announces;
231     struct sap_announce_t **pp_announces;
232
233     /* Modes */
234     vlc_bool_t  b_strict;
235     vlc_bool_t  b_parse;
236     vlc_bool_t  b_timeshift;
237
238     int i_timeout;
239 };
240
241 struct demux_sys_t
242 {
243     sdp_t *p_sdp;
244 };
245
246 /*****************************************************************************
247  * Local prototypes
248  *****************************************************************************/
249
250
251 /* Main functions */
252     static int Demux( demux_t *p_demux );
253     static int Control( demux_t *, int, va_list );
254     static void Run    ( services_discovery_t *p_sd );
255
256 /* Main parsing functions */
257     static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp );
258     static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read );
259     static sdp_t *  ParseSDP( vlc_object_t *p_sd, char* psz_sdp );
260     static sap_announce_t *CreateAnnounce( services_discovery_t *, uint16_t, sdp_t * );
261     static int RemoveAnnounce( services_discovery_t *p_sd, sap_announce_t *p_announce );
262
263 /* Cache */
264     static void CacheLoad( services_discovery_t *p_sd );
265     static void CacheSave( services_discovery_t *p_sd );
266 /* Helper functions */
267     static char *GetAttribute( sdp_t *p_sdp, const char *psz_search );
268     static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 );
269     static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port );
270 #ifdef HAVE_ZLIB_H
271     static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );
272 #endif
273     static void FreeSDP( sdp_t *p_sdp );
274
275 /*****************************************************************************
276  * Open: initialize and create stuff
277  *****************************************************************************/
278 static int Open( vlc_object_t *p_this )
279 {
280     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
281     services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)
282                                 malloc( sizeof( services_discovery_sys_t ) );
283
284     p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" );
285
286     p_sd->pf_run = Run;
287     p_sd->p_sys  = p_sys;
288
289     p_sys->pi_fd = NULL;
290     p_sys->i_fd = 0;
291
292     p_sys->b_strict = var_CreateGetInteger( p_sd, "sap-strict");
293     p_sys->b_parse = var_CreateGetInteger( p_sd, "sap-parse" );
294
295 #if 0
296     if( var_CreateGetInteger( p_sd, "sap-cache" ) )
297     {
298         CacheLoad( p_sd );
299     }
300 #endif
301
302     /* Cache sap_timeshift value */
303     p_sys->b_timeshift = var_CreateGetInteger( p_sd, "sap-timeshift" )
304             ? VLC_TRUE : VLC_FALSE;
305
306     /* Create our playlist node */
307     p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd,
308                                                        VLC_OBJECT_PLAYLIST,
309                                                        FIND_ANYWHERE );
310     if( !p_sys->p_playlist )
311     {
312         msg_Warn( p_sd, "unable to find playlist, cancelling SAP listening");
313         return VLC_EGENERIC;
314     }
315
316     playlist_NodesPairCreate( p_sys->p_playlist, _("SAP sessions"),
317                               &p_sys->p_node_cat, &p_sys->p_node_one,
318                               VLC_TRUE );
319
320     p_sys->i_announces = 0;
321     p_sys->pp_announces = NULL;
322
323     return VLC_SUCCESS;
324 }
325
326 /*****************************************************************************
327  * OpenDemux: initialize and create stuff
328  *****************************************************************************/
329 static int OpenDemux( vlc_object_t *p_this )
330 {
331     demux_t *p_demux = (demux_t *)p_this;
332     uint8_t *p_peek;
333     int i_max_sdp = 1024;
334     int i_sdp = 0;
335     char *psz_sdp = NULL;
336     sdp_t *p_sdp = NULL;
337
338     if( !var_CreateGetInteger( p_demux, "sap-parse" ) )
339     {
340         /* We want livedotcom module to parse this SDP file */
341         return VLC_EGENERIC;
342     }
343
344     /* Probe for SDP */
345     if( p_demux->s )
346     {
347         if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
348
349         if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
350             strncmp( (char*)p_peek, "v=0\n", 4 ) &&
351             ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
352         {
353             return VLC_EGENERIC;
354         }
355     }
356
357     psz_sdp = (char *)malloc( i_max_sdp );
358     if( !psz_sdp ) return VLC_EGENERIC;
359
360     /* Gather the complete sdp file */
361     for( ;; )
362     {
363         int i_read = stream_Read( p_demux->s,
364                                   &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 );
365
366         if( i_read < 0 )
367         {
368             msg_Err( p_demux, "failed to read SDP" );
369             goto error;
370         }
371
372         i_sdp += i_read;
373
374         if( i_read < i_max_sdp - i_sdp - 1 )
375         {
376             psz_sdp[i_sdp] = '\0';
377             break;
378         }
379
380         i_max_sdp += 1000;
381         psz_sdp = (char *)realloc( psz_sdp, i_max_sdp );
382     }
383
384     p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp );
385
386     if( !p_sdp )
387     {
388         msg_Warn( p_demux, "invalid SDP");
389         goto error;
390     }
391
392     if( p_sdp->i_media > 1 )
393     {
394         goto error;
395     }
396
397     if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) )
398     {
399         p_sdp->psz_uri = NULL;
400     }
401     if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 &&
402         p_sdp->i_media_type != 14 )
403         goto error;
404
405     if( p_sdp->psz_uri == NULL ) goto error;
406
407     p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) );
408     p_demux->p_sys->p_sdp = p_sdp;
409     p_demux->pf_control = Control;
410     p_demux->pf_demux = Demux;
411
412     FREENULL( psz_sdp );
413     return VLC_SUCCESS;
414
415 error:
416     FREENULL( psz_sdp );
417     if( p_sdp ) FreeSDP( p_sdp ); p_sdp = NULL;
418     stream_Seek( p_demux->s, 0 );
419     return VLC_EGENERIC;
420 }
421
422 /*****************************************************************************
423  * Close:
424  *****************************************************************************/
425 static void Close( vlc_object_t *p_this )
426 {
427     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
428     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
429
430     int i;
431
432     for( i = p_sys->i_fd-1 ; i >= 0 ; i-- )
433     {
434         net_Close( p_sys->pi_fd[i] );
435     }
436     FREENULL( p_sys->pi_fd );
437
438 #if 0
439     if( config_GetInt( p_sd, "sap-cache" ) )
440     {
441         CacheSave( p_sd );
442     }
443 #endif
444
445     for( i = p_sys->i_announces  - 1;  i>= 0; i-- )
446     {
447         RemoveAnnounce( p_sd, p_sys->pp_announces[i] );
448     }
449     FREENULL( p_sys->pp_announces );
450
451     if( p_sys->p_playlist )
452     {
453         playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE,
454                              VLC_TRUE );
455         playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_one, VLC_TRUE,
456                              VLC_TRUE );
457         vlc_object_release( p_sys->p_playlist );
458     }
459
460     free( p_sys );
461 }
462
463 /*****************************************************************************
464  * CloseDemux: Close the demuxer
465  *****************************************************************************/
466 static void CloseDemux( vlc_object_t *p_this )
467 {
468     demux_t *p_demux = (demux_t *)p_this;
469     if( p_demux->p_sys )
470     {
471         if( p_demux->p_sys->p_sdp ) { FreeSDP( p_demux->p_sys->p_sdp ); p_demux->p_sys->p_sdp = NULL; }
472         free( p_demux->p_sys );
473     }
474 }
475
476 /*****************************************************************************
477  * Run: main SAP thread
478  *****************************************************************************
479  * Listens to SAP packets, and sends them to packet_handle
480  *****************************************************************************/
481 #define MAX_SAP_BUFFER 5000
482
483 static void Run( services_discovery_t *p_sd )
484 {
485     char *psz_addr;
486     int i;
487
488     /* Braindead Winsock DNS resolver will get stuck over 2 seconds per failed
489      * DNS queries, even if the DNS server returns an error with milliseconds.
490      * You don't want to know why the bug (as of XP SP2) wasn't fixed since
491      * Winsock 1.1 from Windows 95, if not Windows 3.1.
492      * Anyway, to avoid a 30 seconds delay for failed IPv6 socket creation,
493      * we have to open sockets in Run() rather than Open(). */
494     if( var_CreateGetInteger( p_sd, "sap-ipv4" ) )
495     {
496         InitSocket( p_sd, SAP_V4_GLOBAL_ADDRESS, SAP_PORT );
497         InitSocket( p_sd, SAP_V4_ORG_ADDRESS, SAP_PORT );
498         InitSocket( p_sd, SAP_V4_LOCAL_ADDRESS, SAP_PORT );
499         InitSocket( p_sd, SAP_V4_LINK_ADDRESS, SAP_PORT );
500     }
501     if( var_CreateGetInteger( p_sd, "sap-ipv6" ) )
502     {
503         char psz_address[] = SAP_V6_1"0"SAP_V6_2;
504         const char *c_scope;
505
506         for( c_scope = ipv6_scopes; *c_scope; c_scope++ )
507         {
508             psz_address[sizeof(SAP_V6_1) - 1] = *c_scope;
509             InitSocket( p_sd, psz_address, SAP_PORT );
510         }
511     }
512
513     psz_addr = var_CreateGetString( p_sd, "sap-addr" );
514     if( psz_addr && *psz_addr )
515     {
516         InitSocket( p_sd, psz_addr, SAP_PORT );
517         free( psz_addr );
518     }
519
520     if( p_sd->p_sys->i_fd == 0 )
521     {
522         msg_Err( p_sd, "unable to listen on any address" );
523         return;
524     }
525
526     /* read SAP packets */
527     while( !p_sd->b_die )
528     {
529         int i_read;
530         uint8_t p_buffer[MAX_SAP_BUFFER+1];
531
532         i_read = net_Select( p_sd, p_sd->p_sys->pi_fd, NULL,
533                              p_sd->p_sys->i_fd, p_buffer,
534                              MAX_SAP_BUFFER, 500000 );
535
536         /* Check for items that need deletion */
537         for( i = 0; i < p_sd->p_sys->i_announces; i++ )
538         {
539             mtime_t i_timeout = ( mtime_t ) 1000000 * p_sd->p_sys->i_timeout;
540
541             if( mdate() - p_sd->p_sys->pp_announces[i]->i_last > i_timeout )
542             {
543                 RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i] );
544             }
545         }
546
547         /* Minimum length is > 6 */
548         if( i_read <= 6 )
549         {
550             if( i_read < 0 )
551             {
552                 msg_Warn( p_sd, "socket read error" );
553             }
554             continue;
555         }
556
557         p_buffer[i_read] = '\0';
558
559         /* Parse the packet */
560         ParseSAP( p_sd, p_buffer, i_read );
561     }
562 }
563
564 /**********************************************************************
565  * Demux: reads and demuxes data packets
566  * Return -1 if error, 0 if EOF, 1 else
567  **********************************************************************/
568 static int Demux( demux_t *p_demux )
569 {
570     sdp_t *p_sdp = p_demux->p_sys->p_sdp;
571     playlist_t *p_playlist;
572     input_thread_t *p_input;
573     input_item_t *p_parent_input;
574
575     p_playlist = (playlist_t *)vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
576                                                FIND_ANYWHERE );
577     if( !p_playlist )
578     {
579         msg_Err( p_demux, "playlist could not be found" );
580         return VLC_EGENERIC;
581     }
582
583     p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT,
584                                                FIND_PARENT );
585     if( !p_input )
586     {
587         msg_Err( p_demux, "parent input could not be found" );
588         return VLC_EGENERIC;
589     }
590
591     p_parent_input = p_input->input.p_item;
592
593     vlc_mutex_lock( &p_parent_input->lock );
594     FREENULL( p_parent_input->psz_uri );
595     p_parent_input->psz_uri = strdup( p_sdp->psz_uri );
596     FREENULL( p_parent_input->psz_name );
597     p_parent_input->psz_name = strdup( EnsureUTF8( p_sdp->psz_sessionname ) );
598     p_parent_input->i_type = ITEM_TYPE_NET;
599
600     if( p_playlist->status.p_item &&
601              p_playlist->status.p_item->p_input == p_parent_input )
602     {
603         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
604                           p_playlist->status.p_node, p_playlist->status.p_item );
605     }
606
607     vlc_mutex_unlock( &p_parent_input->lock );
608     vlc_object_release( p_input );
609     vlc_object_release( p_playlist );
610
611     return VLC_SUCCESS;
612 }
613
614 static int Control( demux_t *p_demux, int i_query, va_list args )
615 {
616     return VLC_EGENERIC;
617 }
618
619 /**************************************************************
620  * Local functions
621  **************************************************************/
622
623 /* i_read is at least > 6 */
624 static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read )
625 {
626     int                 i_version, i_address_type, i_hash, i;
627     char                *psz_sdp, *psz_foo, *psz_initial_sdp;
628     uint8_t             *p_decompressed_buffer = NULL;
629     sdp_t               *p_sdp;
630     vlc_bool_t          b_compressed;
631     vlc_bool_t          b_need_delete = VLC_FALSE;
632
633     /* First, check the sap announce is correct */
634     i_version = p_buffer[0] >> 5;
635     if( i_version != 1 )
636     {
637        msg_Dbg( p_sd, "strange sap version %d found", i_version );
638     }
639
640     i_address_type = p_buffer[0] & 0x10;
641
642     if( (p_buffer[0] & 0x08) != 0 )
643     {
644         msg_Dbg( p_sd, "reserved bit incorrectly set" );
645         return VLC_EGENERIC;
646     }
647
648     if( (p_buffer[0] & 0x04) != 0 )
649     {
650         msg_Dbg( p_sd, "session deletion packet" );
651         b_need_delete = VLC_TRUE;
652     }
653
654     if( p_buffer[0] & 0x02  )
655     {
656         msg_Dbg( p_sd, "encrypted packet, unsupported" );
657         return VLC_EGENERIC;
658     }
659
660     b_compressed = p_buffer[0] & 0x01;
661
662     i_hash = ( p_buffer[2] << 8 ) + p_buffer[3];
663
664     if( p_sd->p_sys->b_strict && i_hash == 0 )
665     {
666         msg_Dbg( p_sd, "strict mode, discarding announce with null id hash");
667         return VLC_EGENERIC;
668     }
669
670     psz_sdp  = (char *)p_buffer + 4;
671     psz_initial_sdp = psz_sdp;
672
673     if( i_address_type == 0 ) /* ipv4 source address */
674     {
675         psz_sdp += 4;
676         if( i_read <= 9 )
677         {
678             msg_Warn( p_sd, "too short SAP packet" );
679             return VLC_EGENERIC;
680         }
681     }
682     else /* ipv6 source address */
683     {
684         psz_sdp += 16;
685         if( i_read <= 21 )
686         {
687             msg_Warn( p_sd, "too short SAP packet" );
688             return VLC_EGENERIC;
689         }
690     }
691
692     if( b_compressed )
693     {
694 #ifdef HAVE_ZLIB_H
695         int      i_decompressed_size;
696
697         i_decompressed_size = Decompress( (uint8_t *)psz_sdp,
698                    &p_decompressed_buffer, i_read - ( psz_sdp - (char *)p_buffer ) );
699         if( i_decompressed_size > 0 )
700         {
701             psz_sdp = (char *)p_decompressed_buffer;
702             realloc( p_decompressed_buffer, i_decompressed_size++ );
703             psz_sdp[i_decompressed_size] = '\0';
704         }
705         else
706         {
707             msg_Warn( p_sd, "decompression of sap packet failed" );
708             return VLC_EGENERIC;
709         }
710 #else
711         msg_Warn( p_sd, "ignoring compressed sap packet" );
712         return VLC_EGENERIC;
713 #endif
714     }
715
716     /* Add the size of authentification info */
717     if( i_read < p_buffer[1] + (psz_sdp - psz_initial_sdp ) )
718     {
719         msg_Warn( p_sd, "too short SAP packet\n");
720         return VLC_EGENERIC;
721     }
722     psz_sdp += p_buffer[1];
723     psz_foo = psz_sdp;
724
725     /* Skip payload type */
726     /* Handle announces without \0 between SAP and SDP */
727     while( *psz_sdp != '\0' && ( psz_sdp[0] != 'v' && psz_sdp[1] != '=' ) )
728     {
729         if( psz_sdp - psz_initial_sdp >= i_read - 5 )
730         {
731             msg_Warn( p_sd, "empty SDP ?");
732         }
733         psz_sdp++;
734     }
735
736     if( *psz_sdp == '\0' )
737     {
738         psz_sdp++;
739     }
740     if( ( psz_sdp != psz_foo ) && strcasecmp( psz_foo, "application/sdp" ) )
741     {
742         msg_Dbg( p_sd, "unhandled content type: %s", psz_foo );
743     }
744     if( ( psz_sdp - (char *)p_buffer ) >= i_read )
745     {
746         msg_Warn( p_sd, "package without content" );
747         return VLC_EGENERIC;
748     }
749
750     /* Parse SDP info */
751     p_sdp = ParseSDP( VLC_OBJECT(p_sd), psz_sdp );
752
753     if( p_sdp == NULL )
754     {
755         return VLC_EGENERIC;
756     }
757
758     /* Decide whether we should add a playlist item for this SDP */
759     /* Parse connection information (c= & m= ) */
760     if( ParseConnection( VLC_OBJECT(p_sd), p_sdp ) )
761     {
762         p_sdp->psz_uri = NULL;
763     }
764
765     /* Multi-media or no-parse -> pass to LIVE.COM */
766     if( p_sdp->i_media > 1 || ( p_sdp->i_media_type != 14 &&
767                                 p_sdp->i_media_type != 32 &&
768                                 p_sdp->i_media_type != 33) ||
769         p_sd->p_sys->b_parse == VLC_FALSE )
770     {
771         if( p_sdp->psz_uri ) free( p_sdp->psz_uri );
772         asprintf( &p_sdp->psz_uri, "sdp://%s", p_sdp->psz_sdp );
773     }
774
775     if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;
776
777     for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
778     {
779         /* FIXME: slow */
780         /* FIXME: we create a new announce each time the sdp changes */
781         if( IsSameSession( p_sd->p_sys->pp_announces[i]->p_sdp, p_sdp ) )
782         {
783             if( b_need_delete )
784             {
785                 RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i]);
786             }
787             else
788             {
789                 p_sd->p_sys->pp_announces[i]->i_last = mdate();
790             }
791             FreeSDP( p_sdp ); p_sdp = NULL;
792             return VLC_SUCCESS;
793         }
794     }
795     /* Add item */
796     if( p_sdp->i_media > 1 )
797     {
798         msg_Dbg( p_sd, "passing to liveMedia" );
799     }
800
801     CreateAnnounce( p_sd, i_hash, p_sdp );
802
803     FREENULL( p_decompressed_buffer );
804     return VLC_SUCCESS;
805 }
806
807 sap_announce_t *CreateAnnounce( services_discovery_t *p_sd, uint16_t i_hash,
808                                 sdp_t *p_sdp )
809 {
810     input_item_t *p_input;
811     playlist_item_t     *p_item, *p_child;
812     char *psz_value;
813     sap_announce_t *p_sap = (sap_announce_t *)malloc(
814                                         sizeof(sap_announce_t ) );
815     services_discovery_sys_t *p_sys;
816     if( p_sap == NULL )
817         return NULL;
818
819     p_sys = p_sd->p_sys;
820
821     EnsureUTF8( p_sdp->psz_sessionname );
822     p_sap->i_last = mdate();
823     p_sap->i_hash = i_hash;
824     p_sap->p_sdp = p_sdp;
825
826     /* Create the actual playlist item here */
827     p_input = input_ItemNewWithType( VLC_OBJECT(p_sd),
828                                      p_sap->p_sdp->psz_uri,
829                                      p_sdp->psz_sessionname,
830                                      0, NULL, -1, ITEM_TYPE_NET );
831     p_sap->i_input_id = p_input->i_id;
832     if( !p_input )
833     {
834         free( p_sap );
835         return NULL;
836     }
837
838     if( p_sys->b_timeshift )
839         vlc_input_item_AddOption( p_input, ":access-filter=timeshift" );
840
841     psz_value = GetAttribute( p_sap->p_sdp, "tool" );
842     if( psz_value != NULL )
843     {
844         vlc_input_item_AddInfo( p_input, _("Session"),_("Tool"), psz_value );
845     }
846     if( strcmp( p_sdp->psz_username, "-" ) )
847     {
848         vlc_input_item_AddInfo( p_input, _("Session"),
849                                 _("User"), p_sdp->psz_username );
850     }
851
852     /* Handle group */
853     psz_value = GetAttribute( p_sap->p_sdp, "x-plgroup" );
854     if( psz_value == NULL )
855         psz_value = GetAttribute( p_sap->p_sdp, "plgroup" );
856
857     if( psz_value != NULL )
858     {
859         EnsureUTF8( psz_value );
860
861         p_child = playlist_ChildSearchName( p_sys->p_node_cat, psz_value );
862
863         if( p_child == NULL )
864         {
865             p_child = playlist_NodeCreate( p_sys->p_playlist, psz_value,
866                                            p_sys->p_node_cat );
867             p_child->i_flags &= ~PLAYLIST_SKIP_FLAG;
868         }
869     }
870     else
871     {
872         p_child = p_sys->p_node_cat;
873     }
874
875     p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input, p_child,
876                                     PLAYLIST_APPEND, PLAYLIST_END );
877     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
878     p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
879     p_sap->i_item_id_cat = p_item->i_id;
880
881     p_item = playlist_NodeAddInput( p_sys->p_playlist, p_input,
882                         p_sys->p_node_one, PLAYLIST_APPEND, PLAYLIST_END );
883     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
884     p_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
885     p_sap->i_item_id_one = p_item->i_id;
886
887     TAB_APPEND( p_sys->i_announces, p_sys->pp_announces, p_sap );
888
889     return p_sap;
890 }
891
892 static char *GetAttribute( sdp_t *p_sdp, const char *psz_search )
893 {
894     int i;
895
896     for( i = 0 ; i< p_sdp->i_attributes; i++ )
897     {
898         if( !strncmp( p_sdp->pp_attributes[i]->psz_field, psz_search,
899                       strlen( p_sdp->pp_attributes[i]->psz_field ) ) )
900         {
901             return p_sdp->pp_attributes[i]->psz_value;
902         }
903     }
904     return NULL;
905 }
906
907
908 /* Fill p_sdp->psz_uri */
909 static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp )
910 {
911     char *psz_eof = NULL;
912     char *psz_parse = NULL;
913     char *psz_uri = NULL;
914     char *psz_proto = NULL;
915     char psz_source[256];
916     int i_port = 0;
917
918     /* Parse c= field */
919     if( p_sdp->psz_connection )
920     {
921         psz_parse = p_sdp->psz_connection;
922
923         psz_eof = strchr( psz_parse, ' ' );
924
925         if( psz_eof )
926         {
927             *psz_eof = '\0';
928             psz_parse = psz_eof + 1;
929         }
930         else
931         {
932             msg_Warn( p_obj, "unable to parse c field (1)");
933             return VLC_EGENERIC;
934         }
935
936         psz_eof = strchr( psz_parse, ' ' );
937
938         if( psz_eof )
939         {
940             *psz_eof = '\0';
941             if( !strncmp( psz_parse, "IP4", 3 ) )
942             {
943                 p_sdp->i_in = 4;
944             }
945             else if( !strncmp( psz_parse, "IP6", 3 ) )
946             {
947                 p_sdp->i_in = 6;
948             }
949             else
950             {
951                 p_sdp->i_in = 0;
952             }
953             psz_parse = psz_eof + 1;
954         }
955         else
956         {
957             msg_Warn( p_obj, "unable to parse c field (2)");
958             return VLC_EGENERIC;
959         }
960
961         psz_eof = strchr( psz_parse, '/' );
962
963         if( psz_eof )
964         {
965             *psz_eof = '\0';
966         }
967         else
968         {
969             msg_Dbg( p_obj, "incorrect c field, %s", p_sdp->psz_connection );
970         }
971         if( p_sdp->i_in == 6 && ( isxdigit( *psz_parse ) || *psz_parse == ':' ) )
972         {
973             asprintf( &psz_uri, "[%s]", psz_parse );
974         }
975         else psz_uri = strdup( psz_parse );
976
977     }
978
979     /* Parse m= field */
980     if( p_sdp->psz_media )
981     {
982         psz_parse = p_sdp->psz_media;
983
984         psz_eof = strchr( psz_parse, ' ' );
985
986         if( psz_eof )
987         {
988             *psz_eof = '\0';
989
990             if( strncmp( psz_parse, "audio", 5 )  &&
991                 strncmp( psz_parse, "video", 5 ) )
992             {
993                 msg_Warn( p_obj, "unhandled media type -%s-", psz_parse );
994                 FREENULL( psz_uri );
995                 return VLC_EGENERIC;
996             }
997
998             psz_parse = psz_eof + 1;
999         }
1000         else
1001         {
1002             msg_Warn( p_obj, "unable to parse m field (1)");
1003             FREENULL( psz_uri );
1004             return VLC_EGENERIC;
1005         }
1006
1007         psz_eof = strchr( psz_parse, ' ' );
1008
1009         if( psz_eof )
1010         {
1011             *psz_eof = '\0';
1012
1013             /* FIXME : multiple port ! */
1014             i_port = atoi( psz_parse );
1015
1016             if( i_port <= 0 || i_port >= 65536 )
1017             {
1018                 msg_Warn( p_obj, "invalid transport port %i", i_port );
1019             }
1020
1021             psz_parse = psz_eof + 1;
1022         }
1023         else
1024         {
1025             msg_Warn( p_obj, "unable to parse m field (2)");
1026             FREENULL( psz_uri );
1027             return VLC_EGENERIC;
1028         }
1029
1030         psz_eof = strchr( psz_parse, ' ' );
1031
1032         if( psz_eof )
1033         {
1034             *psz_eof = '\0';
1035             psz_proto = strdup( psz_parse );
1036
1037             psz_parse = psz_eof + 1;
1038             p_sdp->i_media_type = atoi( psz_parse );
1039
1040         }
1041         else
1042         {
1043             msg_Dbg( p_obj, "incorrect m field, %s", p_sdp->psz_media );
1044             p_sdp->i_media_type = 33;
1045             psz_proto = strdup( psz_parse );
1046         }
1047     }
1048
1049     if( psz_proto && !strncmp( psz_proto, "RTP/AVP", 7 ) )
1050     {
1051         free( psz_proto );
1052         psz_proto = strdup( "rtp" );
1053     }
1054     if( psz_proto && !strncasecmp( psz_proto, "UDP", 3 ) )
1055     {
1056         free( psz_proto );
1057         psz_proto = strdup( "udp" );
1058     }
1059
1060     /* FIXME: HTTP support */
1061
1062     if( i_port == 0 )
1063     {
1064         i_port = 1234;
1065     }
1066
1067     /* handle SSM case */
1068     psz_parse = GetAttribute( p_sdp, "source-filter" );
1069     psz_source[0] = '\0';
1070
1071     if( psz_parse ) sscanf( psz_parse, " incl IN IP%*s %*s %255s ", psz_source);
1072
1073     asprintf( &p_sdp->psz_uri, "%s://%s@%s:%i", psz_proto, psz_source,
1074               psz_uri, i_port );
1075
1076     FREENULL( psz_uri );
1077     FREENULL( psz_proto );
1078     return VLC_SUCCESS;
1079 }
1080
1081 /***********************************************************************
1082  * ParseSDP : SDP parsing
1083  * *********************************************************************
1084  * Validate SDP and parse all fields
1085  ***********************************************************************/
1086 static sdp_t *  ParseSDP( vlc_object_t *p_obj, char* psz_sdp )
1087 {
1088     sdp_t *p_sdp;
1089     vlc_bool_t b_invalid = VLC_FALSE;
1090     vlc_bool_t b_end = VLC_FALSE;
1091     if( psz_sdp == NULL )
1092     {
1093         return NULL;
1094     }
1095
1096     if( psz_sdp[0] != 'v' || psz_sdp[1] != '=' )
1097     {
1098         msg_Warn( p_obj, "bad packet" );
1099         return NULL;
1100     }
1101
1102     p_sdp = (sdp_t *)malloc( sizeof( sdp_t ) );
1103     if( p_sdp == NULL )
1104         return NULL;
1105
1106     p_sdp->psz_sdp = strdup( psz_sdp );
1107     if( p_sdp->psz_sdp == NULL )
1108     {
1109         free( p_sdp );
1110         return NULL;
1111     }
1112
1113     p_sdp->psz_sessionname = NULL;
1114     p_sdp->psz_media       = NULL;
1115     p_sdp->psz_connection  = NULL;
1116     p_sdp->psz_uri         = NULL;
1117     p_sdp->psz_address     = NULL;
1118     p_sdp->psz_address_type= NULL;
1119
1120     p_sdp->i_media         = 0;
1121     p_sdp->i_attributes    = 0;
1122     p_sdp->pp_attributes   = NULL;
1123
1124     while( *psz_sdp != '\0' && b_end == VLC_FALSE  )
1125     {
1126         char *psz_eol;
1127         char *psz_eof;
1128         char *psz_parse;
1129         char *psz_sess_id;
1130
1131         while( *psz_sdp == '\r' || *psz_sdp == '\n' ||
1132                *psz_sdp == ' ' || *psz_sdp == '\t' )
1133         {
1134             psz_sdp++;
1135         }
1136
1137         if( ( psz_eol = strchr( psz_sdp, '\n' ) ) == NULL )
1138         {
1139             psz_eol = psz_sdp + strlen( psz_sdp );
1140             b_end = VLC_TRUE;
1141         }
1142         if( psz_eol > psz_sdp && *( psz_eol - 1 ) == '\r' )
1143         {
1144             psz_eol--;
1145         }
1146
1147         if( psz_eol <= psz_sdp )
1148         {
1149             break;
1150         }
1151         *psz_eol++ = '\0';
1152
1153         /* no space allowed between fields */
1154         if( psz_sdp[1] != '=' )
1155         {
1156             msg_Warn( p_obj, "invalid packet" ) ;
1157             FreeSDP( p_sdp ); p_sdp = NULL;
1158             return NULL;
1159         }
1160
1161         /* Now parse each line */
1162         switch( psz_sdp[0] )
1163         {
1164             case( 'v' ):
1165                 break;
1166             case( 's' ):
1167                 p_sdp->psz_sessionname = strdup( &psz_sdp[2] );
1168                 break;
1169             case ( 'o' ):
1170             {
1171                 int i_field = 0;
1172                 /* o field is <username> <session id> <version>
1173                  *  <network type> <address type> <address> */
1174
1175 #define GET_FIELD( store ) \
1176                 psz_eof = strchr( psz_parse, ' ' ); \
1177                 if( psz_eof ) \
1178                 { \
1179                     *psz_eof=0; store = strdup( psz_parse ); \
1180                 } \
1181                 else \
1182                 { \
1183                     if( i_field != 5 ) \
1184                     { \
1185                         b_invalid = VLC_TRUE; break; \
1186                     } \
1187                     else \
1188                     { \
1189                         store = strdup( psz_parse ); \
1190                     } \
1191                 }; \
1192                 psz_parse = psz_eof + 1; i_field++;
1193
1194
1195                 psz_parse = &psz_sdp[2];
1196                 GET_FIELD( p_sdp->psz_username );
1197                 GET_FIELD( psz_sess_id );
1198
1199                 p_sdp->i_session_id = atoll( psz_sess_id );
1200
1201                 FREENULL( psz_sess_id );
1202
1203                 GET_FIELD( psz_sess_id );
1204                 FREENULL( psz_sess_id );
1205
1206                 GET_FIELD( p_sdp->psz_network_type );
1207                 GET_FIELD( p_sdp->psz_address_type );
1208                 GET_FIELD( p_sdp->psz_address );
1209
1210                 break;
1211             }
1212             case( 'i' ):
1213             case( 'u' ):
1214             case( 'e' ):
1215             case( 'p' ):
1216             case( 't' ):
1217             case( 'r' ):
1218                 break;
1219             case( 'a' ): /* attribute */
1220             {
1221                 char *psz_eon = strchr( &psz_sdp[2], ':' );
1222                 attribute_t *p_attr = malloc( sizeof( attribute_t ) );
1223
1224                 /* Attribute with value */
1225                 if( psz_eon )
1226                 {
1227                     *psz_eon++ = '\0';
1228
1229                     p_attr->psz_field = strdup( &psz_sdp[2] );
1230                     p_attr->psz_value = strdup( psz_eon );
1231                 }
1232                 else /* Attribute without value */
1233                 {
1234                     p_attr->psz_field = strdup( &psz_sdp[2] );
1235                     p_attr->psz_value = NULL;
1236                 }
1237
1238                 TAB_APPEND( p_sdp->i_attributes, p_sdp->pp_attributes, p_attr );
1239                 break;
1240             }
1241
1242             case( 'm' ): /* Media announcement */
1243             {
1244                 /* If we have several medias, we pass the announcement to
1245                  * LIVE.COM, so just count them */
1246                 p_sdp->i_media++;
1247                 if( p_sdp->i_media == 1 )
1248                 {
1249                     p_sdp->psz_media = strdup( &psz_sdp[2] );
1250                 }
1251                 break;
1252             }
1253
1254             case( 'c' ):
1255             {
1256                 if( p_sdp->i_media > 1 )
1257                     break;
1258
1259                 p_sdp->psz_connection = strdup( &psz_sdp[2] );
1260                 break;
1261             }
1262
1263             default:
1264                break;
1265         }
1266
1267         if( b_invalid )
1268         {
1269             FreeSDP( p_sdp ); p_sdp = NULL;
1270             return NULL;
1271         }
1272
1273         psz_sdp = psz_eol;
1274     }
1275
1276     return p_sdp;
1277 }
1278
1279 static int InitSocket( services_discovery_t *p_sd, char *psz_address,
1280                        int i_port )
1281 {
1282     int i_fd = net_OpenUDP( p_sd, psz_address, i_port, NULL, 0 );
1283
1284     if( i_fd != -1 )
1285     {
1286         net_StopSend( i_fd );
1287         INSERT_ELEM(  p_sd->p_sys->pi_fd, p_sd->p_sys->i_fd,
1288                       p_sd->p_sys->i_fd, i_fd );
1289         return VLC_SUCCESS;
1290     }
1291
1292     return VLC_EGENERIC;
1293 }
1294
1295 #ifdef HAVE_ZLIB_H
1296 static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len )
1297 {
1298     int i_result, i_dstsize, n;
1299     unsigned char *psz_dst;
1300     z_stream d_stream;
1301
1302     d_stream.zalloc = (alloc_func)0;
1303     d_stream.zfree = (free_func)0;
1304     d_stream.opaque = (voidpf)0;
1305
1306     i_result = inflateInit(&d_stream);
1307     if( i_result != Z_OK )
1308     {
1309         printf( "inflateInit() failed. Result: %d\n", i_result );
1310         return( -1 );
1311     }
1312     d_stream.next_in = (Bytef *)psz_src;
1313     d_stream.avail_in = i_len;
1314     n = 0;
1315
1316     psz_dst = NULL;
1317
1318     do
1319     {
1320         n++;
1321         psz_dst = (unsigned char *)realloc( psz_dst, n * 1000 );
1322         d_stream.next_out = (Bytef *)&psz_dst[(n - 1) * 1000];
1323         d_stream.avail_out = 1000;
1324
1325         i_result = inflate(&d_stream, Z_NO_FLUSH);
1326         if( ( i_result != Z_OK ) && ( i_result != Z_STREAM_END ) )
1327         {
1328             printf( "Zlib decompression failed. Result: %d\n", i_result );
1329             return( -1 );
1330         }
1331     }
1332     while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
1333            ( i_result != Z_STREAM_END ) );
1334
1335     i_dstsize = d_stream.total_out;
1336     inflateEnd( &d_stream );
1337
1338     *_dst = (unsigned char *)realloc( psz_dst, i_dstsize );
1339
1340     return i_dstsize;
1341 }
1342 #endif
1343
1344
1345 static void FreeSDP( sdp_t *p_sdp )
1346 {
1347     int i;
1348     FREENULL( p_sdp->psz_sdp );
1349     FREENULL( p_sdp->psz_sessionname );
1350     FREENULL( p_sdp->psz_connection );
1351     FREENULL( p_sdp->psz_media );
1352     FREENULL( p_sdp->psz_uri );
1353     FREENULL( p_sdp->psz_username );
1354     FREENULL( p_sdp->psz_network_type );
1355
1356     FREENULL( p_sdp->psz_address );
1357     FREENULL( p_sdp->psz_address_type );
1358
1359     for( i= p_sdp->i_attributes - 1; i >= 0 ; i-- )
1360     {
1361         struct attribute_t *p_attr = p_sdp->pp_attributes[i];
1362         FREENULL( p_sdp->pp_attributes[i]->psz_field );
1363         FREENULL( p_sdp->pp_attributes[i]->psz_value );
1364         REMOVE_ELEM( p_sdp->pp_attributes, p_sdp->i_attributes, i);
1365         FREENULL( p_attr );
1366     }
1367     FREENULL( p_sdp );
1368 }
1369
1370 static int RemoveAnnounce( services_discovery_t *p_sd,
1371                            sap_announce_t *p_announce )
1372 {
1373     int i;
1374
1375     if( p_announce->p_sdp )
1376     {
1377         FreeSDP( p_announce->p_sdp );
1378         p_announce->p_sdp = NULL;
1379     }
1380
1381     if( p_announce->i_input_id > -1 )
1382     {
1383         playlist_LockDeleteAllFromInput(
1384                         p_sd->p_sys->p_playlist, p_announce->i_input_id );
1385     }
1386
1387     for( i = 0; i< p_sd->p_sys->i_announces; i++)
1388     {
1389         if( p_sd->p_sys->pp_announces[i] == p_announce )
1390         {
1391             REMOVE_ELEM( p_sd->p_sys->pp_announces, p_sd->p_sys->i_announces,
1392                          i);
1393             break;
1394         }
1395     }
1396
1397     free( p_announce );
1398
1399     return VLC_SUCCESS;
1400 }
1401
1402 static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 )
1403 {
1404     /* A session is identified by
1405      * username, session_id, network type, address type and address */
1406     if( p_sdp1->psz_username && p_sdp2->psz_username &&
1407         p_sdp1->psz_network_type && p_sdp2->psz_network_type &&
1408         p_sdp1->psz_address_type && p_sdp2->psz_address_type &&
1409         p_sdp1->psz_address &&  p_sdp2->psz_address )
1410     {
1411         if(!strcmp( p_sdp1->psz_username , p_sdp2->psz_username ) &&
1412            !strcmp( p_sdp1->psz_network_type , p_sdp2->psz_network_type ) &&
1413            !strcmp( p_sdp1->psz_address_type , p_sdp2->psz_address_type ) &&
1414            !strcmp( p_sdp1->psz_address , p_sdp2->psz_address ) &&
1415            p_sdp1->i_session_id == p_sdp2->i_session_id )
1416         {
1417             return VLC_TRUE;
1418         }
1419         else
1420         {
1421             return VLC_FALSE;
1422         }
1423     }
1424     else
1425     {
1426         return VLC_FALSE;
1427     }
1428 }
1429
1430
1431 static void CacheLoad( services_discovery_t *p_sd )
1432 {
1433     msg_Warn( p_sd, "cache not implemented") ;
1434 }
1435
1436 static void CacheSave( services_discovery_t *p_sd )
1437 {
1438     msg_Warn( p_sd, "cache not implemented") ;
1439 }