source: roaraudio/libroar/vio.c @ 5311:778a3e7b2b66

Last change on this file since 5311:778a3e7b2b66 was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

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