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
Line 
1//vio_dstr.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010
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
74 {ROAR_VIO_DSTR_OBJT_SOCKET,     "socket",
75      NULL, NULL,
76      {ROAR_VIO_DEF_TYPE_EOL}},
77 {ROAR_VIO_DSTR_OBJT_UNIX,       "unix",
78      NULL, NULL,
79      {ROAR_VIO_DEF_TYPE_EOL}},
80 {ROAR_VIO_DSTR_OBJT_DECNET,     "decnet",
81      NULL, NULL,
82      {ROAR_VIO_DEF_TYPE_EOL}},
83 {ROAR_VIO_DSTR_OBJT_TCP,        "tcp",
84      NULL, NULL,
85      {ROAR_VIO_DEF_TYPE_EOL}},
86 {ROAR_VIO_DSTR_OBJT_UDP,        "udp",
87      NULL, NULL,
88      {ROAR_VIO_DEF_TYPE_EOL}},
89 {ROAR_VIO_DSTR_OBJT_TCP6,       "tcp6",
90      NULL, NULL,
91      {ROAR_VIO_DEF_TYPE_EOL}},
92 {ROAR_VIO_DSTR_OBJT_UDP6,       "udp6",
93      NULL, NULL,
94      {ROAR_VIO_DEF_TYPE_EOL}},
95
96 {ROAR_VIO_DSTR_OBJT_SOCKS,      "socks",
97      NULL, NULL,
98      {ROAR_VIO_DEF_TYPE_EOL}},
99 {ROAR_VIO_DSTR_OBJT_SOCKS4,     "socks4",
100      NULL, NULL,
101      {ROAR_VIO_DEF_TYPE_EOL}},
102 {ROAR_VIO_DSTR_OBJT_SOCKS4A,    "socks4a",
103      NULL, NULL,
104      {ROAR_VIO_DEF_TYPE_EOL}},
105 {ROAR_VIO_DSTR_OBJT_SOCKS4D,    "socks4d",
106      NULL, NULL,
107      {ROAR_VIO_DEF_TYPE_EOL}},
108 {ROAR_VIO_DSTR_OBJT_SOCKS5,     "socks5",
109      NULL, NULL,
110      {ROAR_VIO_DEF_TYPE_EOL}},
111 {ROAR_VIO_DSTR_OBJT_SSH,        "ssh",
112      NULL, NULL,
113      {ROAR_VIO_DEF_TYPE_EOL}},
114
115 {ROAR_VIO_DSTR_OBJT_HTTP09,     "http09",
116      NULL, NULL,
117      {ROAR_VIO_DEF_TYPE_EOL}},
118 {ROAR_VIO_DSTR_OBJT_HTTP10,     "http10",
119      NULL, NULL,
120      {ROAR_VIO_DEF_TYPE_EOL}},
121 {ROAR_VIO_DSTR_OBJT_HTTP11,     "http11",
122      NULL, NULL,
123      {ROAR_VIO_DEF_TYPE_EOL}},
124 {ROAR_VIO_DSTR_OBJT_HTTP,       "http",
125      NULL, NULL,
126      {ROAR_VIO_DEF_TYPE_EOL}},
127 {ROAR_VIO_DSTR_OBJT_GOPHER,     "gopher",
128      NULL, NULL,
129      {ROAR_VIO_DEF_TYPE_EOL}},
130 {ROAR_VIO_DSTR_OBJT_GOPHER_PLUS,"gopher+",
131      NULL, NULL,
132      {ROAR_VIO_DEF_TYPE_EOL}},
133 {ROAR_VIO_DSTR_OBJT_ICY,        "icy",
134      NULL, NULL,
135      {ROAR_VIO_DEF_TYPE_EOL}},
136 {ROAR_VIO_DSTR_OBJT_RTP2,       "rtp2",
137      NULL, NULL,
138      {ROAR_VIO_DEF_TYPE_EOL}},
139 {ROAR_VIO_DSTR_OBJT_RTP,        "rtp",
140      NULL, NULL,
141      {ROAR_VIO_DEF_TYPE_EOL}},
142
143 {ROAR_VIO_DSTR_OBJT_GZIP,       "gzip",
144      NULL, NULL,
145      {ROAR_VIO_DEF_TYPE_EOL}},
146 {ROAR_VIO_DSTR_OBJT_BZIP2,      "bzip2",
147      NULL, NULL,
148      {ROAR_VIO_DEF_TYPE_EOL}},
149
150 {ROAR_VIO_DSTR_OBJT_PGP,        "pgp",
151      NULL, NULL,
152      {ROAR_VIO_DEF_TYPE_EOL}},
153 {ROAR_VIO_DSTR_OBJT_PGP_ENC,    "pgp_enc",
154      NULL, NULL,
155      {ROAR_VIO_DEF_TYPE_EOL}},
156 {ROAR_VIO_DSTR_OBJT_PGP_STORE,  "pgp_store",
157      NULL, NULL,
158      {ROAR_VIO_DEF_TYPE_EOL}},
159 {ROAR_VIO_DSTR_OBJT_SSL1,       "ssl1",
160      NULL, NULL,
161      {ROAR_VIO_DEF_TYPE_EOL}},
162 {ROAR_VIO_DSTR_OBJT_SSL2,       "ssl2",
163      NULL, NULL,
164      {ROAR_VIO_DEF_TYPE_EOL}},
165 {ROAR_VIO_DSTR_OBJT_SSL3,       "ssl3",
166      NULL, NULL,
167      {ROAR_VIO_DEF_TYPE_EOL}},
168 {ROAR_VIO_DSTR_OBJT_TLS,        "tls",
169      NULL, NULL,
170      {ROAR_VIO_DEF_TYPE_EOL}},
171 {ROAR_VIO_DSTR_OBJT_SSLTLS,     "ssltls",
172      NULL, NULL,
173      {ROAR_VIO_DEF_TYPE_EOL}},
174
175 {ROAR_VIO_DSTR_OBJT_TRANSCODE,  "transcode", /* TODO  */
176      NULL, NULL,
177      {ROAR_VIO_DEF_TYPE_EOL}},
178
179 {ROAR_VIO_DSTR_OBJT_RAUM,       "raum",      /* TODO */
180      NULL, NULL,
181      {ROAR_VIO_DEF_TYPE_EOL}},
182 {ROAR_VIO_DSTR_OBJT_OGG,        "ogg",       /* TODO */
183      NULL, NULL,
184      {ROAR_VIO_DEF_TYPE_EOL}},
185 {ROAR_VIO_DSTR_OBJT_TAR,        "tar",       /* TODO */
186      NULL, NULL,
187      {ROAR_VIO_DEF_TYPE_EOL}},
188
189
190 {ROAR_VIO_DSTR_OBJT_MAGIC,      "magic",
191      NULL, NULL,
192      {ROAR_VIO_DEF_TYPE_EOL}},
193 {ROAR_VIO_DSTR_OBJT_TANTALOS,   "tantalos",
194      NULL, NULL,
195      {ROAR_VIO_DEF_TYPE_EOL}},
196
197 {ROAR_VIO_DSTR_OBJT_INTERNAL, "INTERNAL",
198      NULL, NULL,
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}},
201 {ROAR_VIO_DSTR_OBJT_EOL, NULL, NULL, NULL, {ROAR_VIO_DEF_TYPE_EOL}}
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
215struct _roar_vio_dstr_type * roar_vio_dstr_get_by_type (int type) {
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 )
220   return &(_roar_vio_dstr_objs[i]);
221 }
222
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
232 if ( type == ROAR_VIO_DSTR_OBJT_EOL )
233  return "<<EOL>>";
234
235 return NULL;
236}
237
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
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
274#endif
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
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
300#ifndef ROAR_WITHOUT_VIO_DSTR
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
304 if ( calls == NULL || def == NULL )
305  return -1;
306
307 switch (def->type) {
308  case ROAR_VIO_DEF_TYPE_NONE:
309   break;
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:
315     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
316      return -1;
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}
332#endif
333
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
338#define _ret(x) roar_mm_free(dstr); return (x)
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) {
343#ifndef ROAR_WITHOUT_VIO_DSTR
344 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
345 char * next;
346 char * this;
347 char * name;
348 char * opts;
349 char * dst;
350 char * c;
351 int    inopts;
352 int    type;
353 int    cc = 1; // current chain element
354
355 if ( calls == NULL || dstr == NULL )
356  return -1;
357
358 if ( dnum != 0 && def == NULL )
359  return -1;
360
361 if ( (dstr = roar_mm_strdup(dstr)) == NULL )
362  return -1;
363
364 memset(chain, 0, sizeof(chain));
365
366 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
367
368 next = dstr;
369
370 while (next != NULL) {
371  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
372   _ret(-1);
373  }
374
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
412  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
413
414  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
415   _ret(-1);
416  }
417
418  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
419
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
432 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
433
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);
444 }
445
446 _ret(0);
447#else
448 return -1;
449#endif
450}
451
452#undef _ret
453
454#ifndef ROAR_WITHOUT_VIO_DSTR
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) {
465 struct _roar_vio_dstr_type * type;
466 struct roar_vio_dstr_chain * c, * next;
467 int i;
468 int tmp[8];
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];
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  }
495
496  memset(tmp, 0, sizeof(tmp));
497
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));
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,
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) {
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;
510   case ROAR_VIO_DSTR_OBJT_PASS:
511   case ROAR_VIO_DSTR_OBJT_RE:
512   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
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:
523     if ( tmp[0] )
524      c->need_vio = 0;
525
526     next->def = c->def;
527    break;
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;
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);
538     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
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;
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;
577#ifdef ROAR_HAVE_UNIX
578   case ROAR_VIO_DSTR_OBJT_UNIX:
579     c->need_vio = 0;
580     next->def = &(next->store_def);
581
582     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
583      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
584       return -1;
585
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
589      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
590       return -1;
591
592      if ( roar_vio_socket_init_unix_def(next->def, c->dst) == -1 )
593       return -1;
594     }
595    break;
596#endif
597   case ROAR_VIO_DSTR_OBJT_SOCKET:
598     c->need_vio = 0;
599     next->def = &(next->store_def);
600
601     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
602      return -1;
603
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
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_dstr_def(next->def, c->dst, AF_DECnet, SOCK_STREAM, c->def) == -1 )
616      return -1;
617    break;
618#endif
619#ifdef ROAR_HAVE_IPV4
620   case ROAR_VIO_DSTR_OBJT_TCP:
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, 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
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, AF_INET, SOCK_DGRAM, c->def) == -1 )
638      return -1;
639    break;
640#endif
641#ifdef ROAR_HAVE_IPV6
642   case ROAR_VIO_DSTR_OBJT_TCP6:
643     c->need_vio = 0;
644     next->def = &(next->store_def);
645
646     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
647      return -1;
648
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
656     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
657      return -1;
658
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
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;
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;
687   default:
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     }
702  }
703
704  if ( next != NULL ) {
705   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
706                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
707                    next->def, next->def == NULL ? -1 : next->def->type);
708   if ( next->def != NULL ) {
709    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
710                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
711                     next->def->o_flags);
712   }
713  } else {
714   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
715                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
716  }
717 }
718
719 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
720
721 return 0;
722}
723
724#define _ret(x) roar_vio_close(calls); ROAR_DBG("roar_vio_dstr_build_chain(*) = %i", (x)); return (x)
725
726int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
727                                  struct roar_vio_calls * vio) {
728 struct _roar_vio_dstr_type * type;
729 struct roar_vio_dstr_chain * c;
730 struct roar_vio_defaults   * def;
731 struct roar_vio_calls      * tc, * prev;
732 int i;
733
734 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
735
736 if ( chain == NULL || calls == NULL )
737  return -1;
738
739 if ( roar_vio_open_stack(calls) == -1 )
740  return -1;
741
742 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
743
744 if ( (def = chain->def) != NULL ) {
745  if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
746   _ret(-1);
747  }
748
749  if ( roar_vio_init_calls(tc) == -1 ) {
750   free(tc);
751   _ret(-1);
752  }
753
754  if ( roar_vio_stack_add(calls, tc) == -1 ) {
755   _ret(-1);
756  }
757
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 ) {
765   _ret(-1);
766  }
767
768  prev = tc;
769 } else {
770  prev = vio;
771 }
772
773 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
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);
776
777  if ( c->need_vio ) {
778   if ( (tc = malloc(sizeof(struct roar_vio_calls))) == NULL ) {
779    _ret(-1);
780   }
781
782   if ( roar_vio_init_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
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;
814    case ROAR_VIO_DSTR_OBJT_HTTP09:
815    case ROAR_VIO_DSTR_OBJT_HTTP10:
816    case ROAR_VIO_DSTR_OBJT_HTTP11:
817      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
818       _ret(-1);
819      }
820     break;
821    case ROAR_VIO_DSTR_OBJT_GOPHER:
822    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
823      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
824       _ret(-1);
825      }
826     break;
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;
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;
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;
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;
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:
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      }
870   }
871
872   prev = tc;
873  } // else we can skip to the next :)
874 }
875
876 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
877 return 0;
878}
879
880#undef _ret
881#endif
882
883//ll
Note: See TracBrowser for help on using the repository browser.