]> git.sesse.net Git - vlc/blob - modules/access/tcp.c
Revert the so-called whitelisting commits that are actually blacklisting
[vlc] / modules / access / tcp.c
1 /*****************************************************************************
2  * tcp.c: TCP input module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_access.h>
30
31 #include <vlc_network.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 #define CACHING_TEXT N_("Caching value in ms")
37 #define CACHING_LONGTEXT N_( \
38     "Caching value for TCP streams. This " \
39     "value should be set in milliseconds." )
40
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 vlc_module_begin();
45     set_shortname( _("TCP") );
46     set_description( _("TCP input") );
47     set_category( CAT_INPUT );
48     set_subcategory( SUBCAT_INPUT_ACCESS );
49
50     add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
51                  CACHING_LONGTEXT, VLC_TRUE );
52
53     set_capability( "access2", 0 );
54     add_shortcut( "tcp" );
55     set_callbacks( Open, Close );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 struct access_sys_t
62 {
63     int        fd;
64 };
65
66
67 static ssize_t Read( access_t *, uint8_t *, size_t );
68 static int Control( access_t *, int, va_list );
69
70 /*****************************************************************************
71  * Open: open the socket
72  *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
74 {
75     access_t     *p_access = (access_t *)p_this;
76     access_sys_t *p_sys;
77
78     char         *psz_dup = strdup(p_access->psz_path);
79     char         *psz_parser = psz_dup;
80
81     /* Parse server:port */
82     if( *psz_parser == '[' )
83     {
84         psz_parser = strchr( psz_parser, ']' );
85         if( psz_parser == NULL )
86             psz_parser = psz_dup;
87     }
88     psz_parser = strchr( psz_parser, ':' );
89
90     if( psz_parser == NULL )
91     {
92         msg_Err( p_access, "missing port number : %s", psz_dup );
93         free( psz_dup );
94         return VLC_EGENERIC;
95     }
96
97     *psz_parser++ = '\0';
98
99     /* Init p_access */
100     access_InitFields( p_access ); \
101     ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL ); \
102     MALLOC_ERR( p_access->p_sys, access_sys_t ); \
103     p_sys = p_access->p_sys; memset( p_sys, 0, sizeof( access_sys_t ) );
104
105     p_sys->fd = net_ConnectTCP( p_access, psz_dup, atoi( psz_parser ) );
106     free( psz_dup );
107
108     if( p_sys->fd < 0 )
109     {
110         free( p_sys );
111         return VLC_EGENERIC;
112     }
113
114     /* Update default_pts to a suitable value for udp access */
115     var_Create( p_access, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Close: free unused data structures
122  *****************************************************************************/
123 static void Close( vlc_object_t *p_this )
124 {
125     access_t     *p_access = (access_t *)p_this;
126     access_sys_t *p_sys = p_access->p_sys;
127
128     net_Close( p_sys->fd );
129     free( p_sys );
130 }
131
132 /*****************************************************************************
133  * Read: read on a file descriptor, checking b_die periodically
134  *****************************************************************************/
135 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
136 {
137     access_sys_t *p_sys = p_access->p_sys;
138     int i_read;
139
140     if( p_access->info.b_eof )
141         return 0;
142
143     i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len,
144                        VLC_FALSE );
145     if( i_read == 0 )
146         p_access->info.b_eof = VLC_TRUE;
147     else if( i_read > 0 )
148         p_access->info.i_pos += i_read;
149
150     return i_read;
151 }
152
153 /*****************************************************************************
154  * Control:
155  *****************************************************************************/
156 static int Control( access_t *p_access, int i_query, va_list args )
157 {
158     vlc_bool_t   *pb_bool;
159     int          *pi_int;
160     int64_t      *pi_64;
161
162     switch( i_query )
163     {
164         /* */
165         case ACCESS_CAN_SEEK:
166         case ACCESS_CAN_FASTSEEK:
167             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
168             *pb_bool = VLC_FALSE;
169             break;
170         case ACCESS_CAN_PAUSE:
171             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
172             *pb_bool = VLC_TRUE;    /* FIXME */
173             break;
174         case ACCESS_CAN_CONTROL_PACE:
175             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
176             *pb_bool = VLC_TRUE;    /* FIXME */
177             break;
178
179         /* */
180         case ACCESS_GET_MTU:
181             pi_int = (int*)va_arg( args, int * );
182             *pi_int = 0;
183             break;
184
185         case ACCESS_GET_PTS_DELAY:
186             pi_64 = (int64_t*)va_arg( args, int64_t * );
187             *pi_64 = (int64_t)var_GetInteger( p_access, "tcp-caching" ) * I64C(1000);
188             break;
189
190         /* */
191         case ACCESS_SET_PAUSE_STATE:
192             /* Nothing to do */
193             break;
194
195         case ACCESS_GET_TITLE_INFO:
196         case ACCESS_SET_TITLE:
197         case ACCESS_SET_SEEKPOINT:
198         case ACCESS_SET_PRIVATE_ID_STATE:
199         case ACCESS_GET_CONTENT_TYPE:
200             return VLC_EGENERIC;
201
202         default:
203             msg_Warn( p_access, "unimplemented query in control" );
204             return VLC_EGENERIC;
205
206     }
207     return VLC_SUCCESS;
208 }