source: roaraudio/libroar/vio.c @ 4968:2c5119fa5798

Last change on this file since 4968:2c5119fa5798 was 4968:2c5119fa5798, checked in by phi, 13 years ago

some updates to error handling

File size: 22.0 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_err_set(ROAR_ERROR_CHERNOBYL);
652     return -1;
653    }
654
655    if ( tmp == -1 )
656     return -1;
657
658    memset(rsockname, 0, sizeof(struct roar_sockname));
659
660    switch (sockaddr.sa.sa_family) {
661#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
662     case AF_UNIX:
663       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
664       if ( sockaddr.un.sun_path[0] == 0 ) {
665        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
666        if ( rsockname->addr == NULL )
667         return -1;
668        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
669       } else {
670        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
671       }
672      break;
673#endif
674#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
675     case AF_DECnet:
676       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
677
678       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
679        roar_err_set(ROAR_ERROR_NOTSUP);
680        return -1;
681       }
682
683       rsockname->addr = roar_mm_malloc(28);
684       if ( rsockname->addr == NULL )
685        return -1;
686
687       snprintf(rsockname->addr, 28, "%i.%i::",
688                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
689                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
690
691       rsockname->port = sockaddr.dn.sdn_objnum;
692       if ( sockaddr.dn.sdn_objnum == 0 ) {
693        tmp = strlen(rsockname->addr);
694        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
695        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
696       }
697      break;
698#endif
699#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
700     case AF_INET:
701       rsockname->type = ROAR_SOCKET_TYPE_INET;
702       rsockname->port = ntohs(sockaddr.in.sin_port);
703       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
704      break;
705#endif
706#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
707     case AF_INET6:
708       rsockname->type = ROAR_SOCKET_TYPE_INET6;
709       rsockname->port = ntohs(sockaddr.in6.sin6_port);
710      break;
711#endif
712     default:
713       roar_err_set(ROAR_ERROR_NOTSUP);
714       return -1;
715    }
716    return 0;
717#endif
718   break;
719#ifdef ROAR_HAVE_H_SYS_IOCTL
720  case ROAR_VIO_CTL_SYSIO_IOCTL:
721    sysioctl = data;
722    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
723   break;
724#endif
725#ifdef ROAR_HAVE_GETSOCKOPT
726  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
727    syssockopt = data;
728    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
729   break;
730#endif
731#ifdef ROAR_HAVE_SETSOCKOPT
732  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
733    syssockopt = data;
734    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
735   break;
736#endif
737 }
738
739 roar_err_set(ROAR_ERROR_BADRQC);
740 return -1;
741}
742
743int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
744#ifdef _CAN_OPERATE
745 if ( roar_vio_get_fh(vio) != -1 )
746  return close(roar_vio_get_fh(vio));
747
748 return 0;
749#else
750 roar_err_set(ROAR_ERROR_NOSYS);
751 return -1;
752#endif
753}
754
755// null
756ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
757 if ( vio == NULL || buf == NULL ) {
758  roar_err_set(ROAR_ERROR_FAULT);
759  return -1;
760 }
761
762 return 0;
763}
764
765int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
766 return 0;
767}
768
769// pass
770int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
771 if ( calls == NULL || dst == NULL ) {
772  roar_err_set(ROAR_ERROR_FAULT);
773  return -1;
774 }
775
776 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
777
778 calls->read     = roar_vio_pass_read;
779 calls->write    = roar_vio_pass_write;
780 calls->lseek    = roar_vio_pass_lseek;
781 calls->nonblock = roar_vio_pass_nonblock;
782 calls->sync     = roar_vio_pass_sync;
783 calls->ctl      = roar_vio_pass_ctl;
784 calls->close    = roar_vio_pass_close;
785
786 calls->inst     = dst;
787
788 return 0;
789}
790
791ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
792 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
793}
794
795ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
796 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
797}
798
799off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
800 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
801}
802
803int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
804 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
805}
806
807int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
808 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
809}
810
811int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
812 if (vio == NULL) {
813  roar_err_set(ROAR_ERROR_FAULT);
814  return -1;
815 }
816
817 if (cmd == -1) {
818  roar_err_set(ROAR_ERROR_INVAL);
819  return -1;
820 }
821
822 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
823
824 switch (cmd) {
825  case ROAR_VIO_CTL_GET_NAME:
826    if ( data == NULL ) {
827     roar_err_set(ROAR_ERROR_FAULT);
828     return -1;
829    }
830
831    // dirty trick to get real name...
832    if ( vio->read == roar_vio_re_read ) {
833     *(char**)data = "re";
834    } else {
835     *(char**)data = "pass";
836    }
837    return 0;
838   break;
839  case ROAR_VIO_CTL_GET_NEXT:
840    *(struct roar_vio_calls **)data = vio->inst;
841    return 0;
842   break;
843  case ROAR_VIO_CTL_SET_NEXT:
844    vio->inst = *(struct roar_vio_calls **)data;
845    return 0;
846   break;
847 }
848
849 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
850}
851
852int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
853 return roar_vio_close((struct roar_vio_calls *) vio->inst);
854}
855
856
857// re
858int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
859 if ( roar_vio_open_pass(calls, dst) == -1 )
860  return -1;
861
862 calls->read  = roar_vio_re_read;
863 calls->write = roar_vio_re_write;
864 calls->lseek = roar_vio_re_lseek;
865
866 return 0;
867}
868ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
869  size_t len =  0;
870 ssize_t r   = -1;
871
872 if ( vio == NULL ) {
873  roar_err_set(ROAR_ERROR_FAULT);
874  return -1;
875 }
876
877 if ( vio->inst == NULL ) {
878  roar_err_set(ROAR_ERROR_FAULT);
879  return -1;
880 }
881
882 roar_err_clear_all();
883
884 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
885  len   += r;
886  buf   += r;
887  count -= r;
888  if ( count == 0 )
889   break;
890 }
891
892 if ( len == 0 && r == -1 )
893  return -1;
894
895 return len;
896}
897
898ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
899  size_t len =  0;
900 ssize_t r   = -1;
901
902 if ( vio == NULL ) {
903  roar_err_set(ROAR_ERROR_FAULT);
904  return -1;
905 }
906
907 if ( vio->inst == NULL ) {
908  roar_err_set(ROAR_ERROR_FAULT);
909  return -1;
910 }
911
912 roar_err_clear_all();
913
914 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
915  len   += r;
916  buf   += r;
917  count -= r;
918  if ( count == 0 )
919   break;
920 }
921
922 if ( len == 0 && r == -1 )
923  return -1;
924
925 return len;
926}
927
928// TODO: we should do a some more intelgent thing here.
929off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
930 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
931}
932
933//ll
Note: See TracBrowser for help on using the repository browser.