source: roaraudio/libroar/vio_proto.c @ 3066:bb8d29a2aa31

Last change on this file since 3066:bb8d29a2aa31 was 3066:bb8d29a2aa31, checked in by phi, 14 years ago

converted proto API to new buffer based API, need to write code to use the buffers

File size: 8.8 KB
Line 
1//vio_proto.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar 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 software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_vio_proto_init_def  (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) {
38#ifndef ROAR_WITHOUT_VIO_PROTO
39 int                        port = 0;
40 int                        ret;
41 char                     * ed;
42
43 if ( def == NULL )
44  return -1;
45
46 switch (proto) {
47  case ROAR_VIO_PROTO_P_HTTP:    port = 80; break;
48  case ROAR_VIO_PROTO_P_GOPHER:  port = 70; break;
49  default:
50    return -1;
51 }
52
53 if ( dstr == NULL )
54  dstr = "//";
55
56 if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 )
57  return -1;
58
59 if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 )
60  return -1;
61
62 if ( !strncmp(dstr, "//", 2) )
63  dstr += 2;
64
65 if ( (ed = strstr(dstr, "/")) != NULL )
66  *ed = 0;
67
68 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
69
70 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
71
72 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
73
74 if ( ed != NULL )
75  *ed = '/';
76
77 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
78
79 return ret;
80#else
81 return -1;
82#endif
83}
84
85int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
86                              char * dstr, int proto, struct roar_vio_defaults * odef) {
87#ifndef ROAR_WITHOUT_VIO_PROTO
88 struct roar_vio_proto * self;
89 char * host;
90 char * tmp;
91
92 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
93
94 if ( calls == NULL || dst == NULL || odef == NULL )
95  return -1;
96
97 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
98 ROAR_DBG("roar_vio_open_proto(*) = ?");
99
100 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL )
101  return -1;
102
103 memset(self, 0, sizeof(struct roar_vio_proto));
104
105 self->next      = dst;
106
107 calls->inst     = self;
108
109 calls->read     = roar_vio_proto_read;
110 calls->write    = roar_vio_proto_write;
111// calls->lseek    = roar_vio_proto_lseek; // TODO: this is currently not supported
112 calls->nonblock = roar_vio_proto_nonblock;
113 calls->sync     = roar_vio_proto_sync;
114 calls->ctl      = roar_vio_proto_ctl;
115 calls->close    = roar_vio_proto_close;
116
117 ROAR_DBG("roar_vio_open_proto(*) = ?");
118
119 if ( dstr != NULL ) {
120  dstr += 2;
121  host  = dstr;
122
123  if ( (tmp = strstr(dstr, "/")) == NULL )
124   return -1;
125
126  *tmp++ = 0;
127  dstr   = tmp;
128
129  if ( (tmp = strstr(dstr, "#")) != NULL )
130   *tmp = 0;
131 } else {
132  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
133  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
134   dstr = odef->d.file;
135   host = "localhost";
136
137   for (; *dstr == '/'; dstr++);
138
139  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
140   dstr = ""; // index document
141   host = odef->d.socket.host;
142  } else {
143   return -1;
144  }
145 }
146
147 ROAR_DBG("roar_vio_open_proto(*) = ?");
148 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr);
149
150 switch (proto) {
151  case ROAR_VIO_PROTO_P_HTTP:
152    return roar_vio_open_proto_http(calls, dst, host, dstr);
153   break;
154  case ROAR_VIO_PROTO_P_GOPHER:
155    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
156   break;
157 }
158
159 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
160 return -1;
161#else
162 return -1;
163#endif
164}
165
166#ifndef ROAR_WITHOUT_VIO_PROTO
167ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
168 struct roar_vio_proto * self = vio->inst;
169 ssize_t ret;
170 ssize_t have = 0;
171 size_t  len;
172
173 if ( self->reader.buffer != NULL ) {
174  len = count;
175  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
176   // This is very bad.
177   return -1;
178  }
179
180  if ( len ) {
181   have   = len;
182   buf   += len;
183   count -= len;
184  }
185 }
186
187 if ( count == 0 )
188  return have;
189
190 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
191  return ret;
192
193 return have + ret;
194}
195
196ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
197 struct roar_vio_proto * self = vio->inst;
198
199 return roar_vio_write(self->next, buf, count);
200}
201
202// TODO: this is currently not implemented as this is hard to implement with buffers:
203off_t   roar_vio_proto_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
204
205int     roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) {
206 struct roar_vio_proto * self = vio->inst;
207
208 /* we can simply use the next layer's nonblock as all we do in addtion *
209  * to call there functions are our buffers which do not block normaly  */
210
211 return roar_vio_nonblock(self->next, state);
212}
213
214int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
215 struct roar_vio_proto * self = vio->inst;
216
217 return roar_vio_sync(self->next);
218}
219
220int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
221 struct roar_vio_proto * self = vio->inst;
222
223 if (vio == NULL || cmd == -1)
224  return -1;
225
226 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
227
228 switch (cmd) {
229  case ROAR_VIO_CTL_GET_NEXT:
230    *(struct roar_vio_calls **)data = self->next;
231    return 0;
232   break;
233  case ROAR_VIO_CTL_SET_NEXT:
234    self->next = *(struct roar_vio_calls **)data;
235    return 0;
236   break;
237 }
238
239 return roar_vio_ctl(self->next, cmd, data);
240}
241
242int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
243 struct roar_vio_proto * self = vio->inst;
244
245 if ( roar_vio_close(self->next) == -1 )
246  return -1;
247
248 roar_mm_free(self);
249
250 return 0;
251}
252
253
254int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
255 char buf[1024];
256 char b0[80], b1[80];
257 int  status;
258 int  len;
259
260 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
261
262 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
263  return -1;
264
265 calls->write = NULL; // Disable write as we do not support this
266
267 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
268
269 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
270 roar_vio_printf(dst, "Host: %s\r\n", host);
271 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
272 roar_vio_printf(dst, "Connection: close\r\n");
273 roar_vio_printf(dst, "\r\n");
274
275 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
276
277 roar_vio_sync(dst);
278
279 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
280
281 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
282  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
283  return -1;
284 }
285
286 buf[len] = 0;
287
288 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
289
290 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
291  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
292  return -1;
293 }
294
295 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
296
297 if ( status != 200 ) {
298  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
299  return -1;
300 }
301
302 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
303// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
304
305 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
306  return 0;
307
308 while (*buf != '\r' && *buf != '\n') {
309  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
310   return -1;
311 }
312
313 return 0;
314}
315
316int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
317 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
318  return -1;
319
320 calls->write = NULL; // Disable write as we do not support this
321
322 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
323
324 if ( file[1] == '/' )
325  file += 2;
326
327 roar_vio_printf(dst, "/%s\r\n", file);
328
329 roar_vio_sync(dst); // for encryption/compression layers
330
331 return 0;
332}
333#endif
334
335//ll
Note: See TracBrowser for help on using the repository browser.