source: roaraudio/libroar/vio.c @ 5109:4f9fc788fe91

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

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

File size: 22.2 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
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
36#define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */
37#include "libroar.h"
38
39#ifdef ROAR_HAVE_H_SYS_IOCTL
40#include <sys/ioctl.h>
41#endif
42
43#ifdef ROAR_HAVE_IO_POSIX
44#define _CAN_OPERATE
45#endif
46
47int roar_vio_init_calls (struct roar_vio_calls * calls) {
48 roar_debug_warn_obsolete("roar_vio_init_calls", "roar_vio_clear_calls", NULL);
49
50#ifdef _CAN_OPERATE
51 if ( calls == NULL ) {
52  roar_err_set(ROAR_ERROR_FAULT);
53  return -1;
54 }
55
56 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
57
58/*
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;
62*/
63
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;
69 calls->ctl      = roar_vio_basic_ctl;
70 calls->close    = roar_vio_basic_close;
71
72 return 0;
73#else
74 return -1;
75#endif
76}
77
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
89int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
90 if ( vio == NULL ) {
91  roar_err_set(ROAR_ERROR_FAULT);
92  return -1;
93 }
94
95 vio->inst = inst;
96
97 return 0;
98}
99
100int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
101 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
102}
103
104int roar_vio_get_fh   (struct roar_vio_calls * vio) {
105 if ( vio == NULL ) {
106  roar_err_set(ROAR_ERROR_FAULT);
107  return -1;
108 }
109
110 return ((int)(ROAR_INSTINT)vio->inst) - 1;
111}
112
113
114ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
115 ssize_t ret;
116
117 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
118
119 if ( vio == NULL ) {
120  roar_err_set(ROAR_ERROR_FAULT);
121  return -1;
122 }
123
124 if ( vio->read == NULL ) {
125  roar_err_set(ROAR_ERROR_NOSYS);
126  return -1;
127 }
128
129 roar_err_clear_all();
130 ret = vio->read(vio, buf, count);
131 roar_err_update();
132
133 return ret;
134}
135
136ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
137 ssize_t ret;
138
139 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
140
141 if ( vio == NULL ) {
142  roar_err_set(ROAR_ERROR_FAULT);
143  return -1;
144 }
145
146 if ( vio->write == NULL ) {
147  roar_err_set(ROAR_ERROR_NOSYS);
148  return -1;
149 }
150
151 roar_err_clear_all();
152 ret = vio->write(vio, buf, count);
153 roar_err_update();
154
155 return ret;
156}
157
158off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
159 off_t ret;
160
161 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%u, whence=%i) = ?", vio, (unsigned int)offset, whence);
162
163 if ( vio == NULL ) {
164  roar_err_set(ROAR_ERROR_FAULT);
165  return -1;
166 }
167
168 if ( vio->lseek == NULL ) {
169  roar_err_set(ROAR_ERROR_NOSYS);
170  return -1;
171 }
172
173 roar_err_clear_all();
174 ret = vio->lseek(vio, offset, whence);
175 roar_err_update();
176
177 return ret;
178}
179
180int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
181 int ret;
182
183 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
184
185 if ( vio == NULL ) {
186  roar_err_set(ROAR_ERROR_FAULT);
187  return -1;
188 }
189
190 if ( vio->nonblock == NULL ) {
191  roar_err_set(ROAR_ERROR_NOSYS);
192  return -1;
193 }
194
195 roar_err_clear_all();
196 ret = vio->nonblock(vio, state);
197 roar_err_update();
198
199 return ret;
200}
201
202int     roar_vio_sync    (struct roar_vio_calls * vio) {
203 int ret;
204
205 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
206
207 if ( vio == NULL ) {
208  roar_err_set(ROAR_ERROR_FAULT);
209  return -1;
210 }
211
212 if ( vio->sync == NULL ) {
213  roar_err_set(ROAR_ERROR_NOSYS);
214  return -1;
215 }
216
217 roar_err_clear_all();
218 ret = vio->sync(vio);
219 roar_err_update();
220
221 return ret;
222}
223
224int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
225 int ret;
226
227 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
228
229 if ( vio == NULL ) {
230  roar_err_set(ROAR_ERROR_FAULT);
231  return -1;
232 }
233
234 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
235
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);
242    roar_err_set(ROAR_ERROR_BADRQC);
243    return -1;
244   break;
245 }
246
247 if ( vio->ctl == NULL ) {
248  roar_err_set(ROAR_ERROR_NOSYS);
249  return -1;
250 }
251
252 roar_err_clear_all();
253 ret = vio->ctl(vio, cmd, data);
254 roar_err_update();
255
256 return ret;
257}
258
259int     roar_vio_close    (struct roar_vio_calls * vio) {
260 int ret;
261
262 ROAR_DBG("roar_vio_close(vio=%p) = ?", vio);
263
264 if ( vio == NULL ) {
265  roar_err_set(ROAR_ERROR_FAULT);
266  return -1;
267 }
268
269 if ( vio->close == NULL ) {
270  roar_err_set(ROAR_ERROR_NOSYS);
271  return -1;
272 }
273
274 roar_err_clear_all();
275 ret = vio->close(vio);
276 roar_err_update();
277
278 return ret;
279}
280
281// specal commands:
282int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
283 if (dst == NULL || calls == NULL) {
284  roar_err_set(ROAR_ERROR_FAULT);
285  return -1;
286 }
287
288 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
289}
290
291int     roar_vio_shutdown(struct roar_vio_calls * vio,   int how) {
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
303 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
304}
305
306// converters:
307int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
308#ifdef _CAN_OPERATE
309 int fh;
310
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);
315  return -1;
316 }
317
318#ifdef ROAR_TARGET_WIN32
319 flags |= O_BINARY;
320#endif
321
322 roar_err_clear_all();
323 if ( (fh = open(filename, flags, mode)) == -1 ) {
324  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
325  roar_err_update();
326  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
327  return -1;
328 }
329
330 if ( roar_vio_open_fh(calls, fh) == -1 ) {
331  close(fh);
332  roar_err_update();
333  return -1;
334 }
335
336 roar_err_update();
337 return 0;
338#else
339 return -1;
340#endif
341}
342
343int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
344 if ( calls == NULL ) {
345  roar_err_set(ROAR_ERROR_FAULT);
346  return -1;
347 }
348
349 roar_libroar_nowarn();
350 if ( roar_vio_init_calls(calls) == -1 ) {
351  roar_libroar_warn();
352  return -1;
353 }
354 roar_libroar_warn();
355
356 return roar_vio_set_fh(calls, fh);
357}
358
359int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
360 if ( calls == NULL ) {
361  roar_err_set(ROAR_ERROR_FAULT);
362  return -1;
363 }
364
365 if ( roar_vio_open_fh(calls, fh) == -1 )
366  return -1;
367
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
378
379 return 0;
380}
381
382int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
383 int fh;
384
385 if ( calls == NULL ) {
386  roar_err_set(ROAR_ERROR_FAULT);
387  return -1;
388 }
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
399 if ( calls == NULL ) {
400  roar_err_set(ROAR_ERROR_FAULT);
401  return -1;
402 }
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
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
414 if ( calls == NULL ) {
415  roar_err_set(ROAR_ERROR_FAULT);
416  return -1;
417 }
418
419 roar_libroar_nowarn();
420 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 ) {
421  roar_libroar_warn();
422  return -1;
423 }
424 roar_libroar_warn();
425
426 return roar_vio_open_fh_socket(calls, fh);
427}
428
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) {
433 struct roar_stream stream;
434 int fh;
435
436 ROAR_DBG("roar_vio_simple_new_stream_obj(*) = ?");
437
438 if ( calls == NULL ) {
439  roar_err_set(ROAR_ERROR_FAULT);
440  return -1;
441 }
442
443 if ( s == NULL )
444  s = &stream;
445
446 roar_libroar_nowarn();
447 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 ) {
448  roar_libroar_warn();
449  ROAR_DBG("roar_vio_simple_new_stream_obj(*) = -1");
450  return -1;
451 }
452 roar_libroar_warn();
453
454 ROAR_DBG("roar_vio_simple_new_stream_obj(*): fh=%i", fh);
455
456 return roar_vio_open_fh_socket(calls, fh);
457}
458
459// VIOs:
460
461// basic
462ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
463#ifdef _CAN_OPERATE
464 return read(roar_vio_get_fh(vio), buf, count);
465#else
466 roar_err_set(ROAR_ERROR_NOSYS);
467 return -1;
468#endif
469}
470
471ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
472#ifdef _CAN_OPERATE
473 return write(roar_vio_get_fh(vio), buf, count);
474#else
475 roar_err_set(ROAR_ERROR_NOSYS);
476 return -1;
477#endif
478}
479
480off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
481#ifdef _CAN_OPERATE
482 return lseek(roar_vio_get_fh(vio), offset, whence);
483#else
484 roar_err_set(ROAR_ERROR_NOSYS);
485 return -1;
486#endif
487}
488
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
496 roar_vio_sync(vio);
497
498 return 0;
499}
500
501int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
502#ifdef ROAR_FDATASYNC
503 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
504#else
505 return 0;
506#endif
507}
508
509int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
510#ifdef ROAR_HAVE_H_SYS_IOCTL
511 struct roar_vio_sysio_ioctl * sysioctl;
512#endif
513#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
514 struct roar_vio_sysio_sockopt  * syssockopt;
515#endif
516 int tmp;
517 int s_r = 0, s_w = 0;
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
540
541 if ( vio == NULL ) {
542  roar_err_set(ROAR_ERROR_FAULT);
543  return -1;
544 }
545
546 if ( cmd == -1 ) {
547  roar_err_set(ROAR_ERROR_INVAL);
548  return -1;
549 }
550
551 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
552
553 switch (cmd) {
554  case ROAR_VIO_CTL_GET_NAME:
555    if ( data == NULL ) {
556     roar_err_set(ROAR_ERROR_FAULT);
557     return -1;
558    }
559
560    *(char**)data = "basic";
561    return 0;
562   break;
563  case ROAR_VIO_CTL_GET_FH:
564  case ROAR_VIO_CTL_GET_READ_FH:
565  case ROAR_VIO_CTL_GET_WRITE_FH:
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:
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));
570    *(int*)data = roar_vio_get_fh(vio);
571    return 0;
572   break;
573  case ROAR_VIO_CTL_SET_NOSYNC:
574    vio->sync = NULL;
575    return 0;
576   break;
577  case ROAR_VIO_CTL_ACCEPT:
578    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
579    if ( tmp == -1 )
580     return -1;
581
582    // most proably a socket.
583    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
584#ifdef ROAR_TARGET_WIN32
585     closesocket(tmp);
586#else
587     close(tmp);
588#endif
589     return -1;
590    }
591
592    return 0;
593   break;
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
607    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
608     roar_err_set(ROAR_ERROR_NOTSUP);
609     return -1;
610    }
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
622    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
623   break;
624#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
625  case ROAR_VIO_CTL_GET_SOCKNAME:
626  case ROAR_VIO_CTL_GET_PEERNAME:
627    if ( data == NULL ) {
628     roar_err_set(ROAR_ERROR_FAULT);
629     return -1;
630    }
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
640     roar_err_set(ROAR_ERROR_NOSYS);
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
647     roar_err_set(ROAR_ERROR_NOSYS);
648     return -1;
649#endif
650    } else {
651     // memory corruption:
652     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
653     roar_err_set(ROAR_ERROR_CHERNOBYL);
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) {
663#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
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
676#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
677     case AF_DECnet:
678       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
679
680       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
681        roar_err_set(ROAR_ERROR_NOTSUP);
682        return -1;
683       }
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
701#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
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
708#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
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:
715       roar_err_set(ROAR_ERROR_NOTSUP);
716       return -1;
717    }
718    return 0;
719#endif
720   break;
721#ifdef ROAR_HAVE_H_SYS_IOCTL
722  case ROAR_VIO_CTL_SYSIO_IOCTL:
723    sysioctl = data;
724    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
725   break;
726#endif
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
739 }
740
741 roar_err_set(ROAR_ERROR_BADRQC);
742 return -1;
743}
744
745int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
746#ifdef _CAN_OPERATE
747 if ( roar_vio_get_fh(vio) != -1 )
748  return close(roar_vio_get_fh(vio));
749
750 return 0;
751#else
752 roar_err_set(ROAR_ERROR_NOSYS);
753 return -1;
754#endif
755}
756
757// null
758ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
759 if ( vio == NULL || buf == NULL ) {
760  roar_err_set(ROAR_ERROR_FAULT);
761  return -1;
762 }
763
764 return 0;
765}
766
767int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
768 return 0;
769}
770
771// pass
772int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
773 if ( calls == NULL || dst == NULL ) {
774  roar_err_set(ROAR_ERROR_FAULT);
775  return -1;
776 }
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;
785 calls->ctl      = roar_vio_pass_ctl;
786 calls->close    = roar_vio_pass_close;
787
788 calls->inst     = dst;
789
790 return 0;
791}
792
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
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) {
814 if (vio == NULL) {
815  roar_err_set(ROAR_ERROR_FAULT);
816  return -1;
817 }
818
819 if (cmd == -1) {
820  roar_err_set(ROAR_ERROR_INVAL);
821  return -1;
822 }
823
824 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
825
826 switch (cmd) {
827  case ROAR_VIO_CTL_GET_NAME:
828    if ( data == NULL ) {
829     roar_err_set(ROAR_ERROR_FAULT);
830     return -1;
831    }
832
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    }
839    return 0;
840   break;
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
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
858
859// re
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}
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
874 if ( vio == NULL ) {
875  roar_err_set(ROAR_ERROR_FAULT);
876  return -1;
877 }
878
879 if ( vio->inst == NULL ) {
880  roar_err_set(ROAR_ERROR_FAULT);
881  return -1;
882 }
883
884 roar_err_clear_all();
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
904 if ( vio == NULL ) {
905  roar_err_set(ROAR_ERROR_FAULT);
906  return -1;
907 }
908
909 if ( vio->inst == NULL ) {
910  roar_err_set(ROAR_ERROR_FAULT);
911  return -1;
912 }
913
914 roar_err_clear_all();
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
930// TODO: we should do a some more intelgent thing here.
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
935//ll
Note: See TracBrowser for help on using the repository browser.