source: roaraudio/roard/network.c @ 6052:d48765b2475e

Last change on this file since 6052:d48765b2475e was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

File size: 2.6 KB
RevLine 
[0]1//network.c:
2
[668]3/*
[6052]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015
[668]5 *
6 *  This file is part of roard 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 *  RoarAudio 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.
[668]23 *
24 */
25
[0]26#include "roard.h"
27
[1494]28#ifdef ROAR_SUPPORT_LISTEN
29
[1055]30#ifdef ROAR_BROKEN_PEERCRED
31#undef SO_PEERCRED
32#endif
33
[1484]34#ifdef ROAR_HAVE_SELECT
35#define _CAN_OPERATE
36#endif
37
[0]38int net_check_listen  (void) {
[3802]39 struct roar_vio_selecttv rtv;
40 struct roar_vio_select   sv[ROAR_MAX_LISTEN_SOCKETS];
41 size_t num = 0;
42 ssize_t ret;
[5586]43 size_t i;
[2530]44
45 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]46  if ( g_listen[i].used ) {
47   ROAR_VIO_SELECT_SETVIO(&(sv[num]), &(g_listen[i].sock), ROAR_VIO_SELECT_READ);
48   sv[num].ud.si = i;
49   num++;
[2530]50  }
51 }
52
[3802]53 if ( num == 0 )
[2530]54  return 0;
[0]55
[3802]56 rtv.sec  = 0;
57 rtv.nsec = 1000;
[0]58
[3802]59 if ( (ret = roar_vio_select(sv, num, &rtv, NULL)) > 0 ) {
60  for (i = 0; i < num; i++) {
61   if ( sv[i].eventsa & ROAR_VIO_SELECT_READ ) {
62    if ( net_get_new_client(&(g_listen[sv[i].ud.si])) == -1 )
63     return -1;
[2530]64   }
65  }
[0]66 }
67
[3802]68 return ret;
[0]69}
70
[3253]71int net_get_new_client (struct roard_listen * lsock) {
[0]72 int fh;
[3802]73 int socket;
[2815]74 struct roar_vio_calls    vio;
75 struct sockaddr_storage  addr;
76 socklen_t                addrlen = sizeof(addr);
[0]77
[3912]78 if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_FH, &socket) == -1 ) {
79  // next is needed for winsock:
80  if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_SELECT_FH, &socket) == -1 ) {
81   ROAR_DBG("net_get_new_client(void) = -1 // can not find any acceptable socket to accept() on");
82   return -1;
83  }
84 }
[3802]85
86 fh = accept(socket, (struct sockaddr*)&addr, &addrlen);
[0]87
88 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
89
[4228]90 if ( fh == -1 )
91  return -1;
92
[5585]93 if ( clients_new_from_fh2(fh, lsock->proto, ROAR_BYTEORDER_NETWORK, 1, lsock, (struct sockaddr*)&addr, addrlen) == -1 ) {
[5571]94  if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) {
95   close(fh);
96   return -1;
[3684]97  }
[5571]98  roar_vio_close(&vio);
[2529]99  return -1;
[2789]100 }
[2529]101
[0]102 return 0;
103}
104
[1494]105#endif
106
[0]107//ll
Note: See TracBrowser for help on using the repository browser.