source: roaraudio/libroar/vio.c @ 5276:0eb24ca6810e

Last change on this file since 5276:0eb24ca6810e was 5276:0eb24ca6810e, checked in by phi, 12 years ago

merged VIO's _nonblock() into _ctl() (Closes: #135)

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