1 | //network.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015 |
---|
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 |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #ifdef ROAR_SUPPORT_LISTEN |
---|
29 | |
---|
30 | #ifdef ROAR_BROKEN_PEERCRED |
---|
31 | #undef SO_PEERCRED |
---|
32 | #endif |
---|
33 | |
---|
34 | #ifdef ROAR_HAVE_SELECT |
---|
35 | #define _CAN_OPERATE |
---|
36 | #endif |
---|
37 | |
---|
38 | int net_check_listen (void) { |
---|
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; |
---|
43 | size_t i; |
---|
44 | |
---|
45 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
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++; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | if ( num == 0 ) |
---|
54 | return 0; |
---|
55 | |
---|
56 | rtv.sec = 0; |
---|
57 | rtv.nsec = 1000; |
---|
58 | |
---|
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; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | return ret; |
---|
69 | } |
---|
70 | |
---|
71 | int net_get_new_client (struct roard_listen * lsock) { |
---|
72 | int fh; |
---|
73 | int socket; |
---|
74 | struct roar_vio_calls vio; |
---|
75 | struct sockaddr_storage addr; |
---|
76 | socklen_t addrlen = sizeof(addr); |
---|
77 | |
---|
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 | } |
---|
85 | |
---|
86 | fh = accept(socket, (struct sockaddr*)&addr, &addrlen); |
---|
87 | |
---|
88 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
89 | |
---|
90 | if ( fh == -1 ) |
---|
91 | return -1; |
---|
92 | |
---|
93 | if ( clients_new_from_fh2(fh, lsock->proto, ROAR_BYTEORDER_NETWORK, 1, lsock, (struct sockaddr*)&addr, addrlen) == -1 ) { |
---|
94 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) { |
---|
95 | close(fh); |
---|
96 | return -1; |
---|
97 | } |
---|
98 | roar_vio_close(&vio); |
---|
99 | return -1; |
---|
100 | } |
---|
101 | |
---|
102 | return 0; |
---|
103 | } |
---|
104 | |
---|
105 | #endif |
---|
106 | |
---|
107 | //ll |
---|