]> git.sesse.net Git - vlc/blob - modules/access/udp.c
* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc] / modules / 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.1 2002/08/04 17:23:41 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 int  Open       ( vlc_object_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
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 );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * Open: open the socket
64  *****************************************************************************/
65 static int Open( vlc_object_t *p_this )
66 {
67     input_thread_t *    p_input = (input_thread_t *)p_this;
68     input_socket_t *    p_access_data;
69     module_t *          p_network;
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;
79
80     if( config_GetInt( p_input, "ipv4" ) )
81     {
82         psz_network = "ipv4";
83     }
84     if( config_GetInt( p_input, "ipv6" ) )
85     {
86         psz_network = "ipv6";
87     }
88
89     if( *p_input->psz_access )
90     {
91         /* Find out which shortcut was used */
92         if( !strncmp( p_input->psz_access, "udp6", 5 ) )
93         {
94             psz_network = "ipv6";
95         }
96         else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
97         {
98             psz_network = "ipv4";
99         }
100     }
101
102     /* Parse psz_name syntax :
103      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
104
105     if( *psz_parser && *psz_parser != '@' )
106     {
107         /* Found server */
108         psz_server_addr = psz_parser;
109
110         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
111         {
112             if( *psz_parser == '[' )
113             {
114                 /* IPv6 address */
115                 while( *psz_parser && *psz_parser != ']' )
116                 {
117                     psz_parser++;
118                 }
119             }
120             psz_parser++;
121         }
122
123         if( *psz_parser == ':' )
124         {
125             /* Found server port */
126             *psz_parser = '\0'; /* Terminate server name */
127             psz_parser++;
128             psz_server_port = psz_parser;
129
130             while( *psz_parser && *psz_parser != '@' )
131             {
132                 psz_parser++;
133             }
134         }
135     }
136
137     if( *psz_parser == '@' )
138     {
139         /* Found bind address or bind port */
140         *psz_parser = '\0'; /* Terminate server port or name if necessary */
141         psz_parser++;
142
143         if( *psz_parser && *psz_parser != ':' )
144         {
145             /* Found bind address */
146             psz_bind_addr = psz_parser;
147
148             while( *psz_parser && *psz_parser != ':' )
149             {
150                 if( *psz_parser == '[' )
151                 {
152                     /* IPv6 address */
153                     while( *psz_parser && *psz_parser != ']' )
154                     {
155                         psz_parser++;
156                     }
157                 }
158                 psz_parser++;
159             }
160         }
161
162         if( *psz_parser == ':' )
163         {
164             /* Found bind port */
165             *psz_parser = '\0'; /* Terminate bind address if necessary */
166             psz_parser++;
167
168             psz_bind_port = psz_parser;
169         }
170     }
171
172     /* Convert ports format */
173     if( *psz_server_port )
174     {
175         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
176         if( *psz_parser )
177         {
178             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
179             free(psz_name);
180             return( -1 );
181         }
182     }
183
184     if( *psz_bind_port )
185     {
186         i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
187         if( *psz_parser )
188         {
189             msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
190             free(psz_name);
191             return( -1 );
192         }
193     }
194
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;
199
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 );
206
207     if( *psz_server_addr || i_server_port )
208     {
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 );
215
216         i_server_port = 0;
217         psz_server_addr = "";
218     }
219  
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 );
222
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;
229
230     /* Find an appropriate network module */
231     p_input->p_private = (void*) &socket_desc;
232     p_network = module_Need( p_input, "network", psz_network );
233     free(psz_name);
234     if( p_network == NULL )
235     {
236         return( -1 );
237     }
238     module_Unneed( p_input, p_network );
239     
240     p_access_data = p_input->p_access_data = malloc( sizeof(input_socket_t) );
241     if( p_access_data == NULL )
242     {
243         msg_Err( p_input, "out of memory" );
244         return( -1 );
245     }
246
247     p_access_data->i_handle = socket_desc.i_handle;
248     p_input->i_mtu = socket_desc.i_mtu;
249
250     return( 0 );
251 }