source: roaraudio/libroar/vio_dstr.c @ 4964:bba084fba0fa

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

avoide most external uses of roar_vio_init_calls()

File size: 25.8 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_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(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
249char *  roar_vio_dstr_get_name(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
324int     roar_vio_open_default (struct roar_vio_calls * calls, struct roar_vio_defaults * def, char * opts) {
325 ROAR_DBG("roar_vio_open_default(calls=%p, def=%p, opts='%s') = ?", calls, def, opts);
326
327 if ( calls == NULL || def == NULL )
328  return -1;
329
330 switch (def->type) {
331  case ROAR_VIO_DEF_TYPE_NONE:
332   break;
333  case ROAR_VIO_DEF_TYPE_FILE:
334    roar_libroar_nowarn();
335    if ( roar_vio_open_file(calls, def->d.file, def->o_flags, def->o_mode) == -1 ) {
336     roar_libroar_warn();
337     ROAR_DBG("roar_vio_open_default(*): Can not open file: %i", roar_error);
338     return -1;
339    }
340    roar_libroar_warn();
341   break;
342  case ROAR_VIO_DEF_TYPE_SOCKET:
343     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
344      return -1;
345   break;
346  case ROAR_VIO_DEF_TYPE_FH:
347    if ( roar_vio_open_fh(calls, def->d.fh) == -1 )
348     return -1;
349   break;
350  case ROAR_VIO_DEF_TYPE_SOCKETFH:
351    if ( roar_vio_open_fh_socket(calls, def->d.fh) == -1 )
352     return -1;
353   break;
354  default:
355    return -1;
356 }
357
358 return 0;
359}
360#endif
361
362int     roar_vio_open_dstr    (struct roar_vio_calls * calls, char * dstr, struct roar_vio_defaults * def, int dnum) {
363 return roar_vio_open_dstr_vio(calls, dstr, def, dnum, NULL);
364}
365
366#define _ret(x) do { int _err = roar_error; roar_mm_free(dstr); roar_error = _err; roar_err_to_errno(); return (x); } while (0)
367
368int     roar_vio_open_dstr_vio(struct roar_vio_calls * calls,
369                               char * dstr, struct roar_vio_defaults * def, int dnum,
370                               struct roar_vio_calls * vio) {
371#ifndef ROAR_WITHOUT_VIO_DSTR
372 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
373 char * next;
374 char * this;
375 char * name;
376 char * opts;
377 char * dst;
378 char * c;
379 int    inopts;
380 int    type;
381 int    cc = 1; // current chain element
382
383 if ( calls == NULL || dstr == NULL )
384  return -1;
385
386 if ( dnum != 0 && def == NULL )
387  return -1;
388
389 if ( (dstr = roar_mm_strdup(dstr)) == NULL )
390  return -1;
391
392 memset(chain, 0, sizeof(chain));
393
394 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
395
396 next = dstr;
397
398 while (next != NULL) {
399  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
400   _ret(-1);
401  }
402
403  this = next;
404  next = strstr(next, "##");
405
406  if (next != NULL) {
407   *next = 0;
408   next += 2;
409  }
410
411  // we have the current token in 'this'.
412
413  opts   = NULL;
414  dst    = NULL;
415
416  if ( strstr(this, ":") != NULL ) {
417   name   = this;
418   inopts = 0;
419   for (c = this; *c != 0; c++) {
420    if ( *c == '[' ) {
421     *c     = 0;
422     opts   = c + 1;
423     inopts = 1;
424    } else if ( *c == ']' &&  inopts ) {
425     *c     = 0;
426     inopts = 0;
427    } else if ( *c == ':' && !inopts ) {
428     *c     = 0;
429     dst    = *(c+1) == 0 ? NULL : c + 1;
430     break;
431    }
432   }
433  } else {
434   // we need to guess that this is here...
435   // currently we guess this is a file in all cases
436   name = "file";
437   dst  = this;
438  }
439
440  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
441
442  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
443   _ret(-1);
444  }
445
446  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
447
448  chain[cc].type     = type;
449  chain[cc].opts     = opts;
450  chain[cc].dst      = dst;
451  chain[cc].def      = NULL;
452  chain[cc].vio      = NULL;
453  chain[cc].need_vio = -1;
454  cc++;
455
456 }
457
458 chain[cc].type = ROAR_VIO_DSTR_OBJT_EOL;
459
460 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
461
462 if ( roar_vio_dstr_parse_opts(chain) == -1 ) {
463  _ret(-1);
464 }
465
466 if ( roar_vio_dstr_set_defaults(chain, cc, def, dnum) == -1 ) {
467  _ret(-1);
468 }
469
470 if ( roar_vio_dstr_build_chain(chain, calls, vio) == -1 ) {
471  _ret(-1);
472 }
473
474 _ret(0);
475#else
476 return -1;
477#endif
478}
479
480#undef _ret
481
482#ifndef ROAR_WITHOUT_VIO_DSTR
483int     roar_vio_dstr_parse_opts(struct roar_vio_dstr_chain * chain) {
484 if ( chain == NULL )
485  return -1;
486
487 // TODO: we should add some code here later...
488
489 return 0;
490}
491
492int     roar_vio_dstr_set_defaults(struct roar_vio_dstr_chain * chain, int len, struct roar_vio_defaults * def, int dnum) {
493 struct _roar_vio_dstr_type * type;
494 struct roar_vio_dstr_chain * c, * next;
495 int i;
496 int tmp[8];
497
498 if ( chain == NULL )
499  return -1;
500
501 if ( def == NULL && dnum != 0 )
502  return -1;
503
504 if ( dnum > 1 ) /* currently not supported */
505  return -1;
506
507 if ( dnum == 0 )
508  def = NULL;
509
510 chain[len].def = def;
511
512 for (i = len; i >= 0; i--) {
513  c    = &chain[i];
514
515  if ( i > 0 ) {
516   next = &chain[i-1];
517  } else {
518   next = NULL;
519
520   if ( c->type != ROAR_VIO_DSTR_OBJT_INTERNAL )
521    return -1;
522  }
523
524  memset(tmp, 0, sizeof(tmp));
525
526  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));
527  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,
528                   roar_vio_dstr_get_name(c->type), c->def, c->def == NULL ? -1 : c->def->type);
529
530  c->need_vio = 1;
531
532  switch (c->type) {
533   case ROAR_VIO_DSTR_OBJT_INTERNAL:
534     c->need_vio = 0;
535    break;
536   case ROAR_VIO_DSTR_OBJT_EOL:
537     tmp[0] = 1;
538   case ROAR_VIO_DSTR_OBJT_PASS:
539   case ROAR_VIO_DSTR_OBJT_RE:
540   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
541   case ROAR_VIO_DSTR_OBJT_GZIP:
542   case ROAR_VIO_DSTR_OBJT_ZLIB:
543   case ROAR_VIO_DSTR_OBJT_BZIP2:
544   case ROAR_VIO_DSTR_OBJT_PGP:
545   case ROAR_VIO_DSTR_OBJT_PGP_ENC:
546   case ROAR_VIO_DSTR_OBJT_PGP_STORE:
547   case ROAR_VIO_DSTR_OBJT_SSL1:
548   case ROAR_VIO_DSTR_OBJT_SSL2:
549   case ROAR_VIO_DSTR_OBJT_SSL3:
550   case ROAR_VIO_DSTR_OBJT_TLS:
551   case ROAR_VIO_DSTR_OBJT_MAGIC:
552     if ( tmp[0] )
553      c->need_vio = 0;
554
555     next->def = c->def;
556    break;
557   case ROAR_VIO_DSTR_OBJT_NULL:
558   case ROAR_VIO_DSTR_OBJT_ZERO:
559   case ROAR_VIO_DSTR_OBJT_FULL:
560   case ROAR_VIO_DSTR_OBJT_NRANDOM:
561   case ROAR_VIO_DSTR_OBJT_TANTALOS:
562     next->def = &(next->store_def);
563     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_NONE, NULL, -1);
564    break;
565   case ROAR_VIO_DSTR_OBJT_FILE:
566     if ( c->dst == NULL ) /* should we allow multible cascaed file: objects? */
567      return -1;
568
569     c->need_vio = 0;
570     next->def = &(next->store_def);
571     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
572
573     if ( c->dst[0] == '/' && c->dst[1] == '/' ) {
574      next->def->d.file = c->dst + 1;
575     } else {
576      next->def->d.file = c->dst;
577     }
578    break;
579   case ROAR_VIO_DSTR_OBJT_FH:
580     tmp[0] = 1;
581   case ROAR_VIO_DSTR_OBJT_SOCKETFH:
582     c->need_vio = 0;
583     next->def = &(next->store_def);
584
585     if ( c->def != NULL ) {
586      tmp[2] = c->def->o_flags;
587      tmp[3] = c->def->o_mode;
588     } else {
589      tmp[2] = O_RDONLY;
590      tmp[3] = 0644;
591     }
592
593     if ( !strcasecmp(c->dst, "stdin") ) {
594      tmp[1] = ROAR_STDIN;
595      tmp[2] = O_RDONLY;
596     } else if ( !strcasecmp(c->dst, "stdout") ) {
597      tmp[1] = ROAR_STDOUT;
598      tmp[2] = O_WRONLY;
599     } else if ( !strcasecmp(c->dst, "stderr") ) {
600      tmp[1] = ROAR_STDERR;
601      tmp[2] = O_WRONLY;
602     } else {
603      if ( sscanf(c->dst, "%i", &tmp[1]) != 1 )
604       return -1;
605     }
606
607     roar_vio_dstr_init_defaults(next->def, tmp[0] ? ROAR_VIO_DEF_TYPE_FH : ROAR_VIO_DEF_TYPE_SOCKETFH, tmp[2], tmp[3]);
608     next->def->d.fh = tmp[1];
609    break;
610#ifdef ROAR_HAVE_UNIX
611   case ROAR_VIO_DSTR_OBJT_UNIX:
612     c->need_vio = 0;
613     next->def = &(next->store_def);
614
615     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
616      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
617       return -1;
618
619      if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_UNIX, SOCK_STREAM, c->def) == -1 )
620       return -1;
621     } else {                // we have a destination? -> fast way
622      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
623       return -1;
624
625      if ( roar_vio_socket_init_unix_def(next->def, c->dst) == -1 )
626       return -1;
627     }
628    break;
629#endif
630   case ROAR_VIO_DSTR_OBJT_SOCKET:
631     c->need_vio = 0;
632     next->def = &(next->store_def);
633
634     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
635      return -1;
636
637     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, -1, SOCK_STREAM, c->def) == -1 )
638      return -1;
639    break;
640#ifdef ROAR_HAVE_LIBDNET
641   case ROAR_VIO_DSTR_OBJT_DECNET:
642     c->need_vio = 0;
643     next->def = &(next->store_def);
644
645     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
646      return -1;
647
648     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_DECnet, SOCK_STREAM, c->def) == -1 )
649      return -1;
650    break;
651#endif
652#ifdef ROAR_HAVE_IPV4
653   case ROAR_VIO_DSTR_OBJT_TCP:
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_STREAM, c->def) == -1 )
661      return -1;
662    break;
663   case ROAR_VIO_DSTR_OBJT_UDP:
664     c->need_vio = 0;
665     next->def = &(next->store_def);
666
667     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
668      return -1;
669
670     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_DGRAM, c->def) == -1 )
671      return -1;
672    break;
673#endif
674#ifdef ROAR_HAVE_IPV6
675   case ROAR_VIO_DSTR_OBJT_TCP6:
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_STREAM, c->def) == -1 )
683      return -1;
684    break;
685   case ROAR_VIO_DSTR_OBJT_UDP6:
686     c->need_vio = 0;
687     next->def = &(next->store_def);
688
689     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
690      return -1;
691
692     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET6, SOCK_DGRAM, c->def) == -1 )
693      return -1;
694    break;
695#endif
696   case ROAR_VIO_DSTR_OBJT_HTTP09:
697   case ROAR_VIO_DSTR_OBJT_HTTP10:
698   case ROAR_VIO_DSTR_OBJT_HTTP11:
699     c->need_vio = 1;
700     next->def = &(next->store_def);
701
702     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 )
703      return -1;
704    break;
705   case ROAR_VIO_DSTR_OBJT_GOPHER:
706   case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
707     c->need_vio = 1;
708     next->def = &(next->store_def);
709
710     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 )
711      return -1;
712    break;
713   case ROAR_VIO_DSTR_OBJT_ICY:
714     c->need_vio = 1;
715     next->def = &(next->store_def);
716
717     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_ICY, c->def) == -1 )
718      return -1;
719    break;
720   default:
721     if ( (type = roar_vio_dstr_get_by_type(c->type)) == NULL ) {
722      return -1;
723     }
724
725     if ( type->setdef == NULL )
726      _roar_vio_dstr_init_otherlibs();
727
728     if ( type->setdef == NULL ) {
729      return -1;
730     }
731
732     if ( type->setdef(c, next) == -1 ) {
733      return -1;
734     }
735  }
736
737  if ( next != NULL ) {
738   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
739                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
740                    next->def, next->def == NULL ? -1 : next->def->type);
741   if ( next->def != NULL ) {
742    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
743                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
744                     next->def->o_flags);
745   }
746  } else {
747   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
748                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
749  }
750 }
751
752 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
753
754 return 0;
755}
756
757#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)
758
759int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
760                                  struct roar_vio_calls * vio) {
761 struct _roar_vio_dstr_type * type;
762 struct roar_vio_dstr_chain * c;
763 struct roar_vio_defaults   * def;
764 struct roar_vio_calls      * tc, * prev;
765 int i;
766
767 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
768
769 if ( chain == NULL || calls == NULL )
770  return -1;
771
772 if ( roar_vio_open_stack(calls) == -1 )
773  return -1;
774
775 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
776
777 if ( (def = chain->def) != NULL ) {
778  if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
779   _ret(-1);
780  }
781
782  if ( roar_vio_clear_calls(tc) == -1 ) {
783   free(tc);
784   _ret(-1);
785  }
786
787  if ( roar_vio_stack_add(calls, tc) == -1 ) {
788   _ret(-1);
789  }
790
791  if ( chain->opts == NULL ) {
792   if ( chain[1].type != ROAR_VIO_DSTR_OBJT_EOL ) {
793    chain->opts = chain[1].opts;
794   }
795  }
796
797  if ( roar_vio_open_default(tc, def, chain->opts) == -1 ) {
798   _ret(-1);
799  }
800
801  prev = tc;
802 } else {
803  prev = vio;
804 }
805
806 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
807  ROAR_DBG("roar_vio_dstr_build_chain(*): i=%i, c->type=0x%.4x(%s): need_vio=%i, def->o_flags=%i", i,
808                   c->type & 0xFFFF, roar_vio_dstr_get_name(c->type), c->need_vio, c->def->o_flags);
809
810  if ( c->need_vio ) {
811   if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
812    _ret(-1);
813   }
814
815   if ( roar_vio_clear_calls(tc) == -1 ) {
816    free(tc);
817    _ret(-1);
818   }
819
820   if ( roar_vio_stack_add(calls, tc) == -1 ) {
821    _ret(-1);
822   }
823
824
825   switch (c->type) {
826    case ROAR_VIO_DSTR_OBJT_PASS:
827      if ( roar_vio_open_pass(tc, prev) == -1 ) {
828       _ret(-1);
829      }
830     break;
831    case ROAR_VIO_DSTR_OBJT_RE:
832      if ( roar_vio_open_re(tc, prev) == -1 ) {
833       _ret(-1);
834      }
835     break;
836    case ROAR_VIO_DSTR_OBJT_GZIP:
837      //if ( roar_vio_open_gzip(tc, prev, -1) == -1 ) {
838      if ( roar_vio_open_zlib(tc, prev, -1, 1) == -1 ) {
839       _ret(-1);
840      }
841     break;
842    case ROAR_VIO_DSTR_OBJT_ZLIB:
843      //if ( roar_vio_open_gzip(tc, prev, -1) == -1 ) {
844      if ( roar_vio_open_zlib(tc, prev, -1, 0) == -1 ) {
845       _ret(-1);
846      }
847     break;
848    case ROAR_VIO_DSTR_OBJT_BZIP2:
849    case ROAR_VIO_DSTR_OBJT_PGP:
850      if ( roar_vio_open_pgp_decrypt(tc, prev, NULL) == -1 ) {
851       _ret(-1);
852      }
853     break;
854    case ROAR_VIO_DSTR_OBJT_HTTP09:
855    case ROAR_VIO_DSTR_OBJT_HTTP10:
856    case ROAR_VIO_DSTR_OBJT_HTTP11:
857      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
858       _ret(-1);
859      }
860     break;
861    case ROAR_VIO_DSTR_OBJT_GOPHER:
862    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
863      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
864       _ret(-1);
865      }
866     break;
867    case ROAR_VIO_DSTR_OBJT_ICY:
868      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_ICY, c->def) == -1 ) {
869       _ret(-1);
870      }
871     break;
872    case ROAR_VIO_DSTR_OBJT_RTP2:
873      if ( roar_vio_open_rtp(tc, prev, c->dst, c->def) == -1 ) {
874       _ret(-1);
875      }
876     break;
877    case ROAR_VIO_DSTR_OBJT_PGP_ENC:
878    case ROAR_VIO_DSTR_OBJT_PGP_STORE:
879      if ( roar_vio_open_pgp_store(tc, prev, ROAR_VIO_PGP_OPTS_NONE) == -1 ) {
880       _ret(-1);
881      }
882     break;
883    case ROAR_VIO_DSTR_OBJT_TANTALOS:
884      if ( roar_vio_open_tantalos(tc, prev, c->dst, c->def) == -1 ) {
885       _ret(-1);
886      }
887     break;
888    case ROAR_VIO_DSTR_OBJT_NULL:
889    case ROAR_VIO_DSTR_OBJT_ZERO:
890    case ROAR_VIO_DSTR_OBJT_FULL:
891    case ROAR_VIO_DSTR_OBJT_NRANDOM:
892      if ( roar_vio_open_misc_by_name(tc, roar_vio_dstr_get_name(c->type)) == -1 ) {
893       _ret(-1);
894      }
895     break;
896    case ROAR_VIO_DSTR_OBJT_SSL1:
897    case ROAR_VIO_DSTR_OBJT_SSL2:
898    case ROAR_VIO_DSTR_OBJT_SSL3:
899    case ROAR_VIO_DSTR_OBJT_TLS:
900    case ROAR_VIO_DSTR_OBJT_MAGIC:
901      _ret(-1);
902     break;
903    default:
904      if ( (type = roar_vio_dstr_get_by_type(c->type)) == NULL ) {
905       _ret(-1);
906      }
907
908      if ( type->openvio == NULL )
909       _roar_vio_dstr_init_otherlibs();
910
911      if ( type->openvio == NULL ) {
912       _ret(-1);
913      }
914
915      if ( type->openvio(tc, prev, c) == -1 ) {
916       _ret(-1);
917      }
918   }
919
920   prev = tc;
921  } // else we can skip to the next :)
922 }
923
924 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
925 return 0;
926}
927
928#undef _ret
929#endif
930
931//ll
Note: See TracBrowser for help on using the repository browser.