source: roaraudio/libroar/vio.c @ 5425:2c3f247e8f9a

Last change on this file since 5425:2c3f247e8f9a was 5408:c6d31c2e4a51, checked in by phi, 12 years ago

re-ported to f* s* bla win32 !*!*Dd*S!, I hate this.

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