]> git.sesse.net Git - vlc/blob - plugins/access/udp.c
ea872ab47d5fa206d63565c078fbf25ef76738b5
[vlc] / plugins / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.10 2002/05/15 13:07:18 marcari Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include <videolan/vlc.h>
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
39 #   include <io.h>
40 #endif
41
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
44 #include "input_ext-dec.h"
45 #include "input_ext-plugins.h"
46
47 #include "network.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static void input_getfunctions( function_list_t * );
53 static int  UDPOpen       ( struct input_thread_s * );
54
55 /*****************************************************************************
56  * Build configuration tree.
57  *****************************************************************************/
58 MODULE_CONFIG_START
59 MODULE_CONFIG_STOP
60  
61 MODULE_INIT_START
62     SET_DESCRIPTION( _("Raw UDP access plug-in") )
63     ADD_CAPABILITY( ACCESS, 0 )
64     ADD_SHORTCUT( "udp" )
65     ADD_SHORTCUT( "udpstream" )
66     ADD_SHORTCUT( "udp4" )
67     ADD_SHORTCUT( "udp6" )
68 MODULE_INIT_STOP
69  
70 MODULE_ACTIVATE_START
71     input_getfunctions( &p_module->p_functions->access );
72 MODULE_ACTIVATE_STOP
73  
74 MODULE_DEACTIVATE_START
75 MODULE_DEACTIVATE_STOP
76
77 /*****************************************************************************
78  * Functions exported as capabilities. They are declared as static so that
79  * we don't pollute the namespace too much.
80  *****************************************************************************/
81 static void input_getfunctions( function_list_t * p_function_list )
82 {
83 #define input p_function_list->functions.access
84     input.pf_open             = UDPOpen;
85     input.pf_read             = input_FDNetworkRead;
86     input.pf_close            = input_FDNetworkClose;
87     input.pf_set_program      = input_SetProgram;
88     input.pf_set_area         = NULL;
89     input.pf_seek             = NULL;
90 #undef input
91 }
92
93 /*****************************************************************************
94  * UDPOpen: open the socket
95  *****************************************************************************/
96 static int UDPOpen( input_thread_t * p_input )
97 {
98     input_socket_t *    p_access_data;
99     struct module_s *   p_network;
100     char *              psz_network = "";
101     char *              psz_name = strdup(p_input->psz_name);
102     char *              psz_parser = psz_name;
103     char *              psz_server_addr = "";
104     char *              psz_server_port = "";
105     char *              psz_bind_addr = "";
106     char *              psz_bind_port = "";
107     int                 i_bind_port = 0, i_server_port = 0;
108     network_socket_t    socket_desc;
109
110     if( config_GetIntVariable( "ipv4" ) )
111     {
112         psz_network = "ipv4";
113     }
114     if( config_GetIntVariable( "ipv6" ) )
115     {
116         psz_network = "ipv6";
117     }
118
119     if( *p_input->psz_access )
120     {
121         /* Find out which shortcut was used */
122         if( !strncmp( p_input->psz_access, "udp6", 5 ) )
123         {
124             psz_network = "ipv6";
125         }
126         else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
127         {
128             psz_network = "ipv4";
129         }
130     }
131
132     /* Parse psz_name syntax :
133      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
134
135     if( *psz_parser && *psz_parser != '@' )
136     {
137         /* Found server */
138         psz_server_addr = psz_parser;
139
140         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
141         {
142             if( *psz_parser == '[' )
143             {
144                 /* IPv6 address */
145                 while( *psz_parser && *psz_parser != ']' )
146                 {
147                     psz_parser++;
148                 }
149             }
150             psz_parser++;
151         }
152
153         if( *psz_parser == ':' )
154         {
155             /* Found server port */
156             *psz_parser = '\0'; /* Terminate server name */
157             psz_parser++;
158             psz_server_port = psz_parser;
159
160             while( *psz_parser && *psz_parser != '@' )
161             {
162                 psz_parser++;
163             }
164         }
165     }
166
167     if( *psz_parser == '@' )
168     {
169         /* Found bind address or bind port */
170         *psz_parser = '\0'; /* Terminate server port or name if necessary */
171         psz_parser++;
172
173         if( *psz_parser && *psz_parser != ':' )
174         {
175             /* Found bind address */
176             psz_bind_addr = psz_parser;
177
178             while( *psz_parser && *psz_parser != ':' )
179             {
180                 if( *psz_parser == '[' )
181                 {
182                     /* IPv6 address */
183                     while( *psz_parser && *psz_parser != ']' )
184                     {
185                         psz_parser++;
186                     }
187                 }
188                 psz_parser++;
189             }
190         }
191
192         if( *psz_parser == ':' )
193         {
194             /* Found bind port */
195             *psz_parser = '\0'; /* Terminate bind address if necessary */
196             psz_parser++;
197
198             psz_bind_port = psz_parser;
199         }
200     }
201
202     /* Convert ports format */
203     if( *psz_server_port )
204     {
205         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
206         if( *psz_parser )
207         {
208             intf_ErrMsg( "input error: cannot parse server port near %s",
209                          psz_parser );
210             free(psz_name);
211             return( -1 );
212         }
213     }
214
215     if( *psz_bind_port )
216     {
217         i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
218         if( *psz_parser )
219         {
220             intf_ErrMsg( "input error: cannot parse bind port near %s",
221                          psz_parser );
222             free(psz_name);
223             return( -1 );
224         }
225     }
226
227     vlc_mutex_lock( &p_input->stream.stream_lock );
228     p_input->stream.b_pace_control = 0;
229     p_input->stream.b_seekable = 0;
230     p_input->stream.p_selected_area->i_tell = 0;
231     p_input->stream.i_method = INPUT_METHOD_NETWORK;
232     vlc_mutex_unlock( &p_input->stream.stream_lock );
233
234     if( *psz_server_addr || i_server_port )
235     {
236         intf_ErrMsg("input warning: this UDP syntax is deprecated ; the server argument will be");
237         intf_ErrMsg("ignored (%s:%d). If you wanted to enter a multicast address",
238                     psz_server_addr, i_server_port);
239         intf_ErrMsg("or local port, type : %s:@%s:%d",
240                     *p_input->psz_access ? p_input->psz_access : "udp",
241                     psz_server_addr, i_server_port );
242
243         i_server_port = 0;
244         psz_server_addr = "";
245     }
246  
247     intf_WarnMsg( 2, "input: opening server=%s:%d local=%s:%d",
248                   psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
249
250     /* Prepare the network_socket_t structure */
251     socket_desc.i_type = NETWORK_UDP;
252     socket_desc.psz_bind_addr = psz_bind_addr;
253     socket_desc.i_bind_port = i_bind_port;
254     socket_desc.psz_server_addr = psz_server_addr;
255     socket_desc.i_server_port = i_server_port;
256
257     /* Find an appropriate network module */
258     p_network = module_Need( MODULE_CAPABILITY_NETWORK, psz_network,
259                              &socket_desc );
260     free(psz_name);
261     if( p_network == NULL )
262     {
263         return( -1 );
264     }
265     module_Unneed( p_network );
266     
267     p_access_data = p_input->p_access_data = malloc( sizeof(input_socket_t) );
268     if( p_access_data == NULL )
269     {
270         intf_ErrMsg( "input error: Out of memory" );
271         return( -1 );
272     }
273
274     p_access_data->i_handle = socket_desc.i_handle;
275     p_input->i_mtu = socket_desc.i_mtu;
276
277     return( 0 );
278 }