source: roaraudio/libroar/vio.c @ 5380:0504bc7766aa

Last change on this file since 5380:0504bc7766aa was 5378:481cd842e596, checked in by phi, 12 years ago

started with some flags needed for #127

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