source: roaraudio/libroar/vio_dstr.c @ 4824:bd21c2bc0214

Last change on this file since 4824:bd21c2bc0214 was 4824:bd21c2bc0214, checked in by phi, 13 years ago

Added VIOs and DSTR elements: null:, zero:, nrandom:

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