source: roaraudio/libroar/vio.c @ 5242:97239101cee9

Last change on this file since 5242:97239101cee9 was 5242:97239101cee9, checked in by phi, 12 years ago

some roard compiler warnings cleanup

File size: 20.8 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-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#define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */
37#include "libroar.h"
38
39#ifdef ROAR_HAVE_H_SYS_IOCTL
40#include <sys/ioctl.h>
41#endif
42
43#ifdef ROAR_HAVE_IO_POSIX
44#define _CAN_OPERATE
45#endif
46
47int roar_vio_init_calls (struct roar_vio_calls * calls) {
48 roar_debug_warn_obsolete("roar_vio_init_calls", "roar_vio_clear_calls", NULL);
49
50#ifdef _CAN_OPERATE
51 if ( calls == NULL ) {
52  roar_err_set(ROAR_ERROR_FAULT);
53  return -1;
54 }
55
56 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
57
58/*
59 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
60 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
61 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
62*/
63
64 calls->read     = roar_vio_basic_read;
65 calls->write    = roar_vio_basic_write;
66 calls->lseek    = roar_vio_basic_lseek;
67 calls->nonblock = roar_vio_basic_nonblock;
68 calls->sync     = roar_vio_basic_sync;
69 calls->ctl      = roar_vio_basic_ctl;
70 calls->close    = roar_vio_basic_close;
71
72 return 0;
73#else
74 return -1;
75#endif
76}
77
78int roar_vio_clear_calls (struct roar_vio_calls * calls) {
79 if ( calls == NULL ) {
80  roar_err_set(ROAR_ERROR_FAULT);
81  return -1;
82 }
83
84 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
85
86 return 0;
87}
88
89int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
90 if ( vio == NULL ) {
91  roar_err_set(ROAR_ERROR_FAULT);
92  return -1;
93 }
94
95 vio->inst = inst;
96
97 return 0;
98}
99
100int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
101 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
102}
103
104int roar_vio_get_fh   (struct roar_vio_calls * vio) {
105 if ( vio == NULL ) {
106  roar_err_set(ROAR_ERROR_FAULT);
107  return -1;
108 }
109
110 return ((int)(ROAR_INSTINT)vio->inst) - 1;
111}
112
113
114ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
115 ssize_t ret;
116
117 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
118
119 if ( vio == NULL ) {
120  roar_err_set(ROAR_ERROR_FAULT);
121  return -1;
122 }
123
124 if ( vio->read == NULL ) {
125  roar_err_set(ROAR_ERROR_NOSYS);
126  return -1;
127 }
128
129 roar_err_clear_all();
130 ret = vio->read(vio, buf, count);
131 roar_err_update();
132
133 return ret;
134}
135
136ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
137 ssize_t ret;
138
139 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
140
141 if ( vio == NULL ) {
142  roar_err_set(ROAR_ERROR_FAULT);
143  return -1;
144 }
145
146 if ( vio->write == NULL ) {
147  roar_err_set(ROAR_ERROR_NOSYS);
148  return -1;
149 }
150
151 roar_err_clear_all();
152 ret = vio->write(vio, buf, count);
153 roar_err_update();
154
155 return ret;
156}
157
158off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
159 off_t ret;
160
161 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%u, whence=%i) = ?", vio, (unsigned int)offset, whence);
162
163 if ( vio == NULL ) {
164  roar_err_set(ROAR_ERROR_FAULT);
165  return -1;
166 }
167
168 if ( vio->lseek == NULL ) {
169  roar_err_set(ROAR_ERROR_NOSYS);
170  return -1;
171 }
172
173 roar_err_clear_all();
174 ret = vio->lseek(vio, offset, whence);
175 roar_err_update();
176
177 return ret;
178}
179
180int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
181 int ret;
182
183 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
184
185 if ( vio == NULL ) {
186  roar_err_set(ROAR_ERROR_FAULT);
187  return -1;
188 }
189
190 if ( vio->nonblock == NULL ) {
191  roar_err_set(ROAR_ERROR_NOSYS);
192  return -1;
193 }
194
195 roar_err_clear_all();
196 ret = vio->nonblock(vio, state);
197 roar_err_update();
198
199 return ret;
200}
201
202int     roar_vio_sync    (struct roar_vio_calls * vio) {
203 int ret;
204
205 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
206
207 if ( vio == NULL ) {
208  roar_err_set(ROAR_ERROR_FAULT);
209  return -1;
210 }
211
212 if ( vio->sync == NULL ) {
213  roar_err_set(ROAR_ERROR_NOSYS);
214  return -1;
215 }
216
217 roar_err_clear_all();
218 ret = vio->sync(vio);
219 roar_err_update();
220
221 return ret;
222}
223
224int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
225 int ret;
226
227 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
228
229 if ( vio == NULL ) {
230  roar_err_set(ROAR_ERROR_FAULT);
231  return -1;
232 }
233
234 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
235
236 switch (cmd) {
237  case ROAR_VIO_CTL_CONFLICTING_ID_0:
238  case ROAR_VIO_CTL_CONFLICTING_ID_1:
239    ROAR_ERR("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): Your progam uses a VIO ctl call with a conflicting ID.", vio, cmd, data);
240    ROAR_ERR("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): Please recompile your program to fix this. (No additional steps are required beside that.)", vio, cmd, data);
241    ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p) = -1", vio, cmd, data);
242    roar_err_set(ROAR_ERROR_BADRQC);
243    return -1;
244   break;
245 }
246
247 if ( vio->ctl == NULL ) {
248  roar_err_set(ROAR_ERROR_NOSYS);
249  return -1;
250 }
251
252 roar_err_clear_all();
253 ret = vio->ctl(vio, cmd, data);
254 roar_err_update();
255
256 return ret;
257}
258
259int     roar_vio_close    (struct roar_vio_calls * vio) {
260 int ret;
261
262 ROAR_DBG("roar_vio_close(vio=%p) = ?", vio);
263
264 if ( vio == NULL ) {
265  roar_err_set(ROAR_ERROR_FAULT);
266  return -1;
267 }
268
269 if ( vio->close == NULL ) {
270  roar_err_set(ROAR_ERROR_NOSYS);
271  return -1;
272 }
273
274 roar_err_clear_all();
275 ret = vio->close(vio);
276 roar_err_update();
277
278 return ret;
279}
280
281// specal commands:
282int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
283 if (dst == NULL || calls == NULL) {
284  roar_err_set(ROAR_ERROR_FAULT);
285  return -1;
286 }
287
288 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
289}
290
291int     roar_vio_shutdown(struct roar_vio_calls * vio,   int how) {
292 if (vio == NULL) {
293  roar_err_set(ROAR_ERROR_FAULT);
294  return -1;
295 }
296
297 if ( ( (how | ROAR_VIO_SHUTDOWN_READ|ROAR_VIO_SHUTDOWN_WRITE|ROAR_VIO_SHUTDOWN_LISTEN) -
298        (ROAR_VIO_SHUTDOWN_READ|ROAR_VIO_SHUTDOWN_WRITE|ROAR_VIO_SHUTDOWN_LISTEN) ) != 0 ) {
299  roar_err_set(ROAR_ERROR_INVAL);
300  return -1;
301 }
302
303 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
304}
305
306// converters:
307int     roar_vio_open_file     (struct roar_vio_calls * calls, const char * filename, int flags, mode_t mode) {
308#ifdef _CAN_OPERATE
309 int fh;
310
311 roar_debug_warn_obsolete("roar_vio_open_file", "roar_vio_open_dstr", NULL);
312
313 if ( calls == NULL || filename == NULL ) {
314  roar_err_set(ROAR_ERROR_FAULT);
315  return -1;
316 }
317
318#ifdef ROAR_TARGET_WIN32
319 flags |= O_BINARY;
320#endif
321
322 roar_err_clear_all();
323 if ( (fh = open(filename, flags, mode)) == -1 ) {
324  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
325  roar_err_update();
326  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
327  return -1;
328 }
329
330 if ( roar_vio_open_fh(calls, fh) == -1 ) {
331  close(fh);
332  roar_err_update();
333  return -1;
334 }
335
336 roar_err_update();
337 return 0;
338#else
339 return -1;
340#endif
341}
342
343int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
344 if ( calls == NULL ) {
345  roar_err_set(ROAR_ERROR_FAULT);
346  return -1;
347 }
348
349 roar_libroar_nowarn();
350 if ( roar_vio_init_calls(calls) == -1 ) {
351  roar_libroar_warn();
352  return -1;
353 }
354 roar_libroar_warn();
355
356 return roar_vio_set_fh(calls, fh);
357}
358
359int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
360 if ( calls == NULL ) {
361  roar_err_set(ROAR_ERROR_FAULT);
362  return -1;
363 }
364
365 if ( roar_vio_open_fh(calls, fh) == -1 )
366  return -1;
367
368#ifdef ROAR_TARGET_WIN32
369 calls->read     = roar_vio_winsock_read;
370 calls->write    = roar_vio_winsock_write;
371 calls->nonblock = roar_vio_winsock_nonblock;
372 calls->sync     = roar_vio_winsock_sync;
373 calls->ctl      = roar_vio_winsock_ctl;
374 calls->close    = roar_vio_winsock_close;
375#else
376 calls->sync     = roar_vio_null_sync;
377#endif
378
379 return 0;
380}
381
382int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
383 int fh;
384
385 if ( calls == NULL ) {
386  roar_err_set(ROAR_ERROR_FAULT);
387  return -1;
388 }
389
390 if ( (fh = roar_socket_connect(host, port)) == -1 )
391  return -1;
392
393 return roar_vio_open_fh_socket(calls, fh);
394}
395
396int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
397 int fh;
398
399 if ( calls == NULL ) {
400  roar_err_set(ROAR_ERROR_FAULT);
401  return -1;
402 }
403
404 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
405  return -1;
406
407 return roar_vio_open_fh_socket(calls, fh);
408}
409
410
411// VIOs:
412
413// basic
414ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
415#ifdef _CAN_OPERATE
416 return read(roar_vio_get_fh(vio), buf, count);
417#else
418 roar_err_set(ROAR_ERROR_NOSYS);
419 return -1;
420#endif
421}
422
423ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
424#ifdef _CAN_OPERATE
425 return write(roar_vio_get_fh(vio), buf, count);
426#else
427 roar_err_set(ROAR_ERROR_NOSYS);
428 return -1;
429#endif
430}
431
432off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
433#ifdef _CAN_OPERATE
434 return lseek(roar_vio_get_fh(vio), offset, whence);
435#else
436 roar_err_set(ROAR_ERROR_NOSYS);
437 return -1;
438#endif
439}
440
441int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
442 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
443  return -1;
444
445 if ( state == ROAR_SOCKET_NONBLOCK )
446  return 0;
447
448 roar_vio_sync(vio);
449
450 return 0;
451}
452
453int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
454#ifdef ROAR_FDATASYNC
455 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
456#else
457 return 0;
458#endif
459}
460
461int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
462#ifdef ROAR_HAVE_H_SYS_IOCTL
463 struct roar_vio_sysio_ioctl * sysioctl;
464#endif
465#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
466 struct roar_vio_sysio_sockopt  * syssockopt;
467#endif
468 int tmp;
469 int s_r = 0, s_w = 0;
470#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
471 union {
472  struct sockaddr     sa;
473#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
474  struct sockaddr_in  in;
475#endif
476#ifdef ROAR_HAVE_UNIX
477  struct sockaddr_un  un;
478#endif
479#ifdef ROAR_HAVE_LIBDNET
480  struct sockaddr_dn  dn;
481#endif
482#ifdef ROAR_HAVE_IPV6
483  struct sockaddr_in6 in6;
484#endif
485#ifdef ROAR_HAVE_IPX
486  struct sockaddr_ipx ipx;
487#endif
488 } sockaddr;
489 socklen_t socklen;
490 struct roar_sockname * rsockname;
491#endif
492
493 if ( vio == NULL ) {
494  roar_err_set(ROAR_ERROR_FAULT);
495  return -1;
496 }
497
498 if ( cmd == -1 ) {
499  roar_err_set(ROAR_ERROR_INVAL);
500  return -1;
501 }
502
503 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
504
505 switch (cmd) {
506  case ROAR_VIO_CTL_GET_NAME:
507    if ( data == NULL ) {
508     roar_err_set(ROAR_ERROR_FAULT);
509     return -1;
510    }
511
512    *(char**)data = "basic";
513    return 0;
514   break;
515  case ROAR_VIO_CTL_GET_FH:
516  case ROAR_VIO_CTL_GET_READ_FH:
517  case ROAR_VIO_CTL_GET_WRITE_FH:
518  case ROAR_VIO_CTL_GET_SELECT_FH:
519  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
520  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
521    ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_*FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, roar_vio_get_fh(vio));
522    *(int*)data = roar_vio_get_fh(vio);
523    return 0;
524   break;
525  case ROAR_VIO_CTL_SET_NOSYNC:
526    vio->sync = NULL;
527    return 0;
528   break;
529  case ROAR_VIO_CTL_ACCEPT:
530    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
531    if ( tmp == -1 )
532     return -1;
533
534    // most proably a socket.
535    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
536#ifdef ROAR_TARGET_WIN32
537     closesocket(tmp);
538#else
539     close(tmp);
540#endif
541     return -1;
542    }
543
544    return 0;
545   break;
546  case ROAR_VIO_CTL_SHUTDOWN:
547    tmp = *(int*)data;
548
549    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
550     s_r = 1;
551     tmp -= ROAR_VIO_SHUTDOWN_READ;
552    }
553
554    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
555     s_w = 1;
556     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
557    }
558
559    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
560     roar_err_set(ROAR_ERROR_NOTSUP);
561     return -1;
562    }
563
564    if ( s_r && s_w ) {
565     tmp = SHUT_RDWR;
566    } else if ( s_r ) {
567     tmp = SHUT_RD;
568    } else if ( s_w ) {
569     tmp = SHUT_WR;
570    } else {
571     return 0; // nothing to do.
572    }
573
574    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
575   break;
576#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
577  case ROAR_VIO_CTL_GET_SOCKNAME:
578  case ROAR_VIO_CTL_GET_PEERNAME:
579    if ( data == NULL ) {
580     roar_err_set(ROAR_ERROR_FAULT);
581     return -1;
582    }
583
584    rsockname = data;
585
586    socklen = sizeof(sockaddr);
587
588    if ( cmd == ROAR_VIO_CTL_GET_SOCKNAME ) {
589#ifdef ROAR_HAVE_GETSOCKNAME
590     tmp = getsockname(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
591#else
592     roar_err_set(ROAR_ERROR_NOSYS);
593     return -1;
594#endif
595    } else if ( cmd == ROAR_VIO_CTL_GET_PEERNAME ) {
596#ifdef ROAR_HAVE_GETPEERNAME
597     tmp = getpeername(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
598#else
599     roar_err_set(ROAR_ERROR_NOSYS);
600     return -1;
601#endif
602    } else {
603     // memory corruption:
604     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
605     roar_err_set(ROAR_ERROR_CHERNOBYL);
606     return -1;
607    }
608
609    if ( tmp == -1 )
610     return -1;
611
612    memset(rsockname, 0, sizeof(struct roar_sockname));
613
614    switch (sockaddr.sa.sa_family) {
615#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
616     case AF_UNIX:
617       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
618       if ( sockaddr.un.sun_path[0] == 0 ) {
619        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
620        if ( rsockname->addr == NULL )
621         return -1;
622        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
623       } else {
624        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
625       }
626      break;
627#endif
628#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
629     case AF_DECnet:
630       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
631
632       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
633        roar_err_set(ROAR_ERROR_NOTSUP);
634        return -1;
635       }
636
637       rsockname->addr = roar_mm_malloc(28);
638       if ( rsockname->addr == NULL )
639        return -1;
640
641       snprintf(rsockname->addr, 28, "%i.%i::",
642                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
643                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
644
645       rsockname->port = sockaddr.dn.sdn_objnum;
646       if ( sockaddr.dn.sdn_objnum == 0 ) {
647        tmp = strlen(rsockname->addr);
648        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
649        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
650       }
651      break;
652#endif
653#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
654     case AF_INET:
655       rsockname->type = ROAR_SOCKET_TYPE_INET;
656       rsockname->port = ntohs(sockaddr.in.sin_port);
657       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
658      break;
659#endif
660#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
661     case AF_INET6:
662       rsockname->type = ROAR_SOCKET_TYPE_INET6;
663       rsockname->port = ntohs(sockaddr.in6.sin6_port);
664      break;
665#endif
666     default:
667       roar_err_set(ROAR_ERROR_NOTSUP);
668       return -1;
669    }
670    return 0;
671#endif
672   break;
673#ifdef ROAR_HAVE_H_SYS_IOCTL
674  case ROAR_VIO_CTL_SYSIO_IOCTL:
675    sysioctl = data;
676    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
677   break;
678#endif
679#ifdef ROAR_HAVE_GETSOCKOPT
680  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
681    syssockopt = data;
682    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
683   break;
684#endif
685#ifdef ROAR_HAVE_SETSOCKOPT
686  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
687    syssockopt = data;
688    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
689   break;
690#endif
691 }
692
693 roar_err_set(ROAR_ERROR_BADRQC);
694 return -1;
695}
696
697int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
698#ifdef _CAN_OPERATE
699 if ( roar_vio_get_fh(vio) != -1 )
700  return close(roar_vio_get_fh(vio));
701
702 return 0;
703#else
704 roar_err_set(ROAR_ERROR_NOSYS);
705 return -1;
706#endif
707}
708
709// null
710ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
711 if ( vio == NULL || buf == NULL ) {
712  roar_err_set(ROAR_ERROR_FAULT);
713  return -1;
714 }
715
716 return 0;
717}
718
719int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
720 return 0;
721}
722
723// pass
724int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
725 if ( calls == NULL || dst == NULL ) {
726  roar_err_set(ROAR_ERROR_FAULT);
727  return -1;
728 }
729
730 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
731
732 calls->read     = roar_vio_pass_read;
733 calls->write    = roar_vio_pass_write;
734 calls->lseek    = roar_vio_pass_lseek;
735 calls->nonblock = roar_vio_pass_nonblock;
736 calls->sync     = roar_vio_pass_sync;
737 calls->ctl      = roar_vio_pass_ctl;
738 calls->close    = roar_vio_pass_close;
739
740 calls->inst     = dst;
741
742 return 0;
743}
744
745ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
746 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
747}
748
749ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
750 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
751}
752
753off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
754 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
755}
756
757int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
758 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
759}
760
761int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
762 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
763}
764
765int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
766 if (vio == NULL) {
767  roar_err_set(ROAR_ERROR_FAULT);
768  return -1;
769 }
770
771 if (cmd == -1) {
772  roar_err_set(ROAR_ERROR_INVAL);
773  return -1;
774 }
775
776 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
777
778 switch (cmd) {
779  case ROAR_VIO_CTL_GET_NAME:
780    if ( data == NULL ) {
781     roar_err_set(ROAR_ERROR_FAULT);
782     return -1;
783    }
784
785    // dirty trick to get real name...
786    if ( vio->read == roar_vio_re_read ) {
787     *(char**)data = "re";
788    } else {
789     *(char**)data = "pass";
790    }
791    return 0;
792   break;
793  case ROAR_VIO_CTL_GET_NEXT:
794    *(struct roar_vio_calls **)data = vio->inst;
795    return 0;
796   break;
797  case ROAR_VIO_CTL_SET_NEXT:
798    vio->inst = *(struct roar_vio_calls **)data;
799    return 0;
800   break;
801 }
802
803 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
804}
805
806int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
807 return roar_vio_close((struct roar_vio_calls *) vio->inst);
808}
809
810
811// re
812int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
813 if ( roar_vio_open_pass(calls, dst) == -1 )
814  return -1;
815
816 calls->read  = roar_vio_re_read;
817 calls->write = roar_vio_re_write;
818 calls->lseek = roar_vio_re_lseek;
819
820 return 0;
821}
822ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
823  size_t len =  0;
824 ssize_t r   = -1;
825
826 if ( vio == NULL ) {
827  roar_err_set(ROAR_ERROR_FAULT);
828  return -1;
829 }
830
831 if ( vio->inst == NULL ) {
832  roar_err_set(ROAR_ERROR_FAULT);
833  return -1;
834 }
835
836 roar_err_clear_all();
837
838 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
839  len   += r;
840  buf   += r;
841  count -= r;
842  if ( count == 0 )
843   break;
844 }
845
846 if ( len == 0 && r == -1 )
847  return -1;
848
849 return len;
850}
851
852ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
853  size_t len =  0;
854 ssize_t r   = -1;
855
856 if ( vio == NULL ) {
857  roar_err_set(ROAR_ERROR_FAULT);
858  return -1;
859 }
860
861 if ( vio->inst == NULL ) {
862  roar_err_set(ROAR_ERROR_FAULT);
863  return -1;
864 }
865
866 roar_err_clear_all();
867
868 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
869  len   += r;
870  buf   += r;
871  count -= r;
872  if ( count == 0 )
873   break;
874 }
875
876 if ( len == 0 && r == -1 )
877  return -1;
878
879 return len;
880}
881
882// TODO: we should do a some more intelgent thing here.
883off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
884 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
885}
886
887//ll
Note: See TracBrowser for help on using the repository browser.