source: roaraudio/roard/clients.c @ 3713:38a2d99a3bed

Last change on this file since 3713:38a2d99a3bed was 3713:38a2d99a3bed, checked in by phi, 14 years ago

moved socket cred things out of the network.c into the clients.c/client_set_fh()

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