source: roaraudio/libroar/vio.c @ 5110:1a60f34b8a9f

Last change on this file since 5110:1a60f34b8a9f was 5109:4f9fc788fe91, checked in by phi, 13 years ago

Started to use compiler attributes (Also see: #130)

File size: 22.2 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:
307int     roar_vio_open_file     (struct roar_vio_calls * calls, 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
[1275]410int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
411                                                               char * server, int dir, char * name) {
412 int fh;
413
[4968]414 if ( calls == NULL ) {
415  roar_err_set(ROAR_ERROR_FAULT);
[1275]416  return -1;
[4968]417 }
[1275]418
[3860]419 roar_libroar_nowarn();
420 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 ) {
421  roar_libroar_warn();
[1275]422  return -1;
[3860]423 }
424 roar_libroar_warn();
[1275]425
[1290]426 return roar_vio_open_fh_socket(calls, fh);
[1275]427}
428
[2841]429int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
430                                        struct roar_connection * con,
431                                        struct roar_stream * s,
432                                        int rate, int channels, int bits, int codec, int dir) {
[3859]433 struct roar_stream stream;
[2841]434 int fh;
435
[4692]436 ROAR_DBG("roar_vio_simple_new_stream_obj(*) = ?");
437
[4968]438 if ( calls == NULL ) {
439  roar_err_set(ROAR_ERROR_FAULT);
[2841]440  return -1;
[4968]441 }
[2841]442
[3859]443 if ( s == NULL )
444  s = &stream;
445
[3860]446 roar_libroar_nowarn();
447 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 ) {
448  roar_libroar_warn();
[4692]449  ROAR_DBG("roar_vio_simple_new_stream_obj(*) = -1");
[2841]450  return -1;
[3860]451 }
452 roar_libroar_warn();
[2841]453
[4692]454 ROAR_DBG("roar_vio_simple_new_stream_obj(*): fh=%i", fh);
455
[2841]456 return roar_vio_open_fh_socket(calls, fh);
457}
458
[886]459// VIOs:
[881]460
[886]461// basic
[881]462ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]463#ifdef _CAN_OPERATE
[881]464 return read(roar_vio_get_fh(vio), buf, count);
[1474]465#else
[4968]466 roar_err_set(ROAR_ERROR_NOSYS);
[1474]467 return -1;
468#endif
[881]469}
470
471ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]472#ifdef _CAN_OPERATE
[881]473 return write(roar_vio_get_fh(vio), buf, count);
[1474]474#else
[4968]475 roar_err_set(ROAR_ERROR_NOSYS);
[1474]476 return -1;
477#endif
[881]478}
479
480off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
[1474]481#ifdef _CAN_OPERATE
[881]482 return lseek(roar_vio_get_fh(vio), offset, whence);
[1474]483#else
[4968]484 roar_err_set(ROAR_ERROR_NOSYS);
[1474]485 return -1;
486#endif
[881]487}
488
[1118]489int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
490 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
491  return -1;
492
493 if ( state == ROAR_SOCKET_NONBLOCK )
494  return 0;
495
[1125]496 roar_vio_sync(vio);
497
498 return 0;
[1118]499}
500
501int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
[1397]502#ifdef ROAR_FDATASYNC
[1171]503 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
[1397]504#else
505 return 0;
506#endif
[1118]507}
508
[1505]509int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
[3895]510#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]511 struct roar_vio_sysio_ioctl * sysioctl;
[3895]512#endif
[4718]513#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
514 struct roar_vio_sysio_sockopt  * syssockopt;
515#endif
[3796]516 int tmp;
517 int s_r = 0, s_w = 0;
[4705]518#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
519 union {
520  struct sockaddr     sa;
521#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
522  struct sockaddr_in  in;
523#endif
524#ifdef ROAR_HAVE_UNIX
525  struct sockaddr_un  un;
526#endif
527#ifdef ROAR_HAVE_LIBDNET
528  struct sockaddr_dn  dn;
529#endif
530#ifdef ROAR_HAVE_IPV6
531  struct sockaddr_in6 in6;
532#endif
533#ifdef ROAR_HAVE_IPX
534  struct sockaddr_ipx ipx;
535#endif
536 } sockaddr;
537 socklen_t socklen;
538 struct roar_sockname * rsockname;
539#endif
[1505]540
[4968]541 if ( vio == NULL ) {
542  roar_err_set(ROAR_ERROR_FAULT);
[1505]543  return -1;
[4968]544 }
545
546 if ( cmd == -1 ) {
547  roar_err_set(ROAR_ERROR_INVAL);
548  return -1;
549 }
[1505]550
[1615]551 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
552
[1505]553 switch (cmd) {
[3795]554  case ROAR_VIO_CTL_GET_NAME:
[4968]555    if ( data == NULL ) {
556     roar_err_set(ROAR_ERROR_FAULT);
[3795]557     return -1;
[4968]558    }
[3795]559
560    *(char**)data = "basic";
561    return 0;
562   break;
[1505]563  case ROAR_VIO_CTL_GET_FH:
564  case ROAR_VIO_CTL_GET_READ_FH:
565  case ROAR_VIO_CTL_GET_WRITE_FH:
[3436]566  case ROAR_VIO_CTL_GET_SELECT_FH:
567  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
568  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
[1615]569    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]570    *(int*)data = roar_vio_get_fh(vio);
571    return 0;
572   break;
[2054]573  case ROAR_VIO_CTL_SET_NOSYNC:
574    vio->sync = NULL;
575    return 0;
576   break;
[3769]577  case ROAR_VIO_CTL_ACCEPT:
[3898]578    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
[3796]579    if ( tmp == -1 )
[3769]580     return -1;
581
582    // most proably a socket.
[3796]583    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
[3769]584#ifdef ROAR_TARGET_WIN32
[3796]585     closesocket(tmp);
[3769]586#else
[3796]587     close(tmp);
[3769]588#endif
589     return -1;
590    }
591
592    return 0;
593   break;
[3796]594  case ROAR_VIO_CTL_SHUTDOWN:
595    tmp = *(int*)data;
596
597    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
598     s_r = 1;
599     tmp -= ROAR_VIO_SHUTDOWN_READ;
600    }
601
602    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
603     s_w = 1;
604     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
605    }
606
[4968]607    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
608     roar_err_set(ROAR_ERROR_NOTSUP);
[3796]609     return -1;
[4968]610    }
[3796]611
612    if ( s_r && s_w ) {
613     tmp = SHUT_RDWR;
614    } else if ( s_r ) {
615     tmp = SHUT_RD;
616    } else if ( s_w ) {
617     tmp = SHUT_WR;
618    } else {
619     return 0; // nothing to do.
620    }
621
[3858]622    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
[3796]623   break;
[4705]624#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
625  case ROAR_VIO_CTL_GET_SOCKNAME:
626  case ROAR_VIO_CTL_GET_PEERNAME:
[4968]627    if ( data == NULL ) {
628     roar_err_set(ROAR_ERROR_FAULT);
[4705]629     return -1;
[4968]630    }
[4705]631
632    rsockname = data;
633
634    socklen = sizeof(sockaddr);
635
636    if ( cmd == ROAR_VIO_CTL_GET_SOCKNAME ) {
637#ifdef ROAR_HAVE_GETSOCKNAME
638     tmp = getsockname(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
639#else
[4968]640     roar_err_set(ROAR_ERROR_NOSYS);
[4705]641     return -1;
642#endif
643    } else if ( cmd == ROAR_VIO_CTL_GET_PEERNAME ) {
644#ifdef ROAR_HAVE_GETPEERNAME
645     tmp = getpeername(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
646#else
[4968]647     roar_err_set(ROAR_ERROR_NOSYS);
[4705]648     return -1;
649#endif
650    } else {
[4968]651     // memory corruption:
[4973]652     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
[4968]653     roar_err_set(ROAR_ERROR_CHERNOBYL);
[4705]654     return -1;
655    }
656
657    if ( tmp == -1 )
658     return -1;
659
660    memset(rsockname, 0, sizeof(struct roar_sockname));
661
662    switch (sockaddr.sa.sa_family) {
[4730]663#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
[4705]664     case AF_UNIX:
665       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
666       if ( sockaddr.un.sun_path[0] == 0 ) {
667        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
668        if ( rsockname->addr == NULL )
669         return -1;
670        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
671       } else {
672        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
673       }
674      break;
675#endif
[4730]676#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
[4705]677     case AF_DECnet:
678       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
679
[4968]680       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
681        roar_err_set(ROAR_ERROR_NOTSUP);
[4705]682        return -1;
[4968]683       }
[4705]684
685       rsockname->addr = roar_mm_malloc(28);
686       if ( rsockname->addr == NULL )
687        return -1;
688
689       snprintf(rsockname->addr, 28, "%i.%i::",
690                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
691                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
692
693       rsockname->port = sockaddr.dn.sdn_objnum;
694       if ( sockaddr.dn.sdn_objnum == 0 ) {
695        tmp = strlen(rsockname->addr);
696        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
697        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
698       }
699      break;
700#endif
[4730]701#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
[4705]702     case AF_INET:
703       rsockname->type = ROAR_SOCKET_TYPE_INET;
704       rsockname->port = ntohs(sockaddr.in.sin_port);
705       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
706      break;
707#endif
[4730]708#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
[4705]709     case AF_INET6:
710       rsockname->type = ROAR_SOCKET_TYPE_INET6;
711       rsockname->port = ntohs(sockaddr.in6.sin6_port);
712      break;
713#endif
714     default:
[4968]715       roar_err_set(ROAR_ERROR_NOTSUP);
[4705]716       return -1;
717    }
718    return 0;
719#endif
720   break;
[3895]721#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]722  case ROAR_VIO_CTL_SYSIO_IOCTL:
723    sysioctl = data;
724    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
725   break;
[3895]726#endif
[4718]727#ifdef ROAR_HAVE_GETSOCKOPT
728  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
729    syssockopt = data;
730    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
731   break;
732#endif
733#ifdef ROAR_HAVE_SETSOCKOPT
734  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
735    syssockopt = data;
736    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
737   break;
738#endif
[1505]739 }
740
[4873]741 roar_err_set(ROAR_ERROR_BADRQC);
[1505]742 return -1;
743}
744
[1241]745int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
[1474]746#ifdef _CAN_OPERATE
[1336]747 if ( roar_vio_get_fh(vio) != -1 )
748  return close(roar_vio_get_fh(vio));
749
750 return 0;
[1474]751#else
[4968]752 roar_err_set(ROAR_ERROR_NOSYS);
[1474]753 return -1;
754#endif
[1241]755}
756
[943]757// null
758ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
[4968]759 if ( vio == NULL || buf == NULL ) {
760  roar_err_set(ROAR_ERROR_FAULT);
[943]761  return -1;
[4968]762 }
[943]763
764 return 0;
765}
766
[1665]767int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
768 return 0;
769}
770
[886]771// pass
[1247]772int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4968]773 if ( calls == NULL || dst == NULL ) {
774  roar_err_set(ROAR_ERROR_FAULT);
[1247]775  return -1;
[4968]776 }
[1247]777
778 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
779
780 calls->read     = roar_vio_pass_read;
781 calls->write    = roar_vio_pass_write;
782 calls->lseek    = roar_vio_pass_lseek;
783 calls->nonblock = roar_vio_pass_nonblock;
784 calls->sync     = roar_vio_pass_sync;
[1615]785 calls->ctl      = roar_vio_pass_ctl;
[1247]786 calls->close    = roar_vio_pass_close;
787
788 calls->inst     = dst;
789
790 return 0;
791}
792
[886]793ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
794 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
795}
796
797ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
798 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
799}
800
801off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
802 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
803}
804
[1241]805int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
806 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
807}
808
809int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
810 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
811}
812
813int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
[4968]814 if (vio == NULL) {
815  roar_err_set(ROAR_ERROR_FAULT);
[1505]816  return -1;
[4968]817 }
818
819 if (cmd == -1) {
820  roar_err_set(ROAR_ERROR_INVAL);
821  return -1;
822 }
[1505]823
[1615]824 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
825
[1505]826 switch (cmd) {
[3795]827  case ROAR_VIO_CTL_GET_NAME:
[4968]828    if ( data == NULL ) {
829     roar_err_set(ROAR_ERROR_FAULT);
[3795]830     return -1;
[4968]831    }
[3795]832
[4829]833    // dirty trick to get real name...
834    if ( vio->read == roar_vio_re_read ) {
835     *(char**)data = "re";
836    } else {
837     *(char**)data = "pass";
838    }
[3795]839    return 0;
840   break;
[1505]841  case ROAR_VIO_CTL_GET_NEXT:
842    *(struct roar_vio_calls **)data = vio->inst;
843    return 0;
844   break;
845  case ROAR_VIO_CTL_SET_NEXT:
846    vio->inst = *(struct roar_vio_calls **)data;
847    return 0;
848   break;
849 }
850
[1241]851 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
852}
853
854int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
855 return roar_vio_close((struct roar_vio_calls *) vio->inst);
856}
857
[886]858
859// re
[1247]860int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
861 if ( roar_vio_open_pass(calls, dst) == -1 )
862  return -1;
863
864 calls->read  = roar_vio_re_read;
865 calls->write = roar_vio_re_write;
866 calls->lseek = roar_vio_re_lseek;
867
868 return 0;
869}
[886]870ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
871  size_t len =  0;
872 ssize_t r   = -1;
873
[4968]874 if ( vio == NULL ) {
875  roar_err_set(ROAR_ERROR_FAULT);
[886]876  return -1;
[4968]877 }
[886]878
[4968]879 if ( vio->inst == NULL ) {
880  roar_err_set(ROAR_ERROR_FAULT);
[886]881  return -1;
[4968]882 }
[886]883
[4873]884 roar_err_clear_all();
[886]885
886 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
887  len   += r;
888  buf   += r;
889  count -= r;
890  if ( count == 0 )
891   break;
892 }
893
894 if ( len == 0 && r == -1 )
895  return -1;
896
897 return len;
898}
899
900ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
901  size_t len =  0;
902 ssize_t r   = -1;
903
[4968]904 if ( vio == NULL ) {
905  roar_err_set(ROAR_ERROR_FAULT);
[886]906  return -1;
[4968]907 }
[886]908
[4968]909 if ( vio->inst == NULL ) {
910  roar_err_set(ROAR_ERROR_FAULT);
[886]911  return -1;
[4968]912 }
[886]913
[4873]914 roar_err_clear_all();
[886]915
916 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
917  len   += r;
918  buf   += r;
919  count -= r;
920  if ( count == 0 )
921   break;
922 }
923
924 if ( len == 0 && r == -1 )
925  return -1;
926
927 return len;
928}
929
[1247]930// TODO: we should do a some more intelgent thing here.
[886]931off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
932 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
933}
934
[590]935//ll
Note: See TracBrowser for help on using the repository browser.