source: roaraudio/libroar/vio.c @ 4973:20bee823ab67

Last change on this file since 4973:20bee823ab67 was 4973:20bee823ab67, checked in by phi, 13 years ago

use memory corruption roar_panic()

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