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