source: roaraudio/roard/clients.c @ 3926:7b30ce201137

Last change on this file since 3926:7b30ce201137 was 3926:7b30ce201137, checked in by phi, 14 years ago

changed a lot prototyoes

File size: 14.3 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_get       (int id, struct roar_client ** client) {
156 _CHECK_CID(id);
157
158 *client = g_clients[id];
159
160 if ( *client == NULL )
161  return -1;
162
163 return 0;
164}
165
166int clients_set_fh    (int id, int    fh) {
167 struct roar_client * c;
168#ifdef SO_PEERCRED
169 struct ucred cred;
170 socklen_t cred_len = sizeof(cred);
171#endif
172
173 _CHECK_CID(id);
174
175 if ( (c = g_clients[id]) == NULL )
176  return -1;
177
178 c->fh = fh;
179
180#ifdef SO_PEERCRED
181 if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
182  if ( cred.pid != 0 ) {
183   c->pid = cred.pid;
184   c->uid = cred.uid;
185   c->gid = cred.gid;
186  }
187 } else {
188  ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
189 }
190#elif defined(ROAR_HAVE_GETPEEREID)
191 if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
192  ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
193 }
194#endif
195
196 return 0;
197}
198
199int clients_get_fh    (int id) {
200 _CHECK_CID(id);
201
202 return g_clients[id]->fh;
203}
204
205int clients_set_pid   (int id, int    pid) {
206 _CHECK_CID(id);
207
208 g_clients[id]->pid = pid;
209
210 return 0;
211}
212
213int clients_set_uid   (int id, int    uid) {
214 _CHECK_CID(id);
215
216 g_clients[id]->uid = uid;
217
218 return 0;
219}
220
221int clients_set_gid   (int id, int    gid) {
222 _CHECK_CID(id);
223
224 g_clients[id]->gid = gid;
225
226 return 0;
227}
228
229int clients_set_proto (int id, int    proto) {
230 int byteorder = ROAR_BYTEORDER_UNKNOWN;
231
232 _CHECK_CID(id);
233
234 switch (proto) {
235  case ROAR_PROTO_ROARAUDIO:
236  case ROAR_PROTO_ESOUND:
237  case ROAR_PROTO_SIMPLE:
238    byteorder = ROAR_BYTEORDER_NETWORK;
239   break;
240 }
241
242 g_clients[id]->proto     = proto;
243 g_clients[id]->byteorder = byteorder;
244
245 return 0;
246}
247
248#define MAX_STREAMLESS 8
249
250int clients_check_all (void) {
251#ifdef ROAR_HAVE_SELECT
252 struct timeval tv;
253 fd_set r, e;
254 int i, j;
255 int ret;
256 int fh;
257 int max_fh = -1;
258 int have = 0;
259 struct {
260  int id;
261  int fh;
262 } streamless[MAX_STREAMLESS];
263 int have_streamless = 0;
264 int have_stream;
265
266 FD_ZERO(&r);
267 FD_ZERO(&e);
268
269 tv.tv_sec  = 0;
270 tv.tv_usec = 1;
271
272 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
273  if ( g_clients[i] == NULL )
274   continue;
275
276  if ( (fh = g_clients[i]->fh) != -1 ) {
277   have++;
278
279   ROAR_DBG("clients_check_all(*): fh=%i", fh);
280
281   FD_SET(fh, &r);
282   FD_SET(fh, &e);
283
284   if ( fh > max_fh )
285    max_fh = fh;
286  }
287
288  have_stream = 0;
289
290  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
291   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
292    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
293    if ( fh > -1 ) {
294     FD_SET(fh, &r);
295
296     if ( fh > max_fh )
297      max_fh = fh;
298    } else if ( fh == -2 ) {
299     streams_check(g_clients[i]->streams[j]);
300    }
301
302    have_stream = 1;
303   }
304   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
305  }
306
307  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
308   streamless[have_streamless  ].id = i;
309   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
310    have_streamless--;
311  }
312 }
313
314 if ( max_fh == -1 )
315  return 0;
316
317 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
318  return ret < 0 ? ret : have;
319 }
320
321 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
322  if ( g_clients[i] == NULL )
323   continue;
324
325  if ( (fh = g_clients[i]->fh) != -1 ) {
326   if ( FD_ISSET(fh, &r) ) {
327    if ( g_clients[i]->execed == -1 ) {
328     clients_check(i);
329     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
330      FD_CLR(fh, &r);
331     }
332/*
333    } else {
334     streams_check(g_clients[i]->execed);
335*/
336    }
337   }
338
339   if ( FD_ISSET(fh, &e) ) {
340    clients_delete(i);
341    continue;
342   }
343  }
344
345  if ( g_clients[i] == NULL )
346   continue;
347
348  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
349   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
350   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
351    break;
352
353   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
354   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
355
356   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
357    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
358    if ( fh > -1 && FD_ISSET(fh, &r) ) {
359     streams_check(g_clients[i]->streams[j]);
360    }
361   }
362  }
363 }
364
365 if ( have_streamless ) {
366   FD_ZERO(&r);
367
368   tv.tv_sec  = 0;
369   tv.tv_usec = 1;
370
371   max_fh = -1;
372
373   for (i = 0; i < have_streamless; i++) {
374    if ( ! g_clients[j = streamless[i].id] )
375     continue;
376
377    if ( g_clients[j]->execed != -1 )
378     continue;
379
380    fh = streamless[i].fh;
381
382    ROAR_DBG("clients_check_all(void): fh=%i", fh);
383    FD_SET(fh, &r);
384
385    if ( fh > max_fh )
386     max_fh = fh;
387   }
388
389   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
390    return ret;
391   }
392
393   for (i = 0; i < have_streamless; i++) {
394    if ( FD_ISSET(streamless[i].fh, &r) ) {
395     clients_check(streamless[i].id);
396    }
397   }
398 }
399
400 ROAR_DBG("clients_check_all(void) = %i // have value", have);
401 return have;
402#else
403 return -1;
404#endif
405}
406
407int clients_check     (int id) {
408 struct roar_message    m;
409 struct roar_connection con;
410 char * data = NULL;
411 int oldcmd;
412 int r;
413 int rv = 0;
414 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
415
416 _CHECK_CID(id);
417
418 if ( g_clients[id]->fh == -1 )
419  return -1;
420
421 roar_connect_fh(&con, g_clients[id]->fh);
422
423 switch (g_clients[id]->proto) {
424  case ROAR_PROTO_ROARAUDIO:
425    r = roar_recv_message(&con, &m, &data);
426
427    if ( r == -1 ) { // should we drop the client?
428     clients_delete(id);
429     return -1;
430    }
431
432    roar_debug_message_print(&m);
433
434    oldcmd = m.cmd;
435
436    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
437     m.cmd     = ROAR_CMD_ERROR;
438     m.datalen = 0;
439     ROAR_DBG("clients_check(*): Exec of command faild!");
440    } else {
441     if ( m.cmd == oldcmd ) {
442      m.cmd     = ROAR_CMD_OK;
443      m.datalen = 0;
444     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
445      m.cmd     = ROAR_CMD_OK;
446      rv        = 1;
447     }
448    }
449
450    roar_send_message(&con, &m, NULL);
451   break;
452#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
453#ifdef ROAR_HAVE_H_ESD
454  case ROAR_PROTO_ESOUND:
455    rv = emul_esd_check_client(id, NULL);
456   break;
457#endif
458#endif
459#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
460  case ROAR_PROTO_RSOUND:
461    rv = emul_rsound_check_client(id, NULL);
462    if ( rv == 0 ) { // loop as long as we don't get an error.
463     while (rv == 0)
464      rv = emul_rsound_check_client(id, NULL);
465     rv = 0; // restore
466    } else { // in case of error delete the client
467     rv = clients_delete(id);
468    }
469   break;
470#endif
471  default:
472    rv = -1;
473 }
474
475 if ( data )
476  free(data);
477
478 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
479 return rv;
480}
481
482int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
483 int i;
484// int fh;
485 int j;
486 int keep_going;
487
488 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
489  if ( g_clients[i] == NULL )
490   continue;
491
492  keep_going = 1;
493
494/*
495  if ( (fh = g_clients[i]->fh) == -1 )
496   continue;
497*/
498
499  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
500
501/*
502  if ( g_clients[i]->execed == -1 ) {
503   // TODO: add some code to send a message to the client insetd of the raw data.
504*/
505   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
506    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
507    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
508    if ( g_clients[i]->streams[j] != -1 ) {
509     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
510     streams_send_mon(g_clients[i]->streams[j]);
511
512     // the client may be deleted here, check if it still exists:
513     if ( g_clients[i] == NULL )
514      keep_going = 0;
515    }
516   }
517/*
518  } else {
519//   streams_check(g_clients[i]->execed);
520   streams_send_mon(g_clients[i]->execed);
521//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
522//    clients_delete(i); // delete client in case we could not write
523  }
524*/
525 }
526
527 // TODO: FIXME: should this really be -1?
528 return -1;
529}
530
531int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
532 int i;
533 int fh;
534
535 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
536  if ( g_clients[i] == NULL )
537   continue;
538
539  if ( (fh = g_clients[i]->fh) == -1 )
540   continue;
541
542  if ( g_clients[i]->execed == -1 ) {
543   // TODO: add some code to send a message to the client insetd of the raw data.
544  } else {
545//   streams_check(g_clients[i]->execed);
546   streams_send_filter(g_clients[i]->execed);
547//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
548//    clients_delete(i); // delete client in case we could not write
549  }
550 }
551
552 return -1;
553}
554
555int client_stream_exec   (int client, int stream) {
556 int i;
557 int fh;
558
559 _CHECK_CID(client);
560
561#if 0
562 if ( g_clients[client] == NULL ) {
563  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
564  return -1;
565 }
566#endif
567
568 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
569  if ( g_clients[client]->streams[i] == stream ) {
570   g_clients[client]->execed = stream;
571   if ( streams_is_ready(stream) == 0 ) {
572    streams_set_fh(stream, g_clients[client]->fh);
573    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
574   } else {
575    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=%i", client, stream, fh);
576    if ( (fh = g_clients[client]->fh) != -1 ) {
577     close(fh);
578     g_clients[client]->fh = -1;
579    }
580   }
581   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
582   return 0;
583  }
584 }
585
586 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
587 return -1;
588}
589
590int client_stream_set_fh (int client, int stream, int fh) {
591 int i;
592
593 _CHECK_CID(client);
594
595 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
596  if ( g_clients[client]->streams[i] == stream ) {
597   streams_set_fh(stream, fh);
598   return 0;
599  }
600 }
601
602 return -1;
603}
604
605int client_stream_add    (int client, int stream) {
606 int i;
607
608 _CHECK_CID(client);
609
610 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
611  if ( g_clients[client]->streams[i] == -1 ) {
612   g_clients[client]->streams[i] = stream;
613   streams_set_client(stream, client);
614   return 0;
615  }
616 }
617
618 return -1;
619}
620
621int client_stream_delete (int client, int stream) {
622 int i;
623
624 _CHECK_CID(client);
625
626 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
627  if ( g_clients[client]->streams[i] == stream ) {
628   g_clients[client]->streams[i] = -1;
629
630   if ( stream == g_clients[client]->execed ) {
631    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
632    clients_delete(client);
633   }
634
635   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
636   return 0;
637  }
638 }
639
640 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
641 return -1;
642}
643
644int client_stream_move   (int client, int stream) {
645 int old_client = streams_get_client(stream);
646
647 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
648
649 if ( old_client != -1 )
650  if ( client_stream_delete(old_client, stream) == -1 )
651   return -1;
652
653 return client_stream_add(client, stream);
654}
655
656//ll
Note: See TracBrowser for help on using the repository browser.