source: roaraudio/libroar/vio.c @ 5921:2fa512f52879

Last change on this file since 5921:2fa512f52879 was 5834:22e75d31bfd8, checked in by phi, 11 years ago

Fix #296 by using a workaround (disabling RTLD_DEEPBIND). Needs to be fully fixed next release. (See #296)

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