source: roaraudio/libroar/vio_dstr.c @ 3313:24bb2b78bb91

Last change on this file since 3313:24bb2b78bb91 was 3313:24bb2b78bb91, checked in by phi, 14 years ago

support registered dstr types

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