source: roaraudio/libroar/vio.c @ 4998:b404387a2080

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

use memory corruption roar_panic()

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