source: roaraudio/libroar/vio_proto.c @ 5231:8b30ddb689b8

Last change on this file since 5231:8b30ddb689b8 was 5028:bda7085a07b8, checked in by phi, 13 years ago

fixed some ckport warnings about poissible buffer overflows which are not possible because buffer length is ensured before the call

File size: 16.5 KB
Line 
1//vio_proto.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#ifndef ROAR_WITHOUT_VIO_PROTO
39#include <roaraudio/proto_gopher.h>
40#endif
41
42int roar_vio_proto_init_def  (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) {
43#ifndef ROAR_WITHOUT_VIO_PROTO
44 int                        port = 0;
45 int                        ret;
46 char                     * ed;
47 char                     * tmp;
48
49 if ( def == NULL )
50  return -1;
51
52 switch (proto) {
53  case ROAR_VIO_PROTO_P_HTTP:    port =   80; break;
54  case ROAR_VIO_PROTO_P_GOPHER:  port =   70; break;
55  case ROAR_VIO_PROTO_P_ICY:     port = 8000; break;
56  default:
57    return -1;
58 }
59
60 if ( dstr == NULL )
61  dstr = "//";
62
63 if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 )
64  return -1;
65
66 if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 )
67  return -1;
68
69 if ( !strncmp(dstr, "//", 2) )
70  dstr += 2;
71
72 if ( (ed = strstr(dstr, "/")) != NULL )
73  *ed = 0;
74
75 if ( (tmp = strstr(dstr, "@")) != NULL )
76  dstr = tmp + 1;
77
78 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
79
80 ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def);
81
82 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags);
83
84 if ( ed != NULL )
85  *ed = '/';
86
87 ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr);
88
89 return ret;
90#else
91 return -1;
92#endif
93}
94
95int roar_vio_open_proto      (struct roar_vio_calls * calls, struct roar_vio_calls * dst,
96                              char * dstr, int proto, struct roar_vio_defaults * odef) {
97#ifndef ROAR_WITHOUT_VIO_PROTO
98 struct roar_userpass userpass = {.subtype = -1, .user = NULL, .pass = NULL};
99 struct roar_vio_proto * self;
100 char * host;
101 char * tmp;
102
103 ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef);
104
105 if ( calls == NULL || dst == NULL || odef == NULL )
106  return -1;
107
108 ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags);
109 ROAR_DBG("roar_vio_open_proto(*) = ?");
110
111 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL )
112  return -1;
113
114 memset(self, 0, sizeof(struct roar_vio_proto));
115
116 self->next      = dst;
117
118 calls->inst     = self;
119
120 calls->read     = roar_vio_proto_read;
121 calls->write    = roar_vio_proto_write;
122// calls->lseek    = roar_vio_proto_lseek; // TODO: this is currently not supported
123 calls->nonblock = roar_vio_proto_nonblock;
124 calls->sync     = roar_vio_proto_sync;
125 calls->ctl      = roar_vio_proto_ctl;
126 calls->close    = roar_vio_proto_close;
127
128 ROAR_DBG("roar_vio_open_proto(*) = ?");
129
130 if ( dstr != NULL ) {
131  dstr += 2;
132  host  = dstr;
133
134  if ( (tmp = strstr(dstr, "/")) == NULL )
135   return -1;
136
137  *tmp++ = 0;
138  dstr   = tmp;
139
140  if ( (tmp = strstr(dstr, "#")) != NULL )
141   *tmp = 0;
142 } else {
143  ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type);
144  if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) {
145   dstr = odef->d.file;
146   host = "localhost";
147
148   for (; *dstr == '/'; dstr++);
149
150  } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) {
151   dstr = ""; // index document
152   host = odef->d.socket.host;
153  } else {
154   return -1;
155  }
156 }
157
158 if ( (tmp = strstr(host, "@")) != NULL ) {
159  userpass.user = host;
160  *tmp = 0;
161  host = tmp + 1;
162  if ( (tmp = strstr(userpass.user, ":")) != NULL ) {
163   *tmp = 0;
164   userpass.pass = tmp + 1;
165  }
166 }
167
168 ROAR_DBG("roar_vio_open_proto(*) = ?");
169 ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s', userpass={.user='%s', .pass='%s'}", proto, host, dstr, userpass.user, userpass.pass);
170
171 self->proto = proto;
172
173 switch (proto) {
174  case ROAR_VIO_PROTO_P_HTTP:
175  case ROAR_VIO_PROTO_P_ICY:
176    return roar_vio_open_proto_http(calls, dst, host, dstr, userpass.user != NULL ? &userpass : NULL);
177   break;
178  case ROAR_VIO_PROTO_P_GOPHER:
179    return roar_vio_open_proto_gopher(calls, dst, host, dstr);
180   break;
181 }
182
183 ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol");
184 return -1;
185#else
186 return -1;
187#endif
188}
189
190#ifndef ROAR_WITHOUT_VIO_PROTO
191ssize_t roar_vio_proto_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
192 struct roar_vio_proto * self = vio->inst;
193 ssize_t ret;
194 ssize_t have = 0;
195 size_t  len;
196
197 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
198
199 if ( self->reader.buffer != NULL ) {
200  len = count;
201  if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) {
202   // This is very bad.
203   return -1;
204  }
205
206  if ( len ) {
207   have   = len;
208   buf   += len;
209   count -= len;
210  }
211 }
212
213 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
214
215 if ( count == 0 )
216  return have;
217
218 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count);
219
220 if ( (ret = roar_vio_read(self->next, buf, count)) == -1 )
221  return ret;
222
223 return have + ret;
224}
225
226ssize_t roar_vio_proto_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
227 struct roar_vio_proto * self = vio->inst;
228
229 return roar_vio_write(self->next, buf, count);
230}
231
232// TODO: this is currently not implemented as this is hard to implement with buffers:
233off_t   roar_vio_proto_lseek   (struct roar_vio_calls * vio, off_t offset, int whence);
234
235int     roar_vio_proto_nonblock(struct roar_vio_calls * vio, int state) {
236 struct roar_vio_proto * self = vio->inst;
237
238 /* we can simply use the next layer's nonblock as all we do in addtion *
239  * to call there functions are our buffers which do not block normaly  */
240
241 return roar_vio_nonblock(self->next, state);
242}
243
244int     roar_vio_proto_sync    (struct roar_vio_calls * vio) {
245 struct roar_vio_proto * self = vio->inst;
246
247 return roar_vio_sync(self->next);
248}
249
250int     roar_vio_proto_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
251 struct roar_vio_proto * self = vio->inst;
252
253 if (vio == NULL || cmd == -1)
254  return -1;
255
256 ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
257
258 switch (cmd) {
259  case ROAR_VIO_CTL_GET_NAME:
260    if ( data == NULL )
261     return -1;
262
263    switch (self->proto) {
264     case ROAR_VIO_PROTO_P_HTTP:
265       *(char**)data = "http";
266      break;
267     case ROAR_VIO_PROTO_P_GOPHER:
268       *(char**)data = "gopher";
269      break;
270     case ROAR_VIO_PROTO_P_ICY:
271       *(char**)data = "icy";
272      break;
273     default:
274       *(char**)data = "proto";
275      break;
276    }
277    return 0;
278   break;
279  case ROAR_VIO_CTL_GET_NEXT:
280    *(struct roar_vio_calls **)data = self->next;
281    return 0;
282   break;
283  case ROAR_VIO_CTL_SET_NEXT:
284    self->next = *(struct roar_vio_calls **)data;
285    return 0;
286   break;
287  case ROAR_VIO_CTL_GET_MIMETYPE:
288    if ( data == NULL )
289     return -1;
290
291    if ( self->content_type == NULL )
292     return -1;
293
294    *(char**)data = self->content_type;
295    return 0;
296   break;
297  case ROAR_VIO_CTL_GET_FH:
298  case ROAR_VIO_CTL_GET_SELECT_FH:
299    if ( self->reader.buffer == NULL && self->writer.buffer == NULL )
300     return roar_vio_ctl(self->next, cmd, data);
301    return -1;
302   break;
303  case ROAR_VIO_CTL_GET_READ_FH:
304  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
305    if ( self->reader.buffer == NULL )
306     return roar_vio_ctl(self->next, cmd, data);
307    return -1;
308   break;
309  case ROAR_VIO_CTL_GET_WRITE_FH:
310  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
311    if ( self->writer.buffer == NULL )
312     return roar_vio_ctl(self->next, cmd, data);
313    return -1;
314   break;
315 }
316
317 return roar_vio_ctl(self->next, cmd, data);
318}
319
320int     roar_vio_proto_close   (struct roar_vio_calls * vio) {
321 struct roar_vio_proto * self = vio->inst;
322
323 if ( roar_vio_close(self->next) == -1 )
324  return -1;
325
326 if ( self->content_type != NULL )
327  roar_mm_free(self->content_type);
328
329 roar_mm_free(self);
330
331 return 0;
332}
333
334static int _parse_header(struct roar_keyval * kv, char ** buf, int * aligned, char * endofheader) {
335 char * p = *buf;
336 char   c = 0;
337
338 if ( !(*aligned) ) {
339  for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
340  p++;
341  if ( *p == '\n' )
342   p++;
343 }
344
345 if ( p >= endofheader )
346  return -1;
347
348/*
349 if ( *p == '\r' || *p == '\n' )
350  return 0;
351 */
352
353 kv->key = p;
354
355 for (; *p != 0 && *p != '\r' && *p != '\n' && *p != ':'; p++);
356
357 if ( *p == 0 )
358  return -1;
359
360 if ( p >= endofheader )
361  return -1;
362
363 c = *p;
364 *p = 0;
365
366 if ( c == '\r' && *(p+1) == '\n' )
367  p++;
368
369 p++;
370
371 if ( c == '\r' || c == '\n' ) {
372  if ( *(kv->key) == '\r' || *(kv->key) == '\n' )
373   return 0;
374//  printf("Key-only\n");
375  kv->value = kv->key;
376  kv->key   = NULL;
377  *buf = p;
378  return 1;
379 }
380
381 for (; *p == ' '; p++);
382
383 if ( *p == 0 )
384  return -1;
385
386 kv->value = p;
387
388 for (; *p != 0 && *p != '\r' && *p != '\n'; p++);
389
390 if ( *p == 0 )
391  return -1;
392
393 if ( p >= endofheader )
394  return -1;
395
396 c = *p;
397 *p = 0;
398
399 if ( c == '\r' && *(p+1) == '\n' )
400  p++;
401
402 p++;
403
404 *buf = p;
405
406 if ( c == '\r' || c == '\n' ) {
407//  printf("aligned\n");
408  *aligned = 1;
409  p++;
410 } else {
411//  printf("non-aligned(c=0x%x)\n", (int)c);
412  *aligned = 0;
413 }
414
415 if ( *(kv->key) != 0 )
416  return 1;
417
418 return 0;
419}
420
421static void _handle_header (struct roar_vio_proto * self, struct roar_keyval * kv) {
422 ROAR_DBG("_handle_header(*): Header: key='%s', value='%s'", kv->key, kv->value);
423
424 if ( kv->key == NULL || kv->value == NULL )
425  return;
426
427 if ( !strcasecmp(kv->key, "Content-Type") ) {
428  if ( self->content_type != NULL )
429   roar_mm_free(self->content_type);
430
431  self->content_type = roar_mm_strdup(kv->value);
432 }
433}
434
435static inline char * _up2http_auth (struct roar_userpass * up) {
436 char * inbuf, * outbuf;
437 size_t inlen, outlen;
438 ssize_t ret;
439 //Authorization: Basic dXNlcjpwdw==
440
441 if ( up == NULL )
442  return NULL;
443
444 if ( up->subtype != -1 )
445  return NULL;
446
447 if ( up->user == NULL || up->pass == NULL )
448  return NULL;
449
450 inlen = roar_mm_strlen(up->user) + roar_mm_strlen(up->pass) + 2;
451 inbuf = roar_mm_malloc(inlen);
452 if ( inbuf == NULL )
453  return NULL;
454
455 inbuf[0] = 0;
456 roar_mm_strlcat(inbuf, up->user, inlen);
457 roar_mm_strlcat(inbuf, ":", inlen);
458 roar_mm_strlcat(inbuf, up->pass, inlen);
459
460 outlen = ((inlen * 3) / 2) + 3 /* padding... */ + 6 /* 'Basic ' */;
461 outbuf = roar_mm_malloc(outlen);
462 if ( outbuf == NULL ) {
463  roar_mm_free(inbuf);
464  return NULL;
465 }
466
467 strncpy(outbuf, "Basic ", 7);
468
469 ROAR_DBG("_up2http_auth(up=%p{.subtype=%i, .user='%s', .pass='%s'): inbuf='%s', outbuf='%s'", up, up->subtype, up->user, up->pass, inbuf, outbuf);
470 ret = roar_base64_encode(NULL, outbuf + 6, outlen - 6, inbuf, inlen - 1, NULL, 1);
471 ROAR_DBG("_up2http_auth(up=%p{.subtype=%i, .user='%s', .pass='%s'): inbuf='%s', outbuf='%s'", up, up->subtype, up->user, up->pass, inbuf, outbuf);
472
473 roar_mm_free(inbuf);
474
475 if ( ret == -1 ) {
476  roar_mm_free(outbuf);
477  return NULL;
478 }
479
480 return outbuf;
481}
482
483int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file, struct roar_userpass * up) {
484 struct roar_keyval kv;
485 struct roar_vio_proto * self;
486 struct roar_buffer * bufbuf;
487 void * vpbuf;
488 char * authbuf;
489 char * buf;
490 char * endofheader = NULL;
491 char * p;
492 char b0[80], b1[80];
493 int  status;
494 int  len;
495 int  oeflen = 4;
496 int  aligned = 1;
497
498 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
499
500 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
501  return -1;
502
503 self         = calls->inst;
504 calls->write = NULL; // Disable write as we do not support this
505
506 if ( roar_buffer_new_data(&bufbuf, 1024, &vpbuf) == -1 )
507  return -1;
508
509 buf = vpbuf;
510
511 ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
512
513 roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file);
514 roar_vio_printf(dst, "Host: %s\r\n", host);
515 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n");
516 roar_vio_printf(dst, "Connection: close\r\n");
517 if ( up != NULL ) {
518  if ( (authbuf = _up2http_auth(up)) != NULL ) {
519   roar_vio_printf(dst, "Authorization: %s\r\n", authbuf);
520   roar_mm_free(authbuf);
521  }
522 }
523 roar_vio_printf(dst, "\r\n");
524
525 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
526
527 roar_vio_sync(dst);
528
529 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
530
531 if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) {
532  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
533  roar_buffer_free(bufbuf);
534  return -1;
535 }
536
537 ROAR_DBG("roar_vio_open_proto_http(*): got %i bytes from server.", len);
538
539 buf[len] = 0;
540
541 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
542
543 if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) {
544  ROAR_DBG("roar_vio_open_proto_http(*) = -1");
545  roar_buffer_free(bufbuf);
546  return -1;
547 }
548
549 ROAR_DBG("roar_vio_open_proto_http(*): b0='%s'", b0);
550
551 ROAR_DBG("roar_vio_open_proto_http(*) = ?");
552
553 if ( status != 200 ) {
554  ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status);
555  roar_buffer_free(bufbuf);
556  return -1;
557 }
558
559 ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status);
560// ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf);
561
562 endofheader = strstr(buf, "\r\n\r\n");
563 if ( endofheader == NULL ) {
564  endofheader = strstr(buf, "\n\n");
565  oeflen = 2;
566 }
567
568 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
569
570 p = buf;
571 while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
572  if ( aligned )
573   _handle_header(self, &kv);
574
575 while ( endofheader == NULL ) {
576  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
577   return -1;
578
579  buf[len] = 0;
580  endofheader = strstr(buf, "\r\n\r\n");
581  if ( endofheader == NULL ) {
582   endofheader = strstr(buf, "\n\n");
583   oeflen = 2;
584  }
585
586/* Doesn't work good.
587  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
588   if ( aligned )
589    _handle_header(self, &kv);
590*/
591
592  p = buf;
593  while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 )
594   if ( aligned )
595    _handle_header(self, &kv);
596
597  ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
598 }
599
600 ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader);
601 ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf);
602
603 if ( (endofheader - buf) == (len - oeflen) ) {
604  roar_buffer_free(bufbuf);
605  bufbuf = NULL;
606 }
607
608 if ( bufbuf != NULL ) {
609  roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen);
610  roar_buffer_set_len(bufbuf,    len - (endofheader - buf + oeflen) - 0 /* ??? */);
611 }
612 self->reader.buffer = bufbuf;
613
614/*
615 if ( !strcmp((buf+len)-4, "\r\n\r\n") )
616  return 0;
617
618 while (*buf != '\r' && *buf != '\n') {
619  if ( (len = roar_vio_read(dst, buf, 1023)) < 1 )
620   return -1;
621 }
622*/
623
624 return 0;
625}
626
627int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) {
628 struct roar_vio_proto * self;
629 char type;
630 const char * mime = NULL;
631
632 if ( calls == NULL || dst == NULL || host == NULL || file == NULL )
633  return -1;
634
635 self         = calls->inst;
636 calls->write = NULL; // Disable write as we do not support this
637
638 ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file);
639
640 type = file[0];
641
642 file++;
643
644 ROAR_DBG("roar_vio_open_proto_gopher(*): type='%c'", type);
645
646 switch (type) {
647  case ROAR_GOPHER_TYPE_FILE:
648    mime = "text/plain";
649   break;
650  case ROAR_GOPHER_TYPE_DIR:
651    mime = "inode/directory";
652   break;
653  case ROAR_GOPHER_TYPE_BIN:
654    mime = "application/octet-stream";
655   break;
656  case ROAR_GOPHER_TYPE_GIF:
657    mime = "image/gif";
658   break;
659  case ROAR_GOPHER_TYPE_HTML:
660    mime = "text/html";
661   break;
662 }
663
664 if ( mime != NULL ) {
665  self->content_type = roar_mm_strdup(mime);
666 }
667
668 roar_vio_printf(dst, "%s\r\n", file);
669
670 roar_vio_sync(dst); // for encryption/compression layers
671
672 return 0;
673}
674#endif
675
676//ll
Note: See TracBrowser for help on using the repository browser.