source: roaraudio/libroar/vio_dstr.c @ 4956:dc85aeaedd54

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

Use zlib for gzip and zlib format (Closes: #29)

File size: 25.7 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}},
[4956]156 {ROAR_VIO_DSTR_OBJT_ZLIB,       "zlib",
157      NULL, NULL,
158      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]159 {ROAR_VIO_DSTR_OBJT_BZIP2,      "bzip2",
[3310]160      NULL, NULL,
[1323]161      {ROAR_VIO_DEF_TYPE_EOL}},
162
163 {ROAR_VIO_DSTR_OBJT_PGP,        "pgp",
[3310]164      NULL, NULL,
[1323]165      {ROAR_VIO_DEF_TYPE_EOL}},
166 {ROAR_VIO_DSTR_OBJT_PGP_ENC,    "pgp_enc",
[3310]167      NULL, NULL,
[1323]168      {ROAR_VIO_DEF_TYPE_EOL}},
169 {ROAR_VIO_DSTR_OBJT_PGP_STORE,  "pgp_store",
[3310]170      NULL, NULL,
[1323]171      {ROAR_VIO_DEF_TYPE_EOL}},
172 {ROAR_VIO_DSTR_OBJT_SSL1,       "ssl1",
[3310]173      NULL, NULL,
[1323]174      {ROAR_VIO_DEF_TYPE_EOL}},
175 {ROAR_VIO_DSTR_OBJT_SSL2,       "ssl2",
[3310]176      NULL, NULL,
[1323]177      {ROAR_VIO_DEF_TYPE_EOL}},
178 {ROAR_VIO_DSTR_OBJT_SSL3,       "ssl3",
[3310]179      NULL, NULL,
[1323]180      {ROAR_VIO_DEF_TYPE_EOL}},
181 {ROAR_VIO_DSTR_OBJT_TLS,        "tls",
[3310]182      NULL, NULL,
[1323]183      {ROAR_VIO_DEF_TYPE_EOL}},
184 {ROAR_VIO_DSTR_OBJT_SSLTLS,     "ssltls",
[3310]185      NULL, NULL,
[1323]186      {ROAR_VIO_DEF_TYPE_EOL}},
[4746]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}},
[1323]197
[3309]198 {ROAR_VIO_DSTR_OBJT_TRANSCODE,  "transcode", /* TODO  */
[3310]199      NULL, NULL,
[3309]200      {ROAR_VIO_DEF_TYPE_EOL}},
201
202 {ROAR_VIO_DSTR_OBJT_RAUM,       "raum",      /* TODO */
[3310]203      NULL, NULL,
[3309]204      {ROAR_VIO_DEF_TYPE_EOL}},
205 {ROAR_VIO_DSTR_OBJT_OGG,        "ogg",       /* TODO */
[3310]206      NULL, NULL,
[3309]207      {ROAR_VIO_DEF_TYPE_EOL}},
208 {ROAR_VIO_DSTR_OBJT_TAR,        "tar",       /* TODO */
[3310]209      NULL, NULL,
[3309]210      {ROAR_VIO_DEF_TYPE_EOL}},
211
212
[1323]213 {ROAR_VIO_DSTR_OBJT_MAGIC,      "magic",
[3310]214      NULL, NULL,
[1323]215      {ROAR_VIO_DEF_TYPE_EOL}},
[3116]216 {ROAR_VIO_DSTR_OBJT_TANTALOS,   "tantalos",
[3310]217      NULL, NULL,
[3116]218      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]219
220 {ROAR_VIO_DSTR_OBJT_INTERNAL, "INTERNAL",
[3310]221      NULL, NULL,
[1323]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}},
[3310]224 {ROAR_VIO_DSTR_OBJT_EOL, NULL, NULL, NULL, {ROAR_VIO_DEF_TYPE_EOL}}
[1323]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
[3312]238struct _roar_vio_dstr_type * roar_vio_dstr_get_by_type (int type) {
[1323]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 )
[3312]243   return &(_roar_vio_dstr_objs[i]);
[1323]244 }
245
[3312]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
[1325]255 if ( type == ROAR_VIO_DSTR_OBJT_EOL )
256  return "<<EOL>>";
257
[1323]258 return NULL;
259}
[3311]260
[3312]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
[3311]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
[1365]297#endif
[1323]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
[1353]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
[1365]323#ifndef ROAR_WITHOUT_VIO_DSTR
[3292]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
[1330]327 if ( calls == NULL || def == NULL )
328  return -1;
329
330 switch (def->type) {
[3118]331  case ROAR_VIO_DEF_TYPE_NONE:
332   break;
[1330]333  case ROAR_VIO_DEF_TYPE_FILE:
[4876]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);
[1330]338     return -1;
[4876]339    }
340    roar_libroar_warn();
[1330]341   break;
342  case ROAR_VIO_DEF_TYPE_SOCKET:
[3293]343     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
[1333]344      return -1;
[1330]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}
[1365]360#endif
[1330]361
[1323]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
[4876]366#define _ret(x) do { int _err = roar_error; roar_mm_free(dstr); roar_error = _err; roar_err_to_errno(); return (x); } while (0)
[1323]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) {
[1365]371#ifndef ROAR_WITHOUT_VIO_DSTR
[1325]372 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
[1323]373 char * next;
374 char * this;
375 char * name;
376 char * opts;
377 char * dst;
378 char * c;
379 int    inopts;
380 int    type;
[1328]381 int    cc = 1; // current chain element
[1323]382
383 if ( calls == NULL || dstr == NULL )
384  return -1;
385
386 if ( dnum != 0 && def == NULL )
387  return -1;
388
[3834]389 if ( (dstr = roar_mm_strdup(dstr)) == NULL )
[1323]390  return -1;
391
[1325]392 memset(chain, 0, sizeof(chain));
[1323]393
[1328]394 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
395
[1323]396 next = dstr;
397
398 while (next != NULL) {
[1325]399  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
400   _ret(-1);
401  }
402
[1323]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
[1329]440  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
[1323]441
442  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
443   _ret(-1);
444  }
445
[1329]446  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
[1323]447
[1325]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
[1616]460 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
[1328]461
[1325]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);
[1323]472 }
473
[1325]474 _ret(0);
[1365]475#else
476 return -1;
477#endif
[1325]478}
479
480#undef _ret
481
[1365]482#ifndef ROAR_WITHOUT_VIO_DSTR
[1325]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) {
[3313]493 struct _roar_vio_dstr_type * type;
[1325]494 struct roar_vio_dstr_chain * c, * next;
495 int i;
[1328]496 int tmp[8];
[1325]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];
[1329]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  }
[1325]523
[1328]524  memset(tmp, 0, sizeof(tmp));
525
[1350]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));
[1616]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,
[1325]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) {
[1328]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;
[1325]538   case ROAR_VIO_DSTR_OBJT_PASS:
539   case ROAR_VIO_DSTR_OBJT_RE:
[3277]540   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
[1325]541   case ROAR_VIO_DSTR_OBJT_GZIP:
[4956]542   case ROAR_VIO_DSTR_OBJT_ZLIB:
[1325]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:
[1328]552     if ( tmp[0] )
553      c->need_vio = 0;
554
[1325]555     next->def = c->def;
556    break;
[4824]557   case ROAR_VIO_DSTR_OBJT_NULL:
558   case ROAR_VIO_DSTR_OBJT_ZERO:
[4876]559   case ROAR_VIO_DSTR_OBJT_FULL:
[4824]560   case ROAR_VIO_DSTR_OBJT_NRANDOM:
[3118]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;
[1325]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);
[1353]571     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
[1325]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;
[1328]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;
[1337]610#ifdef ROAR_HAVE_UNIX
[1333]611   case ROAR_VIO_DSTR_OBJT_UNIX:
612     c->need_vio = 0;
613     next->def = &(next->store_def);
614
[1337]615     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
[1353]616      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
617       return -1;
618
[1337]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
[1353]622      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
623       return -1;
[1337]624
625      if ( roar_vio_socket_init_unix_def(next->def, c->dst) == -1 )
626       return -1;
[1333]627     }
628    break;
[1337]629#endif
[1335]630   case ROAR_VIO_DSTR_OBJT_SOCKET:
631     c->need_vio = 0;
632     next->def = &(next->store_def);
633
[1353]634     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
635      return -1;
636
[1335]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
[1353]645     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
646      return -1;
647
[1335]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
[1359]652#ifdef ROAR_HAVE_IPV4
[1335]653   case ROAR_VIO_DSTR_OBJT_TCP:
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_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
[1353]667     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
668      return -1;
669
[1335]670     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_DGRAM, c->def) == -1 )
671      return -1;
672    break;
[1359]673#endif
[1335]674#ifdef ROAR_HAVE_IPV6
675   case ROAR_VIO_DSTR_OBJT_TCP6:
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_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
[1353]689     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
690      return -1;
691
[1335]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
[1349]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;
[3078]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;
[1325]720   default:
[3313]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     }
[1325]735  }
736
[1329]737  if ( next != NULL ) {
[1350]738   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
[1329]739                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
740                    next->def, next->def == NULL ? -1 : next->def->type);
[1349]741   if ( next->def != NULL ) {
[1616]742    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
[1349]743                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
744                     next->def->o_flags);
745   }
[1329]746  } else {
[1350]747   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
[1329]748                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
749  }
[1325]750 }
751
[1616]752 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
[1328]753
[1325]754 return 0;
755}
756
[4876]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)
[1328]758
759int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
760                                  struct roar_vio_calls * vio) {
[3313]761 struct _roar_vio_dstr_type * type;
[1328]762 struct roar_vio_dstr_chain * c;
763 struct roar_vio_defaults   * def;
764 struct roar_vio_calls      * tc, * prev;
765 int i;
766
[1616]767 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
[1328]768
[1325]769 if ( chain == NULL || calls == NULL )
770  return -1;
771
[1328]772 if ( roar_vio_open_stack(calls) == -1 )
773  return -1;
774
[1329]775 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
[1328]776
777 if ( (def = chain->def) != NULL ) {
778  if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
779   _ret(-1);
780  }
781
[1337]782  if ( roar_vio_init_calls(tc) == -1 ) {
783   free(tc);
784   _ret(-1);
785  }
786
[1328]787  if ( roar_vio_stack_add(calls, tc) == -1 ) {
788   _ret(-1);
789  }
790
[3292]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 ) {
[1330]798   _ret(-1);
[1328]799  }
[1330]800
[1328]801  prev = tc;
802 } else {
803  prev = vio;
804 }
805
806 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
[1353]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);
[1349]809
[1328]810  if ( c->need_vio ) {
811   if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
812    _ret(-1);
813   }
814
[1337]815   if ( roar_vio_init_calls(tc) == -1 ) {
816    free(tc);
817    _ret(-1);
818   }
819
[1328]820   if ( roar_vio_stack_add(calls, tc) == -1 ) {
821    _ret(-1);
822   }
823
[1349]824
[1328]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:
[4956]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 ) {
[1328]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;
[1349]854    case ROAR_VIO_DSTR_OBJT_HTTP09:
855    case ROAR_VIO_DSTR_OBJT_HTTP10:
856    case ROAR_VIO_DSTR_OBJT_HTTP11:
[1353]857      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
[1349]858       _ret(-1);
859      }
860     break;
861    case ROAR_VIO_DSTR_OBJT_GOPHER:
862    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
[1353]863      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
[1349]864       _ret(-1);
865      }
866     break;
[3078]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;
[3277]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;
[1328]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;
[3116]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;
[4824]888    case ROAR_VIO_DSTR_OBJT_NULL:
889    case ROAR_VIO_DSTR_OBJT_ZERO:
[4876]890    case ROAR_VIO_DSTR_OBJT_FULL:
[4824]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;
[1328]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:
[3313]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      }
[1328]918   }
919
920   prev = tc;
921  } // else we can skip to the next :)
922 }
923
[1616]924 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
[1328]925 return 0;
[1323]926}
927
[1328]928#undef _ret
[1365]929#endif
[1328]930
[1321]931//ll
Note: See TracBrowser for help on using the repository browser.