source: roaraudio/libroar/vio_dstr.c @ 3834:b6ffac42c34e

Last change on this file since 3834:b6ffac42c34e was 3834:b6ffac42c34e, checked in by phi, 14 years ago

use roar_mm_*()

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