1 /*****************************************************************************
2 * sap.c : SAP interface module
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: sap.c,v 1.52 2004/01/29 17:51:08 zorglub Exp $
7 * Authors: Arnaud Schauly <gitan@via.ecp.fr>
8 * Clément Stenac <zorglub@via.ecp.fr>
9 * Damien Lucas <nitrox@videolan.org>
10 * Laurent Aimar <fenrir@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
35 #include <errno.h> /* ENOMEM */
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
46 # include <winsock2.h>
47 # include <ws2tcpip.h>
49 # define IN_MULTICAST(a) IN_CLASSD(a)
52 # include <sys/socket.h>
53 # include <netinet/in.h>
55 # include <arpa/inet.h>
56 # elif defined( SYS_BEOS )
57 # include <net/netdb.h>
62 # define close(a) CloseHandle(a)
63 #elif defined( WIN32 )
64 # define close(a) closesocket(a)
73 #define MAX_LINE_LENGTH 256
75 /* SAP is always on that port */
76 #define HELLO_PORT 9875
77 #define HELLO_GROUP "224.2.127.254"
80 #define IPV6_ADDR_1 "FF0" /* Scope is inserted between them */
81 #define IPV6_ADDR_2 "::2:7FFE"
84 /*****************************************************************************
86 *****************************************************************************/
87 #define SAP_ADDR_TEXT N_("SAP multicast address")
88 #define SAP_ADDR_LONGTEXT N_("SAP multicast address")
89 #define SAP_IPV4_TEXT N_("IPv4-SAP listening")
90 #define SAP_IPV4_LONGTEXT N_( \
91 "Set this if you want the SAP module to listen to IPv4 announces")
92 #define SAP_IPV6_TEXT N_( "IPv6-SAP listening")
93 #define SAP_IPV6_LONGTEXT N_(\
94 "Set this if you want the SAP module to listen to IPv6 announces")
95 #define SAP_SCOPE_TEXT N_("IPv6 SAP scope")
96 #define SAP_SCOPE_LONGTEXT N_( \
97 "Sets the scope for IPv6 announces (default is 8)")
98 #define SAP_TIMEOUT_TEXT N_("SAP timeout (seconds)")
99 #define SAP_TIMEOUT_LONGTEXT N_( \
100 "Sets the time before SAP items get deleted if no new announce" \
103 static int Open ( vlc_object_t * );
104 static void Close( vlc_object_t * );
107 set_description( _("SAP interface") );
109 add_string( "sap-addr", NULL, NULL,
110 SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE );
111 add_bool( "sap-ipv4", 1 , NULL,
112 SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE);
113 add_bool( "sap-ipv6", 0 , NULL,
114 SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE);
115 add_string( "sap-ipv6-scope", "8" , NULL,
116 SAP_SCOPE_TEXT, SAP_SCOPE_LONGTEXT, VLC_TRUE);
117 add_integer( "sap-timeout", 1800, NULL,
118 SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE);
120 set_capability( "interface", 0 );
121 set_callbacks( Open, Close );
124 /*****************************************************************************
126 *****************************************************************************/
128 static void Run ( intf_thread_t *p_intf );
129 static ssize_t NetRead( intf_thread_t *, int fd[2], uint8_t *, int );
131 typedef struct media_descr_t media_descr_t;
132 typedef struct sess_descr_t sess_descr_t;
133 typedef struct attr_descr_t attr_descr_t;
135 static void sess_toitem( intf_thread_t *, sess_descr_t * );
137 static sess_descr_t * parse_sdp( intf_thread_t *, char * ) ;
138 static void free_sd( sess_descr_t * );
140 /* Detect multicast addresses */
141 static int ismult( char * );
143 /* The struct that contains sdp informations */
147 char *psz_sessionname;
148 char *psz_connection;
152 media_descr_t **pp_media;
154 attr_descr_t **pp_attributes;
157 /* All this informations are not useful yet. */
161 char *psz_mediaconnection;
170 struct sap_announce_t
186 /* Table of announces */
188 struct sap_announce_t **pp_announces;
194 int do_decompress(unsigned char *src, unsigned char **_dst, int slen) {
195 int result, dstsize, n;
199 d_stream.zalloc = (alloc_func)0;
200 d_stream.zfree = (free_func)0;
201 d_stream.opaque = (voidpf)0;
202 result = inflateInit(&d_stream);
203 if (result != Z_OK) {
204 printf("inflateInit() failed. Result: %d\n", result);
208 d_stream.next_in = (Bytef *)src;
209 d_stream.avail_in = slen;
214 dst = (unsigned char *)realloc(dst, n * 1000);
215 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
216 d_stream.avail_out = 1000;
217 result = inflate(&d_stream, Z_NO_FLUSH);
218 if ((result != Z_OK) && (result != Z_STREAM_END)) {
219 printf("Zlib decompression failed. Result: %d\n", result);
222 } while ((d_stream.avail_out == 0) && (d_stream.avail_in != 0) &&
223 (result != Z_STREAM_END));
225 dstsize = d_stream.total_out;
226 inflateEnd(&d_stream);
228 *_dst = (unsigned char *)realloc(dst, dstsize);
234 /*****************************************************************************
235 * Open: initialize and create stuff
236 *****************************************************************************/
237 static int Open( vlc_object_t *p_this )
239 intf_thread_t *p_intf = (intf_thread_t*)p_this;
240 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
242 playlist_t *p_playlist;
244 p_sys->i_timeout = config_GetInt(p_intf,"sap-timeout");
247 if( config_GetInt( p_intf, "sap-ipv4" ) )
249 char *psz_address = config_GetPsz( p_intf, "sap-addr" );
250 network_socket_t sock;
252 if( psz_address == NULL || *psz_address == '\0' )
254 psz_address = strdup( HELLO_GROUP );
257 /* Prepare the network_socket_t structure */
258 sock.i_type = NETWORK_UDP;
259 sock.psz_bind_addr = psz_address;
260 sock.i_bind_port = HELLO_PORT;
261 sock.psz_server_addr = "";
262 sock.i_server_port = 0;
264 p_intf->p_private = (void*) &sock;
266 p_network = module_Need( p_intf, "network", "ipv4" );
269 p_sys->fd[0] = sock.i_handle;
270 module_Unneed( p_intf, p_network );
274 msg_Warn( p_intf, "failed to open %s:%d", psz_address, HELLO_PORT );
279 if( config_GetInt( p_intf, "sap-ipv6" ) )
281 char psz_address[100];
282 char *psz_scope = config_GetPsz( p_intf, "sap-ipv6-scope" );
283 network_socket_t sock;
286 if( psz_scope == NULL || *psz_scope == '\0' )
288 psz_scope = strdup( "8" );
290 snprintf( psz_address, 100, "[%s%c%s]",IPV6_ADDR_1,
291 psz_scope[0], IPV6_ADDR_2 );
294 sock.i_type = NETWORK_UDP;
295 sock.psz_bind_addr = psz_address;
296 sock.i_bind_port = HELLO_PORT;
297 sock.psz_server_addr = "";
298 sock.i_server_port = 0;
300 p_intf->p_private = (void*) &sock;
302 p_network = module_Need( p_intf, "network", "ipv6" );
305 p_sys->fd[1] = sock.i_handle;
306 module_Unneed( p_intf, p_network );
310 msg_Warn( p_intf, "failed to open %s:%d", psz_address, HELLO_PORT );
313 if( p_sys->fd[0] <= 0 && p_sys->fd[1] <= 0 )
315 msg_Warn( p_intf, "IPV4 and IPV6 failed" );
320 /* Create our playlist group */
321 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
325 playlist_group_t *p_group = playlist_CreateGroup( p_playlist , "SAP" );
326 p_sys->i_group = p_group->i_id;
327 vlc_object_release( p_playlist );
330 p_sys->i_announces = 0;
331 p_sys->pp_announces = NULL;
333 p_intf->pf_run = Run;
334 p_intf->p_sys = p_sys;
339 /*****************************************************************************
341 *****************************************************************************/
342 static void Close( vlc_object_t *p_this )
344 intf_thread_t *p_intf = (intf_thread_t*)p_this;
345 intf_sys_t *p_sys = p_intf->p_sys;
348 if( p_sys->fd[0] > 0 )
350 close( p_sys->fd[0] );
352 if( p_sys->fd[1] > 0 )
354 close( p_sys->fd[1] );
357 for( i = 0 ; i< p_sys->i_announces ; i++ )
359 if( p_sys->pp_announces[i]->psz_name )
360 free( p_sys->pp_announces[i]->psz_name );
361 if( p_sys->pp_announces[i]->psz_uri )
362 free( p_sys->pp_announces[i]->psz_uri );
363 free( p_sys->pp_announces[i] );
369 /*****************************************************************************
371 *****************************************************************************
372 * Listens to SAP packets, and sends them to packet_handle
373 *****************************************************************************/
374 #define MAX_SAP_BUFFER 5000
376 static void Run( intf_thread_t *p_intf )
378 intf_sys_t *p_sys = p_intf->p_sys;
379 uint8_t buffer[MAX_SAP_BUFFER + 1];
382 /* read SAP packets */
383 while( !p_intf->b_die )
385 playlist_t *p_playlist= NULL;
387 int i_read = NetRead( p_intf, p_sys->fd, buffer, MAX_SAP_BUFFER );
395 unsigned char *p_decompressed_buffer;
396 int i_decompressed_size;
398 /* Check for items that need deletion */
399 for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
401 struct sap_announce_t *p_announce;
402 mtime_t i_timeout = (mtime_t)1000000*p_sys->i_timeout;
403 if( mdate() - p_intf->p_sys->pp_announces[i]->i_last > i_timeout )
405 msg_Dbg(p_intf,"Time out for %s, deleting (%i/%i)",
406 p_intf->p_sys->pp_announces[i]->psz_name,
407 i , p_intf->p_sys->i_announces );
409 /* Remove the playlist item */
410 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
414 int i_pos = playlist_GetPositionById( p_playlist,
415 p_intf->p_sys->pp_announces[i]->i_id );
416 playlist_Delete( p_playlist, i_pos );
419 vlc_object_release( p_playlist );
421 /* Free the p_announce */
422 p_announce = p_intf->p_sys->pp_announces[i];
423 if( p_announce->psz_name )
424 free( p_announce->psz_name );
425 if( p_announce->psz_uri )
426 free( p_announce->psz_uri );
428 /* Remove the sap_announce from the array */
429 REMOVE_ELEM( p_intf->p_sys->pp_announces,
430 p_intf->p_sys->i_announces,
438 /* Minimum length is > 6 */
443 msg_Warn( p_intf, "Cannot read in the socket" );
448 buffer[i_read] = '\0';
449 p_end = &buffer[i_read];
451 /* Parse the SAP header */
452 i_version = buffer[0] >> 5;
455 msg_Warn( p_intf, "strange sap version %d found", i_version );
457 i_address_type = buffer[0] & 0x10;
458 b_reserved = buffer[0] & 0x08;
459 if( b_reserved != 0 )
461 msg_Warn( p_intf, "reserved bit incorrectly set" );
463 b_message_type = buffer[0] & 0x04;
464 if( b_message_type != 0 )
466 msg_Warn( p_intf, "got session deletion packet" );
468 b_encrypted = buffer[0] & 0x02;
471 msg_Warn( p_intf, "encrypted packet" );
473 b_compressed = buffer[0] & 0x01;
475 if( i_address_type == 0 ) /* ipv4 source address */
479 else /* ipv6 source address */
486 i_decompressed_size = do_decompress( p_sdp, &p_decompressed_buffer, i_read - ( p_sdp - buffer ) );
487 if( i_decompressed_size > 0 && i_decompressed_size < MAX_SAP_BUFFER )
489 memcpy( p_sdp, p_decompressed_buffer, i_decompressed_size );
490 p_sdp[i_decompressed_size] = '\0';
491 p_end = &p_sdp[i_decompressed_size];
492 free( p_decompressed_buffer );
495 msg_Warn( p_intf, "Ignoring compressed sap packet" );
498 p_sdp += buffer[1]; /* size of signature */
499 while( p_sdp < p_end - 1 && *p_sdp != '\0' && p_sdp[0] != 'v' && p_sdp[1] != '=' )
510 sess_descr_t *p_sd = parse_sdp( p_intf, p_sdp );
513 sess_toitem ( p_intf, p_sd );
519 msg_Warn( p_intf, "ditching sap packet" );
525 /**********************************************************************
527 *********************************************************************
528 * put into *ppsz_uri, the the uri in the cfield, psz_cfield.
529 *********************************************************************/
531 static void cfield_parse( char *psz_cfield, char **ppsz_uri )
537 psz_pos = psz_cfield;
539 while( *psz_pos != ' ' && *psz_pos !='\0' )
544 while( *psz_pos != ' ' && *psz_pos !='\0' )
550 while( *psz_pos != ' ' && *psz_pos !='/'
551 && *psz_pos != '\0' )
567 /**********************************************************************
569 *********************************************************************
570 * put into *ppsz_proto, and *ppsz_port, the protocol and the port.
571 *********************************************************************/
574 static void mfield_parse( char *psz_mfield, char **ppsz_proto,
581 psz_pos = psz_mfield;
582 psz_media = psz_mfield;
583 while( *psz_pos != '\0' && *psz_pos != ' ' )
587 if( *psz_pos != '\0' )
590 if( strcmp( psz_media, "video" ) && strcmp( psz_media, "audio" ) )
598 *ppsz_port = psz_pos;
599 while( *psz_pos != '\0' && *psz_pos !=' ' && *psz_pos!='/' )
603 if( *psz_pos == '/' ) // FIXME does not support multi-port
607 while( *psz_pos != '\0' && *psz_pos !=' ' )
614 *ppsz_proto = psz_pos;
615 while( *psz_pos!='\0' && *psz_pos !=' ' &&
618 *psz_pos = tolower( *psz_pos );
632 /*******************************************************************
633 * sess_toitem : changes a sess_descr_t into a hurd of
634 * playlist_item_t, which are enqueued.
635 *******************************************************************
636 * Note : does not support sessions that take place on consecutive
637 * port or adresses yet.
638 *******************************************************************/
640 static void sess_toitem( intf_thread_t * p_intf, sess_descr_t * p_sd )
642 struct sap_announce_t *p_announce;
643 char *psz_uri, *psz_proto, *psz_item_uri;
645 char *psz_uri_default;
646 int i_count , i , i_pos , i_id = 0;
647 vlc_bool_t b_http = VLC_FALSE;
648 char *psz_http_path = NULL;
649 playlist_t *p_playlist = NULL;
650 playlist_item_t *p_item;
652 psz_uri_default = NULL;
653 if( p_sd->i_media > 1 )
655 asprintf( &psz_uri, "sdp://%s", p_sd->psz_sdp );
656 /* Check if we have already added the item */
657 for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
659 if( !strcmp(p_intf->p_sys->pp_announces[i]->psz_uri,
662 p_intf->p_sys->pp_announces[i]->i_last = mdate();
667 /* Add it to the playlist */
668 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
670 i_id = playlist_Add( p_playlist, psz_uri, p_sd->psz_sessionname ,
671 PLAYLIST_CHECK_INSERT, PLAYLIST_END );
674 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
675 playlist_ItemSetGroup( p_item, p_intf->p_sys->i_group );
679 p_announce = (struct sap_announce_t *)malloc(
680 sizeof(struct sap_announce_t) );
681 if( p_sd->psz_sessionname )
683 p_announce->psz_name = strdup( p_sd->psz_sessionname );
687 p_announce->psz_name = strdup( "" );
691 p_announce->psz_uri = strdup( psz_uri );
695 p_announce->psz_uri = strdup( "" );
697 p_announce->i_id = i_id;
698 p_announce->i_last = mdate();
700 INSERT_ELEM( p_intf->p_sys->pp_announces,
701 p_intf->p_sys->i_announces,
702 p_intf->p_sys->i_announces,
705 vlc_object_release( p_playlist );
710 cfield_parse( p_sd->psz_connection, &psz_uri_default );
712 for( i_count = 0 ; i_count < p_sd->i_media ; i_count++ )
714 int i_group = p_intf->p_sys->i_group;
716 /* Build what we have to put in psz_item_uri, with the m and
719 if( !p_sd->pp_media[i_count] )
724 mfield_parse( p_sd->pp_media[i_count]->psz_medianame,
725 & psz_proto, & psz_port );
727 if( !psz_proto || !psz_port )
732 if( p_sd->pp_media[i_count]->psz_mediaconnection )
734 cfield_parse( p_sd->pp_media[i_count]->psz_mediaconnection,
739 psz_uri = psz_uri_default;
742 if( psz_uri == NULL )
747 for( i = 0 ; i< p_sd->i_attributes ; i++ )
749 if(!strcasecmp( p_sd->pp_attributes[i]->psz_field , "type") &&
750 strstr( p_sd->pp_attributes[i]->psz_value, "http") )
754 if(!strcasecmp( p_sd->pp_attributes[i]->psz_field , "http-path"))
756 psz_http_path = strdup( p_sd->pp_attributes[i]->psz_value );
758 if(!strcasecmp( p_sd->pp_attributes[i]->psz_field , "plgroup"))
762 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
764 if( p_playlist == NULL )
769 i_group_id = playlist_GroupToId( p_playlist,
770 p_sd->pp_attributes[i]->psz_value);
771 if( i_group_id != 0 )
773 i_group = i_group_id;
777 playlist_group_t *p_group =
778 playlist_CreateGroup( p_playlist,
779 p_sd->pp_attributes[i]->psz_value);
780 i_group = p_group->i_id;
782 vlc_object_release( p_playlist );
786 /* Filling psz_uri */
787 if( b_http == VLC_FALSE )
789 psz_item_uri = malloc( strlen( psz_proto ) + strlen( psz_uri ) +
790 strlen( psz_port ) + 7 );
791 if( ismult( psz_uri ) )
793 sprintf( psz_item_uri, "%s://@%s:%s",
794 psz_proto, psz_uri, psz_port );
798 sprintf( psz_item_uri, "%s://%s:%s",
799 psz_proto, psz_uri, psz_port );
804 if( psz_http_path == NULL )
806 psz_http_path = strdup( "/" );
809 psz_item_uri = malloc( strlen( psz_proto ) + strlen( psz_uri ) +
810 strlen( psz_port ) + strlen(psz_http_path) +
812 sprintf( psz_item_uri, "%s://%s:%s%s", psz_proto,
813 psz_uri, psz_port,psz_http_path );
817 free( psz_http_path );
821 /* Check if we already know this item */
822 for( i = 0 ; i< p_intf->p_sys->i_announces ; i++ )
824 if( !strcmp( p_intf->p_sys->pp_announces[i]->psz_uri,
827 p_intf->p_sys->pp_announces[i]->i_last = mdate();
829 /* Check if the name changed */
830 if( strcmp( p_intf->p_sys->pp_announces[i]->psz_name,
831 p_sd->psz_sessionname ) )
833 playlist_item_t *p_item;
834 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
837 msg_Dbg(p_intf, "Name changed (%s -> %s) for %s",
838 p_intf->p_sys->pp_announces[i]->psz_name,
839 p_sd->psz_sessionname,
842 p_item = playlist_ItemGetById( p_playlist,
843 p_intf->p_sys->pp_announces[i]->i_id );
845 /* Change the name in the item */
846 if( p_item->psz_name )
847 free( p_item->psz_name );
848 p_item->psz_name = strdup( p_sd->psz_sessionname);
850 /* Update the stored name */
851 if( p_intf->p_sys->pp_announces[i]->psz_name )
852 free( p_intf->p_sys->pp_announces[i]->psz_name );
853 p_intf->p_sys->pp_announces[i]->psz_name =
854 strdup( p_sd->psz_sessionname );
856 vlc_object_release( p_playlist );
862 /* Add the item in the playlist */
863 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
865 i_id = playlist_Add ( p_playlist, psz_item_uri ,
866 p_sd->psz_sessionname,
867 PLAYLIST_CHECK_INSERT, PLAYLIST_END );
868 p_item = playlist_ItemGetById( p_playlist, i_id );
871 vlc_mutex_lock( &p_item->lock );
872 playlist_ItemSetGroup( p_item, i_group );
873 vlc_mutex_unlock( &p_item->lock );
876 /* Then remember it */
877 p_announce = (struct sap_announce_t *)malloc(
878 sizeof(struct sap_announce_t) );
879 if( p_sd->psz_sessionname )
881 p_announce->psz_name = strdup( p_sd->psz_sessionname );
885 p_announce->psz_name = strdup( "" );
889 p_announce->psz_uri = strdup( psz_item_uri );
893 p_announce->psz_uri = strdup( "" );
895 p_announce->i_id = i_id;
897 p_announce->i_last = mdate();
899 vlc_object_release( p_playlist );
901 INSERT_ELEM( p_intf->p_sys->pp_announces,
902 p_intf->p_sys->i_announces,
903 p_intf->p_sys->i_announces,
905 free( psz_item_uri );
909 /***********************************************************************
910 * parse_sdp : SDP parsing
911 * *********************************************************************
912 * Make a sess_descr_t with a psz
913 ***********************************************************************/
915 static sess_descr_t * parse_sdp( intf_thread_t * p_intf, char *p_packet )
919 if( p_packet[0] != 'v' || p_packet[1] != '=' )
921 msg_Warn(p_intf, "bad SDP packet");
925 sd = malloc( sizeof( sess_descr_t ) );
926 sd->psz_sessionname = NULL;
927 sd->psz_connection = NULL;
928 sd->psz_sdp = strdup( p_packet );
932 sd->i_attributes = 0;
933 sd->pp_attributes = NULL;
935 while( *p_packet != '\0' )
939 /* Search begin of field */
940 while( *p_packet == '\n' || *p_packet == ' ' || *p_packet == '\t' )
944 /* search end of line */
945 if( ( psz_end = strchr( p_packet, '\n' ) ) == NULL )
947 psz_end = p_packet + strlen( p_packet );
949 if( psz_end > p_packet && *(psz_end - 1 ) == '\r' )
954 if( psz_end <= p_packet )
960 if( p_packet[1] != '=' )
962 msg_Warn( p_intf, "packet invalid" );
967 switch( p_packet[0] )
970 sd->i_version = atoi( &p_packet[2] );
973 sd->psz_sessionname = strdup( &p_packet[2] );
985 char *psz_eof = strchr( &p_packet[2], ':' );
987 if( psz_eof && psz_eof[1] != '\0' )
989 attr_descr_t *attr = malloc( sizeof( attr_descr_t ) );
993 attr->psz_field = strdup( &p_packet[2] );
994 attr->psz_value = strdup( psz_eof );
996 TAB_APPEND( sd->i_attributes, sd->pp_attributes, attr );
1003 media_descr_t *media = malloc( sizeof( media_descr_t ) );
1005 media->psz_medianame = strdup( &p_packet[2] );
1006 media->psz_mediaconnection = NULL;
1008 TAB_APPEND( sd->i_media, sd->pp_media, media );
1013 if( sd->i_media <= 0 )
1015 sd->psz_connection = strdup( &p_packet[2] );
1019 sd->pp_media[sd->i_media-1]->psz_mediaconnection = strdup( &p_packet[2] );
1034 if( p ) { free( p ); (p) = NULL; }
1035 static void free_sd( sess_descr_t * p_sd )
1039 FREE( p_sd->psz_sessionname );
1040 FREE( p_sd->psz_connection );
1041 FREE( p_sd->psz_sdp );
1043 for( i = 0; i < p_sd->i_media ; i++ )
1045 FREE( p_sd->pp_media[i]->psz_medianame );
1046 FREE( p_sd->pp_media[i]->psz_mediaconnection );
1047 FREE( p_sd->pp_media[i] );
1049 for( i = 0; i < p_sd->i_attributes ; i++ )
1051 FREE( p_sd->pp_attributes[i]->psz_field );
1052 FREE( p_sd->pp_attributes[i]->psz_value );
1053 FREE( p_sd->pp_attributes[i] );
1055 FREE( p_sd->pp_attributes );
1056 FREE( p_sd->pp_media );
1061 /***********************************************************************
1062 * ismult: returns true if we have a multicast address
1063 ***********************************************************************/
1065 static int ismult( char *psz_uri )
1070 i_value = strtol( psz_uri, &psz_end, 0 );
1073 if( psz_uri[0] == '[')
1075 if( strncasecmp( &psz_uri[1], "FF0" , 3) ||
1076 strncasecmp( &psz_uri[2], "FF0" , 3))
1079 return( VLC_FALSE );
1082 if( *psz_end != '.' ) { return( VLC_FALSE ); }
1084 return( i_value < 224 ? VLC_FALSE : VLC_TRUE );
1089 /*****************************************************************************
1090 * Read: read on a file descriptor, checking b_die periodically
1091 *****************************************************************************
1093 *****************************************************************************/
1094 static ssize_t NetRead( intf_thread_t *p_intf,
1095 int fd[2], uint8_t *p_buffer, int i_len )
1100 struct timeval timeout;
1103 int i_handle_max = __MAX( fd[0], fd[1] );
1105 /* Initialize file descriptor set */
1107 if( fd[0] > 0 ) FD_SET( fd[0], &fds );
1108 if( fd[1] > 0 ) FD_SET( fd[1], &fds );
1110 /* We'll wait 0.5 second if nothing happens */
1112 timeout.tv_usec = 500000;
1114 /* Find if some data is available */
1115 i_ret = select( i_handle_max + 1, &fds, NULL, NULL, &timeout );
1117 if( i_ret == -1 && errno != EINTR )
1119 msg_Err( p_intf, "network select error (%s)", strerror(errno) );
1121 else if( i_ret > 0 )
1123 if( fd[0] > 0 && FD_ISSET( fd[0], &fds ) )
1125 return recv( fd[0], p_buffer, i_len, 0 );
1127 else if( fd[1] > 0 && FD_ISSET( fd[1], &fds ) )
1129 return recv( fd[1], p_buffer, i_len, 0 );