source: roaraudio/roard/clients.c @ 5012:b263759832f1

Last change on this file since 5012:b263759832f1 was 4947:b4a9a8ce2235, checked in by phi, 13 years ago

only do try to get peer creds if socket is != -1

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