📄 sap.c
字号:
/***************************************************************************** * sap.c : SAP interface module ***************************************************************************** * Copyright (C) 2004-2005 VideoLAN * $Id: sap.c 11360 2005-06-08 16:57:11Z courmisch $ * * Authors: Cl閙ent Stenac <zorglub@videolan.org> * * 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. * * 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 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//***************************************************************************** * Includes *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/intf.h>#include <vlc/input.h>#include "network.h"#include "charset.h"#include <ctype.h>#include <errno.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef HAVE_SYS_TIME_H# include <sys/time.h>#endif#ifdef HAVE_ZLIB_H# include <zlib.h>#endif/************************************************************************ * Macros and definitions ************************************************************************/#define MAX_LINE_LENGTH 256/* SAP is always on that port */#define SAP_PORT 9875/* 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"#define ADD_SESSION 1#define IPV6_ADDR_1 "FF0" /* Scope is inserted between them */#define IPV6_ADDR_2 "::2:7FFE"/***************************************************************************** * Module descriptor *****************************************************************************/#define SAP_ADDR_TEXT N_( "SAP multicast address" )#define SAP_ADDR_LONGTEXT N_( "Listen for SAP announces on another address" )#define SAP_IPV4_TEXT N_( "IPv4-SAP listening" )#define SAP_IPV4_LONGTEXT N_( \ "Set this if you want the SAP module to listen to IPv4 announces " \ "on the standard address." )#define SAP_IPV6_TEXT N_( "IPv6-SAP listening" )#define SAP_IPV6_LONGTEXT N_( \ "Set this if you want the SAP module to listen to IPv6 announces " \ "on the standard address." )#define SAP_SCOPE_TEXT N_( "IPv6 SAP scope" )#define SAP_SCOPE_LONGTEXT N_( \ "Sets the scope for IPv6 announces (default is 8)." )#define SAP_TIMEOUT_TEXT N_( "SAP timeout (seconds)" )#define SAP_TIMEOUT_LONGTEXT N_( \ "Sets the time before SAP items get deleted if no new announce " \ "is received." )#define SAP_PARSE_TEXT N_( "Try to parse the SAP" )#define SAP_PARSE_LONGTEXT N_( \ "When SAP can it will try to parse the SAP. If you don't select " \ "this, all announces will be parsed by the livedotcom module." )#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 " \ "announces." )#define SAP_CACHE_TEXT N_("Use SAP cache")#define SAP_CACHE_LONGTEXT N_( \ "If this option is selected, a SAP caching mechanism will be used. " \ "This will result in lower SAP startup time, but you could end up " \ "with items corresponding to legacy streams." )/* 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 * );vlc_module_begin(); set_shortname( _("SAP")); set_description( _("SAP announces") ); set_category( CAT_PLAYLIST ); set_subcategory( SUBCAT_PLAYLIST_SD ); add_string( "sap-addr", NULL, NULL, SAP_ADDR_TEXT, SAP_ADDR_LONGTEXT, VLC_TRUE ); add_bool( "sap-ipv4", 1 , NULL, SAP_IPV4_TEXT,SAP_IPV4_LONGTEXT, VLC_TRUE ); add_bool( "sap-ipv6", 1 , NULL, SAP_IPV6_TEXT, SAP_IPV6_LONGTEXT, VLC_TRUE ); add_string( "sap-ipv6-scope", "8" , NULL, SAP_SCOPE_TEXT, SAP_SCOPE_LONGTEXT, VLC_TRUE); add_integer( "sap-timeout", 1800, NULL, SAP_TIMEOUT_TEXT, SAP_TIMEOUT_LONGTEXT, VLC_TRUE ); add_bool( "sap-parse", 1 , NULL, SAP_PARSE_TEXT,SAP_PARSE_LONGTEXT, VLC_TRUE ); add_bool( "sap-strict", 0 , NULL, SAP_STRICT_TEXT,SAP_STRICT_LONGTEXT, VLC_TRUE ); add_bool( "sap-cache", 0 , NULL, SAP_CACHE_TEXT,SAP_CACHE_LONGTEXT, VLC_TRUE ); set_capability( "services_discovery", 0 ); set_callbacks( Open, Close ); add_submodule(); set_description( _("SDP file parser for UDP") ); add_shortcut( "sdp" ); set_capability( "demux2", 51 ); set_callbacks( OpenDemux, CloseDemux );vlc_module_end();/***************************************************************************** * Local structures *****************************************************************************/typedef struct sdp_t sdp_t;typedef struct attribute_t attribute_t;typedef struct sap_announce_t sap_announce_t;/* The structure that contains sdp information */struct sdp_t{ char *psz_sdp; /* s= field */ char *psz_sessionname; /* Raw m= and c= fields */ char *psz_connection; char *psz_media; /* o field */ char *psz_username; char *psz_network_type; char *psz_address_type; char *psz_address; int64_t i_session_id; /* "computed" URI */ char *psz_uri; int i_in; /* IP version */ int i_media; int i_media_type; int i_attributes; attribute_t **pp_attributes;};struct attribute_t{ char *psz_field; char *psz_value;};struct sap_announce_t{ mtime_t i_last; uint16_t i_hash; uint32_t i_source[4]; /* SAP annnounces must only contain one SDP */ sdp_t *p_sdp; int i_item_id;// playlist_item_t *p_item;};struct services_discovery_sys_t{ /* Socket descriptors */ int i_fd; int *pi_fd; /* playlist node */ playlist_item_t *p_node; playlist_t *p_playlist; /* charset conversion */ vlc_iconv_t iconvHandle; /* Table of announces */ int i_announces; struct sap_announce_t **pp_announces; /* Modes */ vlc_bool_t b_strict; vlc_bool_t b_parse; int i_timeout;};struct demux_sys_t{ sdp_t *p_sdp;};/***************************************************************************** * Local prototypes *****************************************************************************//* Main functions */ static int Demux( demux_t *p_demux ); static int Control( demux_t *, int, va_list ); static void Run ( services_discovery_t *p_sd );/* Main parsing functions */ static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp ); static int ParseSAP( services_discovery_t *p_sd, uint8_t *p_buffer, int i_read ); static sdp_t * ParseSDP( vlc_object_t *p_sd, char* psz_sdp ); 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 );/* Cache */ static void CacheLoad( services_discovery_t *p_sd ); static void CacheSave( services_discovery_t *p_sd );/* Helper functions */ static char *GetAttribute( sdp_t *p_sdp, const char *psz_search ); static vlc_bool_t IsSameSession( sdp_t *p_sdp1, sdp_t *p_sdp2 ); static char *convert_from_utf8( struct services_discovery_t *p_sd, char *psz_unicode ); static int InitSocket( services_discovery_t *p_sd, char *psz_address, int i_port );#ifdef HAVE_ZLIB_H static int Decompress( unsigned char *psz_src, unsigned char **_dst, int i_len );#endif static void FreeSDP( sdp_t *p_sdp );/* Detect multicast addresses */static vlc_bool_t ismult( char * );#define FREE( p ) \ if( p ) { free( p ); (p) = NULL; }/***************************************************************************** * Open: initialize and create stuff *****************************************************************************/static int Open( vlc_object_t *p_this ){ services_discovery_t *p_sd = ( services_discovery_t* )p_this; services_discovery_sys_t *p_sys = (services_discovery_sys_t *) malloc( sizeof( services_discovery_sys_t ) ); playlist_view_t *p_view; char *psz_addr, *psz_charset; vlc_value_t val; p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" ); vlc_current_charset( &psz_charset ); p_sys->iconvHandle = vlc_iconv_open( psz_charset, "UTF-8" ); free( psz_charset ); if( p_sys->iconvHandle == (vlc_iconv_t)(-1) ) { msg_Err( p_sd, "unable to perform characters conversion" ); return VLC_EGENERIC; } p_sd->pf_run = Run; p_sd->p_sys = p_sys; p_sys->pi_fd = NULL; p_sys->i_fd = 0; p_sys->b_strict = var_CreateGetInteger( p_sd, "sap-strict"); p_sys->b_parse = var_CreateGetInteger( p_sd, "sap-parse" ); if( var_CreateGetInteger( p_sd, "sap-cache" ) ) { CacheLoad( p_sd ); } 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" ) ) { /* [ + 8x4+7*':' + ] */ char psz_address[42]; char c_scope; char *psz_scope = var_CreateGetString( p_sd, "sap-ipv6-scope" ); if( psz_scope == NULL || *psz_scope == '\0') { c_scope = '8'; } else { c_scope = psz_scope[0]; } snprintf( psz_address, 42, "[%s%c%s]", IPV6_ADDR_1, c_scope, IPV6_ADDR_2 ); InitSocket( p_sd, psz_address, SAP_PORT ); } psz_addr = var_CreateGetString( p_sd, "sap-addr" ); if( psz_addr && *psz_addr ) { InitSocket( p_sd, psz_addr, SAP_PORT ); } if( p_sys->i_fd == 0 ) { msg_Err( p_sd, "unable to read on any address" ); return VLC_EGENERIC; } /* Create our playlist node */ p_sys->p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE ); if( !p_sys->p_playlist ) { msg_Warn( p_sd, "unable to find playlist, cancelling SAP listening"); return VLC_EGENERIC; } p_view = playlist_ViewFind( p_sys->p_playlist, VIEW_CATEGORY ); p_sys->p_node = playlist_NodeCreate( p_sys->p_playlist, VIEW_CATEGORY, _("SAP"), p_view->p_root ); p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG; p_sys->p_node->i_flags =~ PLAYLIST_SKIP_FLAG; val.b_bool = VLC_TRUE; var_Set( p_sys->p_playlist, "intf-change", val ); p_sys->i_announces = 0; p_sys->pp_announces = NULL; 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; int i_max_sdp = 1024; int i_sdp = 0; char *psz_sdp = NULL; sdp_t *p_sdp = NULL; /* Probe for SDP */ if( p_demux->s ) { if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC; if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) && strncmp( (char*)p_peek, "v=0\n", 4 ) && ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) ) { return VLC_EGENERIC; } } psz_sdp = (char *)malloc( i_max_sdp ); if( !psz_sdp ) return VLC_EGENERIC; /* Gather the complete sdp file */ for( ;; ) { int i_read = stream_Read( p_demux->s, &psz_sdp[i_sdp], i_max_sdp - i_sdp - 1 ); if( i_read < 0 ) { msg_Err( p_demux, "failed to read SDP" ); goto error; } i_sdp += i_read; if( i_read < i_max_sdp - i_sdp - 1 ) { psz_sdp[i_sdp] = '\0'; break; } i_max_sdp += 1000; psz_sdp = (uint8_t*)realloc( psz_sdp, i_max_sdp ); } p_sdp = ParseSDP( VLC_OBJECT(p_demux), psz_sdp ); if( !p_sdp ) { msg_Warn( p_demux, "invalid SDP"); goto error; } if( p_sdp->i_media > 1 ) { goto error; } if( ParseConnection( VLC_OBJECT( p_demux ), p_sdp ) ) { p_sdp->psz_uri = NULL; } if( p_sdp->i_media_type != 33 && p_sdp->i_media_type != 32 && p_sdp->i_media_type != 14 ) goto error; 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; p_demux->pf_control = Control; p_demux->pf_demux = Demux; free( psz_sdp ); return VLC_SUCCESS;error: free( psz_sdp ); if( p_sdp ) FreeSDP( p_sdp ); stream_Seek( p_demux->s, 0 ); return VLC_EGENERIC; }/***************************************************************************** * Close: *****************************************************************************/static void Close( vlc_object_t *p_this ){ services_discovery_t *p_sd = ( services_discovery_t* )p_this; services_discovery_sys_t *p_sys = p_sd->p_sys; int i; for( i = p_sys->i_fd-1 ; i >= 0 ; i-- ) { net_Close( p_sys->pi_fd[i] ); } FREE( p_sys->pi_fd ); if( config_GetInt( p_sd, "sap-cache" ) ) { CacheSave( p_sd ); } for( i = p_sys->i_announces - 1; i>= 0; i-- ) { RemoveAnnounce( p_sd, p_sys->pp_announces[i] ); } FREE( p_sys->pp_announces ); if( p_sys->p_playlist ) { playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE ); vlc_object_release( p_sys->p_playlist ); } vlc_iconv_close( p_sys->iconvHandle ); free( p_sys );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -