source: roaraudio/libroar/vio.c @ 4963:1fea81784c37

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

added roar_vio_clear_calls()

File size: 20.5 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 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
292}
293
294// converters:
295int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
296#ifdef _CAN_OPERATE
297 int fh;
298
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);
303  return -1;
304 }
305
306#ifdef ROAR_TARGET_WIN32
307 flags |= O_BINARY;
308#endif
309
310 roar_err_clear_all();
311 if ( (fh = open(filename, flags, mode)) == -1 ) {
312  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
313  roar_err_update();
314  ROAR_DBG("roar_vio_open_file(*): errno=%s", strerror(errno));
315  return -1;
316 }
317
318 if ( roar_vio_open_fh(calls, fh) == -1 ) {
319  close(fh);
320  roar_err_update();
321  return -1;
322 }
323
324 roar_err_update();
325 return 0;
326#else
327 return -1;
328#endif
329}
330
331int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
332 if ( calls == NULL )
333  return -1;
334
335 roar_libroar_nowarn();
336 if ( roar_vio_init_calls(calls) == -1 ) {
337  roar_libroar_warn();
338  return -1;
339 }
340 roar_libroar_warn();
341
342 return roar_vio_set_fh(calls, fh);
343}
344
345int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
346 if ( calls == NULL )
347  return -1;
348
349 if ( roar_vio_open_fh(calls, fh) == -1 )
350  return -1;
351
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
362
363 return 0;
364}
365
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
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
397 roar_libroar_nowarn();
398 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 ) {
399  roar_libroar_warn();
400  return -1;
401 }
402 roar_libroar_warn();
403
404 return roar_vio_open_fh_socket(calls, fh);
405}
406
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) {
411 struct roar_stream stream;
412 int fh;
413
414 ROAR_DBG("roar_vio_simple_new_stream_obj(*) = ?");
415
416 if ( calls == NULL )
417  return -1;
418
419 if ( s == NULL )
420  s = &stream;
421
422 roar_libroar_nowarn();
423 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 ) {
424  roar_libroar_warn();
425  ROAR_DBG("roar_vio_simple_new_stream_obj(*) = -1");
426  return -1;
427 }
428 roar_libroar_warn();
429
430 ROAR_DBG("roar_vio_simple_new_stream_obj(*): fh=%i", fh);
431
432 return roar_vio_open_fh_socket(calls, fh);
433}
434
435// VIOs:
436
437// basic
438ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
439#ifdef _CAN_OPERATE
440 return read(roar_vio_get_fh(vio), buf, count);
441#else
442 return -1;
443#endif
444}
445
446ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
447#ifdef _CAN_OPERATE
448 return write(roar_vio_get_fh(vio), buf, count);
449#else
450 return -1;
451#endif
452}
453
454off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
455#ifdef _CAN_OPERATE
456 return lseek(roar_vio_get_fh(vio), offset, whence);
457#else
458 return -1;
459#endif
460}
461
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
469 roar_vio_sync(vio);
470
471 return 0;
472}
473
474int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
475#ifdef ROAR_FDATASYNC
476 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
477#else
478 return 0;
479#endif
480}
481
482int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
483#ifdef ROAR_HAVE_H_SYS_IOCTL
484 struct roar_vio_sysio_ioctl * sysioctl;
485#endif
486#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
487 struct roar_vio_sysio_sockopt  * syssockopt;
488#endif
489 int tmp;
490 int s_r = 0, s_w = 0;
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
513
514 if ( vio == NULL || cmd == -1 )
515  return -1;
516
517 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
518
519 switch (cmd) {
520  case ROAR_VIO_CTL_GET_NAME:
521    if ( data == NULL )
522     return -1;
523
524    *(char**)data = "basic";
525    return 0;
526   break;
527  case ROAR_VIO_CTL_GET_FH:
528  case ROAR_VIO_CTL_GET_READ_FH:
529  case ROAR_VIO_CTL_GET_WRITE_FH:
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:
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));
534    *(int*)data = roar_vio_get_fh(vio);
535    return 0;
536   break;
537  case ROAR_VIO_CTL_SET_NOSYNC:
538    vio->sync = NULL;
539    return 0;
540   break;
541  case ROAR_VIO_CTL_ACCEPT:
542    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
543    if ( tmp == -1 )
544     return -1;
545
546    // most proably a socket.
547    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
548#ifdef ROAR_TARGET_WIN32
549     closesocket(tmp);
550#else
551     close(tmp);
552#endif
553     return -1;
554    }
555
556    return 0;
557   break;
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
584    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
585   break;
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) {
618#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
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
631#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
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
654#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
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
661#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
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;
673#ifdef ROAR_HAVE_H_SYS_IOCTL
674  case ROAR_VIO_CTL_SYSIO_IOCTL:
675    sysioctl = data;
676    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
677   break;
678#endif
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
691 }
692
693 roar_err_set(ROAR_ERROR_BADRQC);
694 return -1;
695}
696
697int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
698#ifdef _CAN_OPERATE
699 if ( roar_vio_get_fh(vio) != -1 )
700  return close(roar_vio_get_fh(vio));
701
702 return 0;
703#else
704 return -1;
705#endif
706}
707
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
716int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
717 return 0;
718}
719
720// pass
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;
732 calls->ctl      = roar_vio_pass_ctl;
733 calls->close    = roar_vio_pass_close;
734
735 calls->inst     = dst;
736
737 return 0;
738}
739
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
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) {
761 if (vio == NULL || cmd == -1)
762  return -1;
763
764 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
765
766 switch (cmd) {
767  case ROAR_VIO_CTL_GET_NAME:
768    if ( data == NULL )
769     return -1;
770
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    }
777    return 0;
778   break;
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
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
796
797// re
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}
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
818 roar_err_clear_all();
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
844 roar_err_clear_all();
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
860// TODO: we should do a some more intelgent thing here.
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
865//ll
Note: See TracBrowser for help on using the repository browser.