source: roaraudio/libroar/vio_dstr.c @ 5270:e25346c13638

Last change on this file since 5270:e25346c13638 was 5270:e25346c13638, checked in by phi, 12 years ago

fixed some gcc -Wextra warnings

File size: 26.7 KB
Line 
1//vio_dstr.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_DSTR
39static struct _roar_vio_dstr_type {
40 const int    id;
41 const char * name;
42 int (* setdef) (struct roar_vio_dstr_chain * cur,   struct roar_vio_dstr_chain * next);
43 int (* openvio)(struct roar_vio_calls      * calls, struct roar_vio_calls      * dst, struct roar_vio_dstr_chain * cur);
44 int    pdeftype[16];
45} _roar_vio_dstr_objs[] = {
46/*
47grep '^#define ROAR_VIO_DSTR_OBJT_' vio_dstr.h | cut -d' ' -f2 | while read objt; do name=`cut -d_ -f5,6,7,8,9,10 <<<$objt | tr A-Z a-z`; echo -e " {$objt,\t \"$name\","; echo "      {ROAR_VIO_DEF_TYPE_EOL}},"; done;
48*/
49 {ROAR_VIO_DSTR_OBJT_FILE,       "file",
50      NULL, NULL,
51      {ROAR_VIO_DEF_TYPE_EOL}},
52 {ROAR_VIO_DSTR_OBJT_FH,         "fh",
53      NULL, NULL,
54      {ROAR_VIO_DEF_TYPE_EOL}},
55 {ROAR_VIO_DSTR_OBJT_FD,         "fd",
56      NULL, NULL,
57      {ROAR_VIO_DEF_TYPE_EOL}},
58 {ROAR_VIO_DSTR_OBJT_SOCKETFH,   "socketfh",
59      NULL, NULL,
60      {ROAR_VIO_DEF_TYPE_EOL}},
61 {ROAR_VIO_DSTR_OBJT_PASS,       "pass",
62      NULL, NULL,
63      {ROAR_VIO_DEF_TYPE_EOL}},
64 {ROAR_VIO_DSTR_OBJT_RE,         "re",
65      NULL, NULL,
66      {ROAR_VIO_DEF_TYPE_EOL}},
67 {ROAR_VIO_DSTR_OBJT_JUMBO,      "jumbo", /* TODO */
68      NULL, NULL,
69      {ROAR_VIO_DEF_TYPE_EOL}},
70 {ROAR_VIO_DSTR_OBJT_EXEC,       "exec",
71      NULL, NULL,
72      {ROAR_VIO_DEF_TYPE_EOL}},
73/* special devices */
74 {ROAR_VIO_DSTR_OBJT_NULL,       "null",
75      NULL, NULL,
76      {ROAR_VIO_DEF_TYPE_EOL}},
77 {ROAR_VIO_DSTR_OBJT_ZERO,       "zero",
78      NULL, NULL,
79      {ROAR_VIO_DEF_TYPE_EOL}},
80 {ROAR_VIO_DSTR_OBJT_FULL,       "full",
81      NULL, NULL,
82      {ROAR_VIO_DEF_TYPE_EOL}},
83
84 {ROAR_VIO_DSTR_OBJT_SOCKET,     "socket",
85      NULL, NULL,
86      {ROAR_VIO_DEF_TYPE_EOL}},
87 {ROAR_VIO_DSTR_OBJT_UNIX,       "unix",
88      NULL, NULL,
89      {ROAR_VIO_DEF_TYPE_EOL}},
90 {ROAR_VIO_DSTR_OBJT_DECNET,     "decnet",
91      NULL, NULL,
92      {ROAR_VIO_DEF_TYPE_EOL}},
93 {ROAR_VIO_DSTR_OBJT_TCP,        "tcp",
94      NULL, NULL,
95      {ROAR_VIO_DEF_TYPE_EOL}},
96 {ROAR_VIO_DSTR_OBJT_UDP,        "udp",
97      NULL, NULL,
98      {ROAR_VIO_DEF_TYPE_EOL}},
99 {ROAR_VIO_DSTR_OBJT_TCP6,       "tcp6",
100      NULL, NULL,
101      {ROAR_VIO_DEF_TYPE_EOL}},
102 {ROAR_VIO_DSTR_OBJT_UDP6,       "udp6",
103      NULL, NULL,
104      {ROAR_VIO_DEF_TYPE_EOL}},
105
106 {ROAR_VIO_DSTR_OBJT_SOCKS,      "socks",
107      NULL, NULL,
108      {ROAR_VIO_DEF_TYPE_EOL}},
109 {ROAR_VIO_DSTR_OBJT_SOCKS4,     "socks4",
110      NULL, NULL,
111      {ROAR_VIO_DEF_TYPE_EOL}},
112 {ROAR_VIO_DSTR_OBJT_SOCKS4A,    "socks4a",
113      NULL, NULL,
114      {ROAR_VIO_DEF_TYPE_EOL}},
115 {ROAR_VIO_DSTR_OBJT_SOCKS4D,    "socks4d",
116      NULL, NULL,
117      {ROAR_VIO_DEF_TYPE_EOL}},
118 {ROAR_VIO_DSTR_OBJT_SOCKS5,     "socks5",
119      NULL, NULL,
120      {ROAR_VIO_DEF_TYPE_EOL}},
121 {ROAR_VIO_DSTR_OBJT_SSH,        "ssh",
122      NULL, NULL,
123      {ROAR_VIO_DEF_TYPE_EOL}},
124
125 {ROAR_VIO_DSTR_OBJT_HTTP09,     "http09",
126      NULL, NULL,
127      {ROAR_VIO_DEF_TYPE_EOL}},
128 {ROAR_VIO_DSTR_OBJT_HTTP10,     "http10",
129      NULL, NULL,
130      {ROAR_VIO_DEF_TYPE_EOL}},
131 {ROAR_VIO_DSTR_OBJT_HTTP11,     "http11",
132      NULL, NULL,
133      {ROAR_VIO_DEF_TYPE_EOL}},
134 {ROAR_VIO_DSTR_OBJT_HTTP,       "http",
135      NULL, NULL,
136      {ROAR_VIO_DEF_TYPE_EOL}},
137 {ROAR_VIO_DSTR_OBJT_GOPHER,     "gopher",
138      NULL, NULL,
139      {ROAR_VIO_DEF_TYPE_EOL}},
140 {ROAR_VIO_DSTR_OBJT_GOPHER_PLUS,"gopher+",
141      NULL, NULL,
142      {ROAR_VIO_DEF_TYPE_EOL}},
143 {ROAR_VIO_DSTR_OBJT_ICY,        "icy",
144      NULL, NULL,
145      {ROAR_VIO_DEF_TYPE_EOL}},
146 {ROAR_VIO_DSTR_OBJT_RTP2,       "rtp2",
147      NULL, NULL,
148      {ROAR_VIO_DEF_TYPE_EOL}},
149 {ROAR_VIO_DSTR_OBJT_RTP,        "rtp",
150      NULL, NULL,
151      {ROAR_VIO_DEF_TYPE_EOL}},
152
153 {ROAR_VIO_DSTR_OBJT_GZIP,       "gzip",
154      NULL, NULL,
155      {ROAR_VIO_DEF_TYPE_EOL}},
156 {ROAR_VIO_DSTR_OBJT_ZLIB,       "zlib",
157      NULL, NULL,
158      {ROAR_VIO_DEF_TYPE_EOL}},
159 {ROAR_VIO_DSTR_OBJT_BZIP2,      "bzip2",
160      NULL, NULL,
161      {ROAR_VIO_DEF_TYPE_EOL}},
162
163 {ROAR_VIO_DSTR_OBJT_PGP,        "pgp",
164      NULL, NULL,
165      {ROAR_VIO_DEF_TYPE_EOL}},
166 {ROAR_VIO_DSTR_OBJT_PGP_ENC,    "pgp_enc",
167      NULL, NULL,
168      {ROAR_VIO_DEF_TYPE_EOL}},
169 {ROAR_VIO_DSTR_OBJT_PGP_STORE,  "pgp_store",
170      NULL, NULL,
171      {ROAR_VIO_DEF_TYPE_EOL}},
172 {ROAR_VIO_DSTR_OBJT_SSL1,       "ssl1",
173      NULL, NULL,
174      {ROAR_VIO_DEF_TYPE_EOL}},
175 {ROAR_VIO_DSTR_OBJT_SSL2,       "ssl2",
176      NULL, NULL,
177      {ROAR_VIO_DEF_TYPE_EOL}},
178 {ROAR_VIO_DSTR_OBJT_SSL3,       "ssl3",
179      NULL, NULL,
180      {ROAR_VIO_DEF_TYPE_EOL}},
181 {ROAR_VIO_DSTR_OBJT_TLS,        "tls",
182      NULL, NULL,
183      {ROAR_VIO_DEF_TYPE_EOL}},
184 {ROAR_VIO_DSTR_OBJT_SSLTLS,     "ssltls",
185      NULL, NULL,
186      {ROAR_VIO_DEF_TYPE_EOL}},
187/* Random numbers */
188 {ROAR_VIO_DSTR_OBJT_NRANDOM,    "nrandom",
189      NULL, NULL,
190      {ROAR_VIO_DEF_TYPE_EOL}},
191 {ROAR_VIO_DSTR_OBJT_URANDOM,    "urandom",
192      NULL, NULL,
193      {ROAR_VIO_DEF_TYPE_EOL}},
194 {ROAR_VIO_DSTR_OBJT_SRANDOM,    "srandom",
195      NULL, NULL,
196      {ROAR_VIO_DEF_TYPE_EOL}},
197
198 {ROAR_VIO_DSTR_OBJT_TRANSCODE,  "transcode", /* TODO  */
199      NULL, NULL,
200      {ROAR_VIO_DEF_TYPE_EOL}},
201
202 {ROAR_VIO_DSTR_OBJT_RAUM,       "raum",      /* TODO */
203      NULL, NULL,
204      {ROAR_VIO_DEF_TYPE_EOL}},
205 {ROAR_VIO_DSTR_OBJT_OGG,        "ogg",       /* TODO */
206      NULL, NULL,
207      {ROAR_VIO_DEF_TYPE_EOL}},
208 {ROAR_VIO_DSTR_OBJT_TAR,        "tar",       /* TODO */
209      NULL, NULL,
210      {ROAR_VIO_DEF_TYPE_EOL}},
211
212
213 {ROAR_VIO_DSTR_OBJT_MAGIC,      "magic",
214      NULL, NULL,
215      {ROAR_VIO_DEF_TYPE_EOL}},
216 {ROAR_VIO_DSTR_OBJT_TANTALOS,   "tantalos",
217      NULL, NULL,
218      {ROAR_VIO_DEF_TYPE_EOL}},
219
220 {ROAR_VIO_DSTR_OBJT_INTERNAL, "INTERNAL",
221      NULL, NULL,
222      {ROAR_VIO_DEF_TYPE_FILE, ROAR_VIO_DEF_TYPE_SOCKET, ROAR_VIO_DEF_TYPE_FH, ROAR_VIO_DEF_TYPE_SOCKETFH,
223       ROAR_VIO_DEF_TYPE_EOL}},
224 {ROAR_VIO_DSTR_OBJT_EOL, NULL, NULL, NULL, {ROAR_VIO_DEF_TYPE_EOL}}
225};
226
227int     roar_vio_dstr_get_type(const char * str) {
228 int i;
229
230 for (i = 0; _roar_vio_dstr_objs[i].id != ROAR_VIO_DSTR_OBJT_EOL; i++) {
231  if ( strcasecmp(_roar_vio_dstr_objs[i].name, str) == 0 )
232   return _roar_vio_dstr_objs[i].id;
233 }
234
235 return -1;
236}
237
238struct _roar_vio_dstr_type * roar_vio_dstr_get_by_type (int type) {
239 int i;
240
241 for (i = 0; _roar_vio_dstr_objs[i].id != ROAR_VIO_DSTR_OBJT_EOL; i++) {
242  if ( _roar_vio_dstr_objs[i].id == type )
243   return &(_roar_vio_dstr_objs[i]);
244 }
245
246 return NULL;
247}
248
249const char *  roar_vio_dstr_get_name(const int type) {
250 struct _roar_vio_dstr_type * ret;
251
252 if ( (ret = roar_vio_dstr_get_by_type(type)) != NULL )
253  return ret->name;
254
255 if ( type == ROAR_VIO_DSTR_OBJT_EOL )
256  return "<<EOL>>";
257
258 return NULL;
259}
260
261int     roar_vio_dstr_register_type(int   type,
262                                    char *name,
263                                    int (*setdef) (struct roar_vio_dstr_chain * cur,
264                                                   struct roar_vio_dstr_chain * next),
265                                    int (*openvio)(struct roar_vio_calls      * calls,
266                                                   struct roar_vio_calls      * dst,
267                                                   struct roar_vio_dstr_chain * cur)) {
268 struct _roar_vio_dstr_type * ret;
269
270 if ( (ret = roar_vio_dstr_get_by_type(type)) == NULL ) /* we can currently not register new types */
271  return -1;
272
273
274 // check if things are allready set, we do not want to allow overwrite here.
275 if ( setdef != NULL && ret->setdef != NULL )
276  return -1;
277
278 if ( openvio != NULL && ret->openvio != NULL )
279  return -1;
280
281 if ( setdef != NULL )
282  ret->setdef = setdef;
283
284 if ( openvio != NULL )
285  ret->openvio = openvio;
286
287 return 0;
288}
289
290static void _roar_vio_dstr_init_otherlibs (void) {
291 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroardsp");
292 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroareio");
293 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroarlight");
294 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroarmidi");
295}
296
297#endif
298
299int     roar_vio_dstr_init_defaults (struct roar_vio_defaults * def, int type, int o_flags, mode_t o_mode) {
300 if ( def == NULL )
301  return -1;
302
303 memset(def, 0, sizeof(struct roar_vio_defaults));
304
305 def->type    = type;
306 def->o_flags = o_flags;
307 def->o_mode  = o_mode;
308
309 return 0;
310}
311
312int     roar_vio_dstr_init_defaults_c (struct roar_vio_defaults * def, int type, struct roar_vio_defaults * odef, int o_flags) {
313 if ( o_flags < 1 )
314  o_flags = O_RDONLY;
315
316 if ( odef == NULL ) {
317  return roar_vio_dstr_init_defaults(def, type, o_flags, 0644);
318 } else {
319  return roar_vio_dstr_init_defaults(def, type, odef->o_flags, odef->o_mode);
320 }
321}
322
323#ifndef ROAR_WITHOUT_VIO_DSTR
324#ifdef ROAR_HAVE_IO_POSIX
325static int _open_file(struct roar_vio_calls * calls, const char * filename, int flags, mode_t mode) {
326 int fh;
327
328 if ( calls == NULL || filename == NULL ) {
329  roar_err_set(ROAR_ERROR_FAULT);
330  return -1;
331 }
332
333#ifdef ROAR_TARGET_WIN32
334 flags |= O_BINARY;
335#endif
336
337 roar_err_clear_all();
338 if ( (fh = open(filename, flags, mode)) == -1 ) {
339  roar_err_update();
340  return -1;
341 }
342
343 if ( roar_vio_open_fh(calls, fh) == -1 ) {
344  close(fh);
345  roar_err_to_errno();
346  return -1;
347 }
348
349 roar_err_update();
350 return 0;
351}
352#endif
353
354int     roar_vio_open_default (struct roar_vio_calls * calls, struct roar_vio_defaults * def, char * opts) {
355 ROAR_DBG("roar_vio_open_default(calls=%p, def=%p, opts='%s') = ?", calls, def, opts);
356
357 if ( calls == NULL || def == NULL ) {
358  roar_err_set(ROAR_ERROR_FAULT);
359  return -1;
360 }
361
362 switch (def->type) {
363  case ROAR_VIO_DEF_TYPE_NONE:
364   break;
365  case ROAR_VIO_DEF_TYPE_FILE:
366    if ( _open_file(calls, def->d.file, def->o_flags, def->o_mode) == -1 ) {
367     ROAR_DBG("roar_vio_open_default(*): Can not open file: %i", roar_error);
368     return -1;
369    }
370   break;
371  case ROAR_VIO_DEF_TYPE_SOCKET:
372     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
373      return -1;
374   break;
375  case ROAR_VIO_DEF_TYPE_FH:
376    if ( roar_vio_open_fh(calls, def->d.fh) == -1 )
377     return -1;
378   break;
379  case ROAR_VIO_DEF_TYPE_SOCKETFH:
380    if ( roar_vio_open_fh_socket(calls, def->d.fh) == -1 )
381     return -1;
382   break;
383  default:
384    return -1;
385 }
386
387 return 0;
388}
389#endif
390
391int     roar_vio_open_dstr_simple(struct roar_vio_calls * calls, const char * dstr, int o_flags) {
392 struct roar_vio_defaults def;
393
394 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, o_flags, 0644) == -1 )
395  return -1;
396
397 if ( roar_vio_open_dstr_vio(calls, dstr, &def, 1, NULL) == -1 )
398  return -1;
399
400 return 0;
401}
402
403int     roar_vio_open_dstr    (struct roar_vio_calls * calls, const char * dstr, struct roar_vio_defaults * def, int dnum) {
404 return roar_vio_open_dstr_vio(calls, dstr, def, dnum, NULL);
405}
406
407#define _ret(x) do { int _err = roar_error; roar_mm_free(dstr_copy); roar_error = _err; roar_err_to_errno(); return (x); } while (0)
408
409int     roar_vio_open_dstr_vio(struct roar_vio_calls * calls,
410                               const char * dstr, struct roar_vio_defaults * def, int dnum,
411                               struct roar_vio_calls * vio) {
412#ifndef ROAR_WITHOUT_VIO_DSTR
413 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
414 char * dstr_copy;
415 char * next;
416 char * this;
417 char * name;
418 char * opts;
419 char * dst;
420 char * c;
421 int    inopts;
422 int    type;
423 int    cc = 1; // current chain element
424
425 if ( calls == NULL || dstr == NULL )
426  return -1;
427
428 if ( dnum != 0 && def == NULL )
429  return -1;
430
431 if ( (dstr_copy = roar_mm_strdup(dstr)) == NULL )
432  return -1;
433
434 memset(chain, 0, sizeof(chain));
435
436 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
437
438 next = dstr_copy;
439
440 while (next != NULL) {
441  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
442   _ret(-1);
443  }
444
445  this = next;
446  next = strstr(next, "##");
447
448  if (next != NULL) {
449   *next = 0;
450   next += 2;
451  }
452
453  // we have the current token in 'this'.
454
455  opts   = NULL;
456  dst    = NULL;
457
458  if ( strstr(this, ":") != NULL ) {
459   name   = this;
460   inopts = 0;
461   for (c = this; *c != 0; c++) {
462    if ( *c == '[' ) {
463     *c     = 0;
464     opts   = c + 1;
465     inopts = 1;
466    } else if ( *c == ']' &&  inopts ) {
467     *c     = 0;
468     inopts = 0;
469    } else if ( *c == ':' && !inopts ) {
470     *c     = 0;
471     dst    = *(c+1) == 0 ? NULL : c + 1;
472     break;
473    }
474   }
475  } else {
476   // we need to guess that this is here...
477   // currently we guess this is a file in all cases
478   name = "file";
479   dst  = this;
480  }
481
482  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
483
484  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
485   _ret(-1);
486  }
487
488  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
489
490  chain[cc].type     = type;
491  chain[cc].opts     = opts;
492  chain[cc].dst      = dst;
493  chain[cc].def      = NULL;
494  chain[cc].vio      = NULL;
495  chain[cc].need_vio = -1;
496  cc++;
497
498 }
499
500 chain[cc].type = ROAR_VIO_DSTR_OBJT_EOL;
501
502 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
503
504 if ( roar_vio_dstr_parse_opts(chain) == -1 ) {
505  _ret(-1);
506 }
507
508 if ( roar_vio_dstr_set_defaults(chain, cc, def, dnum) == -1 ) {
509  _ret(-1);
510 }
511
512 if ( roar_vio_dstr_build_chain(chain, calls, vio) == -1 ) {
513  _ret(-1);
514 }
515
516 _ret(0);
517#else
518 return -1;
519#endif
520}
521
522#undef _ret
523
524#ifndef ROAR_WITHOUT_VIO_DSTR
525int     roar_vio_dstr_parse_opts(struct roar_vio_dstr_chain * chain) {
526 if ( chain == NULL )
527  return -1;
528
529 // TODO: we should add some code here later...
530
531 return 0;
532}
533
534int     roar_vio_dstr_set_defaults(struct roar_vio_dstr_chain * chain, int len, struct roar_vio_defaults * def, int dnum) {
535 struct _roar_vio_dstr_type * type;
536 struct roar_vio_dstr_chain * c, * next;
537 int i;
538 int tmp[8];
539
540 if ( chain == NULL )
541  return -1;
542
543 if ( def == NULL && dnum != 0 )
544  return -1;
545
546 if ( dnum > 1 ) /* currently not supported */
547  return -1;
548
549 if ( dnum == 0 )
550  def = NULL;
551
552 chain[len].def = def;
553
554 for (i = len; i >= 0; i--) {
555  c    = &chain[i];
556
557  if ( i > 0 ) {
558   next = &chain[i-1];
559  } else {
560   next = NULL;
561
562   if ( c->type != ROAR_VIO_DSTR_OBJT_INTERNAL )
563    return -1;
564  }
565
566  memset(tmp, 0, sizeof(tmp));
567
568  ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s)", i, c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
569  ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): c->def=%p, c->def->type=%i", i, c->type & 0xFFFF,
570                   roar_vio_dstr_get_name(c->type), c->def, c->def == NULL ? -1 : c->def->type);
571
572  c->need_vio = 1;
573
574  switch (c->type) {
575   case ROAR_VIO_DSTR_OBJT_INTERNAL:
576     c->need_vio = 0;
577    break;
578   case ROAR_VIO_DSTR_OBJT_EOL:
579     tmp[0] = 1;
580   case ROAR_VIO_DSTR_OBJT_PASS:
581   case ROAR_VIO_DSTR_OBJT_RE:
582   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
583   case ROAR_VIO_DSTR_OBJT_GZIP:
584   case ROAR_VIO_DSTR_OBJT_ZLIB:
585   case ROAR_VIO_DSTR_OBJT_BZIP2:
586   case ROAR_VIO_DSTR_OBJT_PGP:
587   case ROAR_VIO_DSTR_OBJT_PGP_ENC:
588   case ROAR_VIO_DSTR_OBJT_PGP_STORE:
589   case ROAR_VIO_DSTR_OBJT_SSL1:
590   case ROAR_VIO_DSTR_OBJT_SSL2:
591   case ROAR_VIO_DSTR_OBJT_SSL3:
592   case ROAR_VIO_DSTR_OBJT_TLS:
593   case ROAR_VIO_DSTR_OBJT_MAGIC:
594     if ( tmp[0] )
595      c->need_vio = 0;
596
597     next->def = c->def;
598    break;
599   case ROAR_VIO_DSTR_OBJT_NULL:
600   case ROAR_VIO_DSTR_OBJT_ZERO:
601   case ROAR_VIO_DSTR_OBJT_FULL:
602   case ROAR_VIO_DSTR_OBJT_NRANDOM:
603   case ROAR_VIO_DSTR_OBJT_TANTALOS:
604     next->def = &(next->store_def);
605     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_NONE, NULL, -1);
606    break;
607   case ROAR_VIO_DSTR_OBJT_FILE:
608     if ( c->dst == NULL ) /* should we allow multible cascaed file: objects? */
609      return -1;
610
611     c->need_vio = 0;
612     next->def = &(next->store_def);
613     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
614
615     if ( c->dst[0] == '/' && c->dst[1] == '/' ) {
616      next->def->d.file = c->dst + 1;
617     } else {
618      next->def->d.file = c->dst;
619     }
620    break;
621   case ROAR_VIO_DSTR_OBJT_FH:
622     tmp[0] = 1;
623   case ROAR_VIO_DSTR_OBJT_SOCKETFH:
624     c->need_vio = 0;
625     next->def = &(next->store_def);
626
627     if ( c->def != NULL ) {
628      tmp[2] = c->def->o_flags;
629      tmp[3] = c->def->o_mode;
630     } else {
631      tmp[2] = O_RDONLY;
632      tmp[3] = 0644;
633     }
634
635     if ( !strcasecmp(c->dst, "stdin") ) {
636      tmp[1] = ROAR_STDIN;
637      tmp[2] = O_RDONLY;
638     } else if ( !strcasecmp(c->dst, "stdout") ) {
639      tmp[1] = ROAR_STDOUT;
640      tmp[2] = O_WRONLY;
641     } else if ( !strcasecmp(c->dst, "stderr") ) {
642      tmp[1] = ROAR_STDERR;
643      tmp[2] = O_WRONLY;
644     } else {
645      if ( sscanf(c->dst, "%i", &tmp[1]) != 1 )
646       return -1;
647     }
648
649     roar_vio_dstr_init_defaults(next->def, tmp[0] ? ROAR_VIO_DEF_TYPE_FH : ROAR_VIO_DEF_TYPE_SOCKETFH, tmp[2], tmp[3]);
650     next->def->d.fh = tmp[1];
651    break;
652#ifdef ROAR_HAVE_UNIX
653   case ROAR_VIO_DSTR_OBJT_UNIX:
654     c->need_vio = 0;
655     next->def = &(next->store_def);
656
657     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
658      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
659       return -1;
660
661      if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_UNIX, SOCK_STREAM, c->def) == -1 )
662       return -1;
663     } else {                // we have a destination? -> fast way
664      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
665       return -1;
666
667      if ( roar_vio_socket_init_unix_def(next->def, c->dst) == -1 )
668       return -1;
669     }
670    break;
671#endif
672   case ROAR_VIO_DSTR_OBJT_SOCKET:
673     c->need_vio = 0;
674     next->def = &(next->store_def);
675
676     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
677      return -1;
678
679     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, -1, SOCK_STREAM, c->def) == -1 )
680      return -1;
681    break;
682#ifdef ROAR_HAVE_LIBDNET
683   case ROAR_VIO_DSTR_OBJT_DECNET:
684     c->need_vio = 0;
685     next->def = &(next->store_def);
686
687     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
688      return -1;
689
690     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_DECnet, SOCK_STREAM, c->def) == -1 )
691      return -1;
692    break;
693#endif
694#ifdef ROAR_HAVE_IPV4
695   case ROAR_VIO_DSTR_OBJT_TCP:
696     c->need_vio = 0;
697     next->def = &(next->store_def);
698
699     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
700      return -1;
701
702     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_STREAM, c->def) == -1 )
703      return -1;
704    break;
705   case ROAR_VIO_DSTR_OBJT_UDP:
706     c->need_vio = 0;
707     next->def = &(next->store_def);
708
709     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
710      return -1;
711
712     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_DGRAM, c->def) == -1 )
713      return -1;
714    break;
715#endif
716#ifdef ROAR_HAVE_IPV6
717   case ROAR_VIO_DSTR_OBJT_TCP6:
718     c->need_vio = 0;
719     next->def = &(next->store_def);
720
721     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
722      return -1;
723
724     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET6, SOCK_STREAM, c->def) == -1 )
725      return -1;
726    break;
727   case ROAR_VIO_DSTR_OBJT_UDP6:
728     c->need_vio = 0;
729     next->def = &(next->store_def);
730
731     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
732      return -1;
733
734     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET6, SOCK_DGRAM, c->def) == -1 )
735      return -1;
736    break;
737#endif
738   case ROAR_VIO_DSTR_OBJT_HTTP09:
739   case ROAR_VIO_DSTR_OBJT_HTTP10:
740   case ROAR_VIO_DSTR_OBJT_HTTP11:
741     c->need_vio = 1;
742     next->def = &(next->store_def);
743
744     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 )
745      return -1;
746    break;
747   case ROAR_VIO_DSTR_OBJT_GOPHER:
748   case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
749     c->need_vio = 1;
750     next->def = &(next->store_def);
751
752     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 )
753      return -1;
754    break;
755   case ROAR_VIO_DSTR_OBJT_ICY:
756     c->need_vio = 1;
757     next->def = &(next->store_def);
758
759     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_ICY, c->def) == -1 )
760      return -1;
761    break;
762   default:
763     if ( (type = roar_vio_dstr_get_by_type(c->type)) == NULL ) {
764      return -1;
765     }
766
767     if ( type->setdef == NULL )
768      _roar_vio_dstr_init_otherlibs();
769
770     if ( type->setdef == NULL ) {
771      return -1;
772     }
773
774     if ( type->setdef(c, next) == -1 ) {
775      return -1;
776     }
777  }
778
779  if ( next != NULL ) {
780   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
781                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
782                    next->def, next->def == NULL ? -1 : next->def->type);
783   if ( next->def != NULL ) {
784    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
785                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
786                     next->def->o_flags);
787   }
788  } else {
789   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
790                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
791  }
792 }
793
794 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
795
796 return 0;
797}
798
799#define _ret(x) do { int _err = roar_error; roar_vio_close(calls); roar_error = _err; roar_err_to_errno(); ROAR_DBG("roar_vio_dstr_build_chain(*) = %i", (x)); return (x); } while(0)
800
801int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
802                                  struct roar_vio_calls * vio) {
803 struct _roar_vio_dstr_type * type;
804 struct roar_vio_dstr_chain * c;
805 struct roar_vio_defaults   * def;
806 struct roar_vio_calls      * tc, * prev;
807 int i;
808
809 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
810
811 if ( chain == NULL || calls == NULL )
812  return -1;
813
814 if ( roar_vio_open_stack2(calls, NULL) == -1 )
815  return -1;
816
817 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
818
819 if ( (def = chain->def) != NULL ) {
820  if ( (tc = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) {
821   _ret(-1);
822  }
823
824  if ( roar_vio_clear_calls(tc) == -1 ) {
825   roar_mm_free(tc);
826   _ret(-1);
827  }
828
829  if ( roar_vio_stack_add(calls, tc) == -1 ) {
830   _ret(-1);
831  }
832
833  if ( chain->opts == NULL ) {
834   if ( chain[1].type != ROAR_VIO_DSTR_OBJT_EOL ) {
835    chain->opts = chain[1].opts;
836   }
837  }
838
839  if ( roar_vio_open_default(tc, def, chain->opts) == -1 ) {
840   _ret(-1);
841  }
842
843  prev = tc;
844 } else {
845  prev = vio;
846 }
847
848 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
849  ROAR_DBG("roar_vio_dstr_build_chain(*): i=%i, c->type=0x%.4x(%s): need_vio=%i, def->o_flags=%i", i,
850                   c->type & 0xFFFF, roar_vio_dstr_get_name(c->type), c->need_vio, c->def->o_flags);
851
852  if ( c->need_vio ) {
853   if ( (tc = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) {
854    _ret(-1);
855   }
856
857   if ( roar_vio_clear_calls(tc) == -1 ) {
858    roar_mm_free(tc);
859    _ret(-1);
860   }
861
862   if ( roar_vio_stack_add(calls, tc) == -1 ) {
863    _ret(-1);
864   }
865
866
867   switch (c->type) {
868    case ROAR_VIO_DSTR_OBJT_PASS:
869      if ( roar_vio_open_pass(tc, prev) == -1 ) {
870       _ret(-1);
871      }
872     break;
873    case ROAR_VIO_DSTR_OBJT_RE:
874      if ( roar_vio_open_re(tc, prev) == -1 ) {
875       _ret(-1);
876      }
877     break;
878#ifdef ROAR_HAVE_LIBZ
879    case ROAR_VIO_DSTR_OBJT_GZIP:
880      //if ( roar_vio_open_gzip(tc, prev, -1) == -1 ) {
881      if ( roar_vio_open_zlib(tc, prev, -1, 1) == -1 ) {
882       _ret(-1);
883      }
884     break;
885    case ROAR_VIO_DSTR_OBJT_ZLIB:
886      //if ( roar_vio_open_gzip(tc, prev, -1) == -1 ) {
887      if ( roar_vio_open_zlib(tc, prev, -1, 0) == -1 ) {
888       _ret(-1);
889      }
890     break;
891#endif
892    case ROAR_VIO_DSTR_OBJT_BZIP2:
893    case ROAR_VIO_DSTR_OBJT_PGP:
894      if ( roar_vio_open_pgp_decrypt(tc, prev, NULL) == -1 ) {
895       _ret(-1);
896      }
897     break;
898    case ROAR_VIO_DSTR_OBJT_HTTP09:
899    case ROAR_VIO_DSTR_OBJT_HTTP10:
900    case ROAR_VIO_DSTR_OBJT_HTTP11:
901      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
902       _ret(-1);
903      }
904     break;
905    case ROAR_VIO_DSTR_OBJT_GOPHER:
906    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
907      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
908       _ret(-1);
909      }
910     break;
911    case ROAR_VIO_DSTR_OBJT_ICY:
912      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_ICY, c->def) == -1 ) {
913       _ret(-1);
914      }
915     break;
916    case ROAR_VIO_DSTR_OBJT_RTP2:
917      if ( roar_vio_open_rtp(tc, prev, c->dst, c->def) == -1 ) {
918       _ret(-1);
919      }
920     break;
921    case ROAR_VIO_DSTR_OBJT_PGP_ENC:
922    case ROAR_VIO_DSTR_OBJT_PGP_STORE:
923      if ( roar_vio_open_pgp_store(tc, prev, ROAR_VIO_PGP_OPTS_NONE) == -1 ) {
924       _ret(-1);
925      }
926     break;
927    case ROAR_VIO_DSTR_OBJT_TANTALOS:
928      if ( roar_vio_open_tantalos(tc, prev, c->dst, c->def) == -1 ) {
929       _ret(-1);
930      }
931     break;
932    case ROAR_VIO_DSTR_OBJT_NULL:
933    case ROAR_VIO_DSTR_OBJT_ZERO:
934    case ROAR_VIO_DSTR_OBJT_FULL:
935    case ROAR_VIO_DSTR_OBJT_NRANDOM:
936      if ( roar_vio_open_misc_by_name(tc, roar_vio_dstr_get_name(c->type)) == -1 ) {
937       _ret(-1);
938      }
939     break;
940    case ROAR_VIO_DSTR_OBJT_SSL1:
941    case ROAR_VIO_DSTR_OBJT_SSL2:
942    case ROAR_VIO_DSTR_OBJT_SSL3:
943    case ROAR_VIO_DSTR_OBJT_TLS:
944    case ROAR_VIO_DSTR_OBJT_MAGIC:
945      _ret(-1);
946     break;
947    default:
948      if ( (type = roar_vio_dstr_get_by_type(c->type)) == NULL ) {
949       _ret(-1);
950      }
951
952      if ( type->openvio == NULL )
953       _roar_vio_dstr_init_otherlibs();
954
955      if ( type->openvio == NULL ) {
956       _ret(-1);
957      }
958
959      if ( type->openvio(tc, prev, c) == -1 ) {
960       _ret(-1);
961      }
962   }
963
964   prev = tc;
965  } // else we can skip to the next :)
966 }
967
968 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
969 return 0;
970}
971
972#undef _ret
973#endif
974
975//ll
Note: See TracBrowser for help on using the repository browser.