source: roaraudio/libroar/vio_dstr.c @ 5373:8da157c10483

Last change on this file since 5373:8da157c10483 was 5367:b2aae2f6b2d7, checked in by phi, 12 years ago

Added VIO/DSTR for SOCKS proxy (See: #187)

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