1 /*****************************************************************************
2 * udp.c: raw UDP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.2 2002/03/04 23:56:37 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <sys/types.h>
34 #include <videolan/vlc.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
44 #include "input_ext-dec.h"
45 #include "input_ext-plugins.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 static void input_getfunctions( function_list_t * );
53 static int UDPOpen ( struct input_thread_s * );
54 static int UDPSetProgram ( struct input_thread_s * , pgrm_descriptor_t * );
56 /*****************************************************************************
57 * Build configuration tree.
58 *****************************************************************************/
63 SET_DESCRIPTION( "Raw UDP access plug-in" )
64 ADD_CAPABILITY( ACCESS, 0 )
66 ADD_SHORTCUT( "udpstream" )
67 ADD_SHORTCUT( "udp4" )
68 ADD_SHORTCUT( "udp6" )
72 input_getfunctions( &p_module->p_functions->access );
75 MODULE_DEACTIVATE_START
76 MODULE_DEACTIVATE_STOP
78 /*****************************************************************************
79 * Functions exported as capabilities. They are declared as static so that
80 * we don't pollute the namespace too much.
81 *****************************************************************************/
82 static void input_getfunctions( function_list_t * p_function_list )
84 #define input p_function_list->functions.access
85 input.pf_open = UDPOpen;
86 input.pf_read = input_FDNetworkRead;
87 input.pf_close = input_FDClose;
88 input.pf_set_program = UDPSetProgram;
89 input.pf_set_area = NULL;
94 /*****************************************************************************
95 * UDPOpen: open the socket
96 *****************************************************************************/
97 static int UDPOpen( input_thread_t * p_input )
99 input_socket_t * p_access_data;
100 struct module_s * p_network;
101 char * psz_network = NULL;
102 char * psz_parser = p_input->psz_name;
103 char * psz_server_addr = NULL;
104 char * psz_server_port = NULL;
105 char * psz_bind_addr = NULL;
106 char * psz_bind_port = NULL;
107 int i_bind_port = 0, i_server_port = 0;
108 network_socket_t socket_desc;
110 if( config_GetIntVariable( INPUT_IPV4_VAR ) )
112 psz_network = "ipv4";
114 if( config_GetIntVariable( INPUT_IPV6_VAR ) )
116 psz_network = "ipv6";
119 if( p_input->psz_access != NULL )
121 /* Find out which shortcut was used */
122 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
124 psz_network = "ipv6";
126 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
128 psz_network = "ipv4";
132 /* Parse psz_name syntax :
133 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
135 if( *psz_parser && *psz_parser != '@' )
138 psz_server_addr = psz_parser;
140 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
142 if( *psz_parser == '[' )
145 while( *psz_parser && *psz_parser != ']' )
153 if( *psz_parser == ':' )
155 /* Found server port */
156 *psz_parser = '\0'; /* Terminate server name */
158 psz_server_port = psz_parser;
160 while( *psz_parser && *psz_parser != '@' )
167 if( *psz_parser == '@' )
169 /* Found bind address or bind port */
170 *psz_parser = '\0'; /* Terminate server port or name if necessary */
173 if( *psz_parser && *psz_parser != ':' )
175 /* Found bind address */
176 psz_bind_addr = psz_parser;
178 while( *psz_parser && *psz_parser != ':' )
180 if( *psz_parser == '[' )
183 while( *psz_parser && *psz_parser != ']' )
192 if( *psz_parser == ':' )
194 /* Found bind port */
195 *psz_parser = '\0'; /* Terminate bind address if necessary */
198 psz_bind_port = psz_parser;
202 /* Convert ports format */
203 if( psz_server_port != NULL )
205 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
208 intf_ErrMsg( "input error: cannot parse server port near %s",
214 if( psz_bind_port != NULL )
216 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
219 intf_ErrMsg( "input error: cannot parse bind port near %s",
225 vlc_mutex_lock( &p_input->stream.stream_lock );
226 p_input->stream.b_pace_control = 0;
227 p_input->stream.b_seekable = 0;
228 p_input->stream.p_selected_area->i_tell = 0;
229 p_input->stream.i_method = INPUT_METHOD_NETWORK;
230 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 intf_WarnMsg( 2, "input: opening server=%s:%d local=%s:%d",
233 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
235 /* Prepare the network_socket_t structure */
236 socket_desc.i_type = NETWORK_UDP;
237 socket_desc.psz_bind_addr = psz_bind_addr;
238 socket_desc.i_bind_port = i_bind_port;
239 socket_desc.psz_server_addr = psz_server_addr;
240 socket_desc.i_server_port = i_server_port;
242 /* Find an appropriate network module */
243 p_network = module_Need( MODULE_CAPABILITY_NETWORK, psz_network,
245 if( p_network == NULL )
249 module_Unneed( p_network );
252 p_access_data = p_input->p_access_data = malloc( sizeof(input_socket_t) );
253 if( p_access_data == NULL )
255 intf_ErrMsg( "input error: Out of memory" );
259 p_access_data->i_handle = socket_desc.i_handle;
260 p_input->i_mtu = socket_desc.i_mtu;
265 /*****************************************************************************
266 * UDPSetProgram: Do nothing
267 *****************************************************************************/
268 static int UDPSetProgram( input_thread_t * p_input,
269 pgrm_descriptor_t * p_program )