source: roaraudio/libroar/vio.c @ 5242:97239101cee9

Last change on this file since 5242:97239101cee9 was 5242:97239101cee9, checked in by phi, 12 years ago

some roard compiler warnings cleanup

File size: 20.8 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) {
[1474]308#ifdef _CAN_OPERATE
[1252]309 int fh;
310
[4873]311 roar_debug_warn_obsolete("roar_vio_open_file", "roar_vio_open_dstr", NULL);
312
313 if ( calls == NULL || filename == NULL ) {
314  roar_err_set(ROAR_ERROR_FAULT);
[1252]315  return -1;
[4873]316 }
[1252]317
[1762]318#ifdef ROAR_TARGET_WIN32
319 flags |= O_BINARY;
320#endif
321
[4873]322 roar_err_clear_all();
323 if ( (fh = open(filename, flags, mode)) == -1 ) {
[4876]324  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
[4873]325  roar_err_update();
[4876]326  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
[1252]327  return -1;
[4873]328 }
[1252]329
330 if ( roar_vio_open_fh(calls, fh) == -1 ) {
331  close(fh);
[4873]332  roar_err_update();
[1252]333  return -1;
334 }
335
[4873]336 roar_err_update();
[1252]337 return 0;
[1474]338#else
339 return -1;
340#endif
[1252]341}
342
343int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
[4968]344 if ( calls == NULL ) {
345  roar_err_set(ROAR_ERROR_FAULT);
[1252]346  return -1;
[4968]347 }
[1252]348
[4963]349 roar_libroar_nowarn();
350 if ( roar_vio_init_calls(calls) == -1 ) {
351  roar_libroar_warn();
[1252]352  return -1;
[4963]353 }
354 roar_libroar_warn();
[1252]355
356 return roar_vio_set_fh(calls, fh);
357}
358
[1290]359int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
[4968]360 if ( calls == NULL ) {
361  roar_err_set(ROAR_ERROR_FAULT);
[1290]362  return -1;
[4968]363 }
[1290]364
[1666]365 if ( roar_vio_open_fh(calls, fh) == -1 )
366  return -1;
367
[1760]368#ifdef ROAR_TARGET_WIN32
369 calls->read     = roar_vio_winsock_read;
370 calls->write    = roar_vio_winsock_write;
371 calls->nonblock = roar_vio_winsock_nonblock;
372 calls->sync     = roar_vio_winsock_sync;
373 calls->ctl      = roar_vio_winsock_ctl;
374 calls->close    = roar_vio_winsock_close;
375#else
376 calls->sync     = roar_vio_null_sync;
377#endif
[1666]378
379 return 0;
[1290]380}
381
[1291]382int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
383 int fh;
384
[4968]385 if ( calls == NULL ) {
386  roar_err_set(ROAR_ERROR_FAULT);
[1291]387  return -1;
[4968]388 }
[1291]389
390 if ( (fh = roar_socket_connect(host, port)) == -1 )
391  return -1;
392
393 return roar_vio_open_fh_socket(calls, fh);
394}
395
396int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
397 int fh;
398
[4968]399 if ( calls == NULL ) {
400  roar_err_set(ROAR_ERROR_FAULT);
[1291]401  return -1;
[4968]402 }
[1291]403
404 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
405  return -1;
406
407 return roar_vio_open_fh_socket(calls, fh);
408}
409
[2841]410
[886]411// VIOs:
[881]412
[886]413// basic
[881]414ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]415#ifdef _CAN_OPERATE
[881]416 return read(roar_vio_get_fh(vio), buf, count);
[1474]417#else
[4968]418 roar_err_set(ROAR_ERROR_NOSYS);
[1474]419 return -1;
420#endif
[881]421}
422
423ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]424#ifdef _CAN_OPERATE
[881]425 return write(roar_vio_get_fh(vio), buf, count);
[1474]426#else
[4968]427 roar_err_set(ROAR_ERROR_NOSYS);
[1474]428 return -1;
429#endif
[881]430}
431
432off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
[1474]433#ifdef _CAN_OPERATE
[881]434 return lseek(roar_vio_get_fh(vio), offset, whence);
[1474]435#else
[4968]436 roar_err_set(ROAR_ERROR_NOSYS);
[1474]437 return -1;
438#endif
[881]439}
440
[1118]441int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
442 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
443  return -1;
444
445 if ( state == ROAR_SOCKET_NONBLOCK )
446  return 0;
447
[1125]448 roar_vio_sync(vio);
449
450 return 0;
[1118]451}
452
453int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
[1397]454#ifdef ROAR_FDATASYNC
[1171]455 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
[1397]456#else
457 return 0;
458#endif
[1118]459}
460
[1505]461int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
[3895]462#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]463 struct roar_vio_sysio_ioctl * sysioctl;
[3895]464#endif
[4718]465#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
466 struct roar_vio_sysio_sockopt  * syssockopt;
467#endif
[3796]468 int tmp;
469 int s_r = 0, s_w = 0;
[4705]470#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
471 union {
472  struct sockaddr     sa;
473#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
474  struct sockaddr_in  in;
475#endif
476#ifdef ROAR_HAVE_UNIX
477  struct sockaddr_un  un;
478#endif
479#ifdef ROAR_HAVE_LIBDNET
480  struct sockaddr_dn  dn;
481#endif
482#ifdef ROAR_HAVE_IPV6
483  struct sockaddr_in6 in6;
484#endif
485#ifdef ROAR_HAVE_IPX
486  struct sockaddr_ipx ipx;
487#endif
488 } sockaddr;
489 socklen_t socklen;
490 struct roar_sockname * rsockname;
491#endif
[1505]492
[4968]493 if ( vio == NULL ) {
494  roar_err_set(ROAR_ERROR_FAULT);
[1505]495  return -1;
[4968]496 }
497
498 if ( cmd == -1 ) {
499  roar_err_set(ROAR_ERROR_INVAL);
500  return -1;
501 }
[1505]502
[1615]503 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
504
[1505]505 switch (cmd) {
[3795]506  case ROAR_VIO_CTL_GET_NAME:
[4968]507    if ( data == NULL ) {
508     roar_err_set(ROAR_ERROR_FAULT);
[3795]509     return -1;
[4968]510    }
[3795]511
512    *(char**)data = "basic";
513    return 0;
514   break;
[1505]515  case ROAR_VIO_CTL_GET_FH:
516  case ROAR_VIO_CTL_GET_READ_FH:
517  case ROAR_VIO_CTL_GET_WRITE_FH:
[3436]518  case ROAR_VIO_CTL_GET_SELECT_FH:
519  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
520  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
[1615]521    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]522    *(int*)data = roar_vio_get_fh(vio);
523    return 0;
524   break;
[2054]525  case ROAR_VIO_CTL_SET_NOSYNC:
526    vio->sync = NULL;
527    return 0;
528   break;
[3769]529  case ROAR_VIO_CTL_ACCEPT:
[3898]530    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
[3796]531    if ( tmp == -1 )
[3769]532     return -1;
533
534    // most proably a socket.
[3796]535    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
[3769]536#ifdef ROAR_TARGET_WIN32
[3796]537     closesocket(tmp);
[3769]538#else
[3796]539     close(tmp);
[3769]540#endif
541     return -1;
542    }
543
544    return 0;
545   break;
[3796]546  case ROAR_VIO_CTL_SHUTDOWN:
547    tmp = *(int*)data;
548
549    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
550     s_r = 1;
551     tmp -= ROAR_VIO_SHUTDOWN_READ;
552    }
553
554    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
555     s_w = 1;
556     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
557    }
558
[4968]559    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
560     roar_err_set(ROAR_ERROR_NOTSUP);
[3796]561     return -1;
[4968]562    }
[3796]563
564    if ( s_r && s_w ) {
565     tmp = SHUT_RDWR;
566    } else if ( s_r ) {
567     tmp = SHUT_RD;
568    } else if ( s_w ) {
569     tmp = SHUT_WR;
570    } else {
571     return 0; // nothing to do.
572    }
573
[3858]574    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
[3796]575   break;
[4705]576#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
577  case ROAR_VIO_CTL_GET_SOCKNAME:
578  case ROAR_VIO_CTL_GET_PEERNAME:
[4968]579    if ( data == NULL ) {
580     roar_err_set(ROAR_ERROR_FAULT);
[4705]581     return -1;
[4968]582    }
[4705]583
584    rsockname = data;
585
586    socklen = sizeof(sockaddr);
587
588    if ( cmd == ROAR_VIO_CTL_GET_SOCKNAME ) {
589#ifdef ROAR_HAVE_GETSOCKNAME
590     tmp = getsockname(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
591#else
[4968]592     roar_err_set(ROAR_ERROR_NOSYS);
[4705]593     return -1;
594#endif
595    } else if ( cmd == ROAR_VIO_CTL_GET_PEERNAME ) {
596#ifdef ROAR_HAVE_GETPEERNAME
597     tmp = getpeername(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
598#else
[4968]599     roar_err_set(ROAR_ERROR_NOSYS);
[4705]600     return -1;
601#endif
602    } else {
[4968]603     // memory corruption:
[4973]604     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
[4968]605     roar_err_set(ROAR_ERROR_CHERNOBYL);
[4705]606     return -1;
607    }
608
609    if ( tmp == -1 )
610     return -1;
611
612    memset(rsockname, 0, sizeof(struct roar_sockname));
613
614    switch (sockaddr.sa.sa_family) {
[4730]615#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
[4705]616     case AF_UNIX:
617       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
618       if ( sockaddr.un.sun_path[0] == 0 ) {
619        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
620        if ( rsockname->addr == NULL )
621         return -1;
622        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
623       } else {
624        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
625       }
626      break;
627#endif
[4730]628#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
[4705]629     case AF_DECnet:
630       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
631
[4968]632       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
633        roar_err_set(ROAR_ERROR_NOTSUP);
[4705]634        return -1;
[4968]635       }
[4705]636
637       rsockname->addr = roar_mm_malloc(28);
638       if ( rsockname->addr == NULL )
639        return -1;
640
641       snprintf(rsockname->addr, 28, "%i.%i::",
642                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
643                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
644
645       rsockname->port = sockaddr.dn.sdn_objnum;
646       if ( sockaddr.dn.sdn_objnum == 0 ) {
647        tmp = strlen(rsockname->addr);
648        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
649        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
650       }
651      break;
652#endif
[4730]653#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
[4705]654     case AF_INET:
655       rsockname->type = ROAR_SOCKET_TYPE_INET;
656       rsockname->port = ntohs(sockaddr.in.sin_port);
657       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
658      break;
659#endif
[4730]660#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
[4705]661     case AF_INET6:
662       rsockname->type = ROAR_SOCKET_TYPE_INET6;
663       rsockname->port = ntohs(sockaddr.in6.sin6_port);
664      break;
665#endif
666     default:
[4968]667       roar_err_set(ROAR_ERROR_NOTSUP);
[4705]668       return -1;
669    }
670    return 0;
671#endif
672   break;
[3895]673#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]674  case ROAR_VIO_CTL_SYSIO_IOCTL:
675    sysioctl = data;
676    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
677   break;
[3895]678#endif
[4718]679#ifdef ROAR_HAVE_GETSOCKOPT
680  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
681    syssockopt = data;
682    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
683   break;
684#endif
685#ifdef ROAR_HAVE_SETSOCKOPT
686  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
687    syssockopt = data;
688    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
689   break;
690#endif
[1505]691 }
692
[4873]693 roar_err_set(ROAR_ERROR_BADRQC);
[1505]694 return -1;
695}
696
[1241]697int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
[1474]698#ifdef _CAN_OPERATE
[1336]699 if ( roar_vio_get_fh(vio) != -1 )
700  return close(roar_vio_get_fh(vio));
701
702 return 0;
[1474]703#else
[4968]704 roar_err_set(ROAR_ERROR_NOSYS);
[1474]705 return -1;
706#endif
[1241]707}
708
[943]709// null
710ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
[4968]711 if ( vio == NULL || buf == NULL ) {
712  roar_err_set(ROAR_ERROR_FAULT);
[943]713  return -1;
[4968]714 }
[943]715
716 return 0;
717}
718
[1665]719int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
720 return 0;
721}
722
[886]723// pass
[1247]724int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4968]725 if ( calls == NULL || dst == NULL ) {
726  roar_err_set(ROAR_ERROR_FAULT);
[1247]727  return -1;
[4968]728 }
[1247]729
730 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
731
732 calls->read     = roar_vio_pass_read;
733 calls->write    = roar_vio_pass_write;
734 calls->lseek    = roar_vio_pass_lseek;
735 calls->nonblock = roar_vio_pass_nonblock;
736 calls->sync     = roar_vio_pass_sync;
[1615]737 calls->ctl      = roar_vio_pass_ctl;
[1247]738 calls->close    = roar_vio_pass_close;
739
740 calls->inst     = dst;
741
742 return 0;
743}
744
[886]745ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
746 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
747}
748
749ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
750 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
751}
752
753off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
754 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
755}
756
[1241]757int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
758 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
759}
760
761int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
762 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
763}
764
765int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
[4968]766 if (vio == NULL) {
767  roar_err_set(ROAR_ERROR_FAULT);
[1505]768  return -1;
[4968]769 }
770
771 if (cmd == -1) {
772  roar_err_set(ROAR_ERROR_INVAL);
773  return -1;
774 }
[1505]775
[1615]776 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
777
[1505]778 switch (cmd) {
[3795]779  case ROAR_VIO_CTL_GET_NAME:
[4968]780    if ( data == NULL ) {
781     roar_err_set(ROAR_ERROR_FAULT);
[3795]782     return -1;
[4968]783    }
[3795]784
[4829]785    // dirty trick to get real name...
786    if ( vio->read == roar_vio_re_read ) {
787     *(char**)data = "re";
788    } else {
789     *(char**)data = "pass";
790    }
[3795]791    return 0;
792   break;
[1505]793  case ROAR_VIO_CTL_GET_NEXT:
794    *(struct roar_vio_calls **)data = vio->inst;
795    return 0;
796   break;
797  case ROAR_VIO_CTL_SET_NEXT:
798    vio->inst = *(struct roar_vio_calls **)data;
799    return 0;
800   break;
801 }
802
[1241]803 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
804}
805
806int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
807 return roar_vio_close((struct roar_vio_calls *) vio->inst);
808}
809
[886]810
811// re
[1247]812int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
813 if ( roar_vio_open_pass(calls, dst) == -1 )
814  return -1;
815
816 calls->read  = roar_vio_re_read;
817 calls->write = roar_vio_re_write;
818 calls->lseek = roar_vio_re_lseek;
819
820 return 0;
821}
[886]822ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
823  size_t len =  0;
824 ssize_t r   = -1;
825
[4968]826 if ( vio == NULL ) {
827  roar_err_set(ROAR_ERROR_FAULT);
[886]828  return -1;
[4968]829 }
[886]830
[4968]831 if ( vio->inst == NULL ) {
832  roar_err_set(ROAR_ERROR_FAULT);
[886]833  return -1;
[4968]834 }
[886]835
[4873]836 roar_err_clear_all();
[886]837
838 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
839  len   += r;
840  buf   += r;
841  count -= r;
842  if ( count == 0 )
843   break;
844 }
845
846 if ( len == 0 && r == -1 )
847  return -1;
848
849 return len;
850}
851
852ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
853  size_t len =  0;
854 ssize_t r   = -1;
855
[4968]856 if ( vio == NULL ) {
857  roar_err_set(ROAR_ERROR_FAULT);
[886]858  return -1;
[4968]859 }
[886]860
[4968]861 if ( vio->inst == NULL ) {
862  roar_err_set(ROAR_ERROR_FAULT);
[886]863  return -1;
[4968]864 }
[886]865
[4873]866 roar_err_clear_all();
[886]867
868 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
869  len   += r;
870  buf   += r;
871  count -= r;
872  if ( count == 0 )
873   break;
874 }
875
876 if ( len == 0 && r == -1 )
877  return -1;
878
879 return len;
880}
881
[1247]882// TODO: we should do a some more intelgent thing here.
[886]883off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
884 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
885}
886
[590]887//ll
Note: See TracBrowser for help on using the repository browser.