1 /*****************************************************************************
2 * smb.c: SMB input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
32 #include <libsmbclient.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43 "Allows you to modify the default caching value for SMB streams. This " \
44 "value should be set in millisecond units." )
45 #define USER_TEXT N_("SMB user name")
46 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
47 "be used for the connection.")
48 #define PASS_TEXT N_("SMB password")
49 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
50 "used for the connection.")
51 #define DOMAIN_TEXT N_("SMB domain")
52 #define DOMAIN_LONGTEXT N_("Allows you to modify the domain/workgroup that " \
53 "will be used for the connection.")
56 set_shortname( "SMB" );
57 set_description( _("SMB input") );
58 set_capability( "access2", 0 );
59 set_category( CAT_INPUT );
60 set_subcategory( SUBCAT_INPUT_ACCESS );
61 add_integer( "smb-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
62 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
63 add_string( "smb-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
65 add_string( "smb-pwd", NULL, NULL, PASS_TEXT,
66 PASS_LONGTEXT, VLC_FALSE );
67 add_string( "smb-domain", NULL, NULL, DOMAIN_TEXT,
68 DOMAIN_LONGTEXT, VLC_FALSE );
69 add_shortcut( "smb" );
70 set_callbacks( Open, Close );
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Read( access_t *, uint8_t *, int );
77 static int Seek( access_t *, int64_t );
78 static int Control( access_t *, int, va_list );
90 void smb_auth( const char *srv, const char *shr, char *wg, int wglen,
91 char *un, int unlen, char *pw, int pwlen )
93 //wglen = unlen = pwlen = 0;
96 /****************************************************************************
97 * Open: connect to smb server and ask for file
98 ****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 access_t *p_access = (access_t*)p_this;
103 struct stat filestat;
104 char *psz_uri, *psz_user, *psz_pwd, *psz_domain;
115 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]] */
117 psz_user = var_CreateGetString( p_access, "smb-user" );
118 if( psz_user && !*psz_user ) { free( psz_user ); psz_user = 0; }
119 psz_pwd = var_CreateGetString( p_access, "smb-pwd" );
120 if( psz_pwd && !*psz_pwd ) { free( psz_pwd ); psz_pwd = 0; }
121 psz_domain = var_CreateGetString( p_access, "smb-domain" );
122 if( psz_domain && !*psz_domain ) { free( psz_domain ); psz_domain = 0; }
124 /* FIXME: will need to parse the URI so we don't override credentials
125 * if there are already present. */
127 asprintf( &psz_uri, "smb://%s%s%s%s%s@%s",
128 psz_domain ? psz_domain : "", psz_domain ? ";" : "",
129 psz_user, psz_pwd ? ":" : "",
130 psz_pwd ? psz_pwd : "", p_access->psz_path );
132 asprintf( &psz_uri, "smb://%s", p_access->psz_path );
134 if( psz_user ) free( psz_user );
135 if( psz_pwd ) free( psz_pwd );
136 if( psz_domain ) free( psz_domain );
139 if( !(p_smb = smbc_new_context()) )
141 msg_Err( p_access, "out of memory" );
146 p_smb->callbacks.auth_fn = smb_auth;
148 if( !smbc_init_context( p_smb ) )
150 msg_Err( p_access, "cannot initialize context (%s)", strerror(errno) );
151 smbc_free_context( p_smb, 1 );
156 if( !(p_file = p_smb->open( p_smb, psz_uri, O_RDONLY, 0 )) )
158 msg_Err( p_access, "open failed for '%s' (%s)",
159 p_access->psz_path, strerror(errno) );
160 smbc_free_context( p_smb, 1 );
165 p_access->info.i_size = 0;
166 i_ret = p_smb->fstat( p_smb, p_file, &filestat );
167 if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(errno) );
168 else p_access->info.i_size = filestat.st_size;
171 if( smbc_init( smb_auth, 1 ) )
177 if( (i_smb = smbc_open( psz_uri, O_RDONLY, 0 )) < 0 )
179 msg_Err( p_access, "open failed for '%s' (%s)",
180 p_access->psz_path, strerror(errno) );
185 p_access->info.i_size = 0;
186 i_ret = smbc_fstat( i_smb, &filestat );
187 if( i_ret ) msg_Err( p_access, "stat failed (%s)", strerror(i_ret) );
188 else p_access->info.i_size = filestat.st_size;
194 p_access->pf_read = Read;
195 p_access->pf_block = NULL;
196 p_access->pf_seek = Seek;
197 p_access->pf_control = Control;
198 p_access->info.i_update = 0;
199 p_access->info.i_pos = 0;
200 p_access->info.b_eof = VLC_FALSE;
201 p_access->info.i_title = 0;
202 p_access->info.i_seekpoint = 0;
203 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
204 memset( p_sys, 0, sizeof( access_sys_t ) );
207 p_sys->p_smb = p_smb;
208 p_sys->p_file = p_file;
210 p_sys->i_smb = i_smb;
213 /* Update default_pts to a suitable value for smb access */
214 var_Create( p_access, "smb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
219 /*****************************************************************************
220 * Close: free unused data structures
221 *****************************************************************************/
222 static void Close( vlc_object_t *p_this )
224 access_t *p_access = (access_t*)p_this;
225 access_sys_t *p_sys = p_access->p_sys;
228 p_sys->p_smb->close( p_sys->p_smb, p_sys->p_file );
229 smbc_free_context( p_sys->p_smb, 1 );
231 smbc_close( p_sys->i_smb );
237 /*****************************************************************************
238 * Seek: try to go at the right place
239 *****************************************************************************/
240 static int Seek( access_t *p_access, int64_t i_pos )
242 access_sys_t *p_sys = p_access->p_sys;
245 if( i_pos < 0 ) return VLC_EGENERIC;
247 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
250 i_ret = p_sys->p_smb->lseek(p_sys->p_smb, p_sys->p_file, i_pos, SEEK_SET);
252 i_ret = smbc_lseek( p_sys->i_smb, i_pos, SEEK_SET );
256 msg_Err( p_access, "seek failed (%s)", strerror(errno) );
260 p_access->info.b_eof = VLC_FALSE;
261 p_access->info.i_pos = i_ret;
266 /*****************************************************************************
268 *****************************************************************************/
269 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
271 access_sys_t *p_sys = p_access->p_sys;
274 if( p_access->info.b_eof ) return 0;
277 i_read = p_sys->p_smb->read(p_sys->p_smb, p_sys->p_file, p_buffer, i_len);
279 i_read = smbc_read( p_sys->i_smb, p_buffer, i_len );
283 msg_Err( p_access, "read failed (%s)", strerror(errno) );
287 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
288 else if( i_read > 0 ) p_access->info.i_pos += i_read;
293 /*****************************************************************************
295 *****************************************************************************/
296 static int Control( access_t *p_access, int i_query, va_list args )
304 case ACCESS_CAN_SEEK:
305 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
308 case ACCESS_CAN_FASTSEEK:
309 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
312 case ACCESS_CAN_PAUSE:
313 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
316 case ACCESS_CAN_CONTROL_PACE:
317 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
322 pi_int = (int*)va_arg( args, int * );
326 case ACCESS_GET_PTS_DELAY:
327 pi_64 = (int64_t*)va_arg( args, int64_t * );
328 *pi_64 = (int64_t)var_GetInteger( p_access, "smb-caching" ) * 1000;
331 case ACCESS_SET_PAUSE_STATE:
335 case ACCESS_GET_TITLE_INFO:
336 case ACCESS_SET_TITLE:
337 case ACCESS_SET_SEEKPOINT:
338 case ACCESS_SET_PRIVATE_ID_STATE:
342 msg_Warn( p_access, "unimplemented query in control" );