source: roaraudio/libroar/vio.c @ 4967:572924bdd4c6

Last change on this file since 4967:572924bdd4c6 was 4963:1fea81784c37, checked in by phi, 13 years ago

added roar_vio_clear_calls()

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