1 /*****************************************************************************
2 * udp.c: raw UDP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.2 2002/08/07 00:29:36 sam 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>
35 #include <vlc/input.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("raw UDP access module") );
55 set_capability( "access", 0 );
56 add_shortcut( "udpstream" );
57 add_shortcut( "udp4" );
58 add_shortcut( "udp6" );
59 set_callbacks( Open, __input_FDNetworkClose );
62 /*****************************************************************************
63 * Open: open the socket
64 *****************************************************************************/
65 static int Open( vlc_object_t *p_this )
67 input_thread_t * p_input = (input_thread_t *)p_this;
68 input_socket_t * p_access_data;
70 char * psz_network = "";
71 char * psz_name = strdup(p_input->psz_name);
72 char * psz_parser = psz_name;
73 char * psz_server_addr = "";
74 char * psz_server_port = "";
75 char * psz_bind_addr = "";
76 char * psz_bind_port = "";
77 int i_bind_port = 0, i_server_port = 0;
78 network_socket_t socket_desc;
80 if( config_GetInt( p_input, "ipv4" ) )
84 if( config_GetInt( p_input, "ipv6" ) )
89 if( *p_input->psz_access )
91 /* Find out which shortcut was used */
92 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
96 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
102 /* Parse psz_name syntax :
103 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
105 if( *psz_parser && *psz_parser != '@' )
108 psz_server_addr = psz_parser;
110 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
112 if( *psz_parser == '[' )
115 while( *psz_parser && *psz_parser != ']' )
123 if( *psz_parser == ':' )
125 /* Found server port */
126 *psz_parser = '\0'; /* Terminate server name */
128 psz_server_port = psz_parser;
130 while( *psz_parser && *psz_parser != '@' )
137 if( *psz_parser == '@' )
139 /* Found bind address or bind port */
140 *psz_parser = '\0'; /* Terminate server port or name if necessary */
143 if( *psz_parser && *psz_parser != ':' )
145 /* Found bind address */
146 psz_bind_addr = psz_parser;
148 while( *psz_parser && *psz_parser != ':' )
150 if( *psz_parser == '[' )
153 while( *psz_parser && *psz_parser != ']' )
162 if( *psz_parser == ':' )
164 /* Found bind port */
165 *psz_parser = '\0'; /* Terminate bind address if necessary */
168 psz_bind_port = psz_parser;
172 /* Convert ports format */
173 if( *psz_server_port )
175 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
178 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
186 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
189 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
195 p_input->pf_read = input_FDNetworkRead;
196 p_input->pf_set_program = input_SetProgram;
197 p_input->pf_set_area = NULL;
198 p_input->pf_seek = NULL;
200 vlc_mutex_lock( &p_input->stream.stream_lock );
201 p_input->stream.b_pace_control = 0;
202 p_input->stream.b_seekable = 0;
203 p_input->stream.p_selected_area->i_tell = 0;
204 p_input->stream.i_method = INPUT_METHOD_NETWORK;
205 vlc_mutex_unlock( &p_input->stream.stream_lock );
207 if( *psz_server_addr || i_server_port )
209 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
210 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
211 psz_server_addr, i_server_port);
212 msg_Err( p_input, "or local port, type : %s:@%s:%d",
213 *p_input->psz_access ? p_input->psz_access : "udp",
214 psz_server_addr, i_server_port );
217 psz_server_addr = "";
220 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
221 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
223 /* Prepare the network_socket_t structure */
224 socket_desc.i_type = NETWORK_UDP;
225 socket_desc.psz_bind_addr = psz_bind_addr;
226 socket_desc.i_bind_port = i_bind_port;
227 socket_desc.psz_server_addr = psz_server_addr;
228 socket_desc.i_server_port = i_server_port;
230 /* Find an appropriate network module */
231 p_input->p_private = (void*) &socket_desc;
232 p_network = module_Need( p_input, "network", psz_network );
234 if( p_network == NULL )
238 module_Unneed( p_input, p_network );
240 p_access_data = malloc( sizeof(input_socket_t) );
241 p_input->p_access_data = (access_sys_t *)p_access_data;
243 if( p_access_data == NULL )
245 msg_Err( p_input, "out of memory" );
249 p_access_data->i_handle = socket_desc.i_handle;
250 p_input->i_mtu = socket_desc.i_mtu;