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
RevLine 
[1321]1//vio_dstr.c:
2
3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
[1321]5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[1321]23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
[1365]38#ifndef ROAR_WITHOUT_VIO_DSTR
[5270]39static struct _roar_vio_dstr_type {
[5254]40 const int    id;
41 const char * name;
[3310]42 int (* setdef) (struct roar_vio_dstr_chain * cur,   struct roar_vio_dstr_chain * next);
43 int (* openvio)(struct roar_vio_calls      * calls, struct roar_vio_calls      * dst, struct roar_vio_dstr_chain * cur);
[1323]44 int    pdeftype[16];
45} _roar_vio_dstr_objs[] = {
46/*
47grep '^#define ROAR_VIO_DSTR_OBJT_' vio_dstr.h | cut -d' ' -f2 | while read objt; do name=`cut -d_ -f5,6,7,8,9,10 <<<$objt | tr A-Z a-z`; echo -e " {$objt,\t \"$name\","; echo "      {ROAR_VIO_DEF_TYPE_EOL}},"; done;
48*/
49 {ROAR_VIO_DSTR_OBJT_FILE,       "file",
[3310]50      NULL, NULL,
[1323]51      {ROAR_VIO_DEF_TYPE_EOL}},
52 {ROAR_VIO_DSTR_OBJT_FH,         "fh",
[3310]53      NULL, NULL,
[1323]54      {ROAR_VIO_DEF_TYPE_EOL}},
[1328]55 {ROAR_VIO_DSTR_OBJT_FD,         "fd",
[3310]56      NULL, NULL,
[1328]57      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]58 {ROAR_VIO_DSTR_OBJT_SOCKETFH,   "socketfh",
[3310]59      NULL, NULL,
[1323]60      {ROAR_VIO_DEF_TYPE_EOL}},
61 {ROAR_VIO_DSTR_OBJT_PASS,       "pass",
[3310]62      NULL, NULL,
[1323]63      {ROAR_VIO_DEF_TYPE_EOL}},
64 {ROAR_VIO_DSTR_OBJT_RE,         "re",
[3310]65      NULL, NULL,
[1323]66      {ROAR_VIO_DEF_TYPE_EOL}},
[3309]67 {ROAR_VIO_DSTR_OBJT_JUMBO,      "jumbo", /* TODO */
[3310]68      NULL, NULL,
[3309]69      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]70 {ROAR_VIO_DSTR_OBJT_EXEC,       "exec",
[3310]71      NULL, NULL,
[1323]72      {ROAR_VIO_DEF_TYPE_EOL}},
[4746]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}},
[1323]83
84 {ROAR_VIO_DSTR_OBJT_SOCKET,     "socket",
[3310]85      NULL, NULL,
[1323]86      {ROAR_VIO_DEF_TYPE_EOL}},
87 {ROAR_VIO_DSTR_OBJT_UNIX,       "unix",
[3310]88      NULL, NULL,
[1323]89      {ROAR_VIO_DEF_TYPE_EOL}},
90 {ROAR_VIO_DSTR_OBJT_DECNET,     "decnet",
[3310]91      NULL, NULL,
[1323]92      {ROAR_VIO_DEF_TYPE_EOL}},
93 {ROAR_VIO_DSTR_OBJT_TCP,        "tcp",
[3310]94      NULL, NULL,
[1323]95      {ROAR_VIO_DEF_TYPE_EOL}},
96 {ROAR_VIO_DSTR_OBJT_UDP,        "udp",
[3310]97      NULL, NULL,
[1323]98      {ROAR_VIO_DEF_TYPE_EOL}},
99 {ROAR_VIO_DSTR_OBJT_TCP6,       "tcp6",
[3310]100      NULL, NULL,
[1323]101      {ROAR_VIO_DEF_TYPE_EOL}},
102 {ROAR_VIO_DSTR_OBJT_UDP6,       "udp6",
[3310]103      NULL, NULL,
[1323]104      {ROAR_VIO_DEF_TYPE_EOL}},
105
106 {ROAR_VIO_DSTR_OBJT_SOCKS,      "socks",
[3310]107      NULL, NULL,
[1323]108      {ROAR_VIO_DEF_TYPE_EOL}},
109 {ROAR_VIO_DSTR_OBJT_SOCKS4,     "socks4",
[3310]110      NULL, NULL,
[1323]111      {ROAR_VIO_DEF_TYPE_EOL}},
112 {ROAR_VIO_DSTR_OBJT_SOCKS4A,    "socks4a",
[3310]113      NULL, NULL,
[1323]114      {ROAR_VIO_DEF_TYPE_EOL}},
115 {ROAR_VIO_DSTR_OBJT_SOCKS4D,    "socks4d",
[3310]116      NULL, NULL,
[1323]117      {ROAR_VIO_DEF_TYPE_EOL}},
118 {ROAR_VIO_DSTR_OBJT_SOCKS5,     "socks5",
[3310]119      NULL, NULL,
[1323]120      {ROAR_VIO_DEF_TYPE_EOL}},
121 {ROAR_VIO_DSTR_OBJT_SSH,        "ssh",
[3310]122      NULL, NULL,
[1323]123      {ROAR_VIO_DEF_TYPE_EOL}},
124
125 {ROAR_VIO_DSTR_OBJT_HTTP09,     "http09",
[3310]126      NULL, NULL,
[1323]127      {ROAR_VIO_DEF_TYPE_EOL}},
128 {ROAR_VIO_DSTR_OBJT_HTTP10,     "http10",
[3310]129      NULL, NULL,
[1323]130      {ROAR_VIO_DEF_TYPE_EOL}},
131 {ROAR_VIO_DSTR_OBJT_HTTP11,     "http11",
[3310]132      NULL, NULL,
[1323]133      {ROAR_VIO_DEF_TYPE_EOL}},
134 {ROAR_VIO_DSTR_OBJT_HTTP,       "http",
[3310]135      NULL, NULL,
[1323]136      {ROAR_VIO_DEF_TYPE_EOL}},
137 {ROAR_VIO_DSTR_OBJT_GOPHER,     "gopher",
[3310]138      NULL, NULL,
[1323]139      {ROAR_VIO_DEF_TYPE_EOL}},
[1349]140 {ROAR_VIO_DSTR_OBJT_GOPHER_PLUS,"gopher+",
[3310]141      NULL, NULL,
[1349]142      {ROAR_VIO_DEF_TYPE_EOL}},
[3078]143 {ROAR_VIO_DSTR_OBJT_ICY,        "icy",
[3310]144      NULL, NULL,
[3078]145      {ROAR_VIO_DEF_TYPE_EOL}},
[3277]146 {ROAR_VIO_DSTR_OBJT_RTP2,       "rtp2",
[3310]147      NULL, NULL,
[3277]148      {ROAR_VIO_DEF_TYPE_EOL}},
149 {ROAR_VIO_DSTR_OBJT_RTP,        "rtp",
[3310]150      NULL, NULL,
[3277]151      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]152
153 {ROAR_VIO_DSTR_OBJT_GZIP,       "gzip",
[3310]154      NULL, NULL,
[1323]155      {ROAR_VIO_DEF_TYPE_EOL}},
[4956]156 {ROAR_VIO_DSTR_OBJT_ZLIB,       "zlib",
157      NULL, NULL,
158      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]159 {ROAR_VIO_DSTR_OBJT_BZIP2,      "bzip2",
[3310]160      NULL, NULL,
[1323]161      {ROAR_VIO_DEF_TYPE_EOL}},
162
163 {ROAR_VIO_DSTR_OBJT_PGP,        "pgp",
[3310]164      NULL, NULL,
[1323]165      {ROAR_VIO_DEF_TYPE_EOL}},
166 {ROAR_VIO_DSTR_OBJT_PGP_ENC,    "pgp_enc",
[3310]167      NULL, NULL,
[1323]168      {ROAR_VIO_DEF_TYPE_EOL}},
169 {ROAR_VIO_DSTR_OBJT_PGP_STORE,  "pgp_store",
[3310]170      NULL, NULL,
[1323]171      {ROAR_VIO_DEF_TYPE_EOL}},
172 {ROAR_VIO_DSTR_OBJT_SSL1,       "ssl1",
[3310]173      NULL, NULL,
[1323]174      {ROAR_VIO_DEF_TYPE_EOL}},
175 {ROAR_VIO_DSTR_OBJT_SSL2,       "ssl2",
[3310]176      NULL, NULL,
[1323]177      {ROAR_VIO_DEF_TYPE_EOL}},
178 {ROAR_VIO_DSTR_OBJT_SSL3,       "ssl3",
[3310]179      NULL, NULL,
[1323]180      {ROAR_VIO_DEF_TYPE_EOL}},
181 {ROAR_VIO_DSTR_OBJT_TLS,        "tls",
[3310]182      NULL, NULL,
[1323]183      {ROAR_VIO_DEF_TYPE_EOL}},
184 {ROAR_VIO_DSTR_OBJT_SSLTLS,     "ssltls",
[3310]185      NULL, NULL,
[1323]186      {ROAR_VIO_DEF_TYPE_EOL}},
[4746]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}},
[1323]197
[3309]198 {ROAR_VIO_DSTR_OBJT_TRANSCODE,  "transcode", /* TODO  */
[3310]199      NULL, NULL,
[3309]200      {ROAR_VIO_DEF_TYPE_EOL}},
201
202 {ROAR_VIO_DSTR_OBJT_RAUM,       "raum",      /* TODO */
[3310]203      NULL, NULL,
[3309]204      {ROAR_VIO_DEF_TYPE_EOL}},
205 {ROAR_VIO_DSTR_OBJT_OGG,        "ogg",       /* TODO */
[3310]206      NULL, NULL,
[3309]207      {ROAR_VIO_DEF_TYPE_EOL}},
208 {ROAR_VIO_DSTR_OBJT_TAR,        "tar",       /* TODO */
[3310]209      NULL, NULL,
[3309]210      {ROAR_VIO_DEF_TYPE_EOL}},
211
212
[1323]213 {ROAR_VIO_DSTR_OBJT_MAGIC,      "magic",
[3310]214      NULL, NULL,
[1323]215      {ROAR_VIO_DEF_TYPE_EOL}},
[3116]216 {ROAR_VIO_DSTR_OBJT_TANTALOS,   "tantalos",
[3310]217      NULL, NULL,
[3116]218      {ROAR_VIO_DEF_TYPE_EOL}},
[1323]219
220 {ROAR_VIO_DSTR_OBJT_INTERNAL, "INTERNAL",
[3310]221      NULL, NULL,
[1323]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}},
[3310]224 {ROAR_VIO_DSTR_OBJT_EOL, NULL, NULL, NULL, {ROAR_VIO_DEF_TYPE_EOL}}
[1323]225};
226
[5254]227int     roar_vio_dstr_get_type(const char * str) {
[1323]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
[3312]238struct _roar_vio_dstr_type * roar_vio_dstr_get_by_type (int type) {
[1323]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 )
[3312]243   return &(_roar_vio_dstr_objs[i]);
[1323]244 }
245
[3312]246 return NULL;
247}
248
[5254]249const char *  roar_vio_dstr_get_name(const int type) {
[3312]250 struct _roar_vio_dstr_type * ret;
251
252 if ( (ret = roar_vio_dstr_get_by_type(type)) != NULL )
253  return ret->name;
254
[1325]255 if ( type == ROAR_VIO_DSTR_OBJT_EOL )
256  return "<<EOL>>";
257
[1323]258 return NULL;
259}
[3311]260
[3312]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
[3311]290static void _roar_vio_dstr_init_otherlibs (void) {
[5275]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);
[3311]295}
296
[1365]297#endif
[1323]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
[1353]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
[1365]323#ifndef ROAR_WITHOUT_VIO_DSTR
[5253]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
[3292]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
[5253]357 if ( calls == NULL || def == NULL ) {
358  roar_err_set(ROAR_ERROR_FAULT);
[1330]359  return -1;
[5253]360 }
[1330]361
362 switch (def->type) {
[3118]363  case ROAR_VIO_DEF_TYPE_NONE:
364   break;
[5282]365#ifdef ROAR_HAVE_IO_POSIX
[1330]366  case ROAR_VIO_DEF_TYPE_FILE:
[5253]367    if ( _open_file(calls, def->d.file, def->o_flags, def->o_mode) == -1 ) {
[4876]368     ROAR_DBG("roar_vio_open_default(*): Can not open file: %i", roar_error);
[1330]369     return -1;
[4876]370    }
[1330]371   break;
[5282]372#endif
[1330]373  case ROAR_VIO_DEF_TYPE_SOCKET:
[3293]374     if ( roar_vio_open_def_socket(calls, def, opts) == -1 )
[1333]375      return -1;
[1330]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:
[5282]386    roar_err_set(ROAR_ERROR_NOTSUP);
[1330]387    return -1;
388 }
389
390 return 0;
391}
[1365]392#endif
[1330]393
[5257]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
[5227]406int     roar_vio_open_dstr    (struct roar_vio_calls * calls, const char * dstr, struct roar_vio_defaults * def, int dnum) {
[1323]407 return roar_vio_open_dstr_vio(calls, dstr, def, dnum, NULL);
408}
409
[5227]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)
[1323]411
412int     roar_vio_open_dstr_vio(struct roar_vio_calls * calls,
[5227]413                               const char * dstr, struct roar_vio_defaults * def, int dnum,
[1323]414                               struct roar_vio_calls * vio) {
[1365]415#ifndef ROAR_WITHOUT_VIO_DSTR
[1325]416 struct roar_vio_dstr_chain chain[ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN];
[5227]417 char * dstr_copy;
[1323]418 char * next;
419 char * this;
420 char * name;
421 char * opts;
422 char * dst;
423 char * c;
424 int    inopts;
425 int    type;
[1328]426 int    cc = 1; // current chain element
[1323]427
428 if ( calls == NULL || dstr == NULL )
429  return -1;
430
431 if ( dnum != 0 && def == NULL )
432  return -1;
433
[5227]434 if ( (dstr_copy = roar_mm_strdup(dstr)) == NULL )
[1323]435  return -1;
436
[1325]437 memset(chain, 0, sizeof(chain));
[1323]438
[1328]439 chain[0].type = ROAR_VIO_DSTR_OBJT_INTERNAL;
440
[5227]441 next = dstr_copy;
[1323]442
443 while (next != NULL) {
[1325]444  if ( (cc+1) == ROAR_VIO_DSTR_MAX_OBJ_PER_CHAIN ) {
445   _ret(-1);
446  }
447
[1323]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
[1329]485  ROAR_DBG("roar_vio_open_dstr_vio(*): name='%s', opts='%s', dst='%s'", name, opts, dst);
[1323]486
487  if ( (type = roar_vio_dstr_get_type(name)) == -1 ) {
488   _ret(-1);
489  }
490
[1329]491  ROAR_DBG("roar_vio_open_dstr_vio(*): type=0x%.4x(%s)", type, roar_vio_dstr_get_name(type));
[1323]492
[1325]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
[1616]505 ROAR_DBG("roar_vio_open_dstr_vio(*): chain=%p", chain);
[1328]506
[1325]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);
[1323]517 }
518
[1325]519 _ret(0);
[1365]520#else
521 return -1;
522#endif
[1325]523}
524
525#undef _ret
526
[1365]527#ifndef ROAR_WITHOUT_VIO_DSTR
[1325]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) {
[3313]538 struct _roar_vio_dstr_type * type;
[1325]539 struct roar_vio_dstr_chain * c, * next;
540 int i;
[1328]541 int tmp[8];
[1325]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];
[1329]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  }
[1325]568
[1328]569  memset(tmp, 0, sizeof(tmp));
570
[1350]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));
[1616]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,
[1325]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) {
[1328]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;
[1325]583   case ROAR_VIO_DSTR_OBJT_PASS:
584   case ROAR_VIO_DSTR_OBJT_RE:
[3277]585   case ROAR_VIO_DSTR_OBJT_RTP2: // we currently only forward the defs
[1325]586   case ROAR_VIO_DSTR_OBJT_GZIP:
[4956]587   case ROAR_VIO_DSTR_OBJT_ZLIB:
[1325]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:
[1328]597     if ( tmp[0] )
598      c->need_vio = 0;
599
[1325]600     next->def = c->def;
601    break;
[4824]602   case ROAR_VIO_DSTR_OBJT_NULL:
603   case ROAR_VIO_DSTR_OBJT_ZERO:
[4876]604   case ROAR_VIO_DSTR_OBJT_FULL:
[4824]605   case ROAR_VIO_DSTR_OBJT_NRANDOM:
[3118]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;
[1325]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);
[1353]616     roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_FILE, c->def, -1);
[1325]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;
[1328]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;
[1337]655#ifdef ROAR_HAVE_UNIX
[1333]656   case ROAR_VIO_DSTR_OBJT_UNIX:
657     c->need_vio = 0;
658     next->def = &(next->store_def);
659
[1337]660     if ( c->dst == NULL ) { // we don't have a destination? -> slow way
[1353]661      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
662       return -1;
663
[1337]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
[1353]667      if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
668       return -1;
[1337]669
670      if ( roar_vio_socket_init_unix_def(next->def, c->dst) == -1 )
671       return -1;
[1333]672     }
673    break;
[1337]674#endif
[1335]675   case ROAR_VIO_DSTR_OBJT_SOCKET:
676     c->need_vio = 0;
677     next->def = &(next->store_def);
678
[1353]679     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
680      return -1;
681
[1335]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
[1353]690     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
691      return -1;
692
[1335]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
[1359]697#ifdef ROAR_HAVE_IPV4
[1335]698   case ROAR_VIO_DSTR_OBJT_TCP:
699     c->need_vio = 0;
700     next->def = &(next->store_def);
701
[1353]702     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
703      return -1;
704
[1335]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
[1353]712     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
713      return -1;
714
[1335]715     if ( roar_vio_socket_init_dstr_def(next->def, c->dst, AF_INET, SOCK_DGRAM, c->def) == -1 )
716      return -1;
717    break;
[1359]718#endif
[1335]719#ifdef ROAR_HAVE_IPV6
720   case ROAR_VIO_DSTR_OBJT_TCP6:
721     c->need_vio = 0;
722     next->def = &(next->store_def);
723
[1353]724     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
725      return -1;
726
[1335]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
[1353]734     if ( roar_vio_dstr_init_defaults_c(next->def, ROAR_VIO_DEF_TYPE_SOCKET, c->def, O_WRONLY) == -1 )
735      return -1;
736
[1335]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
[1349]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;
[3078]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;
[1325]765   default:
[3313]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     }
[1325]780  }
781
[1329]782  if ( next != NULL ) {
[1350]783   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def=%p, next->def->type=%i", i,
[1329]784                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
785                    next->def, next->def == NULL ? -1 : next->def->type);
[1349]786   if ( next->def != NULL ) {
[1616]787    ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next->def->o_flags=%i", i,
[1349]788                     c->type & 0xFFFF, roar_vio_dstr_get_name(c->type),
789                     next->def->o_flags);
790   }
[1329]791  } else {
[1350]792   ROAR_DBG("roar_vio_dstr_set_defaults(*): i=%i, c->type=0x%.4x(%s): next=NULL", i,
[1329]793                    c->type & 0xFFFF, roar_vio_dstr_get_name(c->type));
794  }
[1325]795 }
796
[1616]797 ROAR_DBG("roar_vio_dstr_set_defaults(*) = 0");
[1328]798
[1325]799 return 0;
800}
801
[4876]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)
[1328]803
804int     roar_vio_dstr_build_chain(struct roar_vio_dstr_chain * chain, struct roar_vio_calls * calls,
805                                  struct roar_vio_calls * vio) {
[3313]806 struct _roar_vio_dstr_type * type;
[1328]807 struct roar_vio_dstr_chain * c;
808 struct roar_vio_defaults   * def;
809 struct roar_vio_calls      * tc, * prev;
810 int i;
811
[1616]812 ROAR_DBG("roar_vio_dstr_build_chain(*) = ?");
[1328]813
[1325]814 if ( chain == NULL || calls == NULL )
815  return -1;
816
[4975]817 if ( roar_vio_open_stack2(calls, NULL) == -1 )
[1328]818  return -1;
819
[1329]820 ROAR_DBG("roar_vio_dstr_build_chain(*): chain=%p", chain);
[1328]821
822 if ( (def = chain->def) != NULL ) {
[4975]823  if ( (tc = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) {
[1328]824   _ret(-1);
825  }
826
[4964]827  if ( roar_vio_clear_calls(tc) == -1 ) {
[4975]828   roar_mm_free(tc);
[1337]829   _ret(-1);
830  }
831
[1328]832  if ( roar_vio_stack_add(calls, tc) == -1 ) {
833   _ret(-1);
834  }
835
[3292]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 ) {
[1330]843   _ret(-1);
[1328]844  }
[1330]845
[1328]846  prev = tc;
847 } else {
848  prev = vio;
849 }
850
851 for (i = 0; (c = &chain[i])->type != ROAR_VIO_DSTR_OBJT_EOL; i++) {
[1353]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);
[1349]854
[1328]855  if ( c->need_vio ) {
[4975]856   if ( (tc = roar_mm_malloc(sizeof(struct roar_vio_calls))) == NULL ) {
[1328]857    _ret(-1);
858   }
859
[4964]860   if ( roar_vio_clear_calls(tc) == -1 ) {
[4975]861    roar_mm_free(tc);
[1337]862    _ret(-1);
863   }
864
[1328]865   if ( roar_vio_stack_add(calls, tc) == -1 ) {
866    _ret(-1);
867   }
868
[1349]869
[1328]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;
[4984]881#ifdef ROAR_HAVE_LIBZ
[1328]882    case ROAR_VIO_DSTR_OBJT_GZIP:
[4956]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 ) {
[1328]891       _ret(-1);
892      }
893     break;
[4984]894#endif
[1328]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;
[1349]901    case ROAR_VIO_DSTR_OBJT_HTTP09:
902    case ROAR_VIO_DSTR_OBJT_HTTP10:
903    case ROAR_VIO_DSTR_OBJT_HTTP11:
[1353]904      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_HTTP, c->def) == -1 ) {
[1349]905       _ret(-1);
906      }
907     break;
908    case ROAR_VIO_DSTR_OBJT_GOPHER:
909    case ROAR_VIO_DSTR_OBJT_GOPHER_PLUS:
[1353]910      if ( roar_vio_open_proto(tc, prev, c->dst, ROAR_VIO_PROTO_P_GOPHER, c->def) == -1 ) {
[1349]911       _ret(-1);
912      }
913     break;
[3078]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;
[3277]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;
[1328]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;
[3116]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;
[4824]935    case ROAR_VIO_DSTR_OBJT_NULL:
936    case ROAR_VIO_DSTR_OBJT_ZERO:
[4876]937    case ROAR_VIO_DSTR_OBJT_FULL:
[4824]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;
[1328]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:
[3313]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      }
[1328]965   }
966
967   prev = tc;
968  } // else we can skip to the next :)
969 }
970
[1616]971 ROAR_DBG("roar_vio_dstr_build_chain(*) = 0");
[1328]972 return 0;
[1323]973}
974
[1328]975#undef _ret
[1365]976#endif
[1328]977
[1321]978//ll
Note: See TracBrowser for help on using the repository browser.