X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fstream_output%2Fsap.c;h=279821fbaca3c3243e601d514177ac93e1ece136;hb=3de41d49fdd787ce5114372b637dcacc73cdabad;hp=94facfa352828c8b5c76f0e76adf52119a8f3957;hpb=7ef7ca63118a201c34d2a40b2c5ad968909d6d2e;p=vlc diff --git a/src/stream_output/sap.c b/src/stream_output/sap.c index 94facfa352..279821fbac 100644 --- a/src/stream_output/sap.c +++ b/src/stream_output/sap.c @@ -1,10 +1,11 @@ /***************************************************************************** * sap.c : SAP announce handler ***************************************************************************** - * Copyright (C) 2002-2004 VideoLAN + * Copyright (C) 2002-2005 the VideoLAN team * $Id$ * - * Authors: Clément Stenac + * Authors: Clément Stenac + * Rémi Denis-Courmont * * 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 @@ -27,24 +28,56 @@ #include /* free() */ #include /* sprintf() */ #include /* strerror() */ +#include /* tolower(), isxdigit() */ #include #include -#include +#include "network.h" +#if defined( WIN32 ) || defined( UNDER_CE ) +# if defined(UNDER_CE) && defined(sockaddr_storage) +# undef sockaddr_storage +# endif +# include +# include +#else +# include +#endif +#include "charset.h" -#define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */ +/* SAP is always on that port */ #define SAP_PORT 9875 -#define SAP_IPV6_ADDR_1 "FF0" -#define SAP_IPV6_ADDR_2 "::2:7FFE" - -#define DEFAULT_IPV6_SCOPE '8' - #define DEFAULT_PORT "1234" #undef EXTRA_DEBUG +/* SAP Specific structures */ + +/* 100ms */ +#define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ)) +#define SAP_MAX_BUFFER 65534 +#define MIN_INTERVAL 2 +#define MAX_INTERVAL 300 + +/* A SAP announce address. For each of these, we run the + * control flow algorithm */ +struct sap_address_t +{ + char *psz_address; + int i_port; + int i_rfd; /* Read socket */ + int i_wfd; /* Write socket */ + + /* Used for flow control */ + mtime_t t1; + vlc_bool_t b_enabled; + vlc_bool_t b_ready; + int i_interval; + int i_buff; + int i_limit; +}; + /***************************************************************************** * Local prototypes *****************************************************************************/ @@ -62,6 +95,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, static int announce_SAPAnnounceDel( sap_handler_t *p_sap, session_descriptor_t *p_session ); +static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local ); #define FREE( p ) if( p ) { free( p ); (p) = NULL; } @@ -75,6 +109,7 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap, sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce ) { sap_handler_t *p_sap; + char *psz_charset; p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) ); @@ -86,6 +121,14 @@ sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce ) vlc_mutex_init( p_sap, &p_sap->object_lock ); + vlc_current_charset( &psz_charset ); + p_sap->iconvHandle = vlc_iconv_open( "UTF-8", psz_charset ); + free( psz_charset ); + if( p_sap->iconvHandle == (vlc_iconv_t)(-1) ) + { + msg_Warn( p_sap, "Unable to do requested conversion" ); + } + p_sap->pf_add = announce_SAPAnnounceAdd; p_sap->pf_del = announce_SAPAnnounceDel; @@ -144,6 +187,9 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap ) FREE( p_address ); } + if( p_sap->iconvHandle != (vlc_iconv_t)(-1) ) + vlc_iconv_close( p_sap->iconvHandle ); + /* Free the structure */ vlc_object_destroy( p_sap ); } @@ -213,8 +259,9 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, char *psz_type = "application/sdp"; int i_header_size; char *psz_head; - vlc_bool_t b_found = VLC_FALSE; + vlc_bool_t b_found = VLC_FALSE, b_ipv6 = VLC_FALSE; sap_session_t *p_sap_session; + mtime_t i_hash; vlc_mutex_lock( &p_sap->object_lock ); @@ -228,31 +275,86 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, } } - if( !p_method->psz_address ) + if( p_method->psz_address == NULL ) { - if( p_method->i_ip_version == 6 ) + /* Determine SAP multicast address automatically */ + char psz_buf[NI_MAXNUMERICHOST], *ptr; + const char *psz_addr; + struct addrinfo hints, *res; + + if( p_session->psz_uri == NULL ) + { + msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" ); + msg_Err( p_sap, "This should not happen. VLC needs fixing." ); + vlc_mutex_unlock( &p_sap->object_lock ); + return VLC_EGENERIC; + } + + /* Canonicalize IP address (e.g. 224.00.010.1 => 224.0.10.1) */ + memset( &hints, 0, sizeof( hints ) ); + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = AI_NUMERICHOST; + + i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0, + &hints, &res ); + if( i == 0 ) + i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf, + sizeof( psz_buf ), NULL, NI_NUMERICHOST ); + if( i ) + { + msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s", + p_session->psz_uri, vlc_gai_strerror( i ) ); + vlc_mutex_unlock( &p_sap->object_lock ); + return VLC_EGENERIC; + } + + /* Remove interface specification if present */ + ptr = strchr( psz_buf, '%' ); + if( ptr != NULL ) + *ptr = '\0'; + + if( strchr( psz_buf, ':' ) != NULL ) { - char sz_scope; - if( p_method->psz_ipv6_scope != NULL ) + b_ipv6 = VLC_TRUE; + + /* See RFC3513 for list of valid IPv6 scopes */ + if( ( tolower( psz_buf[0] ) == 'f' ) + && ( tolower( psz_buf[1] ) == 'f' ) + && isxdigit( psz_buf[2] ) && isxdigit( psz_buf[3] ) ) { - sz_scope = *p_method->psz_ipv6_scope; + /* Multicast IPv6 */ + psz_buf[2] = '0'; /* force flags to zero */ + /* keep scope in psz_addr[3] */ + memcpy( &psz_buf[4], "::2:7ffe", sizeof( "::2:7ffe" ) ); + psz_addr = psz_buf; } else - { - sz_scope = DEFAULT_IPV6_SCOPE; - } - p_method->psz_address = (char*)malloc( 30*sizeof(char )); - sprintf( p_method->psz_address, "%s%c%s", - SAP_IPV6_ADDR_1, sz_scope, SAP_IPV6_ADDR_2 ); + /* Unicast IPv6 - assume global scope */ + psz_addr = "ff0e::2:7ffe"; } else { - /* IPv4 */ - p_method->psz_address = (char*)malloc( 15*sizeof(char) ); - sprintf(p_method->psz_address, SAP_IPV4_ADDR ); + /* See RFC2365 for IPv4 scopes */ + if( memcmp( psz_buf, "224.0.0.", 8 ) == 0 ) + psz_addr = "224.0.0.255"; + else + if( memcmp( psz_buf, "239.255.", 8 ) == 0 ) + psz_addr = "239.255.255.255"; + else + if( ( memcmp( psz_buf, "239.19", 6 ) == 0 ) + && ( ( psz_buf[6] >= '2' ) && ( psz_buf[6] <= '5' ) ) ) + psz_addr = "239.195.255.255"; + else + /* assume global scope */ + psz_addr = "224.2.127.254"; } + + p_method->psz_address = strdup( psz_addr ); } - msg_Dbg( p_sap, "using SAP address: %s",p_method->psz_address); + else + b_ipv6 == (strchr( p_method->psz_address, ':' ) != NULL); + + msg_Dbg( p_sap, "using SAP address: %s", p_method->psz_address); /* XXX: Check for dupes */ p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t)); @@ -285,12 +387,16 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, p_address->i_wfd = net_OpenUDP( p_sap, "", 0, p_address->psz_address, p_address->i_port ); + if( p_address->i_wfd != -1 ) + net_StopRecv( p_address->i_wfd ); if( p_sap->b_control == VLC_TRUE ) { p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address, p_address->i_port, "", 0 ); + if( p_address->i_rfd != -1 ) + net_StopSend( p_address->i_rfd ); p_address->i_buff = 0; p_address->b_enabled = VLC_TRUE; p_address->b_ready = VLC_FALSE; @@ -302,6 +408,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, p_address->b_enabled = VLC_TRUE; p_address->b_ready = VLC_TRUE; p_address->i_interval = config_GetInt( p_sap,"sap-interval"); + p_address->i_rfd = -1; } if( p_address->i_wfd == -1 || (p_address->i_rfd == -1 @@ -319,7 +426,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, } /* Build the SAP Headers */ - i_header_size = 8 + strlen( psz_type ) + 1; + i_header_size = ( b_ipv6 ? 20 : 8 ) + strlen( psz_type ) + 1; psz_head = (char *) malloc( i_header_size * sizeof( char ) ); if( ! psz_head ) { @@ -327,28 +434,65 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap, return VLC_ENOMEM; } - psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */ - psz_head[1] = 0x00; /* No authentification */ - psz_head[2] = 0x42; /* Msg id hash */ - psz_head[3] = 0x12; /* Msg id hash 2 */ + psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */ + psz_head[1] = 0x00; /* No authentification length */ - psz_head[4] = 0x01; /* Source IP FIXME: we should get the real address */ - psz_head[5] = 0x02; /* idem */ - psz_head[6] = 0x03; /* idem */ - psz_head[7] = 0x04; /* idem */ + i_hash = mdate(); + psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */ + psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */ + + if( b_ipv6 ) + { + /* in_addr_t ip_server = inet_addr( ip ); */ + psz_head[0] |= 0x10; /* Set IPv6 */ + + psz_head[4] = 0x01; /* Source IP FIXME: we should get the real address */ + psz_head[5] = 0x02; /* idem */ + psz_head[6] = 0x03; /* idem */ + psz_head[7] = 0x04; /* idem */ + + psz_head[8] = 0x01; /* Source IP FIXME: we should get the real address */ + psz_head[9] = 0x02; /* idem */ + psz_head[10] = 0x03; /* idem */ + psz_head[11] = 0x04; /* idem */ + + psz_head[12] = 0x01; /* Source IP FIXME: we should get the real address */ + psz_head[13] = 0x02; /* idem */ + psz_head[14] = 0x03; /* idem */ + psz_head[15] = 0x04; /* idem */ + + psz_head[16] = 0x01; /* Source IP FIXME: we should get the real address */ + psz_head[17] = 0x02; /* idem */ + psz_head[18] = 0x03; /* idem */ + psz_head[19] = 0x04; /* idem */ + + strncpy( psz_head + 20, psz_type, 15 ); + } + else + { + /* in_addr_t ip_server = inet_addr( ip) */ + /* Source IP FIXME: we should get the real address */ + psz_head[4] = 0x01; /* ip_server */ + psz_head[5] = 0x02; /* ip_server>>8 */ + psz_head[6] = 0x03; /* ip_server>>16 */ + psz_head[7] = 0x04; /* ip_server>>24 */ + + strncpy( psz_head + 8, psz_type, 15 ); + } - strncpy( psz_head + 8, psz_type, 15 ); psz_head[ i_header_size-1 ] = '\0'; p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp); - p_sap_session->psz_data = (char *)malloc( sizeof(char)* - p_sap_session->i_length ); + p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)* + p_sap_session->i_length ); /* Build the final message */ memcpy( p_sap_session->psz_data, psz_head, i_header_size ); memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp, strlen( p_sap_session->psz_sdp) ); + free( psz_head ); + /* Enqueue the announce */ INSERT_ELEM( p_sap->pp_sessions, p_sap->i_sessions, @@ -382,12 +526,17 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap, REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions, i ); + + FREE( p_session->p_sap->psz_sdp ); + FREE( p_session->p_sap->psz_data ); + free( p_session->p_sap ); break; } } - /* XXX: Dequeue the adress too if it is not used anymore - * TODO: address refcount */ + /* XXX: Dequeue the address too if it is not used anymore + * TODO: - address refcount + - send a SAP deletion packet */ msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions ); @@ -414,7 +563,7 @@ static int announce_SendSAPAnnounce( sap_handler_t *p_sap, #ifdef EXTRA_DEBUG msg_Dbg( p_sap, "Sending announce"); #endif - i_ret = net_Write( p_sap, p_session->p_address->i_wfd, + i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL, p_session->psz_data, p_session->i_length ); if( i_ret != p_session->i_length ) @@ -438,6 +587,31 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session ) { int64_t i_sdp_id = mdate(); int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff); + char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri; + char ipv; + + psz_group = convert_to_utf8( p_sap, p_session->psz_group ); + psz_name = convert_to_utf8( p_sap, p_session->psz_name ); + + /* FIXME: really check that psz_uri is a real IP address + * FIXME: make a common function to obtain a canonical IP address */ + ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4'; + if( *p_session->psz_uri == '[' ) + { + char *ptr; + + strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) ); + psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0'; + ptr = strchr( psz_uribuf, '%' ); + if( ptr != NULL) + *ptr = '\0'; + ptr = strchr( psz_uribuf, ']' ); + if( ptr != NULL) + *ptr = '\0'; + psz_uri = psz_uribuf; + } + else + psz_uri = p_session->psz_uri; /* see the lists in modules/stream_out/rtp.c for compliance stuff */ p_session->psz_sdp = (char *)malloc( @@ -449,26 +623,40 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session ) "m=video udp\r\n" "a=tool:"PACKAGE_STRING"\r\n" "a=type:broadcast\r\n") - + strlen( p_session->psz_name ) - + strlen( p_session->psz_uri ) + 300 ); - if( !p_session->psz_sdp ) + + strlen( psz_name ) + + strlen( psz_uri ) + 300 + + ( psz_group ? strlen( psz_group ) : 0 ) ); + + if( p_session->psz_sdp == NULL || psz_name == NULL ) { msg_Err( p_sap, "out of memory" ); + FREE( psz_name ); + FREE( psz_group ); return VLC_ENOMEM; } + sprintf( p_session->psz_sdp, "v=0\r\n" "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n" "s=%s\r\n" "t=0 0\r\n" - "c=IN IP4 %s/%d\r\n" + "c=IN IP%c %s/%d\r\n" "m=video %d udp %d\r\n" "a=tool:"PACKAGE_STRING"\r\n" "a=type:broadcast\r\n", i_sdp_id, i_sdp_version, - p_session->psz_name, - p_session->psz_uri, p_session->i_ttl, + psz_name, ipv, + psz_uri, p_session->i_ttl, p_session->i_port, p_session->i_payload ); + free( psz_name ); + + if( psz_group ) + { + sprintf( p_session->psz_sdp, "%sa=x-plgroup:%s\r\n", + p_session->psz_sdp, psz_group ); + free( psz_group ); + } + msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp), p_session->psz_sdp ); return VLC_SUCCESS; @@ -477,7 +665,7 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session ) static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ) { int i_read; - char buffer[SAP_MAX_BUFFER]; + uint8_t buffer[SAP_MAX_BUFFER]; int i_tot = 0; mtime_t i_temp; int i_rate; @@ -490,7 +678,7 @@ static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ) do { /* Might be too slow if we have huge data */ - i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, buffer, + i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer, SAP_MAX_BUFFER, 0 ); i_tot += i_read; } while( i_read > 0 && i_tot < SAP_MAX_BUFFER ); @@ -531,3 +719,34 @@ static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ) return VLC_SUCCESS; } + + +static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local ) +{ + char *psz_unicode, *psz_in, *psz_out; + size_t ret, i_in, i_out; + + if( psz_local == NULL ) + return NULL; + if ( p_this->iconvHandle == (vlc_iconv_t)(-1) ) + return strdup( psz_local ); + + psz_in = psz_local; + i_in = strlen( psz_local ); + + i_out = 6 * i_in; + psz_unicode = malloc( i_out + 1 ); + if( psz_unicode == NULL ) + return strdup( psz_local ); + psz_out = psz_unicode; + + ret = vlc_iconv( p_this->iconvHandle, + &psz_in, &i_in, &psz_out, &i_out); + if( ret == (size_t)(-1) || i_in ) + { + msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local ); + return strdup( psz_local ); + } + *psz_out = '\0'; + return psz_unicode; +}