source: roaraudio/libroar/vio.c @ 5254:4b808f2c219c

Last change on this file since 5254:4b808f2c219c was 5253:a9d4cba9e8dc, checked in by phi, 12 years ago

improved situation on opening files with DSTR/old roar_vio_open_file()

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