source: roaraudio/libroar/vio.c @ 5637:8222fdbd0cdd

Last change on this file since 5637:8222fdbd0cdd was 5637:8222fdbd0cdd, checked in by phi, 12 years ago

corrected typos, thanks to Adam D. Barratt

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