]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Configure: fix typo
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * Copyright (C) 2007 Remi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Tristan Leteurtre <tooney@via.ecp.fr>
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12  *          Remi Denis-Courmont
13  *
14  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
43
44 #define MTU 65535
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 vlc_module_begin ()
53     set_shortname( N_("UDP" ) )
54     set_description( N_("UDP input") )
55     set_category( CAT_INPUT )
56     set_subcategory( SUBCAT_INPUT_ACCESS )
57
58     add_obsolete_integer( "server-port" ) /* since 1.2.0 */
59
60     set_capability( "access", 0 )
61     add_shortcut( "udp", "udpstream", "udp4", "udp6" )
62
63     set_callbacks( Open, Close )
64 vlc_module_end ()
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69 static block_t *BlockUDP( access_t * );
70 static int Control( access_t *, int, va_list );
71
72 /*****************************************************************************
73  * Open: open the socket
74  *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
76 {
77     access_t     *p_access = (access_t*)p_this;
78
79     char *psz_name = strdup( p_access->psz_location );
80     char *psz_parser;
81     const char *psz_server_addr, *psz_bind_addr = "";
82     int  i_bind_port = 1234, i_server_port = 0;
83     int fd;
84
85     /* Set up p_access */
86     access_InitFields( p_access );
87     ACCESS_SET_CALLBACKS( NULL, BlockUDP, Control, NULL );
88
89     /* Parse psz_name syntax :
90      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
91     psz_parser = strchr( psz_name, '@' );
92     if( psz_parser != NULL )
93     {
94         /* Found bind address and/or bind port */
95         *psz_parser++ = '\0';
96         psz_bind_addr = psz_parser;
97
98         if( psz_bind_addr[0] == '[' )
99             /* skips bracket'd IPv6 address */
100             psz_parser = strchr( psz_parser, ']' );
101
102         if( psz_parser != NULL )
103         {
104             psz_parser = strchr( psz_parser, ':' );
105             if( psz_parser != NULL )
106             {
107                 *psz_parser++ = '\0';
108                 i_bind_port = atoi( psz_parser );
109             }
110         }
111     }
112
113     psz_server_addr = psz_name;
114     psz_parser = ( psz_server_addr[0] == '[' )
115         ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
116         : psz_name;
117
118     if( psz_parser != NULL )
119     {
120         psz_parser = strchr( psz_parser, ':' );
121         if( psz_parser != NULL )
122         {
123             *psz_parser++ = '\0';
124             i_server_port = atoi( psz_parser );
125         }
126     }
127
128     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
129              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
130
131     fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
132                         psz_server_addr, i_server_port, IPPROTO_UDP );
133     free (psz_name);
134     if( fd == -1 )
135     {
136         msg_Err( p_access, "cannot open socket" );
137         return VLC_EGENERIC;
138     }
139     p_access->p_sys = (void *)(intptr_t)fd;
140
141     return VLC_SUCCESS;
142 }
143
144 /*****************************************************************************
145  * Close: free unused data structures
146  *****************************************************************************/
147 static void Close( vlc_object_t *p_this )
148 {
149     access_t     *p_access = (access_t*)p_this;
150
151     net_Close( (intptr_t)p_access->p_sys );
152 }
153
154 /*****************************************************************************
155  * Control:
156  *****************************************************************************/
157 static int Control( access_t *p_access, int i_query, va_list args )
158 {
159     bool    *pb_bool;
160     int64_t *pi_64;
161
162     switch( i_query )
163     {
164         /* */
165         case ACCESS_CAN_SEEK:
166         case ACCESS_CAN_FASTSEEK:
167         case ACCESS_CAN_PAUSE:
168         case ACCESS_CAN_CONTROL_PACE:
169             pb_bool = (bool*)va_arg( args, bool* );
170             *pb_bool = false;
171             break;
172         /* */
173         case ACCESS_GET_PTS_DELAY:
174             pi_64 = (int64_t*)va_arg( args, int64_t * );
175             *pi_64 = INT64_C(1000)
176                    * var_InheritInteger(p_access, "network-caching");
177             break;
178
179         /* */
180         case ACCESS_SET_PAUSE_STATE:
181         case ACCESS_GET_TITLE_INFO:
182         case ACCESS_SET_TITLE:
183         case ACCESS_SET_SEEKPOINT:
184         case ACCESS_SET_PRIVATE_ID_STATE:
185         case ACCESS_GET_CONTENT_TYPE:
186             return VLC_EGENERIC;
187
188         default:
189             msg_Warn( p_access, "unimplemented query in control" );
190             return VLC_EGENERIC;
191
192     }
193     return VLC_SUCCESS;
194 }
195
196 /*****************************************************************************
197  * BlockUDP:
198  *****************************************************************************/
199 static block_t *BlockUDP( access_t *p_access )
200 {
201     access_sys_t *p_sys = p_access->p_sys;
202     block_t      *p_block;
203     ssize_t len;
204
205     if( p_access->info.b_eof )
206         return NULL;
207
208     /* Read data */
209     p_block = block_New( p_access, MTU );
210     len = net_Read( p_access, (intptr_t)p_sys, NULL,
211                     p_block->p_buffer, MTU, false );
212     if( len < 0 )
213     {
214         block_Release( p_block );
215         return NULL;
216     }
217
218     return block_Realloc( p_block, 0, len );
219 }