sap.c 45.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * sap.c :  SAP interface module
 *****************************************************************************
4
 * Copyright (C) 2004-2005 the VideoLAN team
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * Copyright © 2007 Rémi Denis-Courmont
Carlo Calabrò's avatar
Carlo Calabrò committed
6
 * $Id$
7
 *
8
 * Authors: Clément Stenac <zorglub@videolan.org>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
9
 *          Rémi Denis-Courmont
zorglub's avatar
zorglub committed
10
 *
11 12 13 14
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
16 17 18 19 20 21 22
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26
 *****************************************************************************/

/*****************************************************************************
zorglub's avatar
zorglub committed
27
 * Includes
28
 *****************************************************************************/
29
#include <vlc/vlc.h>
30
#include <stdlib.h>                                      /* malloc(), free() */
31
#include <assert.h>
32

zorglub's avatar
zorglub committed
33 34
#include <vlc_playlist.h>
#include <vlc_demux.h>
35

zorglub's avatar
zorglub committed
36 37
#include <vlc_network.h>
#include <vlc_charset.h>
zorglub's avatar
zorglub committed
38

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
39 40
#include <ctype.h>
#include <errno.h>
41 42 43 44 45 46 47 48

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif

sigmunau's avatar
sigmunau committed
49 50 51
#ifdef HAVE_ZLIB_H
#   include <zlib.h>
#endif
52 53 54 55

#ifndef WIN32
#   include <net/if.h>
#endif
sigmunau's avatar
sigmunau committed
56

zorglub's avatar
zorglub committed
57 58 59 60
/************************************************************************
 * Macros and definitions
 ************************************************************************/

61 62
#define MAX_LINE_LENGTH 256

63
/* SAP is always on that port */
zorglub's avatar
zorglub committed
64
#define SAP_PORT 9875
65 66 67 68 69 70 71 72
/* Global-scope SAP address */
#define SAP_V4_GLOBAL_ADDRESS   "224.2.127.254"
/* Organization-local SAP address */
#define SAP_V4_ORG_ADDRESS      "239.195.255.255"
/* Local (smallest non-link-local scope) SAP address */
#define SAP_V4_LOCAL_ADDRESS    "239.255.255.255"
/* Link-local SAP address */
#define SAP_V4_LINK_ADDRESS     "224.0.0.255"
73
#define ADD_SESSION 1
74 75 76 77

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
78 79
#define SAP_ADDR_TEXT N_( "SAP multicast address" )
#define SAP_ADDR_LONGTEXT N_( "The SAP module normally chooses itself the " \
80
                              "right addresses to listen to. However, you " \
zorglub's avatar
zorglub committed
81
                              "can specify a specific address." )
82
#define SAP_IPV4_TEXT N_( "IPv4 SAP" )
83
#define SAP_IPV4_LONGTEXT N_( \
84
      "Listen to IPv4 announcements on the standard addresses." )
85
#define SAP_IPV6_TEXT N_( "IPv6 SAP" )
86
#define SAP_IPV6_LONGTEXT N_( \
87
      "Listen to IPv6 announcements on the standard addresses." )
88
#define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )
89
#define SAP_SCOPE_LONGTEXT N_( \
zorglub's avatar
zorglub committed
90
       "Scope for IPv6 announcements (default is 8)." )
91
#define SAP_TIMEOUT_TEXT N_( "SAP timeout (seconds)" )
92
#define SAP_TIMEOUT_LONGTEXT N_( \
zorglub's avatar
zorglub committed
93
       "Delay after which SAP items get deleted if no new announcement " \
94
       "is received." )
zorglub's avatar
zorglub committed
95
#define SAP_PARSE_TEXT N_( "Try to parse the announce" )
96
#define SAP_PARSE_LONGTEXT N_( \
97 98 99
       "This enables actual parsing of the announces by the SAP module. " \
       "Otherwise, all announcements are parsed by the \"livedotcom\" " \
       "(RTP/RTSP) module." )
100 101 102
#define SAP_STRICT_TEXT N_( "SAP Strict mode" )
#define SAP_STRICT_LONGTEXT N_( \
       "When this is set, the SAP parser will discard some non-compliant " \
103
       "announcements." )
zorglub's avatar
zorglub committed
104 105
#define SAP_CACHE_TEXT N_("Use SAP cache")
#define SAP_CACHE_LONGTEXT N_( \
106
       "This enables a SAP caching mechanism. " \
zorglub's avatar
zorglub committed
107
       "This will result in lower SAP startup time, but you could end up " \
108
       "with items corresponding to legacy streams." )
109
#define SAP_TIMESHIFT_TEXT N_("Allow timeshifting")
110 111
#define SAP_TIMESHIFT_LONGTEXT N_( "This automatically enables timeshifting " \
        "for streams discovered through SAP announcements." )
zorglub's avatar
zorglub committed
112

zorglub's avatar
zorglub committed
113 114 115 116 117
/* Callbacks */
    static int  Open ( vlc_object_t * );
    static void Close( vlc_object_t * );
    static int  OpenDemux ( vlc_object_t * );
    static void CloseDemux ( vlc_object_t * );
118

119
vlc_module_begin();
120
    set_shortname( _("SAP"));
121
    set_description( _("SAP Announcements") );
zorglub's avatar
zorglub committed
122 123
    set_category( CAT_PLAYLIST );
    set_subcategory( SUBCAT_PLAYLIST_SD );
124

125 126
    add_string( "sap-addr", NULL, NULL,
                SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
gbazin's avatar
 
gbazin committed
127
    add_bool( "sap-ipv4", 1 , NULL,
128
               SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE );
129
    add_bool( "sap-ipv6", 1 , NULL,
130
              SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE );
gbazin's avatar
 
gbazin committed
131
    add_integer( "sap-timeout", 1800, NULL,
132
                 SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE );
133
    add_bool( "sap-parse", 1 , NULL,
134
               SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE );
135 136
    add_bool( "sap-strict", 0 , NULL,
               SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE );
137
#if 0
zorglub's avatar
zorglub committed
138 139
    add_bool( "sap-cache", 0 , NULL,
               SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE );
140
#endif
141 142
    add_bool( "sap-timeshift", 0 , NULL,
              SAP_TIMESHIFT_TEXT,SAP_TIMESHIFT_LONGTEXT, VLC_TRUE );
143

zorglub's avatar
zorglub committed
144
    set_capability( "services_discovery", 0 );
145
    set_callbacks( Open, Close );
zorglub's avatar
zorglub committed
146

147
    add_submodule();
148
        set_description( _("SDP Descriptions parser") );
149 150
        add_shortcut( "sdp" );
        set_capability( "demux2", 51 );
zorglub's avatar
zorglub committed
151
        set_callbacks( OpenDemux, CloseDemux );
152 153
vlc_module_end();

zorglub's avatar
zorglub committed
154

155
/*****************************************************************************
zorglub's avatar
zorglub committed
156
 * Local structures
157 158
 *****************************************************************************/

zorglub's avatar
zorglub committed
159 160 161
typedef struct sdp_t sdp_t;
typedef struct attribute_t attribute_t;
typedef struct sap_announce_t sap_announce_t;
162

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
163 164 165 166 167 168 169 170 171 172 173 174 175

struct sdp_media_t
{
    struct sdp_t           *parent;
    char                   *fmt;
    struct sockaddr_storage addr;
    socklen_t               addrlen;
    unsigned                n_addr;
    int           i_attributes;
    attribute_t  **pp_attributes;
};


zorglub's avatar
zorglub committed
176 177 178
/* The structure that contains sdp information */
struct  sdp_t
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
179
    const char *psz_sdp;
180

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
181 182 183 184 185 186 187
    /* o field */
    char     username[64];
    uint64_t session_id;
    uint64_t session_version;
    unsigned orig_ip_version;
    char     orig_host[1024];

zorglub's avatar
zorglub committed
188 189
    /* s= field */
    char *psz_sessionname;
190

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
191
    /* old cruft */
zorglub's avatar
zorglub committed
192 193
    /* "computed" URI */
    char *psz_uri;
194
    int           i_media_type;
195

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
196
    /* a= global attributes */
zorglub's avatar
zorglub committed
197 198
    int           i_attributes;
    attribute_t  **pp_attributes;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
199 200 201

    /* medias (well, we only support one atm) */
    unsigned            mediac;
202
    struct sdp_media_t *mediav;
203
};
zorglub's avatar
zorglub committed
204

zorglub's avatar
zorglub committed
205
struct attribute_t
206
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
207 208
    const char *value;
    char name[0];
209
};
zorglub's avatar
zorglub committed
210

211 212
struct sap_announce_t
{
zorglub's avatar
zorglub committed
213
    mtime_t i_last;
zorglub's avatar
zorglub committed
214 215 216 217 218 219 220

    uint16_t    i_hash;
    uint32_t    i_source[4];

    /* SAP annnounces must only contain one SDP */
    sdp_t       *p_sdp;

221 222 223
    int i_input_id;
    int i_item_id_cat;
    int i_item_id_one;
224 225
};

zorglub's avatar
zorglub committed
226
struct services_discovery_sys_t
227
{
zorglub's avatar
zorglub committed
228 229 230
    /* Socket descriptors */
    int i_fd;
    int *pi_fd;
231

zorglub's avatar
zorglub committed
232
    /* playlist node */
233 234
    playlist_item_t *p_node_cat;
    playlist_item_t *p_node_one;
235 236 237 238

    /* Table of announces */
    int i_announces;
    struct sap_announce_t **pp_announces;
239

zorglub's avatar
zorglub committed
240 241 242
    /* Modes */
    vlc_bool_t  b_strict;
    vlc_bool_t  b_parse;
243
    vlc_bool_t  b_timeshift;
zorglub's avatar
zorglub committed
244

245
    int i_timeout;
246
};
247

248 249 250 251 252
struct demux_sys_t
{
    sdp_t *p_sdp;
};

zorglub's avatar
zorglub committed
253 254 255
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
256

sigmunau's avatar
sigmunau committed
257

zorglub's avatar
zorglub committed
258 259 260
/* Main functions */
    static int Demux( demux_t *p_demux );
    static int Control( demux_t *, int, va_list );
zorglub's avatar
zorglub committed
261
    static void Run    ( services_discovery_t *p_sd );
sigmunau's avatar
sigmunau committed
262

zorglub's avatar
zorglub committed
263 264
/* Main parsing functions */
    static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp );
265
    static int ParseSAP( services_discovery_t *p_sd, const uint8_t *p_buffer, size_t i_read );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
266
    static sdp_t *ParseSDP (vlc_object_t *p_sd, const char *psz_sdp);
zorglub's avatar
zorglub committed
267 268
    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 );
sigmunau's avatar
sigmunau committed
269

zorglub's avatar
zorglub committed
270
/* Helper functions */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
271 272 273
    static inline attribute_t *MakeAttribute (const char *str);
    static const char *GetAttribute (attribute_t **tab, unsigned n, const char *name);
    static inline void FreeAttribute (attribute_t *a);
274 275
    static const char *FindAttribute (const sdp_t *sdp, unsigned media,
                                      const char *name);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
276

277
    static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 );
278
    static int InitSocket( services_discovery_t *p_sd, const char *psz_address, int i_port );
279
    static int Decompress( const unsigned char *psz_src, unsigned char **_dst, int i_len );
Christophe Massiot's avatar
Christophe Massiot committed
280
    static void FreeSDP( sdp_t *p_sdp );
sigmunau's avatar
sigmunau committed
281

282 283 284 285 286
/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
zorglub's avatar
zorglub committed
287
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
zorglub's avatar
zorglub committed
288 289
    services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)
                                malloc( sizeof( services_discovery_sys_t ) );
290

291
    p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" );
292

zorglub's avatar
zorglub committed
293 294
    p_sd->pf_run = Run;
    p_sd->p_sys  = p_sys;
zorglub's avatar
zorglub committed
295 296 297 298

    p_sys->pi_fd = NULL;
    p_sys->i_fd = 0;

299 300
    p_sys->b_strict = var_CreateGetInteger( p_sd, "sap-strict");
    p_sys->b_parse = var_CreateGetInteger( p_sd, "sap-parse" );
zorglub's avatar
zorglub committed
301

302
#if 0
303
    if( var_CreateGetInteger( p_sd, "sap-cache" ) )
zorglub's avatar
zorglub committed
304
    {
zorglub's avatar
zorglub committed
305
        CacheLoad( p_sd );
306
    }
307
#endif
308

309 310 311 312
    /* Cache sap_timeshift value */
    p_sys->b_timeshift = var_CreateGetInteger( p_sd, "sap-timeshift" )
            ? VLC_TRUE : VLC_FALSE;

zorglub's avatar
zorglub committed
313
    /* Create our playlist node */
314
    pl_Yield( p_sd );
315

316
    playlist_NodesPairCreate( pl_Get( p_sd ), _("SAP sessions"),
317 318
                              &p_sys->p_node_cat, &p_sys->p_node_one,
                              VLC_TRUE );
319
    p_sys->p_node_cat->p_input->b_prefers_tree = VLC_TRUE;
320 321 322
    p_sys->i_announces = 0;
    p_sys->pp_announces = NULL;

zorglub's avatar
zorglub committed
323 324 325 326 327 328 329 330 331 332
    return VLC_SUCCESS;
}

/*****************************************************************************
 * OpenDemux: initialize and create stuff
 *****************************************************************************/
static int OpenDemux( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t *)p_this;
    uint8_t *p_peek;
333
    char *psz_sdp = NULL;
334
    sdp_t *p_sdp = NULL;
335
    int errval = VLC_EGENERIC;
zorglub's avatar
zorglub committed
336

337 338 339 340 341 342
    if( !var_CreateGetInteger( p_demux, "sap-parse" ) )
    {
        /* We want livedotcom module to parse this SDP file */
        return VLC_EGENERIC;
    }

343
    assert( p_demux->s ); /* this is NOT an access_demux */
344

345 346 347
    /* Probe for SDP */
    if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
        return VLC_EGENERIC;
zorglub's avatar
zorglub committed
348

349 350
    if( memcmp( p_peek, "v=0\r\no=", 7 ) && memcmp( p_peek, "v=0\no=", 6 ) )
        return VLC_EGENERIC;
351

352
    /* Gather the complete sdp file */
353
    psz_sdp = NULL;
354

355 356 357 358 359 360 361
#define SDP_MAX 65536
#define OFFSET 1024

    for( size_t buflen = 0; buflen < SDP_MAX; buflen += OFFSET )
    {
        char *psz_sdp_new = realloc( psz_sdp, buflen + 1 );
        if( psz_sdp_new == NULL )
362
        {
363
            errval = VLC_ENOMEM;
364 365
            goto error;
        }
366
        psz_sdp = psz_sdp_new;
367

368 369
        ssize_t i_read = stream_Read( p_demux->s, psz_sdp + buflen, OFFSET );
        if( i_read < 0 )
370
        {
371 372
            msg_Err( p_demux, "cannot read SDP" );
            goto error;
373 374
        }

375 376 377 378
        psz_sdp[buflen + i_read] = '\0';

        if( i_read < OFFSET )
            break; // EOF
379
    }
380
#undef OFFSET
381

382
    p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp );
383

384 385 386 387 388
    if( !p_sdp )
    {
        msg_Warn( p_demux, "invalid SDP");
        goto error;
    }
389

390 391 392 393
    if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) )
    {
        p_sdp->psz_uri = NULL;
    }
394 395
    if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 &&
        p_sdp->i_media_type != 14 )
396
        goto error;
397

398 399 400 401
    if( p_sdp->psz_uri == NULL ) goto error;

    p_demux->p_sys = (demux_sys_t *)malloc( sizeof(demux_sys_t) );
    p_demux->p_sys->p_sdp = p_sdp;
zorglub's avatar
zorglub committed
402 403
    p_demux->pf_control = Control;
    p_demux->pf_demux = Demux;
404

zorglub's avatar
zorglub committed
405
    FREENULL( psz_sdp );
406
    return VLC_SUCCESS;
407

408
error:
zorglub's avatar
zorglub committed
409
    FREENULL( psz_sdp );
410
    if( p_sdp ) FreeSDP( p_sdp ); p_sdp = NULL;
411
    stream_Seek( p_demux->s, 0 );
412
    return errval;
413 414
}

415 416 417 418
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
419
{
zorglub's avatar
zorglub committed
420 421
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
    services_discovery_sys_t    *p_sys  = p_sd->p_sys;
zorglub's avatar
zorglub committed
422

zorglub's avatar
free  
zorglub committed
423
    int i;
424

zorglub's avatar
zorglub committed
425
    for( i = p_sys->i_fd-1 ; i >= 0 ; i-- )
426
    {
zorglub's avatar
zorglub committed
427
        net_Close( p_sys->pi_fd[i] );
428
    }
zorglub's avatar
zorglub committed
429
    FREENULL( p_sys->pi_fd );
zorglub's avatar
zorglub committed
430

431
#if 0
zorglub's avatar
zorglub committed
432
    if( config_GetInt( p_sd, "sap-cache" ) )
433
    {
zorglub's avatar
zorglub committed
434
        CacheSave( p_sd );
435
    }
436
#endif
437

zorglub's avatar
zorglub committed
438
    for( i = p_sys->i_announces  - 1;  i>= 0; i-- )
zorglub's avatar
free  
zorglub committed
439
    {
zorglub's avatar
zorglub committed
440
        RemoveAnnounce( p_sd, p_sys->pp_announces[i] );
zorglub's avatar
free  
zorglub committed
441
    }
zorglub's avatar
zorglub committed
442
    FREENULL( p_sys->pp_announces );
zorglub's avatar
free  
zorglub committed
443

444 445 446 447 448
    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 );
449 450
    free( p_sys );
}
451

452
/*****************************************************************************
zorglub's avatar
zorglub committed
453 454 455 456
 * CloseDemux: Close the demuxer
 *****************************************************************************/
static void CloseDemux( vlc_object_t *p_this )
{
457 458 459
    demux_t *p_demux = (demux_t *)p_this;
    if( p_demux->p_sys )
    {
460
        if( p_demux->p_sys->p_sdp ) { FreeSDP( p_demux->p_sys->p_sdp ); p_demux->p_sys->p_sdp = NULL; }
461 462
        free( p_demux->p_sys );
    }
zorglub's avatar
zorglub committed
463 464 465 466
}

/*****************************************************************************
 * Run: main SAP thread
467 468 469
 *****************************************************************************
 * Listens to SAP packets, and sends them to packet_handle
 *****************************************************************************/
sigmunau's avatar
sigmunau committed
470
#define MAX_SAP_BUFFER 5000
471

zorglub's avatar
zorglub committed
472
static void Run( services_discovery_t *p_sd )
473
{
474
    char *psz_addr;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
475
    int i;
476

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    /* Braindead Winsock DNS resolver will get stuck over 2 seconds per failed
     * DNS queries, even if the DNS server returns an error with milliseconds.
     * You don't want to know why the bug (as of XP SP2) wasn't fixed since
     * Winsock 1.1 from Windows 95, if not Windows 3.1.
     * Anyway, to avoid a 30 seconds delay for failed IPv6 socket creation,
     * we have to open sockets in Run() rather than Open(). */
    if( var_CreateGetInteger( p_sd, "sap-ipv4" ) )
    {
        InitSocket( p_sd, SAP_V4_GLOBAL_ADDRESS, SAP_PORT );
        InitSocket( p_sd, SAP_V4_ORG_ADDRESS, SAP_PORT );
        InitSocket( p_sd, SAP_V4_LOCAL_ADDRESS, SAP_PORT );
        InitSocket( p_sd, SAP_V4_LINK_ADDRESS, SAP_PORT );
    }
    if( var_CreateGetInteger( p_sd, "sap-ipv6" ) )
    {
492 493
        char psz_address[NI_MAXNUMERICHOST] = "ff02::2:7ffe%";

494
#ifndef WIN32
495 496 497 498 499 500 501 502 503 504 505
        struct if_nameindex *l = if_nameindex ();
        if (l != NULL)
        {
            char *ptr = strchr (psz_address, '%') + 1;
            for (unsigned i = 0; l[i].if_index; i++)
            {
                strcpy (ptr, l[i].if_name);
                InitSocket (p_sd, psz_address, SAP_PORT);
            }
            if_freenameindex (l);
        }
506 507 508 509 510 511 512 513 514 515
#else
        /* this is the Winsock2 equivalant of SIOCGIFCONF on BSD stacks,
           which if_nameindex uses internally anyway */

        // first create a dummy socket to pin down the protocol family
        SOCKET s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        if( s != INVALID_SOCKET )
        {
            INTERFACE_INFO ifaces[10]; // Assume there will be no more than 10 IP interfaces
            size_t len = sizeof(ifaces); 
516

517 518 519 520 521 522 523 524 525 526 527 528 529
            if( SOCKET_ERROR != WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, &ifaces, len, &len, NULL, NULL) )
            {
                unsigned ifcount = len/sizeof(INTERFACE_INFO);
                char *ptr = strchr (psz_address, '%') + 1;
                for(unsigned i = 1; i<=ifcount; ++i )
                {
                    // append link-local zone identifier
                    sprintf(ptr, "%d", i);
                }
            }
            closesocket(s);
        }
#endif
530
        *strchr (psz_address, '%') = '\0';
531

532 533
        static const char ipv6_scopes[] = "1456789ABCDE";
        for (const char *c_scope = ipv6_scopes; *c_scope; c_scope++)
534
        {
535
            psz_address[3] = *c_scope;
536 537 538 539
            InitSocket( p_sd, psz_address, SAP_PORT );
        }
    }

540 541 542 543
    psz_addr = var_CreateGetString( p_sd, "sap-addr" );
    if( psz_addr && *psz_addr )
    {
        InitSocket( p_sd, psz_addr, SAP_PORT );
544
        free( psz_addr );
545 546
    }

547 548 549 550 551 552
    if( p_sd->p_sys->i_fd == 0 )
    {
        msg_Err( p_sd, "unable to listen on any address" );
        return;
    }

553
    /* read SAP packets */
zorglub's avatar
zorglub committed
554
    while( !p_sd->b_die )
555
    {
556
        int i_read;
zorglub's avatar
zorglub committed
557
        uint8_t p_buffer[MAX_SAP_BUFFER+1];
zorglub's avatar
zorglub committed
558

559
        i_read = net_Select( p_sd, p_sd->p_sys->pi_fd,
560
                             p_sd->p_sys->i_fd, p_buffer,
561
                             MAX_SAP_BUFFER );
562

zorglub's avatar
zorglub committed
563
        /* Check for items that need deletion */
564
        for( i = 0; i < p_sd->p_sys->i_announces; i++ )
zorglub's avatar
zorglub committed
565
        {
566 567 568 569
            mtime_t i_timeout = ( mtime_t ) 1000000 * p_sd->p_sys->i_timeout;

            if( mdate() - p_sd->p_sys->pp_announces[i]->i_last > i_timeout )
            {
570
                RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i] );
571
            }
zorglub's avatar
zorglub committed
572 573
        }

574 575
        /* Minimum length is > 6 */
        if( i_read <= 6 )
576
        {
577 578
            if( i_read < 0 )
            {
zorglub's avatar
zorglub committed
579
                msg_Warn( p_sd, "socket read error" );
580 581
            }
            continue;
582 583
        }

zorglub's avatar
zorglub committed
584 585 586
        p_buffer[i_read] = '\0';

        /* Parse the packet */
zorglub's avatar
zorglub committed
587
        ParseSAP( p_sd, p_buffer, i_read );
zorglub's avatar
zorglub committed
588 589 590 591 592 593 594 595 596
    }
}

/**********************************************************************
 * Demux: reads and demuxes data packets
 * Return -1 if error, 0 if EOF, 1 else
 **********************************************************************/
static int Demux( demux_t *p_demux )
{
597
    sdp_t *p_sdp = p_demux->p_sys->p_sdp;
598 599
    input_thread_t *p_input;
    input_item_t *p_parent_input;
zorglub's avatar
zorglub committed
600

601
    playlist_t *p_playlist = pl_Yield( p_demux );
602
    p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT,
603 604
                                                 FIND_PARENT );
    assert( p_input );
605 606 607 608 609 610
    if( !p_input )
    {
        msg_Err( p_demux, "parent input could not be found" );
        return VLC_EGENERIC;
    }

zorglub's avatar
zorglub committed
611
    p_parent_input = input_GetItem(p_input);
zorglub's avatar
zorglub committed
612

613
    vlc_mutex_lock( &p_parent_input->lock );
zorglub's avatar
zorglub committed
614
    FREENULL( p_parent_input->psz_uri );
615
    p_parent_input->psz_uri = strdup( p_sdp->psz_uri );
zorglub's avatar
zorglub committed
616
    FREENULL( p_parent_input->psz_name );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
617
    p_parent_input->psz_name = strdup( p_sdp->psz_sessionname );
618
    p_parent_input->i_type = ITEM_TYPE_NET;
zorglub's avatar
zorglub committed
619

620 621 622
    if( p_playlist->status.p_item &&
             p_playlist->status.p_item->p_input == p_parent_input )
    {
623
        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
624 625 626 627 628
                          p_playlist->status.p_node, p_playlist->status.p_item );
    }

    vlc_mutex_unlock( &p_parent_input->lock );
    vlc_object_release( p_input );
629
    vlc_object_release( p_playlist );
zorglub's avatar
zorglub committed
630

631
    return VLC_SUCCESS;
632 633
}

zorglub's avatar
zorglub committed
634 635 636 637 638 639 640 641
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    return VLC_EGENERIC;
}

/**************************************************************
 * Local functions
 **************************************************************/
642

zorglub's avatar
zorglub committed
643
/* i_read is at least > 6 */
644 645
static int ParseSAP( services_discovery_t *p_sd, const uint8_t *buf,
                     size_t len )
646
{
647 648 649
    int i;
    const char          *psz_sdp;
    const uint8_t *end = buf + len;
zorglub's avatar
zorglub committed
650 651
    sdp_t               *p_sdp;

652
    assert (buf[len] == '\0');
653

654 655
    if (len < 4)
        return VLC_EGENERIC;
656

657 658 659 660
    uint8_t flags = buf[0];

    /* First, check the sap announce is correct */
    if ((flags >> 5) != 1)
zorglub's avatar
zorglub committed
661 662
        return VLC_EGENERIC;

663 664
    vlc_bool_t b_ipv6 = (flags & 0x10) != 0;
    vlc_bool_t b_need_delete = (flags & 0x04) != 0;
665

666
    if (flags & 0x02)
zorglub's avatar
zorglub committed
667
    {
zorglub's avatar
zorglub committed
668
        msg_Dbg( p_sd, "encrypted packet, unsupported" );
zorglub's avatar
zorglub committed
669 670
        return VLC_EGENERIC;
    }
671

672
    vlc_bool_t b_compressed = (flags & 0x01) != 0;
673

674
    uint16_t i_hash = U16_AT (buf + 2);
675

zorglub's avatar
zorglub committed
676
    if( p_sd->p_sys->b_strict && i_hash == 0 )
677
    {
zorglub's avatar
zorglub committed
678
        msg_Dbg( p_sd, "strict mode, discarding announce with null id hash");
zorglub's avatar
zorglub committed
679 680 681
        return VLC_EGENERIC;
    }

682 683 684 685
    // Skips source address and auth data
    buf += 4 + (b_ipv6 ? 16 : 4) + buf[1];
    if (buf > end)
        return VLC_EGENERIC;
zorglub's avatar
zorglub committed
686

687
    uint8_t *decomp = NULL;
688
    if( b_compressed )
zorglub's avatar
zorglub committed
689
    {
690 691
        int newsize = Decompress (buf, &decomp, end - buf);
        if (newsize < 0)
zorglub's avatar
zorglub committed
692
        {
693
            msg_Dbg( p_sd, "decompression of SAP packet failed" );
zorglub's avatar
zorglub committed
694 695
            return VLC_EGENERIC;
        }
zorglub's avatar
zorglub committed
696

697
        decomp = realloc (decomp, newsize + 1);
698
        decomp[newsize] = '\0';
699 700
        psz_sdp = (const char *)decomp;
        len = newsize;
701
    }
702
    else
zorglub's avatar
zorglub committed
703
    {
704 705
        psz_sdp = (const char *)buf;
        len = end - buf;
zorglub's avatar
zorglub committed
706
    }
707

708 709
    /* 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');
710

zorglub's avatar
zorglub committed
711
    /* Skip payload type */
712 713
    /* SAPv1 has implicit "application/sdp" payload type: first line is v=0 */
    if (strncmp (psz_sdp, "v=0", 3))
714
    {
715 716 717
        size_t clen = strlen (psz_sdp) + 1;

        if (strcmp (psz_sdp, "application/sdp"))
zorglub's avatar
zorglub committed
718
        {
719 720
            msg_Dbg (p_sd, "unsupported content type: %s", psz_sdp);
            return VLC_EGENERIC;
zorglub's avatar
zorglub committed
721
        }
722

723 724 725 726 727 728
        // skips content type
        if (len <= clen)
            return VLC_EGENERIC;

        len -= clen;
        psz_sdp += clen;
729
    }
730

zorglub's avatar
zorglub committed
731
    /* Parse SDP info */
zorglub's avatar
zorglub committed
732
    p_sdp = ParseSDP( VLC_OBJECT(p_sd), psz_sdp );
733

zorglub's avatar
zorglub committed
734 735 736
    if( p_sdp == NULL )
        return VLC_EGENERIC;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
737 738
    p_sdp->psz_sdp = psz_sdp;

zorglub's avatar
zorglub committed
739
    /* Decide whether we should add a playlist item for this SDP */
zorglub's avatar
zorglub committed
740 741 742
    /* Parse connection information (c= & m= ) */
    if( ParseConnection( VLC_OBJECT(p_sd), p_sdp ) )
        p_sdp->psz_uri = NULL;
zorglub's avatar
zorglub committed
743 744

    /* Multi-media or no-parse -> pass to LIVE.COM */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
745 746 747 748
    if( ( p_sdp->i_media_type != 14
       && p_sdp->i_media_type != 32
       && p_sdp->i_media_type != 33)
     || p_sd->p_sys->b_parse == VLC_FALSE )
zorglub's avatar
zorglub committed
749
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
750 751 752
        free( p_sdp->psz_uri );
        if (asprintf( &p_sdp->psz_uri, "sdp://%s", p_sdp->psz_sdp ) == -1)
            p_sdp->psz_uri = NULL;
zorglub's avatar
zorglub committed
753 754 755 756
    }

    if( p_sdp->psz_uri == NULL ) return VLC_EGENERIC;

zorglub's avatar
zorglub committed
757
    for( i = 0 ; i< p_sd->p_sys->i_announces ; i++ )
zorglub's avatar
zorglub committed
758 759 760
    {
        /* FIXME: slow */
        /* FIXME: we create a new announce each time the sdp changes */
761
        if( IsSameSession( p_sd->p_sys->pp_announces[i]->p_sdp, p_sdp ) )
762
        {
zorglub's avatar
zorglub committed
763 764
            if( b_need_delete )
            {
zorglub's avatar
zorglub committed
765
                RemoveAnnounce( p_sd, p_sd->p_sys->pp_announces[i]);