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
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:
[5391]452#ifdef ROAR_HAVE_ACCEPT
[3898]453    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
[3796]454    if ( tmp == -1 )
[3769]455     return -1;
456
457    // most proably a socket.
[3796]458    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
[3769]459#ifdef ROAR_TARGET_WIN32
[3796]460     closesocket(tmp);
[3769]461#else
[3796]462     close(tmp);
[3769]463#endif
464     return -1;
465    }
466
467    return 0;
[5391]468#else
469    roar_err_set(ROAR_ERROR_NOSYS);
470    return -1;
471#endif
[3769]472   break;
[3796]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
[4968]486    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
487     roar_err_set(ROAR_ERROR_NOTSUP);
[3796]488     return -1;
[4968]489    }
[3796]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
[3858]501    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
[3796]502   break;
[4705]503#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
504  case ROAR_VIO_CTL_GET_SOCKNAME:
505  case ROAR_VIO_CTL_GET_PEERNAME:
[4968]506    if ( data == NULL ) {
507     roar_err_set(ROAR_ERROR_FAULT);
[4705]508     return -1;
[4968]509    }
[4705]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
[4968]519     roar_err_set(ROAR_ERROR_NOSYS);
[4705]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
[4968]526     roar_err_set(ROAR_ERROR_NOSYS);
[4705]527     return -1;
528#endif
529    } else {
[4968]530     // memory corruption:
[4973]531     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
[4968]532     roar_err_set(ROAR_ERROR_CHERNOBYL);
[4705]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) {
[4730]542#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
[4705]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
[4730]555#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
[4705]556     case AF_DECnet:
557       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
558
[4968]559       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
560        roar_err_set(ROAR_ERROR_NOTSUP);
[4705]561        return -1;
[4968]562       }
[4705]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
[4730]580#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
[4705]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
[4730]587#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
[4705]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:
[4968]594       roar_err_set(ROAR_ERROR_NOTSUP);
[4705]595       return -1;
596    }
597    return 0;
598#endif
599   break;
[3895]600#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]601  case ROAR_VIO_CTL_SYSIO_IOCTL:
602    sysioctl = data;
603    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
604   break;
[3895]605#endif
[4718]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
[5276]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;
[1505]628 }
629
[4873]630 roar_err_set(ROAR_ERROR_BADRQC);
[1505]631 return -1;
632}
633
[1241]634int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
[1474]635#ifdef _CAN_OPERATE
[1336]636 if ( roar_vio_get_fh(vio) != -1 )
637  return close(roar_vio_get_fh(vio));
638
639 return 0;
[1474]640#else
[4968]641 roar_err_set(ROAR_ERROR_NOSYS);
[1474]642 return -1;
643#endif
[1241]644}
645
[943]646// null
647ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
[5270]648 (void)count;
649
[4968]650 if ( vio == NULL || buf == NULL ) {
651  roar_err_set(ROAR_ERROR_FAULT);
[943]652  return -1;
[4968]653 }
[943]654
655 return 0;
656}
657
[1665]658int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
[5270]659 (void)vio;
[1665]660 return 0;
661}
662
[886]663// pass
[1247]664int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4968]665 if ( calls == NULL || dst == NULL ) {
666  roar_err_set(ROAR_ERROR_FAULT);
[1247]667  return -1;
[4968]668 }
[1247]669
670 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
[5388]671 calls->flags    = ROAR_VIO_FLAGS_NONE;
672 calls->refc     = 1;
[1247]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;
[1615]678 calls->ctl      = roar_vio_pass_ctl;
[1247]679 calls->close    = roar_vio_pass_close;
680
681 calls->inst     = dst;
682
683 return 0;
684}
685
[886]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
[5278]694roar_off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[886]695 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
696}
697
[1241]698int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
699 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
700}
701
[5278]702int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[4968]703 if (vio == NULL) {
704  roar_err_set(ROAR_ERROR_FAULT);
[1505]705  return -1;
[4968]706 }
707
708 if (cmd == -1) {
709  roar_err_set(ROAR_ERROR_INVAL);
710  return -1;
711 }
[1505]712
[1615]713 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
714
[1505]715 switch (cmd) {
[3795]716  case ROAR_VIO_CTL_GET_NAME:
[4968]717    if ( data == NULL ) {
718     roar_err_set(ROAR_ERROR_FAULT);
[3795]719     return -1;
[4968]720    }
[3795]721
[4829]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    }
[3795]728    return 0;
729   break;
[1505]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
[1241]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
[886]747
748// re
[1247]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}
[886]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
[4968]763 if ( vio == NULL ) {
764  roar_err_set(ROAR_ERROR_FAULT);
[886]765  return -1;
[4968]766 }
[886]767
[4968]768 if ( vio->inst == NULL ) {
769  roar_err_set(ROAR_ERROR_FAULT);
[886]770  return -1;
[4968]771 }
[886]772
[4873]773 roar_err_clear_all();
[886]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
[4968]793 if ( vio == NULL ) {
794  roar_err_set(ROAR_ERROR_FAULT);
[886]795  return -1;
[4968]796 }
[886]797
[4968]798 if ( vio->inst == NULL ) {
799  roar_err_set(ROAR_ERROR_FAULT);
[886]800  return -1;
[4968]801 }
[886]802
[4873]803 roar_err_clear_all();
[886]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
[1247]819// TODO: we should do a some more intelgent thing here.
[5278]820roar_off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[886]821 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
822}
823
[590]824//ll
Note: See TracBrowser for help on using the repository browser.