source: roaraudio/libroar/vio.c @ 5407:edd703e264d0

Last change on this file since 5407:edd703e264d0 was 5391:7847fd46ebbf, checked in by phi, 12 years ago

only accept() if we have accept()

File size: 19.5 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#ifdef ROAR_HAVE_ACCEPT
453    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
454    if ( tmp == -1 )
455     return -1;
456
457    // most proably a socket.
458    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
459#ifdef ROAR_TARGET_WIN32
460     closesocket(tmp);
461#else
462     close(tmp);
463#endif
464     return -1;
465    }
466
467    return 0;
468#else
469    roar_err_set(ROAR_ERROR_NOSYS);
470    return -1;
471#endif
472   break;
473  case ROAR_VIO_CTL_SHUTDOWN:
474    tmp = *(int*)data;
475
476    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
477     s_r = 1;
478     tmp -= ROAR_VIO_SHUTDOWN_READ;
479    }
480
481    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
482     s_w = 1;
483     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
484    }
485
486    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
487     roar_err_set(ROAR_ERROR_NOTSUP);
488     return -1;
489    }
490
491    if ( s_r && s_w ) {
492     tmp = SHUT_RDWR;
493    } else if ( s_r ) {
494     tmp = SHUT_RD;
495    } else if ( s_w ) {
496     tmp = SHUT_WR;
497    } else {
498     return 0; // nothing to do.
499    }
500
501    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
502   break;
503#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
504  case ROAR_VIO_CTL_GET_SOCKNAME:
505  case ROAR_VIO_CTL_GET_PEERNAME:
506    if ( data == NULL ) {
507     roar_err_set(ROAR_ERROR_FAULT);
508     return -1;
509    }
510
511    rsockname = data;
512
513    socklen = sizeof(sockaddr);
514
515    if ( cmd == ROAR_VIO_CTL_GET_SOCKNAME ) {
516#ifdef ROAR_HAVE_GETSOCKNAME
517     tmp = getsockname(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
518#else
519     roar_err_set(ROAR_ERROR_NOSYS);
520     return -1;
521#endif
522    } else if ( cmd == ROAR_VIO_CTL_GET_PEERNAME ) {
523#ifdef ROAR_HAVE_GETPEERNAME
524     tmp = getpeername(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
525#else
526     roar_err_set(ROAR_ERROR_NOSYS);
527     return -1;
528#endif
529    } else {
530     // memory corruption:
531     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
532     roar_err_set(ROAR_ERROR_CHERNOBYL);
533     return -1;
534    }
535
536    if ( tmp == -1 )
537     return -1;
538
539    memset(rsockname, 0, sizeof(struct roar_sockname));
540
541    switch (sockaddr.sa.sa_family) {
542#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
543     case AF_UNIX:
544       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
545       if ( sockaddr.un.sun_path[0] == 0 ) {
546        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
547        if ( rsockname->addr == NULL )
548         return -1;
549        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
550       } else {
551        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
552       }
553      break;
554#endif
555#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
556     case AF_DECnet:
557       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
558
559       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
560        roar_err_set(ROAR_ERROR_NOTSUP);
561        return -1;
562       }
563
564       rsockname->addr = roar_mm_malloc(28);
565       if ( rsockname->addr == NULL )
566        return -1;
567
568       snprintf(rsockname->addr, 28, "%i.%i::",
569                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
570                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
571
572       rsockname->port = sockaddr.dn.sdn_objnum;
573       if ( sockaddr.dn.sdn_objnum == 0 ) {
574        tmp = strlen(rsockname->addr);
575        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
576        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
577       }
578      break;
579#endif
580#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
581     case AF_INET:
582       rsockname->type = ROAR_SOCKET_TYPE_INET;
583       rsockname->port = ntohs(sockaddr.in.sin_port);
584       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
585      break;
586#endif
587#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
588     case AF_INET6:
589       rsockname->type = ROAR_SOCKET_TYPE_INET6;
590       rsockname->port = ntohs(sockaddr.in6.sin6_port);
591      break;
592#endif
593     default:
594       roar_err_set(ROAR_ERROR_NOTSUP);
595       return -1;
596    }
597    return 0;
598#endif
599   break;
600#ifdef ROAR_HAVE_H_SYS_IOCTL
601  case ROAR_VIO_CTL_SYSIO_IOCTL:
602    sysioctl = data;
603    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
604   break;
605#endif
606#ifdef ROAR_HAVE_GETSOCKOPT
607  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
608    syssockopt = data;
609    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
610   break;
611#endif
612#ifdef ROAR_HAVE_SETSOCKOPT
613  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
614    syssockopt = data;
615    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
616   break;
617#endif
618  case ROAR_VIO_CTL_NONBLOCK:
619    if ( roar_socket_nonblock(roar_vio_get_fh(vio), *(int*)data) == -1 )
620     return -1;
621
622    if ( *(int*)data == ROAR_SOCKET_NONBLOCK )
623     return 0;
624
625    roar_vio_sync(vio);
626    return 0;
627   break;
628 }
629
630 roar_err_set(ROAR_ERROR_BADRQC);
631 return -1;
632}
633
634int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
635#ifdef _CAN_OPERATE
636 if ( roar_vio_get_fh(vio) != -1 )
637  return close(roar_vio_get_fh(vio));
638
639 return 0;
640#else
641 roar_err_set(ROAR_ERROR_NOSYS);
642 return -1;
643#endif
644}
645
646// null
647ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
648 (void)count;
649
650 if ( vio == NULL || buf == NULL ) {
651  roar_err_set(ROAR_ERROR_FAULT);
652  return -1;
653 }
654
655 return 0;
656}
657
658int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
659 (void)vio;
660 return 0;
661}
662
663// pass
664int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
665 if ( calls == NULL || dst == NULL ) {
666  roar_err_set(ROAR_ERROR_FAULT);
667  return -1;
668 }
669
670 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
671 calls->flags    = ROAR_VIO_FLAGS_NONE;
672 calls->refc     = 1;
673
674 calls->read     = roar_vio_pass_read;
675 calls->write    = roar_vio_pass_write;
676 calls->lseek    = roar_vio_pass_lseek;
677 calls->sync     = roar_vio_pass_sync;
678 calls->ctl      = roar_vio_pass_ctl;
679 calls->close    = roar_vio_pass_close;
680
681 calls->inst     = dst;
682
683 return 0;
684}
685
686ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
687 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
688}
689
690ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
691 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
692}
693
694roar_off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
695 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
696}
697
698int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
699 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
700}
701
702int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
703 if (vio == NULL) {
704  roar_err_set(ROAR_ERROR_FAULT);
705  return -1;
706 }
707
708 if (cmd == -1) {
709  roar_err_set(ROAR_ERROR_INVAL);
710  return -1;
711 }
712
713 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
714
715 switch (cmd) {
716  case ROAR_VIO_CTL_GET_NAME:
717    if ( data == NULL ) {
718     roar_err_set(ROAR_ERROR_FAULT);
719     return -1;
720    }
721
722    // dirty trick to get real name...
723    if ( vio->read == roar_vio_re_read ) {
724     *(char**)data = "re";
725    } else {
726     *(char**)data = "pass";
727    }
728    return 0;
729   break;
730  case ROAR_VIO_CTL_GET_NEXT:
731    *(struct roar_vio_calls **)data = vio->inst;
732    return 0;
733   break;
734  case ROAR_VIO_CTL_SET_NEXT:
735    vio->inst = *(struct roar_vio_calls **)data;
736    return 0;
737   break;
738 }
739
740 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
741}
742
743int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
744 return roar_vio_close((struct roar_vio_calls *) vio->inst);
745}
746
747
748// re
749int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
750 if ( roar_vio_open_pass(calls, dst) == -1 )
751  return -1;
752
753 calls->read  = roar_vio_re_read;
754 calls->write = roar_vio_re_write;
755 calls->lseek = roar_vio_re_lseek;
756
757 return 0;
758}
759ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
760  size_t len =  0;
761 ssize_t r   = -1;
762
763 if ( vio == NULL ) {
764  roar_err_set(ROAR_ERROR_FAULT);
765  return -1;
766 }
767
768 if ( vio->inst == NULL ) {
769  roar_err_set(ROAR_ERROR_FAULT);
770  return -1;
771 }
772
773 roar_err_clear_all();
774
775 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
776  len   += r;
777  buf   += r;
778  count -= r;
779  if ( count == 0 )
780   break;
781 }
782
783 if ( len == 0 && r == -1 )
784  return -1;
785
786 return len;
787}
788
789ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
790  size_t len =  0;
791 ssize_t r   = -1;
792
793 if ( vio == NULL ) {
794  roar_err_set(ROAR_ERROR_FAULT);
795  return -1;
796 }
797
798 if ( vio->inst == NULL ) {
799  roar_err_set(ROAR_ERROR_FAULT);
800  return -1;
801 }
802
803 roar_err_clear_all();
804
805 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
806  len   += r;
807  buf   += r;
808  count -= r;
809  if ( count == 0 )
810   break;
811 }
812
813 if ( len == 0 && r == -1 )
814  return -1;
815
816 return len;
817}
818
819// TODO: we should do a some more intelgent thing here.
820roar_off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
821 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
822}
823
824//ll
Note: See TracBrowser for help on using the repository browser.