source: roaraudio/roard/clients.c @ 4020:03b0a092d575

Last change on this file since 4020:03b0a092d575 was 4020:03b0a092d575, checked in by phi, 14 years ago

urgs, check for the wrong var

File size: 14.9 KB
Line 
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#define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret)
29#define _CHECK_CID(id)         _CHECK_CID_RET((id), -1)
30
31int clients_init (void) {
32 int i;
33
34 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
35  g_clients[i] = NULL;
36
37 return 0;
38}
39
40int clients_free (void) {
41 int i;
42
43 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
44  if ( g_clients[i] != NULL )
45   clients_delete(i);
46
47 return 0;
48}
49
50int clients_new (void) {
51 int i;
52 int s;
53 struct roar_client * n;
54
55 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
56  if ( g_clients[i] == NULL ) {
57   n = roar_mm_malloc(sizeof(struct roar_client));
58   if ( n != NULL ) {
59    n->pid    = -1;
60    n->uid    = -1;
61    n->gid    = -1;
62    n->fh     = -1;
63
64    *n->name = 0;
65    *n->host = 0;
66
67    n->proto     = ROAR_PROTO_ROARAUDIO;
68    n->byteorder = ROAR_BYTEORDER_NETWORK;
69
70    n->acl   = NULL;
71
72    n->execed = -1;
73    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
74     n->streams[s] = -1;
75
76    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
77     roar_mm_free(n);
78     return -1;
79    }
80
81    g_clients[i] = n;
82
83    ROAR_DBG("clients_new(void) = %i", i);
84    return i;
85   } else {
86    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
87    ROAR_ERR("clients_new(void) = -1");
88    return -1;
89   }
90  }
91 }
92
93 return -1;
94}
95
96int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) {
97 struct roar_client * c;
98 int client;
99
100 if ( fh == -1 )
101  return -1;
102
103 if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK )
104  return -1;
105
106 if ( (client = clients_new()) == -1 )
107  return -1;
108
109 if ( clients_set_fh(client, fh) == -1 ) {
110  clients_delete(client);
111  return -1;
112 }
113
114 if ( update_nnode ) {
115  if ( clients_get(client, &c) != -1 ) {
116   if ( roar_nnode_free(&(c->nnode)) != -1 ) {
117    roar_nnode_new_from_fh(&(c->nnode), fh, 1);
118   }
119  }
120 }
121
122 return 0;
123}
124
125int clients_delete (int id) {
126 int i;
127 int close_client_fh = 1;
128
129 ROAR_DBG("clients_delete(id=%i) = ?", id);
130
131 _CHECK_CID(id);
132
133 if (g_clients[id]->execed != -1) {
134//  return streams_delete(g_clients[id]->execed);
135  g_clients[id]->execed = -1;
136  close_client_fh = 0;
137 }
138
139 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
140  streams_delete(g_clients[id]->streams[i]);
141 }
142
143 if ( g_clients[id]->fh != -1 && close_client_fh )
144  close(g_clients[id]->fh);
145
146 roar_nnode_free(&(g_clients[id]->nnode));
147
148 roar_mm_free(g_clients[id]);
149 g_clients[id] = NULL;
150
151 ROAR_DBG("clients_delete(id=%i) = 0", id);
152 return 0;
153}
154
155int clients_close      (int id, int nocheck_exec) {
156 struct roar_client * c;
157
158 ROAR_DBG("clients_close(id=%i) = ?", id);
159
160 _CHECK_CID(id);
161
162 c = g_clients[id];
163
164 if ( c->fh == -1 ) {
165  ROAR_DBG("clients_delete(id=%i) = 0", id);
166  return 0;
167 }
168
169 if (nocheck_exec || g_clients[id]->execed != -1) {
170  close(c->fh);
171  c->fh = -1;
172 }
173
174 ROAR_DBG("clients_delete(id=%i) = 0", id);
175 return 0;
176}
177
178int clients_get       (int id, struct roar_client ** client) {
179 _CHECK_CID(id);
180
181 *client = g_clients[id];
182
183 if ( *client == NULL )
184  return -1;
185
186 return 0;
187}
188
189int clients_set_fh    (int id, int    fh) {
190 struct roar_client * c;
191#ifdef SO_PEERCRED
192 struct ucred cred;
193 socklen_t cred_len = sizeof(cred);
194#endif
195
196 _CHECK_CID(id);
197
198 if ( (c = g_clients[id]) == NULL )
199  return -1;
200
201 c->fh = fh;
202
203#ifdef SO_PEERCRED
204 if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
205  if ( cred.pid != 0 ) {
206   c->pid = cred.pid;
207   c->uid = cred.uid;
208   c->gid = cred.gid;
209  }
210 } else {
211  ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
212 }
213#elif defined(ROAR_HAVE_GETPEEREID)
214 if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
215  ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
216 }
217#endif
218
219 return 0;
220}
221
222int clients_get_fh    (int id) {
223 _CHECK_CID(id);
224
225 return g_clients[id]->fh;
226}
227
228int clients_set_pid   (int id, int    pid) {
229 _CHECK_CID(id);
230
231 g_clients[id]->pid = pid;
232
233 return 0;
234}
235
236int clients_set_uid   (int id, int    uid) {
237 _CHECK_CID(id);
238
239 g_clients[id]->uid = uid;
240
241 return 0;
242}
243
244int clients_set_gid   (int id, int    gid) {
245 _CHECK_CID(id);
246
247 g_clients[id]->gid = gid;
248
249 return 0;
250}
251
252int clients_set_proto (int id, int    proto) {
253 int byteorder = ROAR_BYTEORDER_UNKNOWN;
254
255 _CHECK_CID(id);
256
257 switch (proto) {
258  case ROAR_PROTO_ROARAUDIO:
259  case ROAR_PROTO_ESOUND:
260  case ROAR_PROTO_RPLAY:
261  case ROAR_PROTO_SIMPLE:
262    byteorder = ROAR_BYTEORDER_NETWORK;
263   break;
264 }
265
266 g_clients[id]->proto     = proto;
267 g_clients[id]->byteorder = byteorder;
268
269 return 0;
270}
271
272#define MAX_STREAMLESS 8
273
274int clients_check_all (void) {
275#ifdef ROAR_HAVE_SELECT
276 struct timeval tv;
277 fd_set r, e;
278 int i, j;
279 int ret;
280 int fh;
281 int max_fh = -1;
282 int have = 0;
283 struct {
284  int id;
285  int fh;
286 } streamless[MAX_STREAMLESS];
287 int have_streamless = 0;
288 int have_stream;
289
290 FD_ZERO(&r);
291 FD_ZERO(&e);
292
293 tv.tv_sec  = 0;
294 tv.tv_usec = 1;
295
296 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
297  if ( g_clients[i] == NULL )
298   continue;
299
300  if ( (fh = g_clients[i]->fh) != -1 ) {
301   have++;
302
303   ROAR_DBG("clients_check_all(*): fh=%i", fh);
304
305   FD_SET(fh, &r);
306   FD_SET(fh, &e);
307
308   if ( fh > max_fh )
309    max_fh = fh;
310  }
311
312  have_stream = 0;
313
314  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
315   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
316    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
317    if ( fh > -1 ) {
318     FD_SET(fh, &r);
319
320     if ( fh > max_fh )
321      max_fh = fh;
322    } else if ( fh == -2 ) {
323     streams_check(g_clients[i]->streams[j]);
324    }
325
326    have_stream = 1;
327   }
328   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
329  }
330
331  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
332   streamless[have_streamless  ].id = i;
333   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
334    have_streamless--;
335  }
336 }
337
338 if ( max_fh == -1 )
339  return 0;
340
341 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
342  return ret < 0 ? ret : have;
343 }
344
345 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
346  if ( g_clients[i] == NULL )
347   continue;
348
349  if ( (fh = g_clients[i]->fh) != -1 ) {
350   if ( FD_ISSET(fh, &r) ) {
351    if ( g_clients[i]->execed == -1 ) {
352     clients_check(i);
353     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
354      FD_CLR(fh, &r);
355     }
356/*
357    } else {
358     streams_check(g_clients[i]->execed);
359*/
360    }
361   }
362
363   if ( FD_ISSET(fh, &e) ) {
364    clients_delete(i);
365    continue;
366   }
367  }
368
369  if ( g_clients[i] == NULL )
370   continue;
371
372  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
373   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
374   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
375    break;
376
377   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
378   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
379
380   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
381    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
382    if ( fh > -1 && FD_ISSET(fh, &r) ) {
383     streams_check(g_clients[i]->streams[j]);
384    }
385   }
386  }
387 }
388
389 if ( have_streamless ) {
390   FD_ZERO(&r);
391
392   tv.tv_sec  = 0;
393   tv.tv_usec = 1;
394
395   max_fh = -1;
396
397   for (i = 0; i < have_streamless; i++) {
398    if ( ! g_clients[j = streamless[i].id] )
399     continue;
400
401    if ( g_clients[j]->execed != -1 )
402     continue;
403
404    fh = streamless[i].fh;
405
406    ROAR_DBG("clients_check_all(void): fh=%i", fh);
407    FD_SET(fh, &r);
408
409    if ( fh > max_fh )
410     max_fh = fh;
411   }
412
413   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
414    return ret;
415   }
416
417   for (i = 0; i < have_streamless; i++) {
418    if ( FD_ISSET(streamless[i].fh, &r) ) {
419     clients_check(streamless[i].id);
420    }
421   }
422 }
423
424 ROAR_DBG("clients_check_all(void) = %i // have value", have);
425 return have;
426#else
427 return -1;
428#endif
429}
430
431int clients_check     (int id) {
432 struct roar_message    m;
433 struct roar_connection con;
434 char * data = NULL;
435 int oldcmd;
436 int r;
437 int rv = 0;
438 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
439
440 _CHECK_CID(id);
441
442 if ( g_clients[id]->fh == -1 )
443  return -1;
444
445 roar_connect_fh(&con, g_clients[id]->fh);
446
447 switch (g_clients[id]->proto) {
448  case ROAR_PROTO_ROARAUDIO:
449    r = roar_recv_message(&con, &m, &data);
450
451    if ( r == -1 ) { // should we drop the client?
452     clients_delete(id);
453     return -1;
454    }
455
456    roar_debug_message_print(&m);
457
458    oldcmd = m.cmd;
459
460    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
461     m.cmd     = ROAR_CMD_ERROR;
462     m.datalen = 0;
463     ROAR_DBG("clients_check(*): Exec of command faild!");
464    } else {
465     if ( m.cmd == oldcmd ) {
466      m.cmd     = ROAR_CMD_OK;
467      m.datalen = 0;
468     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
469      m.cmd     = ROAR_CMD_OK;
470      rv        = 1;
471     }
472    }
473
474    roar_send_message(&con, &m, NULL);
475
476    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
477     clients_close(id, 1);
478
479   break;
480#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
481#ifdef ROAR_HAVE_H_ESD
482  case ROAR_PROTO_ESOUND:
483    rv = emul_esd_check_client(id, NULL);
484   break;
485#endif
486#endif
487#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
488  case ROAR_PROTO_RPLAY:
489    rv = emul_rplay_check_client(id, NULL);
490   break;
491#endif
492#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
493  case ROAR_PROTO_RSOUND:
494    rv = emul_rsound_check_client(id, NULL);
495    if ( rv == 0 ) { // loop as long as we don't get an error.
496     while (rv == 0)
497      rv = emul_rsound_check_client(id, NULL);
498     rv = 0; // restore
499    } else { // in case of error delete the client
500     rv = clients_delete(id);
501    }
502   break;
503#endif
504  default:
505    rv = -1;
506 }
507
508 if ( data )
509  free(data);
510
511 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
512 return rv;
513}
514
515int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
516 int i;
517// int fh;
518 int j;
519 int keep_going;
520
521 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
522  if ( g_clients[i] == NULL )
523   continue;
524
525  keep_going = 1;
526
527/*
528  if ( (fh = g_clients[i]->fh) == -1 )
529   continue;
530*/
531
532  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
533
534/*
535  if ( g_clients[i]->execed == -1 ) {
536   // TODO: add some code to send a message to the client insetd of the raw data.
537*/
538   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
539    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
540    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
541    if ( g_clients[i]->streams[j] != -1 ) {
542     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
543     streams_send_mon(g_clients[i]->streams[j]);
544
545     // the client may be deleted here, check if it still exists:
546     if ( g_clients[i] == NULL )
547      keep_going = 0;
548    }
549   }
550/*
551  } else {
552//   streams_check(g_clients[i]->execed);
553   streams_send_mon(g_clients[i]->execed);
554//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
555//    clients_delete(i); // delete client in case we could not write
556  }
557*/
558 }
559
560 // TODO: FIXME: should this really be -1?
561 return -1;
562}
563
564int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
565 int i;
566 int fh;
567
568 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
569  if ( g_clients[i] == NULL )
570   continue;
571
572  if ( (fh = g_clients[i]->fh) == -1 )
573   continue;
574
575  if ( g_clients[i]->execed == -1 ) {
576   // TODO: add some code to send a message to the client insetd of the raw data.
577  } else {
578//   streams_check(g_clients[i]->execed);
579   streams_send_filter(g_clients[i]->execed);
580//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
581//    clients_delete(i); // delete client in case we could not write
582  }
583 }
584
585 return -1;
586}
587
588int client_stream_exec   (int client, int stream) {
589 int i;
590 int fh;
591
592 _CHECK_CID(client);
593
594#if 0
595 if ( g_clients[client] == NULL ) {
596  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
597  return -1;
598 }
599#endif
600
601 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
602  if ( g_clients[client]->streams[i] == stream ) {
603   g_clients[client]->execed = stream;
604   if ( streams_is_ready(stream) == 0 ) {
605    streams_set_fh(stream, g_clients[client]->fh);
606    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
607   } else {
608    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=%i", client, stream, fh);
609    if ( (fh = g_clients[client]->fh) != -1 ) {
610     close(fh);
611     g_clients[client]->fh = -1;
612    }
613   }
614   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
615   return 0;
616  }
617 }
618
619 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
620 return -1;
621}
622
623int client_stream_set_fh (int client, int stream, int fh) {
624 int i;
625
626 _CHECK_CID(client);
627
628 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
629  if ( g_clients[client]->streams[i] == stream ) {
630   streams_set_fh(stream, fh);
631   return 0;
632  }
633 }
634
635 return -1;
636}
637
638int client_stream_add    (int client, int stream) {
639 int i;
640
641 _CHECK_CID(client);
642
643 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
644  if ( g_clients[client]->streams[i] == -1 ) {
645   g_clients[client]->streams[i] = stream;
646   streams_set_client(stream, client);
647   return 0;
648  }
649 }
650
651 return -1;
652}
653
654int client_stream_delete (int client, int stream) {
655 int i;
656
657 _CHECK_CID(client);
658
659 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
660  if ( g_clients[client]->streams[i] == stream ) {
661   g_clients[client]->streams[i] = -1;
662
663   if ( stream == g_clients[client]->execed ) {
664    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
665    clients_delete(client);
666   }
667
668   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
669   return 0;
670  }
671 }
672
673 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
674 return -1;
675}
676
677int client_stream_move   (int client, int stream) {
678 int old_client = streams_get_client(stream);
679
680 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
681
682 if ( old_client != -1 )
683  if ( client_stream_delete(old_client, stream) == -1 )
684   return -1;
685
686 return client_stream_add(client, stream);
687}
688
689//ll
Note: See TracBrowser for help on using the repository browser.