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