source: roaraudio/libroar/vio_proto.c @ 3078:75efac535f8b

Last change on this file since 3078:75efac535f8b was 3078:75efac535f8b, checked in by phi, 14 years ago

support protocol name ICY, identical to HTTP at the moment but defaults to port 8000

File size: 10.1 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  case ROAR_VIO_PROTO_P_ICY:     port = 8000; break;
50  default:
51    return -1;
52 }
53
54 if ( dstr == NULL )
55  dstr = "//";
56
57 if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 )
58  return -1;
59
60 if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 )
61  return -1;
62
63 if ( !strncmp(dstr, "//", 2) )
64  dstr += 2;
65
66 if ( (ed = strstr(dstr, "/")) != NULL )
67  *ed = 0;
68
69 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
70
71 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
72
73 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
74
75 if ( ed != NULL )
76  *ed = '/';
77
78 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
79
80 return ret;
81#else
82 return -1;
83#endif
84}
85
86int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
87                              char * dstr, int proto, struct roar_vio_defaults * odef) {
88#ifndef ROAR_WITHOUT_VIO_PROTO
89 struct roar_vio_proto * self;
90 char * host;
91 char * tmp;
92
93 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
94
95 if ( calls == NULL || dst == NULL || odef == NULL )
96  return -1;
97
98 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
99 ROAR_DBG("roar_vio_open_proto(*) = ?");
100
101 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL )
102  return -1;
103
104 memset(self, 0, sizeof(struct roar_vio_proto));
105
106 self->next      = dst;
107
108 calls->inst     = self;
109
110 calls->read     = roar_vio_proto_read;
111 calls->write    = roar_vio_proto_write;
112// calls->lseek    = roar_vio_proto_lseek; // TODO: this is currently not supported
113 calls->nonblock = roar_vio_proto_nonblock;
114 calls->sync     = roar_vio_proto_sync;
115 calls->ctl      = roar_vio_proto_ctl;
116 calls->close    = roar_vio_proto_close;
117
118 ROAR_DBG("roar_vio_open_proto(*) = ?");
119
120 if ( dstr != NULL ) {
121  dstr += 2;
122  host  = dstr;
123
124  if ( (tmp = strstr(dstr, "/")) == NULL )
125   return -1;
126
127  *tmp++ = 0;
128  dstr   = tmp;
129
130  if ( (tmp = strstr(dstr, "#")) != NULL )
131   *tmp = 0;
132 } else {
133  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
134  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
135   dstr = odef->d.file;
136   host = "localhost";
137
138   for (; *dstr == '/'; dstr++);
139
140  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
141   dstr = ""; // index document
142   host = odef->d.socket.host;
143  } else {
144   return -1;
145  }
146 }
147
148 ROAR_DBG("roar_vio_open_proto(*) = ?");
149 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr);
150
151 switch (proto) {
152  case ROAR_VIO_PROTO_P_HTTP:
153  case ROAR_VIO_PROTO_P_ICY:
154    return roar_vio_open_proto_http(calls, dst, host, dstr);
155   break;
156  case ROAR_VIO_PROTO_P_GOPHER:
157    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
158   break;
159 }
160
161 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
162 return -1;
163#else
164 return -1;
165#endif
166}
167
168#ifndef ROAR_WITHOUT_VIO_PROTO
169ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
170 struct roar_vio_proto * self = vio->inst;
171 ssize_t ret;
172 ssize_t have = 0;
173 size_t  len;
174
175 if ( self->reader.buffer != NULL ) {
176  len = count;
177  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
178   // This is very bad.
179   return -1;
180  }
181
182  if ( len ) {
183   have   = len;
184   buf   += len;
185   count -= len;
186  }
187 }
188
189 if ( count == 0 )
190  return have;
191
192 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
193
194 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
195  return ret;
196
197 return have + ret;
198}
199
200ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
201 struct roar_vio_proto * self = vio->inst;
202
203 return roar_vio_write(self->next, buf, count);
204}
205
206// TODO: this is currently not implemented as this is hard to implement with buffers:
207off_t   roar_vio_proto_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
208
209int     roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) {
210 struct roar_vio_proto * self = vio->inst;
211
212 /* we can simply use the next layer's nonblock as all we do in addtion *
213  * to call there functions are our buffers which do not block normaly  */
214
215 return roar_vio_nonblock(self->next, state);
216}
217
218int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
219 struct roar_vio_proto * self = vio->inst;
220
221 return roar_vio_sync(self->next);
222}
223
224int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
225 struct roar_vio_proto * self = vio->inst;
226
227 if (vio == NULL || cmd == -1)
228  return -1;
229
230 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
231
232 switch (cmd) {
233  case ROAR_VIO_CTL_GET_NEXT:
234    *(struct roar_vio_calls **)data = self->next;
235    return 0;
236   break;
237  case ROAR_VIO_CTL_SET_NEXT:
238    self->next = *(struct roar_vio_calls **)data;
239    return 0;
240   break;
241 }
242
243 return roar_vio_ctl(self->next, cmd, data);
244}
245
246int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
247 struct roar_vio_proto * self = vio->inst;
248
249 if ( roar_vio_close(self->next) == -1 )
250  return -1;
251
252 roar_mm_free(self);
253
254 return 0;
255}
256
257
258int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
259 struct roar_vio_proto * self;
260 struct roar_buffer * bufbuf;
261 char * buf;
262 char * endofheader = NULL;
263 char b0[80], b1[80];
264 int  status;
265 int  len;
266
267 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
268
269 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
270  return -1;
271
272 self         = calls->inst;
273 calls->write = NULL; // Disable write as we do not support this
274
275 if ( roar_buffer_new(&bufbuf, 1024) == -1 )
276  return -1;
277
278 if ( roar_buffer_get_data(bufbuf, &buf) == -1 ) {
279  roar_buffer_free(bufbuf);
280  return -1;
281 }
282
283 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
284
285 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
286 roar_vio_printf(dst, "Host: %s\r\n", host);
287 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
288 roar_vio_printf(dst, "Connection: close\r\n");
289 roar_vio_printf(dst, "\r\n");
290
291 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
292
293 roar_vio_sync(dst);
294
295 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
296
297 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
298  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
299  roar_buffer_free(bufbuf);
300  return -1;
301 }
302
303 buf[len] = 0;
304
305 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
306
307 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
308  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
309  roar_buffer_free(bufbuf);
310  return -1;
311 }
312
313 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
314
315 if ( status != 200 ) {
316  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
317  roar_buffer_free(bufbuf);
318  return -1;
319 }
320
321 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
322// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
323
324 endofheader = strstr(buf, "\r\n\r\n");
325
326 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
327
328 while ( endofheader == NULL ) {
329  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
330   return -1;
331
332  buf[len] = 0;
333  endofheader = strstr(buf, "\r\n\r\n");
334  ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
335 }
336
337 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
338 ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf);
339
340 if ( (endofheader - buf) == (len - 4) ) {
341  roar_buffer_free(bufbuf);
342  bufbuf = NULL;
343 }
344
345 if ( bufbuf != NULL ) {
346  roar_buffer_set_offset(bufbuf, endofheader - buf + 4);
347  roar_buffer_set_len(bufbuf,    len - (endofheader - buf + 4) - 1);
348 }
349 self->reader.buffer = bufbuf;
350
351/*
352 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
353  return 0;
354
355 while (*buf != '\r' && *buf != '\n') {
356  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
357   return -1;
358 }
359*/
360
361 return 0;
362}
363
364int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
365 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
366  return -1;
367
368 calls->write = NULL; // Disable write as we do not support this
369
370 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
371
372 if ( file[1] == '/' )
373  file += 2;
374
375 roar_vio_printf(dst, "/%s\r\n", file);
376
377 roar_vio_sync(dst); // for encryption/compression layers
378
379 return 0;
380}
381#endif
382
383//ll
Note: See TracBrowser for help on using the repository browser.