source: roaraudio/libroar/vio_dstr.c @ 5282:efca34f07bfc

Last change on this file since 5282:efca34f07bfc was 5282:efca34f07bfc, checked in by phi, 12 years ago

some ports to AVR and 8 and 16 bit archs

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