source: roaraudio/libroar/vio.c @ 5388:e5cc8e03a3e1

Last change on this file since 5388:e5cc8e03a3e1 was 5388:e5cc8e03a3e1, checked in by phi, 12 years ago

Added support for refcount based VIOs as well as dynamically allocated VIOs (non-stack or global VIOs) (Closes: #127)

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