source: roaraudio/libroar/vio.c @ 5271:09aa484b25f4

Last change on this file since 5271:09aa484b25f4 was 5270:e25346c13638, checked in by phi, 12 years ago

fixed some gcc -Wextra warnings

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