source: roaraudio/libroar/vio.c @ 5311:778a3e7b2b66

Last change on this file since 5311:778a3e7b2b66 was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

File size: 19.9 KB
RevLine 
[590]1//vio.c:
2
[690]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[690]5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
[5109]36#define _LIBROAR_NOATTR_TO_STATIC /* ignore warnings for TO_STATIC functions */
[590]37#include "libroar.h"
[3895]38
39#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]40#include <sys/ioctl.h>
[3895]41#endif
[590]42
[1474]43#ifdef ROAR_HAVE_IO_POSIX
44#define _CAN_OPERATE
45#endif
46
[591]47int roar_vio_init_calls (struct roar_vio_calls * calls) {
[4963]48 roar_debug_warn_obsolete("roar_vio_init_calls", "roar_vio_clear_calls", NULL);
49
[1474]50#ifdef _CAN_OPERATE
[4873]51 if ( calls == NULL ) {
52  roar_err_set(ROAR_ERROR_FAULT);
[591]53  return -1;
[4873]54 }
[591]55
56 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
57
[1118]58 calls->read     = roar_vio_basic_read;
59 calls->write    = roar_vio_basic_write;
60 calls->lseek    = roar_vio_basic_lseek;
61 calls->sync     = roar_vio_basic_sync;
[1505]62 calls->ctl      = roar_vio_basic_ctl;
[1241]63 calls->close    = roar_vio_basic_close;
[881]64
65 return 0;
[1474]66#else
67 return -1;
68#endif
[881]69}
70
[4963]71int roar_vio_clear_calls (struct roar_vio_calls * calls) {
72 if ( calls == NULL ) {
73  roar_err_set(ROAR_ERROR_FAULT);
74  return -1;
75 }
76
77 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
78
79 return 0;
80}
81
[881]82int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
[4873]83 if ( vio == NULL ) {
84  roar_err_set(ROAR_ERROR_FAULT);
[881]85  return -1;
[4873]86 }
[881]87
88 vio->inst = inst;
[591]89
90 return 0;
91}
92
[881]93int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
[989]94 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
[881]95}
96
97int roar_vio_get_fh   (struct roar_vio_calls * vio) {
[4873]98 if ( vio == NULL ) {
99  roar_err_set(ROAR_ERROR_FAULT);
[881]100  return -1;
[4873]101 }
[881]102
[989]103 return ((int)(ROAR_INSTINT)vio->inst) - 1;
[881]104}
105
106
107ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
[4873]108 ssize_t ret;
109
[1319]110 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
111
[4873]112 if ( vio == NULL ) {
113  roar_err_set(ROAR_ERROR_FAULT);
[881]114  return -1;
[4873]115 }
[881]116
[4873]117 if ( vio->read == NULL ) {
118  roar_err_set(ROAR_ERROR_NOSYS);
[881]119  return -1;
[4873]120 }
[881]121
[4873]122 roar_err_clear_all();
123 ret = vio->read(vio, buf, count);
124 roar_err_update();
125
126 return ret;
[881]127}
128
129ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
[4873]130 ssize_t ret;
131
[3276]132 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
133
[4873]134 if ( vio == NULL ) {
135  roar_err_set(ROAR_ERROR_FAULT);
[881]136  return -1;
[4873]137 }
[881]138
[4873]139 if ( vio->write == NULL ) {
140  roar_err_set(ROAR_ERROR_NOSYS);
[881]141  return -1;
[4873]142 }
[881]143
[4873]144 roar_err_clear_all();
145 ret = vio->write(vio, buf, count);
146 roar_err_update();
147
148 return ret;
[881]149}
150
[5278]151roar_off_t   roar_vio_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
152 roar_off_t ret;
[4873]153
[5278]154 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%li, whence=%i) = ?", vio, (long int)offset, whence);
[3276]155
[4873]156 if ( vio == NULL ) {
157  roar_err_set(ROAR_ERROR_FAULT);
[881]158  return -1;
[4873]159 }
[881]160
[4873]161 if ( vio->lseek == NULL ) {
162  roar_err_set(ROAR_ERROR_NOSYS);
[881]163  return -1;
[4873]164 }
[881]165
[4873]166 roar_err_clear_all();
167 ret = vio->lseek(vio, offset, whence);
168 roar_err_update();
169
170 return ret;
[881]171}
172
[1118]173int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
[3276]174 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
175
[4873]176 if ( vio == NULL ) {
177  roar_err_set(ROAR_ERROR_FAULT);
[1118]178  return -1;
[4873]179 }
[1118]180
[5276]181 return roar_vio_ctl(vio, ROAR_VIO_CTL_NONBLOCK, &state);
[1118]182}
183
184int     roar_vio_sync    (struct roar_vio_calls * vio) {
[4873]185 int ret;
186
[3276]187 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
188
[4873]189 if ( vio == NULL ) {
190  roar_err_set(ROAR_ERROR_FAULT);
[1118]191  return -1;
[4873]192 }
[1118]193
[4873]194 if ( vio->sync == NULL ) {
195  roar_err_set(ROAR_ERROR_NOSYS);
[1118]196  return -1;
[4873]197 }
[1118]198
[4873]199 roar_err_clear_all();
200 ret = vio->sync(vio);
201 roar_err_update();
202
203 return ret;
[1118]204}
205
[5278]206int     roar_vio_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[4873]207 int ret;
208
[3276]209 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
210
[4873]211 if ( vio == NULL ) {
212  roar_err_set(ROAR_ERROR_FAULT);
[1140]213  return -1;
[4873]214 }
[1140]215
[1615]216 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
217
[4831]218 switch (cmd) {
219  case ROAR_VIO_CTL_CONFLICTING_ID_0:
220  case ROAR_VIO_CTL_CONFLICTING_ID_1:
221    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);
222    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);
223    ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p) = -1", vio, cmd, data);
[4873]224    roar_err_set(ROAR_ERROR_BADRQC);
[4831]225    return -1;
226   break;
227 }
228
[4873]229 if ( vio->ctl == NULL ) {
230  roar_err_set(ROAR_ERROR_NOSYS);
[1140]231  return -1;
[4873]232 }
[1140]233
[4873]234 roar_err_clear_all();
235 ret = vio->ctl(vio, cmd, data);
236 roar_err_update();
237
238 return ret;
[1140]239}
240
[1241]241int     roar_vio_close    (struct roar_vio_calls * vio) {
[4873]242 int ret;
243
[3276]244 ROAR_DBG("roar_vio_close(vio=%p) = ?", vio);
245
[4873]246 if ( vio == NULL ) {
247  roar_err_set(ROAR_ERROR_FAULT);
[1241]248  return -1;
[4873]249 }
[1241]250
[4873]251 if ( vio->close == NULL ) {
252  roar_err_set(ROAR_ERROR_NOSYS);
[1241]253  return -1;
[4873]254 }
[1241]255
[4873]256 roar_err_clear_all();
257 ret = vio->close(vio);
258 roar_err_update();
259
260 return ret;
[1241]261}
262
[3769]263// specal commands:
264int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4873]265 if (dst == NULL || calls == NULL) {
266  roar_err_set(ROAR_ERROR_FAULT);
[3769]267  return -1;
[4873]268 }
[3769]269
270 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
271}
272
[3796]273int     roar_vio_shutdown(struct roar_vio_calls * vio,   int how) {
[4968]274 if (vio == NULL) {
275  roar_err_set(ROAR_ERROR_FAULT);
276  return -1;
277 }
278
279 if ( ( (how | ROAR_VIO_SHUTDOWN_READ|ROAR_VIO_SHUTDOWN_WRITE|ROAR_VIO_SHUTDOWN_LISTEN) -
280        (ROAR_VIO_SHUTDOWN_READ|ROAR_VIO_SHUTDOWN_WRITE|ROAR_VIO_SHUTDOWN_LISTEN) ) != 0 ) {
281  roar_err_set(ROAR_ERROR_INVAL);
282  return -1;
283 }
284
[3796]285 return roar_vio_ctl(vio, ROAR_VIO_CTL_SHUTDOWN, &how);
286}
287
[1252]288// converters:
[5242]289int     roar_vio_open_file     (struct roar_vio_calls * calls, const char * filename, int flags, mode_t mode) {
[5253]290 struct roar_vio_defaults def;
[1252]291
[4873]292 roar_debug_warn_obsolete("roar_vio_open_file", "roar_vio_open_dstr", NULL);
293
[5253]294 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_FILE, flags, mode) == -1 )
[1252]295  return -1;
[1762]296
[5253]297 def.d.file = filename;
[1252]298
[5253]299 return roar_vio_open_default(calls, &def, NULL);
[1252]300}
301
302int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
[4968]303 if ( calls == NULL ) {
304  roar_err_set(ROAR_ERROR_FAULT);
[1252]305  return -1;
[4968]306 }
[1252]307
[4963]308 roar_libroar_nowarn();
309 if ( roar_vio_init_calls(calls) == -1 ) {
310  roar_libroar_warn();
[1252]311  return -1;
[4963]312 }
313 roar_libroar_warn();
[1252]314
315 return roar_vio_set_fh(calls, fh);
316}
317
[1290]318int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
[4968]319 if ( calls == NULL ) {
320  roar_err_set(ROAR_ERROR_FAULT);
[1290]321  return -1;
[4968]322 }
[1290]323
[1666]324 if ( roar_vio_open_fh(calls, fh) == -1 )
325  return -1;
326
[1760]327#ifdef ROAR_TARGET_WIN32
328 calls->read     = roar_vio_winsock_read;
329 calls->write    = roar_vio_winsock_write;
330 calls->sync     = roar_vio_winsock_sync;
331 calls->ctl      = roar_vio_winsock_ctl;
332 calls->close    = roar_vio_winsock_close;
333#else
334 calls->sync     = roar_vio_null_sync;
335#endif
[1666]336
337 return 0;
[1290]338}
339
[5260]340int     roar_vio_open_socket   (struct roar_vio_calls * calls, const char * host, int port) {
[1291]341 int fh;
342
[4968]343 if ( calls == NULL ) {
344  roar_err_set(ROAR_ERROR_FAULT);
[1291]345  return -1;
[4968]346 }
[1291]347
348 if ( (fh = roar_socket_connect(host, port)) == -1 )
349  return -1;
350
351 return roar_vio_open_fh_socket(calls, fh);
352}
353
[5260]354int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, const char * host, int port) {
[1291]355 int fh;
356
[4968]357 if ( calls == NULL ) {
358  roar_err_set(ROAR_ERROR_FAULT);
[1291]359  return -1;
[4968]360 }
[1291]361
362 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
363  return -1;
364
365 return roar_vio_open_fh_socket(calls, fh);
366}
367
[2841]368
[886]369// VIOs:
[881]370
[886]371// basic
[881]372ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]373#ifdef _CAN_OPERATE
[881]374 return read(roar_vio_get_fh(vio), buf, count);
[1474]375#else
[4968]376 roar_err_set(ROAR_ERROR_NOSYS);
[1474]377 return -1;
378#endif
[881]379}
380
381ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
[1474]382#ifdef _CAN_OPERATE
[881]383 return write(roar_vio_get_fh(vio), buf, count);
[1474]384#else
[4968]385 roar_err_set(ROAR_ERROR_NOSYS);
[1474]386 return -1;
387#endif
[881]388}
389
[5278]390roar_off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[1474]391#ifdef _CAN_OPERATE
[881]392 return lseek(roar_vio_get_fh(vio), offset, whence);
[1474]393#else
[4968]394 roar_err_set(ROAR_ERROR_NOSYS);
[1474]395 return -1;
396#endif
[881]397}
398
[1118]399int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
[1397]400#ifdef ROAR_FDATASYNC
[1171]401 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
[1397]402#else
403 return 0;
404#endif
[1118]405}
406
[5278]407int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[3895]408#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]409 struct roar_vio_sysio_ioctl * sysioctl;
[3895]410#endif
[4718]411#if defined(ROAR_HAVE_GETSOCKOPT) || defined(ROAR_HAVE_SETSOCKOPT)
412 struct roar_vio_sysio_sockopt  * syssockopt;
413#endif
[3796]414 int tmp;
415 int s_r = 0, s_w = 0;
[4705]416#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
417 union {
418  struct sockaddr     sa;
419#if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6)
420  struct sockaddr_in  in;
421#endif
422#ifdef ROAR_HAVE_UNIX
423  struct sockaddr_un  un;
424#endif
425#ifdef ROAR_HAVE_LIBDNET
426  struct sockaddr_dn  dn;
427#endif
428#ifdef ROAR_HAVE_IPV6
429  struct sockaddr_in6 in6;
430#endif
431#ifdef ROAR_HAVE_IPX
432  struct sockaddr_ipx ipx;
433#endif
434 } sockaddr;
435 socklen_t socklen;
436 struct roar_sockname * rsockname;
437#endif
[1505]438
[4968]439 if ( vio == NULL ) {
440  roar_err_set(ROAR_ERROR_FAULT);
[1505]441  return -1;
[4968]442 }
443
444 if ( cmd == -1 ) {
445  roar_err_set(ROAR_ERROR_INVAL);
446  return -1;
447 }
[1505]448
[1615]449 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
450
[1505]451 switch (cmd) {
[3795]452  case ROAR_VIO_CTL_GET_NAME:
[4968]453    if ( data == NULL ) {
454     roar_err_set(ROAR_ERROR_FAULT);
[3795]455     return -1;
[4968]456    }
[3795]457
458    *(char**)data = "basic";
459    return 0;
460   break;
[1505]461  case ROAR_VIO_CTL_GET_FH:
462  case ROAR_VIO_CTL_GET_READ_FH:
463  case ROAR_VIO_CTL_GET_WRITE_FH:
[3436]464  case ROAR_VIO_CTL_GET_SELECT_FH:
465  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
466  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
[1615]467    ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_*FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, roar_vio_get_fh(vio));
[1505]468    *(int*)data = roar_vio_get_fh(vio);
469    return 0;
470   break;
[2054]471  case ROAR_VIO_CTL_SET_NOSYNC:
472    vio->sync = NULL;
473    return 0;
474   break;
[3769]475  case ROAR_VIO_CTL_ACCEPT:
[3898]476    tmp = ROAR_ACCEPT(roar_vio_get_fh(vio), NULL, 0);
[3796]477    if ( tmp == -1 )
[3769]478     return -1;
479
480    // most proably a socket.
[3796]481    if ( roar_vio_open_fh_socket(data, tmp) == -1 ) {
[3769]482#ifdef ROAR_TARGET_WIN32
[3796]483     closesocket(tmp);
[3769]484#else
[3796]485     close(tmp);
[3769]486#endif
487     return -1;
488    }
489
490    return 0;
491   break;
[3796]492  case ROAR_VIO_CTL_SHUTDOWN:
493    tmp = *(int*)data;
494
495    if ( tmp & ROAR_VIO_SHUTDOWN_READ ) {
496     s_r = 1;
497     tmp -= ROAR_VIO_SHUTDOWN_READ;
498    }
499
500    if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) {
501     s_w = 1;
502     tmp -= ROAR_VIO_SHUTDOWN_WRITE;
503    }
504
[4968]505    if ( tmp != 0 ) { /* we currently only support R and W shutdowns */
506     roar_err_set(ROAR_ERROR_NOTSUP);
[3796]507     return -1;
[4968]508    }
[3796]509
510    if ( s_r && s_w ) {
511     tmp = SHUT_RDWR;
512    } else if ( s_r ) {
513     tmp = SHUT_RD;
514    } else if ( s_w ) {
515     tmp = SHUT_WR;
516    } else {
517     return 0; // nothing to do.
518    }
519
[3858]520    return ROAR_SHUTDOWN(roar_vio_get_fh(vio), tmp);
[3796]521   break;
[4705]522#if defined(ROAR_HAVE_GETSOCKNAME) || defined(ROAR_HAVE_GETPEERNAME)
523  case ROAR_VIO_CTL_GET_SOCKNAME:
524  case ROAR_VIO_CTL_GET_PEERNAME:
[4968]525    if ( data == NULL ) {
526     roar_err_set(ROAR_ERROR_FAULT);
[4705]527     return -1;
[4968]528    }
[4705]529
530    rsockname = data;
531
532    socklen = sizeof(sockaddr);
533
534    if ( cmd == ROAR_VIO_CTL_GET_SOCKNAME ) {
535#ifdef ROAR_HAVE_GETSOCKNAME
536     tmp = getsockname(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
537#else
[4968]538     roar_err_set(ROAR_ERROR_NOSYS);
[4705]539     return -1;
540#endif
541    } else if ( cmd == ROAR_VIO_CTL_GET_PEERNAME ) {
542#ifdef ROAR_HAVE_GETPEERNAME
543     tmp = getpeername(roar_vio_get_fh(vio), &(sockaddr.sa), &socklen);
544#else
[4968]545     roar_err_set(ROAR_ERROR_NOSYS);
[4705]546     return -1;
547#endif
548    } else {
[4968]549     // memory corruption:
[4973]550     roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
[4968]551     roar_err_set(ROAR_ERROR_CHERNOBYL);
[4705]552     return -1;
553    }
554
555    if ( tmp == -1 )
556     return -1;
557
558    memset(rsockname, 0, sizeof(struct roar_sockname));
559
560    switch (sockaddr.sa.sa_family) {
[4730]561#if defined(AF_UNIX) && defined(ROAR_HAVE_UNIX)
[4705]562     case AF_UNIX:
563       rsockname->type = ROAR_SOCKET_TYPE_UNIX;
564       if ( sockaddr.un.sun_path[0] == 0 ) {
565        rsockname->addr = roar_mm_malloc(sizeof(sockaddr.un.sun_path));
566        if ( rsockname->addr == NULL )
567         return -1;
568        memcpy(rsockname->addr, sockaddr.un.sun_path, sizeof(sockaddr.un.sun_path));
569       } else {
570        rsockname->addr = roar_mm_strdup(sockaddr.un.sun_path);
571       }
572      break;
573#endif
[4730]574#if defined(AF_DECnet) && defined(ROAR_HAVE_LIBDNET)
[4705]575     case AF_DECnet:
576       rsockname->type = ROAR_SOCKET_TYPE_DECNET;
577
[4968]578       if ( sockaddr.dn.sdn_add.a_len != 2 ) {
579        roar_err_set(ROAR_ERROR_NOTSUP);
[4705]580        return -1;
[4968]581       }
[4705]582
583       rsockname->addr = roar_mm_malloc(28);
584       if ( rsockname->addr == NULL )
585        return -1;
586
587       snprintf(rsockname->addr, 28, "%i.%i::",
588                 sockaddr.dn.sdn_add.a_addr[1] >> 2,
589                 sockaddr.dn.sdn_add.a_addr[0] + ((sockaddr.dn.sdn_add.a_addr[1] & 0x03) << 8));
590
591       rsockname->port = sockaddr.dn.sdn_objnum;
592       if ( sockaddr.dn.sdn_objnum == 0 ) {
593        tmp = strlen(rsockname->addr);
594        memcpy(rsockname->addr + tmp, sockaddr.dn.sdn_objname, sockaddr.dn.sdn_objnamel);
595        rsockname->addr[tmp + sockaddr.dn.sdn_objnamel] = 0;
596       }
597      break;
598#endif
[4730]599#if defined(AF_INET) && (defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_IPV6))
[4705]600     case AF_INET:
601       rsockname->type = ROAR_SOCKET_TYPE_INET;
602       rsockname->port = ntohs(sockaddr.in.sin_port);
603       rsockname->addr = roar_mm_strdup(inet_ntoa(sockaddr.in.sin_addr));
604      break;
605#endif
[4730]606#if defined(AF_INET6) && defined(ROAR_HAVE_IPV6)
[4705]607     case AF_INET6:
608       rsockname->type = ROAR_SOCKET_TYPE_INET6;
609       rsockname->port = ntohs(sockaddr.in6.sin6_port);
610      break;
611#endif
612     default:
[4968]613       roar_err_set(ROAR_ERROR_NOTSUP);
[4705]614       return -1;
615    }
616    return 0;
617#endif
618   break;
[3895]619#ifdef ROAR_HAVE_H_SYS_IOCTL
[3852]620  case ROAR_VIO_CTL_SYSIO_IOCTL:
621    sysioctl = data;
622    return ioctl(roar_vio_get_fh(vio), sysioctl->cmd, sysioctl->argp);
623   break;
[3895]624#endif
[4718]625#ifdef ROAR_HAVE_GETSOCKOPT
626  case ROAR_VIO_CTL_GET_SYSIO_SOCKOPT:
627    syssockopt = data;
628    return getsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, &(syssockopt->optlen));
629   break;
630#endif
631#ifdef ROAR_HAVE_SETSOCKOPT
632  case ROAR_VIO_CTL_SET_SYSIO_SOCKOPT:
633    syssockopt = data;
634    return setsockopt(roar_vio_get_fh(vio), syssockopt->level, syssockopt->optname, syssockopt->optval, syssockopt->optlen);
635   break;
636#endif
[5276]637  case ROAR_VIO_CTL_NONBLOCK:
638    if ( roar_socket_nonblock(roar_vio_get_fh(vio), *(int*)data) == -1 )
639     return -1;
640
641    if ( *(int*)data == ROAR_SOCKET_NONBLOCK )
642     return 0;
643
644    roar_vio_sync(vio);
645    return 0;
646   break;
[1505]647 }
648
[4873]649 roar_err_set(ROAR_ERROR_BADRQC);
[1505]650 return -1;
651}
652
[1241]653int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
[1474]654#ifdef _CAN_OPERATE
[1336]655 if ( roar_vio_get_fh(vio) != -1 )
656  return close(roar_vio_get_fh(vio));
657
658 return 0;
[1474]659#else
[4968]660 roar_err_set(ROAR_ERROR_NOSYS);
[1474]661 return -1;
662#endif
[1241]663}
664
[943]665// null
666ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
[5270]667 (void)count;
668
[4968]669 if ( vio == NULL || buf == NULL ) {
670  roar_err_set(ROAR_ERROR_FAULT);
[943]671  return -1;
[4968]672 }
[943]673
674 return 0;
675}
676
[1665]677int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
[5270]678 (void)vio;
[1665]679 return 0;
680}
681
[886]682// pass
[1247]683int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
[4968]684 if ( calls == NULL || dst == NULL ) {
685  roar_err_set(ROAR_ERROR_FAULT);
[1247]686  return -1;
[4968]687 }
[1247]688
689 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
690
691 calls->read     = roar_vio_pass_read;
692 calls->write    = roar_vio_pass_write;
693 calls->lseek    = roar_vio_pass_lseek;
694 calls->sync     = roar_vio_pass_sync;
[1615]695 calls->ctl      = roar_vio_pass_ctl;
[1247]696 calls->close    = roar_vio_pass_close;
697
698 calls->inst     = dst;
699
700 return 0;
701}
702
[886]703ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
704 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
705}
706
707ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
708 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
709}
710
[5278]711roar_off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[886]712 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
713}
714
[1241]715int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
716 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
717}
718
[5278]719int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
[4968]720 if (vio == NULL) {
721  roar_err_set(ROAR_ERROR_FAULT);
[1505]722  return -1;
[4968]723 }
724
725 if (cmd == -1) {
726  roar_err_set(ROAR_ERROR_INVAL);
727  return -1;
728 }
[1505]729
[1615]730 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
731
[1505]732 switch (cmd) {
[3795]733  case ROAR_VIO_CTL_GET_NAME:
[4968]734    if ( data == NULL ) {
735     roar_err_set(ROAR_ERROR_FAULT);
[3795]736     return -1;
[4968]737    }
[3795]738
[4829]739    // dirty trick to get real name...
740    if ( vio->read == roar_vio_re_read ) {
741     *(char**)data = "re";
742    } else {
743     *(char**)data = "pass";
744    }
[3795]745    return 0;
746   break;
[1505]747  case ROAR_VIO_CTL_GET_NEXT:
748    *(struct roar_vio_calls **)data = vio->inst;
749    return 0;
750   break;
751  case ROAR_VIO_CTL_SET_NEXT:
752    vio->inst = *(struct roar_vio_calls **)data;
753    return 0;
754   break;
755 }
756
[1241]757 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
758}
759
760int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
761 return roar_vio_close((struct roar_vio_calls *) vio->inst);
762}
763
[886]764
765// re
[1247]766int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
767 if ( roar_vio_open_pass(calls, dst) == -1 )
768  return -1;
769
770 calls->read  = roar_vio_re_read;
771 calls->write = roar_vio_re_write;
772 calls->lseek = roar_vio_re_lseek;
773
774 return 0;
775}
[886]776ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
777  size_t len =  0;
778 ssize_t r   = -1;
779
[4968]780 if ( vio == NULL ) {
781  roar_err_set(ROAR_ERROR_FAULT);
[886]782  return -1;
[4968]783 }
[886]784
[4968]785 if ( vio->inst == NULL ) {
786  roar_err_set(ROAR_ERROR_FAULT);
[886]787  return -1;
[4968]788 }
[886]789
[4873]790 roar_err_clear_all();
[886]791
792 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
793  len   += r;
794  buf   += r;
795  count -= r;
796  if ( count == 0 )
797   break;
798 }
799
800 if ( len == 0 && r == -1 )
801  return -1;
802
803 return len;
804}
805
806ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
807  size_t len =  0;
808 ssize_t r   = -1;
809
[4968]810 if ( vio == NULL ) {
811  roar_err_set(ROAR_ERROR_FAULT);
[886]812  return -1;
[4968]813 }
[886]814
[4968]815 if ( vio->inst == NULL ) {
816  roar_err_set(ROAR_ERROR_FAULT);
[886]817  return -1;
[4968]818 }
[886]819
[4873]820 roar_err_clear_all();
[886]821
822 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
823  len   += r;
824  buf   += r;
825  count -= r;
826  if ( count == 0 )
827   break;
828 }
829
830 if ( len == 0 && r == -1 )
831  return -1;
832
833 return len;
834}
835
[1247]836// TODO: we should do a some more intelgent thing here.
[5278]837roar_off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, roar_off_t offset, int whence) {
[886]838 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
839}
840
[590]841//ll
Note: See TracBrowser for help on using the repository browser.