1 | //network.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
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 | #ifdef _CAN_OPERATE |
---|
40 | int r; |
---|
41 | fd_set sl; |
---|
42 | struct timeval tv; |
---|
43 | int i; |
---|
44 | int max_fh = -1; |
---|
45 | |
---|
46 | FD_ZERO(&sl); |
---|
47 | |
---|
48 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
49 | if ( g_listen[i].socket != -1 ) { |
---|
50 | if ( g_listen[i].socket > max_fh ) |
---|
51 | max_fh = g_listen[i].socket; |
---|
52 | |
---|
53 | FD_SET(g_listen[i].socket, &sl); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | if ( max_fh == -1 ) |
---|
58 | return 0; |
---|
59 | |
---|
60 | tv.tv_sec = 0; |
---|
61 | tv.tv_usec = 1; |
---|
62 | |
---|
63 | if ((r = select(max_fh + 1, &sl, NULL, NULL, &tv)) > 0) { |
---|
64 | ROAR_DBG("net_check_listen(void): We have a connection!"); |
---|
65 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
66 | if ( g_listen[i].socket != -1 ) { |
---|
67 | if ( FD_ISSET(g_listen[i].socket, &sl) ) { |
---|
68 | if ( net_get_new_client(&(g_listen[i])) == -1 ) |
---|
69 | return -1; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | return r; |
---|
76 | #else |
---|
77 | return -1; |
---|
78 | #endif |
---|
79 | } |
---|
80 | |
---|
81 | #ifdef _CAN_OPERATE |
---|
82 | int net_get_new_client (struct roard_listen * lsock) { |
---|
83 | int fh; |
---|
84 | int client; |
---|
85 | struct roar_client * c; |
---|
86 | struct roar_vio_calls vio; |
---|
87 | struct sockaddr_storage addr; |
---|
88 | socklen_t addrlen = sizeof(addr); |
---|
89 | |
---|
90 | fh = accept(lsock->socket, (struct sockaddr*)&addr, &addrlen); |
---|
91 | |
---|
92 | ROAR_DBG("net_get_new_client(void): fh = %i", fh); |
---|
93 | |
---|
94 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
95 | if ( lsock->proto == ROAR_PROTO_RSOUND ) { |
---|
96 | client = emul_rsound_on_connect(fh, lsock); |
---|
97 | switch (client) { |
---|
98 | case -1: return -1; break; |
---|
99 | case -2: return 0; break; |
---|
100 | default: // TODO: write error handling |
---|
101 | clients_get(client, &c); |
---|
102 | fh = c->fh; |
---|
103 | break; |
---|
104 | } |
---|
105 | } else { |
---|
106 | #endif |
---|
107 | |
---|
108 | client = clients_new(); |
---|
109 | |
---|
110 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
111 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
112 | |
---|
113 | clients_delete(client); |
---|
114 | close(fh); |
---|
115 | |
---|
116 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
117 | return -1; |
---|
118 | } |
---|
119 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
120 | } |
---|
121 | #endif |
---|
122 | |
---|
123 | if ( clients_get(client, &c) != -1 ) { |
---|
124 | if ( roar_nnode_free(&(c->nnode)) == -1 ) |
---|
125 | return -1; |
---|
126 | |
---|
127 | if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 ) |
---|
128 | return -1; |
---|
129 | } |
---|
130 | |
---|
131 | ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto); |
---|
132 | |
---|
133 | if ( clients_set_proto(client, lsock->proto) == -1 ) { |
---|
134 | ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client); |
---|
135 | return -1; |
---|
136 | } |
---|
137 | |
---|
138 | switch (lsock->proto) { |
---|
139 | case ROAR_PROTO_ROARAUDIO: |
---|
140 | // nothing needed to be done here |
---|
141 | break; |
---|
142 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD |
---|
143 | #ifdef ROAR_HAVE_H_ESD |
---|
144 | case ROAR_PROTO_ESOUND: |
---|
145 | ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command"); |
---|
146 | |
---|
147 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) |
---|
148 | return -1; |
---|
149 | |
---|
150 | ROAR_DBG("net_get_new_client(*): creating VIO OK"); |
---|
151 | |
---|
152 | if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 ) |
---|
153 | return -1; |
---|
154 | |
---|
155 | ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully"); |
---|
156 | break; |
---|
157 | #endif |
---|
158 | #endif |
---|
159 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
160 | case ROAR_PROTO_SIMPLE: |
---|
161 | if ( emul_simple_on_connect(client, lsock) == -1 ) |
---|
162 | return -1; |
---|
163 | break; |
---|
164 | #endif |
---|
165 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
166 | case ROAR_PROTO_RSOUND: // nothing to do here. |
---|
167 | break; |
---|
168 | #endif |
---|
169 | default: |
---|
170 | // OS independiend code to close the socket: |
---|
171 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) |
---|
172 | return -1; |
---|
173 | roar_vio_close(&vio); |
---|
174 | return -1; |
---|
175 | break; |
---|
176 | } |
---|
177 | |
---|
178 | // close(fh); |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | #endif |
---|
183 | |
---|
184 | #endif |
---|
185 | |
---|
186 | //ll |
---|