Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / stream / stream_smb.c
blob4c57176d2f01cbd5729f61f168759c1b37095e06
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <libsmbclient.h>
22 #include <unistd.h>
24 #include "mp_msg.h"
25 #include "stream.h"
26 #include "help_mp.h"
27 #include "m_option.h"
28 #include "m_struct.h"
30 static struct stream_priv_s {
31 } stream_priv_dflts = {
34 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
35 // URL definition
36 static const m_option_t stream_opts_fields[] = {
37 { NULL, NULL, 0, 0, 0, 0, NULL }
40 static const struct m_struct_st stream_opts = {
41 "smb",
42 sizeof(struct stream_priv_s),
43 &stream_priv_dflts,
44 stream_opts_fields
47 static char smb_password[15];
48 static char smb_username[15];
50 static void smb_auth_fn(const char *server, const char *share,
51 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
52 char *password, int pwmaxlen)
54 char temp[128];
56 strcpy(temp, "LAN");
57 if (temp[strlen(temp) - 1] == 0x0a)
58 temp[strlen(temp) - 1] = 0x00;
60 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
62 strcpy(temp, smb_username);
63 if (temp[strlen(temp) - 1] == 0x0a)
64 temp[strlen(temp) - 1] = 0x00;
66 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
68 strcpy(temp, smb_password);
69 if (temp[strlen(temp) - 1] == 0x0a)
70 temp[strlen(temp) - 1] = 0x00;
72 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
75 static int control(stream_t *s, int cmd, void *arg) {
76 switch(cmd) {
77 case STREAM_CTRL_GET_SIZE: {
78 off_t size = smbc_lseek(s->fd,0,SEEK_END);
79 smbc_lseek(s->fd,s->pos,SEEK_SET);
80 if(size != (off_t)-1) {
81 *((off_t*)arg) = size;
82 return 1;
86 return STREAM_UNSUPPORTED;
89 static int seek(stream_t *s,off_t newpos) {
90 s->pos = newpos;
91 if(smbc_lseek(s->fd,s->pos,SEEK_SET)<0) {
92 s->eof=1;
93 return 0;
95 return 1;
98 static int fill_buffer(stream_t *s, char* buffer, int max_len){
99 int r = smbc_read(s->fd,buffer,max_len);
100 return (r <= 0) ? -1 : r;
103 static int write_buffer(stream_t *s, char* buffer, int len) {
104 int r = smbc_write(s->fd,buffer,len);
105 return (r <= 0) ? -1 : r;
108 static void close_f(stream_t *s){
109 smbc_close(s->fd);
112 static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
113 struct stream_priv_s *p = (struct stream_priv_s*)opts;
114 char *filename;
115 mode_t m = 0;
116 off_t len;
117 int fd, err;
119 filename = stream->url;
121 if(mode == STREAM_READ)
122 m = O_RDONLY;
123 else if (mode == STREAM_WRITE) //who's gonna do that ?
124 m = O_RDWR|O_CREAT|O_TRUNC;
125 else {
126 mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
127 m_struct_free (&stream_opts, opts);
128 return STREAM_UNSUPPORTED;
131 if(!filename) {
132 mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
133 m_struct_free(&stream_opts, opts);
134 return STREAM_ERROR;
137 err = smbc_init(smb_auth_fn, 1);
138 if (err < 0) {
139 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBInitError,err);
140 m_struct_free(&stream_opts, opts);
141 return STREAM_ERROR;
144 fd = smbc_open(filename, m,0644);
145 if (fd < 0) {
146 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_SMBFileNotFound, filename);
147 m_struct_free(&stream_opts, opts);
148 return STREAM_ERROR;
151 stream->flags = mode;
152 len = 0;
153 if(mode == STREAM_READ) {
154 len = smbc_lseek(fd,0,SEEK_END);
155 smbc_lseek (fd, 0, SEEK_SET);
157 if(len > 0 || mode == STREAM_WRITE) {
158 stream->flags |= MP_STREAM_SEEK;
159 stream->seek = seek;
160 if(mode == STREAM_READ) stream->end_pos = len;
162 stream->type = STREAMTYPE_SMB;
163 stream->fd = fd;
164 stream->fill_buffer = fill_buffer;
165 stream->write_buffer = write_buffer;
166 stream->close = close_f;
167 stream->control = control;
169 m_struct_free(&stream_opts, opts);
170 return STREAM_OK;
173 const stream_info_t stream_info_smb = {
174 "Server Message Block",
175 "smb",
176 "M. Tourne",
177 "based on the code from 'a bulgarian' (one says)",
178 open_f,
179 {"smb", NULL},
180 &stream_opts,
181 0 //Url is an option string