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
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
39struct _roar_vio_dstr_type {
40 int    id;
41 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_BZIP2,      "bzip2",
157      NULL, NULL,
158      {ROAR_VIO_DEF_TYPE_EOL}},
159
160 {ROAR_VIO_DSTR_OBJT_PGP,        "pgp",
161      NULL, NULL,
162      {ROAR_VIO_DEF_TYPE_EOL}},
163 {ROAR_VIO_DSTR_OBJT_PGP_ENC,    "pgp_enc",
164      NULL, NULL,
165      {ROAR_VIO_DEF_TYPE_EOL}},
166 {ROAR_VIO_DSTR_OBJT_PGP_STORE,  "pgp_store",
167      NULL, NULL,
168      {ROAR_VIO_DEF_TYPE_EOL}},
169 {ROAR_VIO_DSTR_OBJT_SSL1,       "ssl1",
170      NULL, NULL,
171      {ROAR_VIO_DEF_TYPE_EOL}},
172 {ROAR_VIO_DSTR_OBJT_SSL2,       "ssl2",
173      NULL, NULL,
174      {ROAR_VIO_DEF_TYPE_EOL}},
175 {ROAR_VIO_DSTR_OBJT_SSL3,       "ssl3",
176      NULL, NULL,
177      {ROAR_VIO_DEF_TYPE_EOL}},
178 {ROAR_VIO_DSTR_OBJT_TLS,        "tls",
179      NULL, NULL,
180      {ROAR_VIO_DEF_TYPE_EOL}},
181 {ROAR_VIO_DSTR_OBJT_SSLTLS,     "ssltls",
182      NULL, NULL,
183      {ROAR_VIO_DEF_TYPE_EOL}},
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}},
194
195 {ROAR_VIO_DSTR_OBJT_TRANSCODE,  "transcode", /* TODO  */
196      NULL, NULL,
197      {ROAR_VIO_DEF_TYPE_EOL}},
198
199 {ROAR_VIO_DSTR_OBJT_RAUM,       "raum",      /* TODO */
200      NULL, NULL,
201      {ROAR_VIO_DEF_TYPE_EOL}},
202 {ROAR_VIO_DSTR_OBJT_OGG,        "ogg",       /* TODO */
203      NULL, NULL,
204      {ROAR_VIO_DEF_TYPE_EOL}},
205 {ROAR_VIO_DSTR_OBJT_TAR,        "tar",       /* TODO */
206      NULL, NULL,
207      {ROAR_VIO_DEF_TYPE_EOL}},
208
209
210 {ROAR_VIO_DSTR_OBJT_MAGIC,      "magic",
211      NULL, NULL,
212      {ROAR_VIO_DEF_TYPE_EOL}},
213 {ROAR_VIO_DSTR_OBJT_TANTALOS,   "tantalos",
214      NULL, NULL,
215      {ROAR_VIO_DEF_TYPE_EOL}},
216
217 {ROAR_VIO_DSTR_OBJT_INTERNAL, "INTERNAL",
218      NULL, NULL,
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}},
221 {ROAR_VIO_DSTR_OBJT_EOL, NULL, NULL, NULL, {ROAR_VIO_DEF_TYPE_EOL}}
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
235struct _roar_vio_dstr_type * roar_vio_dstr_get_by_type (int type) {
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 )
240   return &(_roar_vio_dstr_objs[i]);
241 }
242
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
252 if ( type == ROAR_VIO_DSTR_OBJT_EOL )
253  return "<<EOL>>";
254
255 return NULL;
256}
257
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
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
294#endif
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
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
320#ifndef ROAR_WITHOUT_VIO_DSTR
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
324 if ( calls == NULL || def == NULL )
325  return -1;
326
327 switch (def->type) {
328  case ROAR_VIO_DEF_TYPE_NONE:
329   break;
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:
335     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
336      return -1;
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}
352#endif
353
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
358#define _ret(x) roar_mm_free(dstr); return (x)
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) {
363#ifndef ROAR_WITHOUT_VIO_DSTR
364 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
365 char * next;
366 char * this;
367 char * name;
368 char * opts;
369 char * dst;
370 char * c;
371 int    inopts;
372 int    type;
373 int    cc = 1; // current chain element
374
375 if ( calls == NULL || dstr == NULL )
376  return -1;
377
378 if ( dnum != 0 && def == NULL )
379  return -1;
380
381 if ( (dstr = roar_mm_strdup(dstr)) == NULL )
382  return -1;
383
384 memset(chain, 0, sizeof(chain));
385
386 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
387
388 next = dstr;
389
390 while (next != NULL) {
391  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
392   _ret(-1);
393  }
394
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
432  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
433
434  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
435   _ret(-1);
436  }
437
438  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
439
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
452 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
453
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);
464 }
465
466 _ret(0);
467#else
468 return -1;
469#endif
470}
471
472#undef _ret
473
474#ifndef ROAR_WITHOUT_VIO_DSTR
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) {
485 struct _roar_vio_dstr_type * type;
486 struct roar_vio_dstr_chain * c, * next;
487 int i;
488 int tmp[8];
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];
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  }
515
516  memset(tmp, 0, sizeof(tmp));
517
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));
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,
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) {
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;
530   case ROAR_VIO_DSTR_OBJT_PASS:
531   case ROAR_VIO_DSTR_OBJT_RE:
532   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
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:
543     if ( tmp[0] )
544      c->need_vio = 0;
545
546     next->def = c->def;
547    break;
548   case ROAR_VIO_DSTR_OBJT_NULL:
549   case ROAR_VIO_DSTR_OBJT_ZERO:
550   case ROAR_VIO_DSTR_OBJT_NRANDOM:
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;
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);
561     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
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;
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;
600#ifdef ROAR_HAVE_UNIX
601   case ROAR_VIO_DSTR_OBJT_UNIX:
602     c->need_vio = 0;
603     next->def = &(next->store_def);
604
605     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
606      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
607       return -1;
608
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
612      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
613       return -1;
614
615      if ( roar_vio_socket_init_unix_def(next->def, c->dst) == -1 )
616       return -1;
617     }
618    break;
619#endif
620   case ROAR_VIO_DSTR_OBJT_SOCKET:
621     c->need_vio = 0;
622     next->def = &(next->store_def);
623
624     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
625      return -1;
626
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
635     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
636      return -1;
637
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
642#ifdef ROAR_HAVE_IPV4
643   case ROAR_VIO_DSTR_OBJT_TCP:
644     c->need_vio = 0;
645     next->def = &(next->store_def);
646
647     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
648      return -1;
649
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
657     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
658      return -1;
659
660     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_DGRAM, c->def) == -1 )
661      return -1;
662    break;
663#endif
664#ifdef ROAR_HAVE_IPV6
665   case ROAR_VIO_DSTR_OBJT_TCP6:
666     c->need_vio = 0;
667     next->def = &(next->store_def);
668
669     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
670      return -1;
671
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
679     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
680      return -1;
681
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
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;
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;
710   default:
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     }
725  }
726
727  if ( next != NULL ) {
728   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
729                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
730                    next->def, next->def == NULL ? -1 : next->def->type);
731   if ( next->def != NULL ) {
732    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
733                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
734                     next->def->o_flags);
735   }
736  } else {
737   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
738                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
739  }
740 }
741
742 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
743
744 return 0;
745}
746
747#define _ret(x) roar_vio_close(calls); ROAR_DBG("roar_vio_dstr_build_chain(*) = %i", (x)); return (x)
748
749int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
750                                  struct roar_vio_calls * vio) {
751 struct _roar_vio_dstr_type * type;
752 struct roar_vio_dstr_chain * c;
753 struct roar_vio_defaults   * def;
754 struct roar_vio_calls      * tc, * prev;
755 int i;
756
757 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
758
759 if ( chain == NULL || calls == NULL )
760  return -1;
761
762 if ( roar_vio_open_stack(calls) == -1 )
763  return -1;
764
765 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
766
767 if ( (def = chain->def) != NULL ) {
768  if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
769   _ret(-1);
770  }
771
772  if ( roar_vio_init_calls(tc) == -1 ) {
773   free(tc);
774   _ret(-1);
775  }
776
777  if ( roar_vio_stack_add(calls, tc) == -1 ) {
778   _ret(-1);
779  }
780
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 ) {
788   _ret(-1);
789  }
790
791  prev = tc;
792 } else {
793  prev = vio;
794 }
795
796 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
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);
799
800  if ( c->need_vio ) {
801   if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
802    _ret(-1);
803   }
804
805   if ( roar_vio_init_calls(tc) == -1 ) {
806    free(tc);
807    _ret(-1);
808   }
809
810   if ( roar_vio_stack_add(calls, tc) == -1 ) {
811    _ret(-1);
812   }
813
814
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;
837    case ROAR_VIO_DSTR_OBJT_HTTP09:
838    case ROAR_VIO_DSTR_OBJT_HTTP10:
839    case ROAR_VIO_DSTR_OBJT_HTTP11:
840      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
841       _ret(-1);
842      }
843     break;
844    case ROAR_VIO_DSTR_OBJT_GOPHER:
845    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
846      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
847       _ret(-1);
848      }
849     break;
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;
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;
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;
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;
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;
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:
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      }
900   }
901
902   prev = tc;
903  } // else we can skip to the next :)
904 }
905
906 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
907 return 0;
908}
909
910#undef _ret
911#endif
912
913//ll
Note: See TracBrowser for help on using the repository browser.