source: roaraudio/libroar/vio.c @ 5276:0eb24ca6810e

Last change on this file since 5276:0eb24ca6810e was 5276:0eb24ca6810e, checked in by phi, 12 years ago

merged VIO's _nonblock() into _ctl() (Closes: #135)

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