source: roaraudio/libroar/vio.c @ 5388:e5cc8e03a3e1

Last change on this file since 5388:e5cc8e03a3e1 was 5388:e5cc8e03a3e1, checked in by phi, 12 years ago

Added support for refcount based VIOs as well as dynamically allocated VIOs (non-stack or global VIOs) (Closes: #127)

File size: 19.4 KB
RevLine 
[590]1//vio.c:
2
[690]3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]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
[5109]36#define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */
[590]37#include "libroar.h"
[3895]38
39#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]40#include <sys/ioctl.h>
[3895]41#endif
[590]42
[1474]43#ifdef ROAR_HAVE_IO_POSIX
44#define _CAN_OPERATE
45#endif
46
[4963]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));
[5378]54 calls->flags = ROAR_VIO_FLAGS_NONE;
[5388]55 calls->refc  = 1;
[4963]56
57 return 0;
58}
59
[5377]60static int roar_vio_get_fh   (struct roar_vio_calls * vio) {
[4873]61 if ( vio == NULL ) {
62  roar_err_set(ROAR_ERROR_FAULT);
[881]63  return -1;
[4873]64 }
[881]65
[989]66 return ((int)(ROAR_INSTINT)vio->inst) - 1;
[881]67}
68
69
70ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
[4873]71 ssize_t ret;
72
[1319]73 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
74
[4873]75 if ( vio == NULL ) {
76  roar_err_set(ROAR_ERROR_FAULT);
[881]77  return -1;
[4873]78 }
[881]79
[4873]80 if ( vio->read == NULL ) {
81  roar_err_set(ROAR_ERROR_NOSYS);
[881]82  return -1;
[4873]83 }
[881]84
[4873]85 roar_err_clear_all();
86 ret = vio->read(vio, buf, count);
87 roar_err_update();
88
89 return ret;
[881]90}
91
92ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
[4873]93 ssize_t ret;
94
[3276]95 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
96
[4873]97 if ( vio == NULL ) {
98  roar_err_set(ROAR_ERROR_FAULT);
[881]99  return -1;
[4873]100 }
[881]101
[4873]102 if ( vio->write == NULL ) {
103  roar_err_set(ROAR_ERROR_NOSYS);
[881]104  return -1;
[4873]105 }
[881]106
[4873]107 roar_err_clear_all();
108 ret = vio->write(vio, buf, count);
109 roar_err_update();
110
111 return ret;
[881]112}
113
[5278]114roar_off_t   roar_vio_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
115 roar_off_t ret;
[4873]116
[5278]117 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%li, whence=%i) = ?", vio, (long int)offset, whence);
[3276]118
[4873]119 if ( vio == NULL ) {
120  roar_err_set(ROAR_ERROR_FAULT);
[881]121  return -1;
[4873]122 }
[881]123
[4873]124 if ( vio->lseek == NULL ) {
125  roar_err_set(ROAR_ERROR_NOSYS);
[881]126  return -1;
[4873]127 }
[881]128
[4873]129 roar_err_clear_all();
130 ret = vio->lseek(vio, offset, whence);
131 roar_err_update();
132
133 return ret;
[881]134}
135
[1118]136int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
[3276]137 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
138
[4873]139 if ( vio == NULL ) {
140  roar_err_set(ROAR_ERROR_FAULT);
[1118]141  return -1;
[4873]142 }
[1118]143
[5276]144 return roar_vio_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &state);
[1118]145}
146
147int     roar_vio_sync    (struct roar_vio_calls * vio) {
[4873]148 int ret;
149
[3276]150 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
151
[4873]152 if ( vio == NULL ) {
153  roar_err_set(ROAR_ERROR_FAULT);
[1118]154  return -1;
[4873]155 }
[1118]156
[4873]157 if ( vio->sync == NULL ) {
158  roar_err_set(ROAR_ERROR_NOSYS);
[1118]159  return -1;
[4873]160 }
[1118]161
[4873]162 roar_err_clear_all();
163 ret = vio->sync(vio);
164 roar_err_update();
165
166 return ret;
[1118]167}
168
[5278]169int     roar_vio_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[4873]170 int ret;
171
[3276]172 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
173
[4873]174 if ( vio == NULL ) {
175  roar_err_set(ROAR_ERROR_FAULT);
[1140]176  return -1;
[4873]177 }
[1140]178
[1615]179 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
180
[4831]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);
[4873]187    roar_err_set(ROAR_ERROR_BADRQC);
[4831]188    return -1;
189   break;
190 }
191
[4873]192 if ( vio->ctl == NULL ) {
193  roar_err_set(ROAR_ERROR_NOSYS);
[1140]194  return -1;
[4873]195 }
[1140]196
[4873]197 roar_err_clear_all();
198 ret = vio->ctl(vio, cmd, data);
199 roar_err_update();
200
201 return ret;
[1140]202}
203
[5388]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) {
[4873]217 int ret;
218
[5388]219 ROAR_DBG("roar_vio_unref(vio=%p) = ?", vio);
[3276]220
[4873]221 if ( vio == NULL ) {
222  roar_err_set(ROAR_ERROR_FAULT);
[1241]223  return -1;
[4873]224 }
[1241]225
[5388]226 if ( vio->refc == 1 && vio->close == NULL ) {
[4873]227  roar_err_set(ROAR_ERROR_NOSYS);
[1241]228  return -1;
[4873]229 }
[1241]230
[5388]231 vio->refc--;
232
233 if ( vio->refc )
234  return 0;
235
[4873]236 roar_err_clear_all();
237 ret = vio->close(vio);
238 roar_err_update();
239
[5388]240 if ( vio->flags & ROAR_VIO_FLAGS_FREESELF )
241  roar_mm_free(vio);
242
[4873]243 return ret;
[1241]244}
245
[3769]246// specal commands:
247int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4873]248 if (dst == NULL || calls == NULL) {
249  roar_err_set(ROAR_ERROR_FAULT);
[3769]250  return -1;
[4873]251 }
[3769]252
253 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
254}
255
[3796]256int     roar_vio_shutdown(struct roar_vio_calls * vio,   int how) {
[4968]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
[3796]268 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
269}
270
[1252]271// converters:
272int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
[4968]273 if ( calls == NULL ) {
274  roar_err_set(ROAR_ERROR_FAULT);
[1252]275  return -1;
[4968]276 }
[1252]277
[5377]278 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
[5388]279 calls->flags    = ROAR_VIO_FLAGS_NONE;
280 calls->refc     = 1;
[1252]281
[5377]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;
[1252]292}
293
[1290]294int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
[4968]295 if ( calls == NULL ) {
296  roar_err_set(ROAR_ERROR_FAULT);
[1290]297  return -1;
[4968]298 }
[1290]299
[1666]300 if ( roar_vio_open_fh(calls, fh) == -1 )
301  return -1;
302
[1760]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
[1666]312
313 return 0;
[1290]314}
315
[5260]316int     roar_vio_open_socket   (struct roar_vio_calls * calls, const char * host, int port) {
[1291]317 int fh;
318
[4968]319 if ( calls == NULL ) {
320  roar_err_set(ROAR_ERROR_FAULT);
[1291]321  return -1;
[4968]322 }
[1291]323
[5375]324 if ( (fh = roar_socket_connect(ROAR_SOCKET_TYPE_UNKNOWN, host, port)) == -1 )
[1291]325  return -1;
326
327 return roar_vio_open_fh_socket(calls, fh);
328}
329
[5260]330int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, const char * host, int port) {
[1291]331 int fh;
332
[4968]333 if ( calls == NULL ) {
334  roar_err_set(ROAR_ERROR_FAULT);
[1291]335  return -1;
[4968]336 }
[1291]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
[2841]344
[886]345// VIOs:
[881]346
[886]347// basic
[881]348ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]349#ifdef _CAN_OPERATE
[881]350 return read(roar_vio_get_fh(vio), buf, count);
[1474]351#else
[4968]352 roar_err_set(ROAR_ERROR_NOSYS);
[1474]353 return -1;
354#endif
[881]355}
356
357ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]358#ifdef _CAN_OPERATE
[881]359 return write(roar_vio_get_fh(vio), buf, count);
[1474]360#else
[4968]361 roar_err_set(ROAR_ERROR_NOSYS);
[1474]362 return -1;
363#endif
[881]364}
365
[5278]366roar_off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[1474]367#ifdef _CAN_OPERATE
[881]368 return lseek(roar_vio_get_fh(vio), offset, whence);
[1474]369#else
[4968]370 roar_err_set(ROAR_ERROR_NOSYS);
[1474]371 return -1;
372#endif
[881]373}
374
[1118]375int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
[1397]376#ifdef ROAR_FDATASYNC
[1171]377 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
[1397]378#else
379 return 0;
380#endif
[1118]381}
382
[5278]383int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[3895]384#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]385 struct roar_vio_sysio_ioctl * sysioctl;
[3895]386#endif
[4718]387#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
388 struct roar_vio_sysio_sockopt  * syssockopt;
389#endif
[3796]390 int tmp;
391 int s_r = 0, s_w = 0;
[4705]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
[1505]414
[4968]415 if ( vio == NULL ) {
416  roar_err_set(ROAR_ERROR_FAULT);
[1505]417  return -1;
[4968]418 }
419
420 if ( cmd == -1 ) {
421  roar_err_set(ROAR_ERROR_INVAL);
422  return -1;
423 }
[1505]424
[1615]425 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
426
[1505]427 switch (cmd) {
[3795]428  case ROAR_VIO_CTL_GET_NAME:
[4968]429    if ( data == NULL ) {
430     roar_err_set(ROAR_ERROR_FAULT);
[3795]431     return -1;
[4968]432    }
[3795]433
434    *(char**)data = "basic";
435    return 0;
436   break;
[1505]437  case ROAR_VIO_CTL_GET_FH:
438  case ROAR_VIO_CTL_GET_READ_FH:
439  case ROAR_VIO_CTL_GET_WRITE_FH:
[3436]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:
[1615]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));
[1505]444    *(int*)data = roar_vio_get_fh(vio);
445    return 0;
446   break;
[2054]447  case ROAR_VIO_CTL_SET_NOSYNC:
448    vio->sync = NULL;
449    return 0;
450   break;
[3769]451  case ROAR_VIO_CTL_ACCEPT:
[3898]452    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
[3796]453    if ( tmp == -1 )
[3769]454     return -1;
455
456    // most proably a socket.
[3796]457    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
[3769]458#ifdef ROAR_TARGET_WIN32
[3796]459     closesocket(tmp);
[3769]460#else
[3796]461     close(tmp);
[3769]462#endif
463     return -1;
464    }
465
466    return 0;
467   break;
[3796]468  case ROAR_VIO_CTL_SHUTDOWN:
469    tmp = *(int*)data;
470
471    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
472     s_r = 1;
473     tmp -= ROAR_VIO_SHUTDOWN_READ;
474    }
475
476    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
477     s_w = 1;
478     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
479    }
480
[4968]481    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
482     roar_err_set(ROAR_ERROR_NOTSUP);
[3796]483     return -1;
[4968]484    }
[3796]485
486    if ( s_r && s_w ) {
487     tmp = SHUT_RDWR;
488    } else if ( s_r ) {
489     tmp = SHUT_RD;
490    } else if ( s_w ) {
491     tmp = SHUT_WR;
492    } else {
493     return 0; // nothing to do.
494    }
495
[3858]496    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
[3796]497   break;
[4705]498#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
499  case ROAR_VIO_CTL_GET_SOCKNAME:
500  case ROAR_VIO_CTL_GET_PEERNAME:
[4968]501    if ( data == NULL ) {
502     roar_err_set(ROAR_ERROR_FAULT);
[4705]503     return -1;
[4968]504    }
[4705]505
506    rsockname = data;
507
508    socklen = sizeof(sockaddr);
509
510    if ( cmd == ROAR_VIO_CTL_GET_SOCKNAME ) {
511#ifdef ROAR_HAVE_GETSOCKNAME
512     tmp = getsockname(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
513#else
[4968]514     roar_err_set(ROAR_ERROR_NOSYS);
[4705]515     return -1;
516#endif
517    } else if ( cmd == ROAR_VIO_CTL_GET_PEERNAME ) {
518#ifdef ROAR_HAVE_GETPEERNAME
519     tmp = getpeername(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
520#else
[4968]521     roar_err_set(ROAR_ERROR_NOSYS);
[4705]522     return -1;
523#endif
524    } else {
[4968]525     // memory corruption:
[4973]526     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
[4968]527     roar_err_set(ROAR_ERROR_CHERNOBYL);
[4705]528     return -1;
529    }
530
531    if ( tmp == -1 )
532     return -1;
533
534    memset(rsockname, 0, sizeof(struct roar_sockname));
535
536    switch (sockaddr.sa.sa_family) {
[4730]537#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
[4705]538     case AF_UNIX:
539       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
540       if ( sockaddr.un.sun_path[0] == 0 ) {
541        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
542        if ( rsockname->addr == NULL )
543         return -1;
544        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
545       } else {
546        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
547       }
548      break;
549#endif
[4730]550#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
[4705]551     case AF_DECnet:
552       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
553
[4968]554       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
555        roar_err_set(ROAR_ERROR_NOTSUP);
[4705]556        return -1;
[4968]557       }
[4705]558
559       rsockname->addr = roar_mm_malloc(28);
560       if ( rsockname->addr == NULL )
561        return -1;
562
563       snprintf(rsockname->addr, 28, "%i.%i::",
564                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
565                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
566
567       rsockname->port = sockaddr.dn.sdn_objnum;
568       if ( sockaddr.dn.sdn_objnum == 0 ) {
569        tmp = strlen(rsockname->addr);
570        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
571        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
572       }
573      break;
574#endif
[4730]575#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
[4705]576     case AF_INET:
577       rsockname->type = ROAR_SOCKET_TYPE_INET;
578       rsockname->port = ntohs(sockaddr.in.sin_port);
579       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
580      break;
581#endif
[4730]582#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
[4705]583     case AF_INET6:
584       rsockname->type = ROAR_SOCKET_TYPE_INET6;
585       rsockname->port = ntohs(sockaddr.in6.sin6_port);
586      break;
587#endif
588     default:
[4968]589       roar_err_set(ROAR_ERROR_NOTSUP);
[4705]590       return -1;
591    }
592    return 0;
593#endif
594   break;
[3895]595#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]596  case ROAR_VIO_CTL_SYSIO_IOCTL:
597    sysioctl = data;
598    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
599   break;
[3895]600#endif
[4718]601#ifdef ROAR_HAVE_GETSOCKOPT
602  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
603    syssockopt = data;
604    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
605   break;
606#endif
607#ifdef ROAR_HAVE_SETSOCKOPT
608  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
609    syssockopt = data;
610    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
611   break;
612#endif
[5276]613  case ROAR_VIO_CTL_NONBLOCK:
614    if ( roar_socket_nonblock(roar_vio_get_fh(vio), *(int*)data) == -1 )
615     return -1;
616
617    if ( *(int*)data == ROAR_SOCKET_NONBLOCK )
618     return 0;
619
620    roar_vio_sync(vio);
621    return 0;
622   break;
[1505]623 }
624
[4873]625 roar_err_set(ROAR_ERROR_BADRQC);
[1505]626 return -1;
627}
628
[1241]629int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
[1474]630#ifdef _CAN_OPERATE
[1336]631 if ( roar_vio_get_fh(vio) != -1 )
632  return close(roar_vio_get_fh(vio));
633
634 return 0;
[1474]635#else
[4968]636 roar_err_set(ROAR_ERROR_NOSYS);
[1474]637 return -1;
638#endif
[1241]639}
640
[943]641// null
642ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
[5270]643 (void)count;
644
[4968]645 if ( vio == NULL || buf == NULL ) {
646  roar_err_set(ROAR_ERROR_FAULT);
[943]647  return -1;
[4968]648 }
[943]649
650 return 0;
651}
652
[1665]653int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
[5270]654 (void)vio;
[1665]655 return 0;
656}
657
[886]658// pass
[1247]659int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4968]660 if ( calls == NULL || dst == NULL ) {
661  roar_err_set(ROAR_ERROR_FAULT);
[1247]662  return -1;
[4968]663 }
[1247]664
665 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
[5388]666 calls->flags    = ROAR_VIO_FLAGS_NONE;
667 calls->refc     = 1;
[1247]668
669 calls->read     = roar_vio_pass_read;
670 calls->write    = roar_vio_pass_write;
671 calls->lseek    = roar_vio_pass_lseek;
672 calls->sync     = roar_vio_pass_sync;
[1615]673 calls->ctl      = roar_vio_pass_ctl;
[1247]674 calls->close    = roar_vio_pass_close;
675
676 calls->inst     = dst;
677
678 return 0;
679}
680
[886]681ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
682 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
683}
684
685ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
686 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
687}
688
[5278]689roar_off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[886]690 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
691}
692
[1241]693int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
694 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
695}
696
[5278]697int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[4968]698 if (vio == NULL) {
699  roar_err_set(ROAR_ERROR_FAULT);
[1505]700  return -1;
[4968]701 }
702
703 if (cmd == -1) {
704  roar_err_set(ROAR_ERROR_INVAL);
705  return -1;
706 }
[1505]707
[1615]708 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
709
[1505]710 switch (cmd) {
[3795]711  case ROAR_VIO_CTL_GET_NAME:
[4968]712    if ( data == NULL ) {
713     roar_err_set(ROAR_ERROR_FAULT);
[3795]714     return -1;
[4968]715    }
[3795]716
[4829]717    // dirty trick to get real name...
718    if ( vio->read == roar_vio_re_read ) {
719     *(char**)data = "re";
720    } else {
721     *(char**)data = "pass";
722    }
[3795]723    return 0;
724   break;
[1505]725  case ROAR_VIO_CTL_GET_NEXT:
726    *(struct roar_vio_calls **)data = vio->inst;
727    return 0;
728   break;
729  case ROAR_VIO_CTL_SET_NEXT:
730    vio->inst = *(struct roar_vio_calls **)data;
731    return 0;
732   break;
733 }
734
[1241]735 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
736}
737
738int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
739 return roar_vio_close((struct roar_vio_calls *) vio->inst);
740}
741
[886]742
743// re
[1247]744int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
745 if ( roar_vio_open_pass(calls, dst) == -1 )
746  return -1;
747
748 calls->read  = roar_vio_re_read;
749 calls->write = roar_vio_re_write;
750 calls->lseek = roar_vio_re_lseek;
751
752 return 0;
753}
[886]754ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
755  size_t len =  0;
756 ssize_t r   = -1;
757
[4968]758 if ( vio == NULL ) {
759  roar_err_set(ROAR_ERROR_FAULT);
[886]760  return -1;
[4968]761 }
[886]762
[4968]763 if ( vio->inst == NULL ) {
764  roar_err_set(ROAR_ERROR_FAULT);
[886]765  return -1;
[4968]766 }
[886]767
[4873]768 roar_err_clear_all();
[886]769
770 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
771  len   += r;
772  buf   += r;
773  count -= r;
774  if ( count == 0 )
775   break;
776 }
777
778 if ( len == 0 && r == -1 )
779  return -1;
780
781 return len;
782}
783
784ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
785  size_t len =  0;
786 ssize_t r   = -1;
787
[4968]788 if ( vio == NULL ) {
789  roar_err_set(ROAR_ERROR_FAULT);
[886]790  return -1;
[4968]791 }
[886]792
[4968]793 if ( vio->inst == NULL ) {
794  roar_err_set(ROAR_ERROR_FAULT);
[886]795  return -1;
[4968]796 }
[886]797
[4873]798 roar_err_clear_all();
[886]799
800 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
801  len   += r;
802  buf   += r;
803  count -= r;
804  if ( count == 0 )
805   break;
806 }
807
808 if ( len == 0 && r == -1 )
809  return -1;
810
811 return len;
812}
813
[1247]814// TODO: we should do a some more intelgent thing here.
[5278]815roar_off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[886]816 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
817}
818
[590]819//ll
Note: See TracBrowser for help on using the repository browser.