1 | //clients.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 | struct roard_proto g_proto[MAX_PROTOS] = { |
---|
29 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD |
---|
30 | #ifdef ROAR_HAVE_H_ESD |
---|
31 | {ROAR_PROTO_ESOUND, NULL, emul_esd_check_client}, |
---|
32 | #endif |
---|
33 | #endif |
---|
34 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY |
---|
35 | {ROAR_PROTO_RPLAY, NULL, emul_rplay_check_client}, |
---|
36 | #endif |
---|
37 | {-1, NULL} |
---|
38 | }; |
---|
39 | |
---|
40 | #define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret) |
---|
41 | #define _CHECK_CID(id) _CHECK_CID_RET((id), -1) |
---|
42 | |
---|
43 | int clients_init (void) { |
---|
44 | int i; |
---|
45 | |
---|
46 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
47 | g_clients[i] = NULL; |
---|
48 | |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | int clients_free (void) { |
---|
53 | int i; |
---|
54 | |
---|
55 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
56 | if ( g_clients[i] != NULL ) |
---|
57 | clients_delete(i); |
---|
58 | |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | int clients_new (void) { |
---|
63 | int i; |
---|
64 | int s; |
---|
65 | struct roar_client_server * ns; |
---|
66 | struct roar_client * n; |
---|
67 | |
---|
68 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
69 | if ( g_clients[i] == NULL ) { |
---|
70 | ns = roar_mm_malloc(sizeof(struct roar_client_server)); |
---|
71 | n = ROAR_CLIENT(ns); |
---|
72 | |
---|
73 | memset(ns, 0, sizeof(struct roar_client_server)); |
---|
74 | |
---|
75 | if ( n != NULL ) { |
---|
76 | n->pid = -1; |
---|
77 | n->uid = -1; |
---|
78 | n->gid = -1; |
---|
79 | n->fh = -1; |
---|
80 | |
---|
81 | *n->name = 0; |
---|
82 | *n->host = 0; |
---|
83 | |
---|
84 | n->proto = ROAR_PROTO_ROARAUDIO; |
---|
85 | n->byteorder = ROAR_BYTEORDER_NETWORK; |
---|
86 | |
---|
87 | n->acl = NULL; |
---|
88 | |
---|
89 | n->execed = -1; |
---|
90 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
91 | n->streams[s] = -1; |
---|
92 | |
---|
93 | if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) { |
---|
94 | roar_mm_free(n); |
---|
95 | return -1; |
---|
96 | } |
---|
97 | |
---|
98 | ns->blockc = 0; |
---|
99 | ns->waits = NULL; |
---|
100 | ns->acclev = ACCLEV_NONE; |
---|
101 | |
---|
102 | g_clients[i] = ns; |
---|
103 | |
---|
104 | counters_inc(clients, 1); |
---|
105 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT); |
---|
106 | ROAR_DBG("clients_new(void) = %i", i); |
---|
107 | return i; |
---|
108 | } else { |
---|
109 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
110 | ROAR_ERR("clients_new(void) = -1"); |
---|
111 | return -1; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | return -1; |
---|
117 | } |
---|
118 | |
---|
119 | int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) { |
---|
120 | struct roar_client * c; |
---|
121 | int client; |
---|
122 | |
---|
123 | if ( fh == -1 ) |
---|
124 | return -1; |
---|
125 | |
---|
126 | if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK ) |
---|
127 | return -1; |
---|
128 | |
---|
129 | if ( (client = clients_new()) == -1 ) |
---|
130 | return -1; |
---|
131 | |
---|
132 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
133 | clients_delete(client); |
---|
134 | return -1; |
---|
135 | } |
---|
136 | |
---|
137 | if ( update_nnode ) { |
---|
138 | if ( clients_get(client, &c) != -1 ) { |
---|
139 | if ( roar_nnode_free(&(c->nnode)) != -1 ) { |
---|
140 | roar_nnode_new_from_fh(&(c->nnode), fh, 1); |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | int clients_delete (int id) { |
---|
149 | struct roar_client_server * cs; |
---|
150 | int i; |
---|
151 | int close_client_fh = 1; |
---|
152 | |
---|
153 | ROAR_DBG("clients_delete(id=%i) = ?", id); |
---|
154 | |
---|
155 | _CHECK_CID(id); |
---|
156 | |
---|
157 | cs = g_clients[id]; |
---|
158 | |
---|
159 | if ( cs->waits != NULL ) { |
---|
160 | for (i = 0; cs->waits[i] != NULL; i++) |
---|
161 | roar_notify_core_unsubscribe(NULL, cs->waits[i]); |
---|
162 | |
---|
163 | roar_mm_free(cs->waits); |
---|
164 | cs->waits = NULL; |
---|
165 | } |
---|
166 | |
---|
167 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT); |
---|
168 | |
---|
169 | counters_inc(clients, -1); |
---|
170 | |
---|
171 | if (ROAR_CLIENT(cs)->execed != -1) { |
---|
172 | // return streams_delete(g_clients[id]->execed); |
---|
173 | ROAR_CLIENT(cs)->execed = -1; |
---|
174 | close_client_fh = 0; |
---|
175 | } |
---|
176 | |
---|
177 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
178 | streams_delete(ROAR_CLIENT(cs)->streams[i]); |
---|
179 | } |
---|
180 | |
---|
181 | if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh ) |
---|
182 | close(ROAR_CLIENT(cs)->fh); |
---|
183 | |
---|
184 | roar_nnode_free(&(ROAR_CLIENT(cs)->nnode)); |
---|
185 | |
---|
186 | roar_mm_free(cs); |
---|
187 | g_clients[id] = NULL; |
---|
188 | |
---|
189 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | int clients_close (int id, int nocheck_exec) { |
---|
194 | struct roar_client * c; |
---|
195 | |
---|
196 | ROAR_DBG("clients_close(id=%i) = ?", id); |
---|
197 | |
---|
198 | _CHECK_CID(id); |
---|
199 | |
---|
200 | c = ROAR_CLIENT(g_clients[id]); |
---|
201 | |
---|
202 | if ( c->fh == -1 ) { |
---|
203 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | if (nocheck_exec || c->execed != -1) { |
---|
208 | close(c->fh); |
---|
209 | c->fh = -1; |
---|
210 | } |
---|
211 | |
---|
212 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
213 | return 0; |
---|
214 | } |
---|
215 | |
---|
216 | int clients_get (int id, struct roar_client ** client) { |
---|
217 | _CHECK_CID(id); |
---|
218 | |
---|
219 | *client = ROAR_CLIENT(g_clients[id]); |
---|
220 | |
---|
221 | if ( *client == NULL ) |
---|
222 | return -1; |
---|
223 | |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | int clients_get_server (int id, struct roar_client_server ** client) { |
---|
228 | _CHECK_CID(id); |
---|
229 | |
---|
230 | *client = g_clients[id]; |
---|
231 | |
---|
232 | if ( *client == NULL ) |
---|
233 | return -1; |
---|
234 | |
---|
235 | return 0; |
---|
236 | } |
---|
237 | |
---|
238 | int clients_set_fh (int id, int fh) { |
---|
239 | struct roar_client * c; |
---|
240 | #ifdef SO_PEERCRED |
---|
241 | struct ucred cred; |
---|
242 | socklen_t cred_len = sizeof(cred); |
---|
243 | #endif |
---|
244 | |
---|
245 | _CHECK_CID(id); |
---|
246 | |
---|
247 | if ( (c = ROAR_CLIENT(g_clients[id])) == NULL ) |
---|
248 | return -1; |
---|
249 | |
---|
250 | c->fh = fh; |
---|
251 | |
---|
252 | #ifdef SO_PEERCRED |
---|
253 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
254 | if ( cred.pid != 0 ) { |
---|
255 | c->pid = cred.pid; |
---|
256 | c->uid = cred.uid; |
---|
257 | c->gid = cred.gid; |
---|
258 | } |
---|
259 | } else { |
---|
260 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
261 | } |
---|
262 | #elif defined(ROAR_HAVE_GETPEEREID) |
---|
263 | if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) { |
---|
264 | ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno)); |
---|
265 | } |
---|
266 | #endif |
---|
267 | |
---|
268 | return 0; |
---|
269 | } |
---|
270 | |
---|
271 | int clients_get_fh (int id) { |
---|
272 | _CHECK_CID(id); |
---|
273 | |
---|
274 | return ROAR_CLIENT(g_clients[id])->fh; |
---|
275 | } |
---|
276 | |
---|
277 | int clients_set_pid (int id, int pid) { |
---|
278 | _CHECK_CID(id); |
---|
279 | |
---|
280 | ROAR_CLIENT(g_clients[id])->pid = pid; |
---|
281 | |
---|
282 | return 0; |
---|
283 | } |
---|
284 | |
---|
285 | int clients_set_uid (int id, int uid) { |
---|
286 | _CHECK_CID(id); |
---|
287 | |
---|
288 | ROAR_CLIENT(g_clients[id])->uid = uid; |
---|
289 | |
---|
290 | return 0; |
---|
291 | } |
---|
292 | |
---|
293 | int clients_set_gid (int id, int gid) { |
---|
294 | _CHECK_CID(id); |
---|
295 | |
---|
296 | ROAR_CLIENT(g_clients[id])->gid = gid; |
---|
297 | |
---|
298 | return 0; |
---|
299 | } |
---|
300 | |
---|
301 | int clients_set_proto (int id, int proto) { |
---|
302 | int byteorder = ROAR_BYTEORDER_UNKNOWN; |
---|
303 | |
---|
304 | _CHECK_CID(id); |
---|
305 | |
---|
306 | switch (proto) { |
---|
307 | case ROAR_PROTO_ROARAUDIO: |
---|
308 | case ROAR_PROTO_ESOUND: |
---|
309 | case ROAR_PROTO_RPLAY: |
---|
310 | case ROAR_PROTO_SIMPLE: |
---|
311 | byteorder = ROAR_BYTEORDER_NETWORK; |
---|
312 | break; |
---|
313 | } |
---|
314 | |
---|
315 | ROAR_CLIENT(g_clients[id])->proto = proto; |
---|
316 | ROAR_CLIENT(g_clients[id])->byteorder = byteorder; |
---|
317 | |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|
321 | int clients_block (int id, int unblock) { |
---|
322 | _CHECK_CID(id); |
---|
323 | |
---|
324 | if ( unblock ) { |
---|
325 | g_clients[id]->blockc--; |
---|
326 | } else { |
---|
327 | g_clients[id]->blockc++; |
---|
328 | } |
---|
329 | |
---|
330 | return 0; |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | #define MAX_STREAMLESS 8 |
---|
335 | |
---|
336 | int clients_check_all (void) { |
---|
337 | #ifdef ROAR_HAVE_SELECT |
---|
338 | struct roar_client * c; |
---|
339 | struct timeval tv; |
---|
340 | fd_set r, e; |
---|
341 | int i, j; |
---|
342 | int ret; |
---|
343 | int fh; |
---|
344 | int max_fh = -1; |
---|
345 | int have = 0; |
---|
346 | struct { |
---|
347 | int id; |
---|
348 | int fh; |
---|
349 | } streamless[MAX_STREAMLESS]; |
---|
350 | int have_streamless = 0; |
---|
351 | int have_stream; |
---|
352 | |
---|
353 | FD_ZERO(&r); |
---|
354 | FD_ZERO(&e); |
---|
355 | |
---|
356 | tv.tv_sec = 0; |
---|
357 | tv.tv_usec = 1; |
---|
358 | |
---|
359 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
360 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
361 | continue; |
---|
362 | |
---|
363 | if ( (fh = c->fh) != -1 ) { |
---|
364 | have++; |
---|
365 | |
---|
366 | ROAR_DBG("clients_check_all(*): fh=%i", fh); |
---|
367 | |
---|
368 | FD_SET(fh, &r); |
---|
369 | FD_SET(fh, &e); |
---|
370 | |
---|
371 | if ( fh > max_fh ) |
---|
372 | max_fh = fh; |
---|
373 | } |
---|
374 | |
---|
375 | have_stream = 0; |
---|
376 | |
---|
377 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
378 | if ( (fh = streams_get_fh(c->streams[j])) != -1 ) { |
---|
379 | ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, c->streams[j], fh); |
---|
380 | if ( fh > -1 ) { |
---|
381 | FD_SET(fh, &r); |
---|
382 | |
---|
383 | if ( fh > max_fh ) |
---|
384 | max_fh = fh; |
---|
385 | } else if ( fh == -2 ) { |
---|
386 | streams_check(c->streams[j]); |
---|
387 | } |
---|
388 | |
---|
389 | have_stream = 1; |
---|
390 | } |
---|
391 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
392 | } |
---|
393 | |
---|
394 | if ( !have_stream && have_streamless < MAX_STREAMLESS ) { |
---|
395 | streamless[have_streamless ].id = i; |
---|
396 | if ( (streamless[have_streamless++].fh = c->fh) == -1 ) |
---|
397 | have_streamless--; |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | if ( max_fh == -1 ) |
---|
402 | return 0; |
---|
403 | |
---|
404 | if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) { |
---|
405 | return ret < 0 ? ret : have; |
---|
406 | } |
---|
407 | |
---|
408 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
409 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
410 | continue; |
---|
411 | |
---|
412 | if ( (fh = c->fh) != -1 ) { |
---|
413 | if ( FD_ISSET(fh, &r) ) { |
---|
414 | if ( c->execed == -1 ) { |
---|
415 | clients_check(i); |
---|
416 | if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) { |
---|
417 | FD_CLR(fh, &r); |
---|
418 | } |
---|
419 | /* |
---|
420 | } else { |
---|
421 | streams_check(g_clients[i]->execed); |
---|
422 | */ |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | if ( FD_ISSET(fh, &e) ) { |
---|
427 | clients_delete(i); |
---|
428 | continue; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
433 | continue; |
---|
434 | |
---|
435 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
436 | ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]); |
---|
437 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
438 | break; |
---|
439 | |
---|
440 | //ROAR_WARN("clients_check_all(*): client=%i: client exists", i); |
---|
441 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]); |
---|
442 | |
---|
443 | if ( (fh = streams_get_fh(c->streams[j])) != -1 ) { |
---|
444 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh); |
---|
445 | if ( fh > -1 && FD_ISSET(fh, &r) ) { |
---|
446 | streams_check(c->streams[j]); |
---|
447 | } |
---|
448 | } |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | if ( have_streamless ) { |
---|
453 | FD_ZERO(&r); |
---|
454 | |
---|
455 | tv.tv_sec = 0; |
---|
456 | tv.tv_usec = 1; |
---|
457 | |
---|
458 | max_fh = -1; |
---|
459 | |
---|
460 | for (i = 0; i < have_streamless; i++) { |
---|
461 | if ( g_clients[j = streamless[i].id] == NULL ) |
---|
462 | continue; |
---|
463 | |
---|
464 | if ( ROAR_CLIENT(g_clients[j])->execed != -1 ) |
---|
465 | continue; |
---|
466 | |
---|
467 | fh = streamless[i].fh; |
---|
468 | |
---|
469 | ROAR_DBG("clients_check_all(void): fh=%i", fh); |
---|
470 | FD_SET(fh, &r); |
---|
471 | |
---|
472 | if ( fh > max_fh ) |
---|
473 | max_fh = fh; |
---|
474 | } |
---|
475 | |
---|
476 | if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) { |
---|
477 | return ret; |
---|
478 | } |
---|
479 | |
---|
480 | for (i = 0; i < have_streamless; i++) { |
---|
481 | if ( FD_ISSET(streamless[i].fh, &r) ) { |
---|
482 | clients_check(streamless[i].id); |
---|
483 | } |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
488 | return have; |
---|
489 | #else |
---|
490 | return -1; |
---|
491 | #endif |
---|
492 | } |
---|
493 | |
---|
494 | int clients_check (int id) { |
---|
495 | struct roar_client * c; |
---|
496 | struct roar_message m; |
---|
497 | struct roar_connection con; |
---|
498 | char * data = NULL; |
---|
499 | int oldcmd; |
---|
500 | int r; |
---|
501 | int rv = 0; |
---|
502 | uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE}; |
---|
503 | uint32_t event; |
---|
504 | size_t i; |
---|
505 | |
---|
506 | _CHECK_CID(id); |
---|
507 | |
---|
508 | c = ROAR_CLIENT(g_clients[id]); |
---|
509 | |
---|
510 | if ( c->fh == -1 ) |
---|
511 | return -1; |
---|
512 | |
---|
513 | roar_connect_fh(&con, c->fh); |
---|
514 | |
---|
515 | switch (c->proto) { |
---|
516 | case ROAR_PROTO_ROARAUDIO: |
---|
517 | r = roar_recv_message(&con, &m, &data); |
---|
518 | |
---|
519 | if ( r == -1 ) { // should we drop the client? |
---|
520 | clients_delete(id); |
---|
521 | return -1; |
---|
522 | } |
---|
523 | |
---|
524 | event = ROAR_NOTIFY_CMD2EVENT(m.cmd); |
---|
525 | |
---|
526 | roar_debug_message_print(&m); |
---|
527 | |
---|
528 | oldcmd = m.cmd; |
---|
529 | |
---|
530 | if ( (r = command_exec(id, &m, &data, flags)) == -1 ) { |
---|
531 | m.cmd = ROAR_CMD_ERROR; |
---|
532 | m.datalen = 0; |
---|
533 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
534 | } else { |
---|
535 | if ( m.cmd == oldcmd ) { |
---|
536 | m.cmd = ROAR_CMD_OK; |
---|
537 | m.datalen = 0; |
---|
538 | } else if ( m.cmd == ROAR_CMD_OK_STOP ) { |
---|
539 | m.cmd = ROAR_CMD_OK; |
---|
540 | rv = 1; |
---|
541 | } |
---|
542 | } |
---|
543 | ROAR_DBG("clients_check(*): data=%p", data); |
---|
544 | |
---|
545 | if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) { |
---|
546 | roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0); |
---|
547 | } else { |
---|
548 | roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0); |
---|
549 | roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL); |
---|
550 | } |
---|
551 | |
---|
552 | if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON ) |
---|
553 | clients_close(id, 1); |
---|
554 | |
---|
555 | break; |
---|
556 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
557 | case ROAR_PROTO_RSOUND: |
---|
558 | rv = emul_rsound_check_client(id, NULL); |
---|
559 | if ( rv == 0 ) { // loop as long as we don't get an error. |
---|
560 | while (rv == 0) |
---|
561 | rv = emul_rsound_check_client(id, NULL); |
---|
562 | rv = 0; // restore |
---|
563 | } else { // in case of error delete the client |
---|
564 | if ( |
---|
565 | #ifdef EAGAIN |
---|
566 | errno != EAGAIN && |
---|
567 | #endif |
---|
568 | #ifdef EWOULDBLOCK |
---|
569 | errno != EWOULDBLOCK && |
---|
570 | #endif |
---|
571 | #ifdef EINTR |
---|
572 | errno != EINTR && |
---|
573 | #endif |
---|
574 | 1 ) { |
---|
575 | rv = clients_delete(id); |
---|
576 | } else { |
---|
577 | rv = 0; |
---|
578 | } |
---|
579 | } |
---|
580 | break; |
---|
581 | #endif |
---|
582 | default: |
---|
583 | rv = -1; |
---|
584 | for (i = 0; g_proto[i].proto != -1; i++) { |
---|
585 | if ( g_proto[i].proto == c->proto ) { |
---|
586 | rv = g_proto[i].check_client(id, NULL); |
---|
587 | } |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | if ( data != NULL ) |
---|
592 | free(data); |
---|
593 | |
---|
594 | ROAR_DBG("clients_check(id=%i) = %i", id, rv); |
---|
595 | return rv; |
---|
596 | } |
---|
597 | |
---|
598 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
599 | int i; |
---|
600 | // int fh; |
---|
601 | int j; |
---|
602 | int keep_going; |
---|
603 | |
---|
604 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
605 | if ( g_clients[i] == NULL ) |
---|
606 | continue; |
---|
607 | |
---|
608 | keep_going = 1; |
---|
609 | |
---|
610 | /* |
---|
611 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
612 | continue; |
---|
613 | */ |
---|
614 | |
---|
615 | ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed); |
---|
616 | |
---|
617 | /* |
---|
618 | if ( g_clients[i]->execed == -1 ) { |
---|
619 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
620 | */ |
---|
621 | for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
622 | //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
623 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j); |
---|
624 | if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) { |
---|
625 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]); |
---|
626 | streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]); |
---|
627 | |
---|
628 | // the client may be deleted here, check if it still exists: |
---|
629 | if ( g_clients[i] == NULL ) |
---|
630 | keep_going = 0; |
---|
631 | } |
---|
632 | } |
---|
633 | /* |
---|
634 | } else { |
---|
635 | // streams_check(g_clients[i]->execed); |
---|
636 | streams_send_mon(g_clients[i]->execed); |
---|
637 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
638 | // clients_delete(i); // delete client in case we could not write |
---|
639 | } |
---|
640 | */ |
---|
641 | } |
---|
642 | |
---|
643 | // TODO: FIXME: should this really be -1? |
---|
644 | return -1; |
---|
645 | } |
---|
646 | |
---|
647 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
648 | struct roar_client * c; |
---|
649 | int i; |
---|
650 | int fh; |
---|
651 | |
---|
652 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
653 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
654 | continue; |
---|
655 | |
---|
656 | if ( (fh = c->fh) == -1 ) |
---|
657 | continue; |
---|
658 | |
---|
659 | if ( c->execed == -1 ) { |
---|
660 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
661 | } else { |
---|
662 | // streams_check(g_clients[i]->execed); |
---|
663 | streams_send_filter(c->execed); |
---|
664 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
665 | // clients_delete(i); // delete client in case we could not write |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | return -1; |
---|
670 | } |
---|
671 | |
---|
672 | // proto support |
---|
673 | int clients_register_proto(struct roard_proto * proto) { |
---|
674 | const size_t len = sizeof(g_proto)/sizeof(*g_proto); |
---|
675 | size_t i; |
---|
676 | |
---|
677 | if ( proto == NULL ) |
---|
678 | return -1; |
---|
679 | |
---|
680 | for (i = 0; g_proto[i].proto != -1; i++); |
---|
681 | |
---|
682 | // i is now at pos of current EOS entry. |
---|
683 | |
---|
684 | // test if we have space for one more entry: |
---|
685 | if ( (i+1) >= len ) |
---|
686 | return -1; |
---|
687 | |
---|
688 | memcpy(&(g_proto[i]), proto, sizeof(*g_proto)); |
---|
689 | |
---|
690 | i++; |
---|
691 | |
---|
692 | memset(&(g_proto[i]), 0, sizeof(*g_proto)); |
---|
693 | g_proto[i].proto = -1; |
---|
694 | |
---|
695 | return 0; |
---|
696 | } |
---|
697 | |
---|
698 | |
---|
699 | int client_stream_exec (int client, int stream) { |
---|
700 | struct roar_client * c; |
---|
701 | int i; |
---|
702 | int fh; |
---|
703 | |
---|
704 | _CHECK_CID(client); |
---|
705 | |
---|
706 | #if 0 |
---|
707 | if ( g_clients[client] == NULL ) { |
---|
708 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream); |
---|
709 | return -1; |
---|
710 | } |
---|
711 | #endif |
---|
712 | |
---|
713 | c = ROAR_CLIENT(g_clients[client]); |
---|
714 | |
---|
715 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
716 | if ( c->streams[i] == stream ) { |
---|
717 | c->execed = stream; |
---|
718 | if ( streams_is_ready(stream) == 0 ) { |
---|
719 | streams_set_fh(stream, c->fh); |
---|
720 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
721 | } else { |
---|
722 | ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream); |
---|
723 | if ( (fh = c->fh) != -1 ) { |
---|
724 | close(fh); |
---|
725 | c->fh = -1; |
---|
726 | } |
---|
727 | } |
---|
728 | ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream); |
---|
729 | return 0; |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream); |
---|
734 | return -1; |
---|
735 | } |
---|
736 | |
---|
737 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
738 | int i; |
---|
739 | |
---|
740 | ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh); |
---|
741 | |
---|
742 | _CHECK_CID(client); |
---|
743 | |
---|
744 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
745 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) { |
---|
746 | ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i); |
---|
747 | return streams_set_fh(stream, fh); |
---|
748 | } |
---|
749 | } |
---|
750 | |
---|
751 | ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh); |
---|
752 | return -1; |
---|
753 | } |
---|
754 | |
---|
755 | int client_stream_add (int client, int stream) { |
---|
756 | int i; |
---|
757 | |
---|
758 | _CHECK_CID(client); |
---|
759 | |
---|
760 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
761 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) { |
---|
762 | ROAR_CLIENT(g_clients[client])->streams[i] = stream; |
---|
763 | streams_set_client(stream, client); |
---|
764 | return 0; |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | return -1; |
---|
769 | } |
---|
770 | |
---|
771 | int client_stream_delete (int client, int stream) { |
---|
772 | int i; |
---|
773 | |
---|
774 | _CHECK_CID(client); |
---|
775 | |
---|
776 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
777 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) { |
---|
778 | ROAR_CLIENT(g_clients[client])->streams[i] = -1; |
---|
779 | |
---|
780 | if ( stream == ROAR_CLIENT(g_clients[client])->execed ) { |
---|
781 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
782 | clients_delete(client); |
---|
783 | } |
---|
784 | |
---|
785 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
786 | return 0; |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
791 | return -1; |
---|
792 | } |
---|
793 | |
---|
794 | int client_stream_move (int client, int stream) { |
---|
795 | int old_client = streams_get_client(stream); |
---|
796 | |
---|
797 | ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client); |
---|
798 | |
---|
799 | if ( old_client != -1 ) |
---|
800 | if ( client_stream_delete(old_client, stream) == -1 ) |
---|
801 | return -1; |
---|
802 | |
---|
803 | return client_stream_add(client, stream); |
---|
804 | } |
---|
805 | |
---|
806 | |
---|
807 | // notify thingys |
---|
808 | int clients_wait (int client, struct roar_event * events, size_t num) { |
---|
809 | struct roar_client_server * cs; |
---|
810 | size_t i, c; |
---|
811 | |
---|
812 | ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num); |
---|
813 | |
---|
814 | _CHECK_CID(client); |
---|
815 | |
---|
816 | cs = g_clients[client]; |
---|
817 | |
---|
818 | if ( cs->waits != NULL ) |
---|
819 | return -1; |
---|
820 | |
---|
821 | cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *)); |
---|
822 | |
---|
823 | if ( cs->waits == NULL ) |
---|
824 | return -1; |
---|
825 | |
---|
826 | if ( clients_block(client, 0) != 0 ) |
---|
827 | return -1; |
---|
828 | |
---|
829 | for (i = 0; i < num; i++) { |
---|
830 | #if defined(DEBUG) && 0 |
---|
831 | dbg_notify_cb(NULL, &(events[i]), cs); |
---|
832 | #endif |
---|
833 | cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs); |
---|
834 | if ( cs->waits[i] == NULL ) { |
---|
835 | for (c = 0; c < i; c++) |
---|
836 | roar_notify_core_unsubscribe(NULL, cs->waits[c]); |
---|
837 | roar_mm_free(cs->waits); |
---|
838 | cs->waits = NULL; |
---|
839 | clients_block(client, 1); |
---|
840 | return -1; |
---|
841 | } |
---|
842 | } |
---|
843 | |
---|
844 | cs->waits[num] = NULL; |
---|
845 | |
---|
846 | ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num); |
---|
847 | return 0; |
---|
848 | } |
---|
849 | |
---|
850 | void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) { |
---|
851 | struct roar_client_server * cs = userdata; |
---|
852 | struct roar_message m; |
---|
853 | struct roar_connection con; |
---|
854 | uint16_t * u16 = (uint16_t *) m.data; |
---|
855 | size_t tmp; |
---|
856 | size_t i; |
---|
857 | |
---|
858 | ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata); |
---|
859 | |
---|
860 | for (i = 0; cs->waits[i] != NULL; i++) |
---|
861 | roar_notify_core_unsubscribe(NULL, cs->waits[i]); |
---|
862 | |
---|
863 | roar_mm_free(cs->waits); |
---|
864 | cs->waits = NULL; |
---|
865 | |
---|
866 | // protocol depended handling... |
---|
867 | memset(&m, 0, sizeof(m)); |
---|
868 | m.cmd = ROAR_CMD_OK; |
---|
869 | u16[0] = ROAR_HOST2NET16(0); // Version |
---|
870 | u16[1] = ROAR_HOST2NET16(0); // flags |
---|
871 | |
---|
872 | tmp = sizeof(m.data) - 4; |
---|
873 | |
---|
874 | roar_event_to_blob(event, m.data + 4, &tmp); |
---|
875 | |
---|
876 | m.datalen = tmp + 4; |
---|
877 | |
---|
878 | roar_connect_fh(&con, ROAR_CLIENT(cs)->fh); |
---|
879 | roar_send_message(&con, &m, NULL); |
---|
880 | // ...end of protocol depended handling. |
---|
881 | |
---|
882 | // clients_block(, 1); |
---|
883 | // TODO: FIXME: bad hack... |
---|
884 | cs->blockc--; |
---|
885 | } |
---|
886 | |
---|
887 | |
---|
888 | // acclev: |
---|
889 | static struct { |
---|
890 | const enum roard_client_acclev acclev; |
---|
891 | const char * name; |
---|
892 | } _g_acclevs[] = { |
---|
893 | {ACCLEV_NONE, "none" }, |
---|
894 | {ACCLEV_IDENTED, "idented"}, |
---|
895 | {ACCLEV_CONCTL, "conctl" }, |
---|
896 | {ACCLEV_GUEST, "guest" }, |
---|
897 | {ACCLEV_USER, "user" }, |
---|
898 | {ACCLEV_PWRUSER, "pwruser"}, |
---|
899 | {ACCLEV_ALL, "all" }, |
---|
900 | {-1, NULL} |
---|
901 | }; |
---|
902 | |
---|
903 | enum roard_client_acclev clients_str2acclev(const char * acclev) { |
---|
904 | int i; |
---|
905 | |
---|
906 | for (i = 0; _g_acclevs[i].name != NULL; i++) |
---|
907 | if ( !strcasecmp(_g_acclevs[i].name, acclev) ) |
---|
908 | return _g_acclevs[i].acclev; |
---|
909 | |
---|
910 | return -1; |
---|
911 | } |
---|
912 | |
---|
913 | const char * clients_acclev2str(const enum roard_client_acclev acclev) { |
---|
914 | int i; |
---|
915 | |
---|
916 | for (i = 0; _g_acclevs[i].name != NULL; i++) |
---|
917 | if ( _g_acclevs[i].acclev == acclev ) |
---|
918 | return _g_acclevs[i].name; |
---|
919 | |
---|
920 | return NULL; |
---|
921 | } |
---|
922 | |
---|
923 | //ll |
---|