source: roaraudio/libroar/vio_dstr.c @ 5248:0133acb5ae31

Last change on this file since 5248:0133acb5ae31 was 5227:beb26bba0901, checked in by phi, 12 years ago

added const keywords to roar_vio_open_dstr(), roar_vio_open_dstr_vio(), roar_vs_file_simple() and roar_vs_new_from_file()

File size: 25.9 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
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/* 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(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
249char *  roar_vio_dstr_get_name(int type) {
250 struct _roar_vio_dstr_type * ret;
251
252 if ( (ret = roar_vio_dstr_get_by_type(type)) != NULL )
253  return ret->name;
254
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");
292 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroareio");
293 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroarlight");
294 roar_dl_ra_init(ROAR_DL_HANDLE_DEFAULT, "libroarmidi");
295}
296
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
324int     roar_vio_open_default (struct roar_vio_calls * calls, struct roar_vio_defaults * def, char * opts) {
325 ROAR_DBG("roar_vio_open_default(calls=%p, def=%p, opts='%s') = ?", calls, def, opts);
326
327 if ( calls == NULL || def == NULL )
328  return -1;
329
330 switch (def->type) {
331  case ROAR_VIO_DEF_TYPE_NONE:
332   break;
333  case ROAR_VIO_DEF_TYPE_FILE:
334    roar_libroar_nowarn();
335    if ( roar_vio_open_file(calls, def->d.file, def->o_flags, def->o_mode) == -1 ) {
336     roar_libroar_warn();
337     ROAR_DBG("roar_vio_open_default(*): Can not open file: %i", roar_error);
338     return -1;
339    }
340    roar_libroar_warn();
341   break;
342  case ROAR_VIO_DEF_TYPE_SOCKET:
343     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
344      return -1;
345   break;
346  case ROAR_VIO_DEF_TYPE_FH:
347    if ( roar_vio_open_fh(calls, def->d.fh) == -1 )
348     return -1;
349   break;
350  case ROAR_VIO_DEF_TYPE_SOCKETFH:
351    if ( roar_vio_open_fh_socket(calls, def->d.fh) == -1 )
352     return -1;
353   break;
354  default:
355    return -1;
356 }
357
358 return 0;
359}
360#endif
361
362int     roar_vio_open_dstr    (struct roar_vio_calls * calls, const char * dstr, struct roar_vio_defaults * def, int dnum) {
363 return roar_vio_open_dstr_vio(calls, dstr, def, dnum, NULL);
364}
365
366#define _ret(x) do { int _err = roar_error; roar_mm_free(dstr_copy); roar_error = _err; roar_err_to_errno(); return (x); } while (0)
367
368int     roar_vio_open_dstr_vio(struct roar_vio_calls * calls,
369                               const char * dstr, struct roar_vio_defaults * def, int dnum,
370                               struct roar_vio_calls * vio) {
371#ifndef ROAR_WITHOUT_VIO_DSTR
372 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
373 char * dstr_copy;
374 char * next;
375 char * this;
376 char * name;
377 char * opts;
378 char * dst;
379 char * c;
380 int    inopts;
381 int    type;
382 int    cc = 1; // current chain element
383
384 if ( calls == NULL || dstr == NULL )
385  return -1;
386
387 if ( dnum != 0 && def == NULL )
388  return -1;
389
390 if ( (dstr_copy = roar_mm_strdup(dstr)) == NULL )
391  return -1;
392
393 memset(chain, 0, sizeof(chain));
394
395 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
396
397 next = dstr_copy;
398
399 while (next != NULL) {
400  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
401   _ret(-1);
402  }
403
404  this = next;
405  next = strstr(next, "##");
406
407  if (next != NULL) {
408   *next = 0;
409   next += 2;
410  }
411
412  // we have the current token in 'this'.
413
414  opts   = NULL;
415  dst    = NULL;
416
417  if ( strstr(this, ":") != NULL ) {
418   name   = this;
419   inopts = 0;
420   for (c = this; *c != 0; c++) {
421    if ( *c == '[' ) {
422     *c     = 0;
423     opts   = c + 1;
424     inopts = 1;
425    } else if ( *c == ']' &&  inopts ) {
426     *c     = 0;
427     inopts = 0;
428    } else if ( *c == ':' && !inopts ) {
429     *c     = 0;
430     dst    = *(c+1) == 0 ? NULL : c + 1;
431     break;
432    }
433   }
434  } else {
435   // we need to guess that this is here...
436   // currently we guess this is a file in all cases
437   name = "file";
438   dst  = this;
439  }
440
441  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
442
443  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
444   _ret(-1);
445  }
446
447  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
448
449  chain[cc].type     = type;
450  chain[cc].opts     = opts;
451  chain[cc].dst      = dst;
452  chain[cc].def      = NULL;
453  chain[cc].vio      = NULL;
454  chain[cc].need_vio = -1;
455  cc++;
456
457 }
458
459 chain[cc].type = ROAR_VIO_DSTR_OBJT_EOL;
460
461 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
462
463 if ( roar_vio_dstr_parse_opts(chain) == -1 ) {
464  _ret(-1);
465 }
466
467 if ( roar_vio_dstr_set_defaults(chain, cc, def, dnum) == -1 ) {
468  _ret(-1);
469 }
470
471 if ( roar_vio_dstr_build_chain(chain, calls, vio) == -1 ) {
472  _ret(-1);
473 }
474
475 _ret(0);
476#else
477 return -1;
478#endif
479}
480
481#undef _ret
482
483#ifndef ROAR_WITHOUT_VIO_DSTR
484int     roar_vio_dstr_parse_opts(struct roar_vio_dstr_chain * chain) {
485 if ( chain == NULL )
486  return -1;
487
488 // TODO: we should add some code here later...
489
490 return 0;
491}
492
493int     roar_vio_dstr_set_defaults(struct roar_vio_dstr_chain * chain, int len, struct roar_vio_defaults * def, int dnum) {
494 struct _roar_vio_dstr_type * type;
495 struct roar_vio_dstr_chain * c, * next;
496 int i;
497 int tmp[8];
498
499 if ( chain == NULL )
500  return -1;
501
502 if ( def == NULL && dnum != 0 )
503  return -1;
504
505 if ( dnum > 1 ) /* currently not supported */
506  return -1;
507
508 if ( dnum == 0 )
509  def = NULL;
510
511 chain[len].def = def;
512
513 for (i = len; i >= 0; i--) {
514  c    = &chain[i];
515
516  if ( i > 0 ) {
517   next = &chain[i-1];
518  } else {
519   next = NULL;
520
521   if ( c->type != ROAR_VIO_DSTR_OBJT_INTERNAL )
522    return -1;
523  }
524
525  memset(tmp, 0, sizeof(tmp));
526
527  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));
528  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,
529                   roar_vio_dstr_get_name(c->type), c->def, c->def == NULL ? -1 : c->def->type);
530
531  c->need_vio = 1;
532
533  switch (c->type) {
534   case ROAR_VIO_DSTR_OBJT_INTERNAL:
535     c->need_vio = 0;
536    break;
537   case ROAR_VIO_DSTR_OBJT_EOL:
538     tmp[0] = 1;
539   case ROAR_VIO_DSTR_OBJT_PASS:
540   case ROAR_VIO_DSTR_OBJT_RE:
541   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
542   case ROAR_VIO_DSTR_OBJT_GZIP:
543   case ROAR_VIO_DSTR_OBJT_ZLIB:
544   case ROAR_VIO_DSTR_OBJT_BZIP2:
545   case ROAR_VIO_DSTR_OBJT_PGP:
546   case ROAR_VIO_DSTR_OBJT_PGP_ENC:
547   case ROAR_VIO_DSTR_OBJT_PGP_STORE:
548   case ROAR_VIO_DSTR_OBJT_SSL1:
549   case ROAR_VIO_DSTR_OBJT_SSL2:
550   case ROAR_VIO_DSTR_OBJT_SSL3:
551   case ROAR_VIO_DSTR_OBJT_TLS:
552   case ROAR_VIO_DSTR_OBJT_MAGIC:
553     if ( tmp[0] )
554      c->need_vio = 0;
555
556     next->def = c->def;
557    break;
558   case ROAR_VIO_DSTR_OBJT_NULL:
559   case ROAR_VIO_DSTR_OBJT_ZERO:
560   case ROAR_VIO_DSTR_OBJT_FULL:
561   case ROAR_VIO_DSTR_OBJT_NRANDOM:
562   case ROAR_VIO_DSTR_OBJT_TANTALOS:
563     next->def = &(next->store_def);
564     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_NONE, NULL, -1);
565    break;
566   case ROAR_VIO_DSTR_OBJT_FILE:
567     if ( c->dst == NULL ) /* should we allow multible cascaed file: objects? */
568      return -1;
569
570     c->need_vio = 0;
571     next->def = &(next->store_def);
572     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
573
574     if ( c->dst[0] == '/' && c->dst[1] == '/' ) {
575      next->def->d.file = c->dst + 1;
576     } else {
577      next->def->d.file = c->dst;
578     }
579    break;
580   case ROAR_VIO_DSTR_OBJT_FH:
581     tmp[0] = 1;
582   case ROAR_VIO_DSTR_OBJT_SOCKETFH:
583     c->need_vio = 0;
584     next->def = &(next->store_def);
585
586     if ( c->def != NULL ) {
587      tmp[2] = c->def->o_flags;
588      tmp[3] = c->def->o_mode;
589     } else {
590      tmp[2] = O_RDONLY;
591      tmp[3] = 0644;
592     }
593
594     if ( !strcasecmp(c->dst, "stdin") ) {
595      tmp[1] = ROAR_STDIN;
596      tmp[2] = O_RDONLY;
597     } else if ( !strcasecmp(c->dst, "stdout") ) {
598      tmp[1] = ROAR_STDOUT;
599      tmp[2] = O_WRONLY;
600     } else if ( !strcasecmp(c->dst, "stderr") ) {
601      tmp[1] = ROAR_STDERR;
602      tmp[2] = O_WRONLY;
603     } else {
604      if ( sscanf(c->dst, "%i", &tmp[1]) != 1 )
605       return -1;
606     }
607
608     roar_vio_dstr_init_defaults(next->def, tmp[0] ? ROAR_VIO_DEF_TYPE_FH : ROAR_VIO_DEF_TYPE_SOCKETFH, tmp[2], tmp[3]);
609     next->def->d.fh = tmp[1];
610    break;
611#ifdef ROAR_HAVE_UNIX
612   case ROAR_VIO_DSTR_OBJT_UNIX:
613     c->need_vio = 0;
614     next->def = &(next->store_def);
615
616     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
617      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
618       return -1;
619
620      if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_UNIX, SOCK_STREAM, c->def) == -1 )
621       return -1;
622     } else {                // we have a destination? -> fast way
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_unix_def(next->def, c->dst) == -1 )
627       return -1;
628     }
629    break;
630#endif
631   case ROAR_VIO_DSTR_OBJT_SOCKET:
632     c->need_vio = 0;
633     next->def = &(next->store_def);
634
635     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
636      return -1;
637
638     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, -1, SOCK_STREAM, c->def) == -1 )
639      return -1;
640    break;
641#ifdef ROAR_HAVE_LIBDNET
642   case ROAR_VIO_DSTR_OBJT_DECNET:
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_DECnet, SOCK_STREAM, c->def) == -1 )
650      return -1;
651    break;
652#endif
653#ifdef ROAR_HAVE_IPV4
654   case ROAR_VIO_DSTR_OBJT_TCP:
655     c->need_vio = 0;
656     next->def = &(next->store_def);
657
658     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
659      return -1;
660
661     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_STREAM, c->def) == -1 )
662      return -1;
663    break;
664   case ROAR_VIO_DSTR_OBJT_UDP:
665     c->need_vio = 0;
666     next->def = &(next->store_def);
667
668     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
669      return -1;
670
671     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_DGRAM, c->def) == -1 )
672      return -1;
673    break;
674#endif
675#ifdef ROAR_HAVE_IPV6
676   case ROAR_VIO_DSTR_OBJT_TCP6:
677     c->need_vio = 0;
678     next->def = &(next->store_def);
679
680     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
681      return -1;
682
683     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET6, SOCK_STREAM, c->def) == -1 )
684      return -1;
685    break;
686   case ROAR_VIO_DSTR_OBJT_UDP6:
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_INET6, SOCK_DGRAM, c->def) == -1 )
694      return -1;
695    break;
696#endif
697   case ROAR_VIO_DSTR_OBJT_HTTP09:
698   case ROAR_VIO_DSTR_OBJT_HTTP10:
699   case ROAR_VIO_DSTR_OBJT_HTTP11:
700     c->need_vio = 1;
701     next->def = &(next->store_def);
702
703     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 )
704      return -1;
705    break;
706   case ROAR_VIO_DSTR_OBJT_GOPHER:
707   case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
708     c->need_vio = 1;
709     next->def = &(next->store_def);
710
711     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 )
712      return -1;
713    break;
714   case ROAR_VIO_DSTR_OBJT_ICY:
715     c->need_vio = 1;
716     next->def = &(next->store_def);
717
718     if ( roar_vio_proto_init_def(next->def, c->dst, ROAR_VIO_PROTO_P_ICY, c->def) == -1 )
719      return -1;
720    break;
721   default:
722     if ( (type = roar_vio_dstr_get_by_type(c->type)) == NULL ) {
723      return -1;
724     }
725
726     if ( type->setdef == NULL )
727      _roar_vio_dstr_init_otherlibs();
728
729     if ( type->setdef == NULL ) {
730      return -1;
731     }
732
733     if ( type->setdef(c, next) == -1 ) {
734      return -1;
735     }
736  }
737
738  if ( next != NULL ) {
739   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
740                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
741                    next->def, next->def == NULL ? -1 : next->def->type);
742   if ( next->def != NULL ) {
743    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
744                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
745                     next->def->o_flags);
746   }
747  } else {
748   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
749                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
750  }
751 }
752
753 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
754
755 return 0;
756}
757
758#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)
759
760int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
761                                  struct roar_vio_calls * vio) {
762 struct _roar_vio_dstr_type * type;
763 struct roar_vio_dstr_chain * c;
764 struct roar_vio_defaults   * def;
765 struct roar_vio_calls      * tc, * prev;
766 int i;
767
768 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
769
770 if ( chain == NULL || calls == NULL )
771  return -1;
772
773 if ( roar_vio_open_stack2(calls, NULL) == -1 )
774  return -1;
775
776 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
777
778 if ( (def = chain->def) != NULL ) {
779  if ( (tc = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) {
780   _ret(-1);
781  }
782
783  if ( roar_vio_clear_calls(tc) == -1 ) {
784   roar_mm_free(tc);
785   _ret(-1);
786  }
787
788  if ( roar_vio_stack_add(calls, tc) == -1 ) {
789   _ret(-1);
790  }
791
792  if ( chain->opts == NULL ) {
793   if ( chain[1].type != ROAR_VIO_DSTR_OBJT_EOL ) {
794    chain->opts = chain[1].opts;
795   }
796  }
797
798  if ( roar_vio_open_default(tc, def, chain->opts) == -1 ) {
799   _ret(-1);
800  }
801
802  prev = tc;
803 } else {
804  prev = vio;
805 }
806
807 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
808  ROAR_DBG("roar_vio_dstr_build_chain(*): i=%i, c->type=0x%.4x(%s): need_vio=%i, def->o_flags=%i", i,
809                   c->type & 0xFFFF, roar_vio_dstr_get_name(c->type), c->need_vio, c->def->o_flags);
810
811  if ( c->need_vio ) {
812   if ( (tc = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) {
813    _ret(-1);
814   }
815
816   if ( roar_vio_clear_calls(tc) == -1 ) {
817    roar_mm_free(tc);
818    _ret(-1);
819   }
820
821   if ( roar_vio_stack_add(calls, tc) == -1 ) {
822    _ret(-1);
823   }
824
825
826   switch (c->type) {
827    case ROAR_VIO_DSTR_OBJT_PASS:
828      if ( roar_vio_open_pass(tc, prev) == -1 ) {
829       _ret(-1);
830      }
831     break;
832    case ROAR_VIO_DSTR_OBJT_RE:
833      if ( roar_vio_open_re(tc, prev) == -1 ) {
834       _ret(-1);
835      }
836     break;
837#ifdef ROAR_HAVE_LIBZ
838    case ROAR_VIO_DSTR_OBJT_GZIP:
839      //if ( roar_vio_open_gzip(tc, prev, -1) == -1 ) {
840      if ( roar_vio_open_zlib(tc, prev, -1, 1) == -1 ) {
841       _ret(-1);
842      }
843     break;
844    case ROAR_VIO_DSTR_OBJT_ZLIB:
845      //if ( roar_vio_open_gzip(tc, prev, -1) == -1 ) {
846      if ( roar_vio_open_zlib(tc, prev, -1, 0) == -1 ) {
847       _ret(-1);
848      }
849     break;
850#endif
851    case ROAR_VIO_DSTR_OBJT_BZIP2:
852    case ROAR_VIO_DSTR_OBJT_PGP:
853      if ( roar_vio_open_pgp_decrypt(tc, prev, NULL) == -1 ) {
854       _ret(-1);
855      }
856     break;
857    case ROAR_VIO_DSTR_OBJT_HTTP09:
858    case ROAR_VIO_DSTR_OBJT_HTTP10:
859    case ROAR_VIO_DSTR_OBJT_HTTP11:
860      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
861       _ret(-1);
862      }
863     break;
864    case ROAR_VIO_DSTR_OBJT_GOPHER:
865    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
866      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
867       _ret(-1);
868      }
869     break;
870    case ROAR_VIO_DSTR_OBJT_ICY:
871      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_ICY, c->def) == -1 ) {
872       _ret(-1);
873      }
874     break;
875    case ROAR_VIO_DSTR_OBJT_RTP2:
876      if ( roar_vio_open_rtp(tc, prev, c->dst, c->def) == -1 ) {
877       _ret(-1);
878      }
879     break;
880    case ROAR_VIO_DSTR_OBJT_PGP_ENC:
881    case ROAR_VIO_DSTR_OBJT_PGP_STORE:
882      if ( roar_vio_open_pgp_store(tc, prev, ROAR_VIO_PGP_OPTS_NONE) == -1 ) {
883       _ret(-1);
884      }
885     break;
886    case ROAR_VIO_DSTR_OBJT_TANTALOS:
887      if ( roar_vio_open_tantalos(tc, prev, c->dst, c->def) == -1 ) {
888       _ret(-1);
889      }
890     break;
891    case ROAR_VIO_DSTR_OBJT_NULL:
892    case ROAR_VIO_DSTR_OBJT_ZERO:
893    case ROAR_VIO_DSTR_OBJT_FULL:
894    case ROAR_VIO_DSTR_OBJT_NRANDOM:
895      if ( roar_vio_open_misc_by_name(tc, roar_vio_dstr_get_name(c->type)) == -1 ) {
896       _ret(-1);
897      }
898     break;
899    case ROAR_VIO_DSTR_OBJT_SSL1:
900    case ROAR_VIO_DSTR_OBJT_SSL2:
901    case ROAR_VIO_DSTR_OBJT_SSL3:
902    case ROAR_VIO_DSTR_OBJT_TLS:
903    case ROAR_VIO_DSTR_OBJT_MAGIC:
904      _ret(-1);
905     break;
906    default:
907      if ( (type = roar_vio_dstr_get_by_type(c->type)) == NULL ) {
908       _ret(-1);
909      }
910
911      if ( type->openvio == NULL )
912       _roar_vio_dstr_init_otherlibs();
913
914      if ( type->openvio == NULL ) {
915       _ret(-1);
916      }
917
918      if ( type->openvio(tc, prev, c) == -1 ) {
919       _ret(-1);
920      }
921   }
922
923   prev = tc;
924  } // else we can skip to the next :)
925 }
926
927 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
928 return 0;
929}
930
931#undef _ret
932#endif
933
934//ll
Note: See TracBrowser for help on using the repository browser.