dvdnav: Support for image highlights for dvdnav menus.
[mplayer/glamo.git] / stream / stream_smb.c
blob684940749fd6c165dff1a85fa26ba681fec7609d
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 "m_option.h"
27 #include "m_struct.h"
29 static struct stream_priv_s {
30 } stream_priv_dflts = {
33 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
34 // URL definition
35 static const m_option_t stream_opts_fields[] = {
36 { NULL, NULL, 0, 0, 0, 0, NULL }
39 static const struct m_struct_st stream_opts = {
40 "smb",
41 sizeof(struct stream_priv_s),
42 &stream_priv_dflts,
43 stream_opts_fields
46 static char smb_password[15];
47 static char smb_username[15];
49 static void smb_auth_fn(const char *server, const char *share,
50 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
51 char *password, int pwmaxlen)
53 char temp[128];
55 strcpy(temp, "LAN");
56 if (temp[strlen(temp) - 1] == 0x0a)
57 temp[strlen(temp) - 1] = 0x00;
59 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
61 strcpy(temp, smb_username);
62 if (temp[strlen(temp) - 1] == 0x0a)
63 temp[strlen(temp) - 1] = 0x00;
65 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
67 strcpy(temp, smb_password);
68 if (temp[strlen(temp) - 1] == 0x0a)
69 temp[strlen(temp) - 1] = 0x00;
71 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
74 static int control(stream_t *s, int cmd, void *arg) {
75 switch(cmd) {
76 case STREAM_CTRL_GET_SIZE: {
77 off_t size = smbc_lseek(s->fd,0,SEEK_END);
78 smbc_lseek(s->fd,s->pos,SEEK_SET);
79 if(size != (off_t)-1) {
80 *((off_t*)arg) = size;
81 return 1;
85 return STREAM_UNSUPPORTED;
88 static int seek(stream_t *s,off_t newpos) {
89 s->pos = newpos;
90 if(smbc_lseek(s->fd,s->pos,SEEK_SET)<0) {
91 s->eof=1;
92 return 0;
94 return 1;
97 static int fill_buffer(stream_t *s, char* buffer, int max_len){
98 int r = smbc_read(s->fd,buffer,max_len);
99 return (r <= 0) ? -1 : r;
102 static int write_buffer(stream_t *s, char* buffer, int len) {
103 int r = smbc_write(s->fd,buffer,len);
104 return (r <= 0) ? -1 : r;
107 static void close_f(stream_t *s){
108 smbc_close(s->fd);
111 static int open_f (stream_t *stream, int mode, void *opts, int* file_format) {
112 struct stream_priv_s *p = (struct stream_priv_s*)opts;
113 char *filename;
114 mode_t m = 0;
115 off_t len;
116 int fd, err;
118 filename = stream->url;
120 if(mode == STREAM_READ)
121 m = O_RDONLY;
122 else if (mode == STREAM_WRITE) //who's gonna do that ?
123 m = O_RDWR|O_CREAT|O_TRUNC;
124 else {
125 mp_msg(MSGT_OPEN, MSGL_ERR, "[smb] Unknown open mode %d\n", mode);
126 m_struct_free (&stream_opts, opts);
127 return STREAM_UNSUPPORTED;
130 if(!filename) {
131 mp_msg(MSGT_OPEN,MSGL_ERR, "[smb] Bad url\n");
132 m_struct_free(&stream_opts, opts);
133 return STREAM_ERROR;
136 err = smbc_init(smb_auth_fn, 1);
137 if (err < 0) {
138 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Cannot init the libsmbclient library: %d\n",err);
139 m_struct_free(&stream_opts, opts);
140 return STREAM_ERROR;
143 fd = smbc_open(filename, m,0644);
144 if (fd < 0) {
145 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Could not open from LAN: '%s'\n", filename);
146 m_struct_free(&stream_opts, opts);
147 return STREAM_ERROR;
150 stream->flags = mode;
151 len = 0;
152 if(mode == STREAM_READ) {
153 len = smbc_lseek(fd,0,SEEK_END);
154 smbc_lseek (fd, 0, SEEK_SET);
156 if(len > 0 || mode == STREAM_WRITE) {
157 stream->flags |= MP_STREAM_SEEK;
158 stream->seek = seek;
159 if(mode == STREAM_READ) stream->end_pos = len;
161 stream->type = STREAMTYPE_SMB;
162 stream->fd = fd;
163 stream->fill_buffer = fill_buffer;
164 stream->write_buffer = write_buffer;
165 stream->close = close_f;
166 stream->control = control;
168 m_struct_free(&stream_opts, opts);
169 return STREAM_OK;
172 const stream_info_t stream_info_smb = {
173 "Server Message Block",
174 "smb",
175 "M. Tourne",
176 "based on the code from 'a bulgarian' (one says)",
177 open_f,
178 {"smb", NULL},
179 &stream_opts,
180 0 //Url is an option string