source: roaraudio/roard/req.c @ 6021:bce8f5f10f07

Last change on this file since 6021:bce8f5f10f07 was 6021:bce8f5f10f07, checked in by phi, 10 years ago

add (still useless) transport_protocol-variable in preparation for v1 packet

File size: 47.4 KB
Line 
1//req.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014
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/* ckport options:
27 * massage <-> object converters:
28 * ckport: ignore-symbol: roar_stream_s2m of target libroar0
29 * ckport: ignore-symbol: roar_stream_m2s of target libroar0
30 * ckport: ignore-symbol: roar_ctl_c2m of target libroar0
31 * ckport: ignore-symbol: roar_ctl_ia2m of target libroar0
32 * ckport: ignore-symbol: roar_ctl_m2f of target libroar0
33 * Caps support:
34 * ckport: ignore-symbol: roar_caps_from_msg of target libroar0
35 * ckport: ignore-symbol: roar_caps_to_msg of target libroar0
36 * Server info:
37 * ckport: ignore-symbol: roar_server_info_to_mes of target libroar0
38 * PASSFH:
39 * ckport: ignore-symbol: roar_socket_recv_fh of target libroar0
40 * AUTH:
41 * ckport: ignore-symbol: roar_auth_from_mes of target libroar0
42 * ckport: ignore-symbol: roar_auth_to_mes of target libroar0
43 * Events:
44 * ckport: ignore-symbol: roar_event_from_blob of target libroar0
45 * Filter:
46 * ckport: ignore-symbol: roar_filter_match of target libroar0
47 */
48
49#include "roard.h"
50
51// include for uname() used by req_on_server_info()
52#ifdef ROAR_HAVE_UNAME
53#include <sys/utsname.h>
54#endif
55
56static void * _dataspace(struct roar_message * mes, char ** data, uint32_t flags[2], size_t len) {
57 if ( len <= LIBROAR_BUFFER_MSGDATA )
58  return mes->data;
59
60 if ( *data != NULL )
61  roar_mm_free(*data);
62
63 *data = roar_mm_malloc(len);
64
65 ROAR_DBG("_dataspace(mes=%p, data=%p, flags=%p, len=%llu): *data=%p", mes, data, flags, (long long unsigned int)len, *data);
66
67 if ( *data == NULL )
68  return NULL;
69
70 flags[1] |= COMMAND_FLAG_OUT_LONGDATA;
71
72 return *data;
73}
74
75int req_on_noop        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
76 (void)client, (void)data, (void)flags;
77 mes->cmd     = ROAR_CMD_OK;
78 mes->pos     = g_pos;
79 mes->datalen = 0;
80 return 0;
81}
82
83int req_on_quit        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
84 (void)client, (void)data;
85 mes->cmd     = ROAR_CMD_OK;
86 mes->pos     = g_pos;
87 mes->datalen = 0;
88 flags[1] |= COMMAND_FLAG_OUT_DELETE;
89 return 0;
90}
91
92int req_on_identify    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
93 struct roar_client_server * cs;
94 struct roar_client        * c;
95 int max_len;
96
97 (void)data, (void)flags;
98
99 if ( mes->datalen < 1 )
100  return -1;
101
102 clients_get_server(client, &cs);
103
104 c = ROAR_CLIENT(cs);
105
106 if ( mes->data[0] == 1 ) {
107  if ( c->pid == -1 ) {
108   c->pid       = ROAR_NET2HOST32(*(uint32_t*)((mes->data)+1));
109   ROAR_DBG("req_on_identify(): new PID: c->pid = %i", c->pid);
110  }
111
112  ROAR_DBG("req_on_identify(): final PID: c->pid = %i", c->pid);
113
114  max_len = (mes->datalen - 5) < (ROAR_BUFFER_NAME-1) ? (mes->datalen - 5) : (ROAR_BUFFER_NAME-1);
115
116  strncpy(c->name, mes->data + 5, max_len);
117  c->name[max_len] = 0;
118
119  // we set the acclevel to IDENTED here.
120  // if it is alreay > IDENTED we reset it anyway
121  // as potential auth relevent data changed.
122  cs->acclev = ACCLEV_IDENTED;
123
124  mes->cmd     = ROAR_CMD_OK;
125  mes->pos     = g_pos;
126  mes->datalen = 0;
127
128  ROAR_DBG("req_on_identify(*): client=%i, pid=%i", client, c->pid);
129  ROAR_DBG("req_on_identify(*) = 0");
130  return 0;
131 }
132
133 return -1;
134}
135
136int req_on_auth        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
137 struct roar_error_frame   error_frame;
138 void                    * error_data;
139 struct roar_message       error_mes;
140 struct roar_client_server * cs;
141 struct roar_auth_message    authmes;
142 int next = -1;
143 int ret;
144
145 (void)flags;
146
147 clients_get_server(client, &cs);
148
149 // TODO: add code to support some auth.
150
151 if ( roar_auth_from_mes(&authmes, mes, *data) == -1 )
152  return -1;
153
154 ROAR_INFO("req_on_auth(client=%i,...): authtype=%s(%i), len=%llu bytes", ROAR_DBG_INFO_VERBOSE,
155                           client, roar_autht2str(authmes.type), authmes.type, (long long unsigned int)authmes.len);
156
157 ROAR_DBG("req_on_auth(client=%i,...): CALL auth_client_ckeck(cs=%p, &authmes=%p, &next=%p)", client, cs, &authmes, &next);
158 ret = auth_client_ckeck(cs, &authmes, &next);
159 ROAR_DBG("req_on_auth(client=%i,...): RET %i // next=%s(%i)", client, ret, roar_autht2str(next), next);
160
161 if ( ret != 1 ) {
162  ROAR_DBG("req_on_auth(client=%i,...) = ?", client);
163
164  if ( next != -1 ) {
165   ROAR_DBG("req_on_auth(client=%i,...) = ?", client);
166   memset(&authmes, 0, sizeof(authmes));
167
168   authmes.type = next;
169
170   if ( roar_auth_to_mes(&error_mes, NULL, &authmes) == -1 )
171    return -1;
172
173   ROAR_DBG("req_on_auth(client=%i,...) = ?", client);
174
175   if ( roar_err_init(&error_frame) == -1 )
176    return -1;
177
178   ROAR_DBG("req_on_auth(client=%i,...) = ?", client);
179
180   error_frame.ra_errno = ROAR_ERROR_PERM;
181   error_frame.datalen  = error_mes.datalen;
182
183   error_data   = roar_err_buildmsg(mes, NULL, &error_frame);
184   ROAR_DBG("req_on_auth(client=%i,...): error_data=%p", client, error_data);
185
186   memcpy(error_data, error_mes.data, error_mes.datalen);
187
188   mes->cmd     = ROAR_CMD_ERROR;
189   mes->pos     = g_pos;
190
191   ROAR_DBG("req_on_auth(client=%i,...): mes=%p{.datalen=%i}", client, mes, (int)mes->datalen);
192   ROAR_DBG("req_on_auth(client=%i,...) = 0 // cmd=ERROR", client);
193   return 0;
194  }
195
196  ROAR_DBG("req_on_auth(client=%i,...) = 0", client);
197  return -1;
198 }
199
200 mes->cmd     = ROAR_CMD_OK;
201 mes->pos     = g_pos;
202 mes->datalen = 0;
203
204 ROAR_DBG("req_on_auth(client=%i,...) = 0", client);
205 return 0;
206}
207
208
209int req_on_whoami      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
210 (void)data, (void)flags;
211
212 mes->cmd     = ROAR_CMD_OK;
213 mes->pos     = g_pos;
214 mes->datalen = 1;
215 mes->data[0] = client;
216 return 0;
217}
218
219
220int req_on_new_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
221 int stream;
222 struct roar_stream_server * ss;
223 struct roar_stream        * s;
224 struct roar_stream * source_stream;
225 struct roar_audio_info * info;
226 struct roar_audio_info * source_info;
227
228 ROAR_DBG("req_on_new_stream(client=%i, ...): creating stream...", client);
229 if ((stream = streams_new()) == -1 )
230  return -1;
231
232 ROAR_DBG("req_on_new_stream(client=%i, ...): stream=%i", client, stream);
233
234 ROAR_DBG("req_on_new_stream(client=%i, ...): getting stream...", client);
235 if ( streams_get(stream, &ss) == -1 ) {
236  streams_delete(stream);
237  return -1;
238 }
239
240 s = ROAR_STREAM(ss);
241
242 ROAR_DBG("req_on_new_stream(client=%i, ...): set client of stream...", client);
243 if ( client_stream_add(client, stream) == -1 ) {
244  streams_delete(stream);
245  return -1;
246 }
247
248 ROAR_DBG("req_on_new_stream(client=%i, ...): loading stream from message...", client);
249 if ( roar_stream_m2s(s, mes) == -1 ) {
250  streams_delete(stream);
251  return -1;
252 }
253
254 ROAR_DBG("req_on_new_stream(client=%i, ...): setting id and codec of stream...", client);
255 ROAR_STREAM(s)->id = stream; // roar_stream_m2s() resets this
256 ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM(s)->info.codec;
257
258 ROAR_DBG("req_on_new_stream(client=%i, ...): setting direction stream...", client);
259 // int streams_set_dir    (int id, int dir, int defaults)
260 if ( streams_set_dir(stream, ROAR_STREAM(s)->dir, 1) == -1 ) {
261  streams_delete(stream);
262  return -1;
263 }
264
265 ROAR_DBG("req_on_new_stream(client=%i, ...): checking mixer settings...", client);
266 if ( mes->stream != -1 && mes->stream != ss->mixer_stream ) {
267  streams_delete(stream);
268  return -1;
269 }
270
271 ROAR_DBG("req_on_new_stream(client=%i, ...): setting up direction specific stream settings...", client);
272 switch (ROAR_STREAM(s)->dir) {
273  case ROAR_DIR_LIGHT_IN:
274  case ROAR_DIR_LIGHT_OUT:
275#ifndef ROAR_WITHOUT_DCOMP_LIGHT
276    info = &(ROAR_STREAM(s)->info);
277
278    info->channels = 0;
279    info->bits     = 0;
280    info->rate     = 0;
281#else
282    streams_delete(stream);
283    return -1;
284#endif
285
286   break;
287  case ROAR_DIR_MIDI_IN:
288  case ROAR_DIR_MIDI_OUT:
289#ifndef ROAR_WITHOUT_DCOMP_MIDI
290    info = &(ROAR_STREAM(s)->info);
291
292    info->channels = ROAR_MIDI_CHANNELS_DEFAULT;
293    info->bits     = ROAR_MIDI_BITS;
294    info->rate     = 0;
295#else
296    streams_delete(stream);
297    return -1;
298#endif
299
300   break;
301
302  case ROAR_DIR_RAW_IN:
303#ifndef ROAR_WITHOUT_DCOMP_RAW
304    if ( ROAR_STREAM(s)->pos_rel_id == -1     ||
305         ROAR_STREAM(s)->pos_rel_id == stream ||
306         streams_get_dir(ROAR_STREAM(s)->pos_rel_id) != ROAR_DIR_RAW_OUT
307       ) {
308     ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting
309                                      // in case rel_id == stream
310     streams_delete(stream);
311     return -1;
312    }
313#else
314  case ROAR_DIR_RAW_OUT:
315    streams_delete(stream);
316    return -1;
317#endif
318
319   break;
320  case ROAR_DIR_THRU:
321
322    if ( ROAR_STREAM(s)->pos_rel_id == -1 || ROAR_STREAM(s)->pos_rel_id == stream ) {
323     ROAR_STREAM(s)->pos_rel_id = -1; // force this here as it will try to delete itself while deleting
324                                      // in case rel_id == stream
325     streams_delete(stream);
326     return -1;
327    }
328
329    if ( streams_get_clientobj(ROAR_STREAM(s)->pos_rel_id, &source_stream) == -1 ) {
330     streams_delete(stream);
331     return -1;
332    }
333
334    info        = &(ROAR_STREAM(s)->info);
335    source_info = &(ROAR_STREAM(source_stream)->info);
336
337    info->channels = source_info->channels;
338    info->bits     = source_info->bits;
339    info->rate     = source_info->rate;
340    info->codec    = source_info->codec;
341    ROAR_STREAM_SERVER(s)->codec_orgi = ROAR_STREAM_SERVER(source_stream)->codec_orgi;
342
343   break;
344  case ROAR_DIR_FILTER:
345    info        = &(ROAR_STREAM(s)->info);
346
347    if ( ROAR_STREAM(s)->pos_rel_id == -1 ) {
348     source_info = g_sa;
349    } else {
350     if ( streams_get_clientobj(ROAR_STREAM(s)->pos_rel_id, &source_stream) == -1 ) {
351      streams_delete(stream);
352      return -1;
353     }
354     source_info = &(ROAR_STREAM(source_stream)->info);
355    }
356
357    if ( info->channels != source_info->channels || info->bits != source_info->bits ||
358         info->codec    != source_info->codec    || info->rate != source_info->rate ) {
359     // the stream parameters don't match the one of the stream being filtered.
360     // -> delete and reject the stream.
361     streams_delete(stream);
362     return -1;
363    }
364   break;
365 }
366
367 ROAR_DBG("req_on_new_stream(client=%i, ...): returning (OK, stream=%i)...", client, stream);
368
369 mes->cmd     = ROAR_CMD_OK;
370 mes->stream  = stream;
371 mes->datalen = 0;
372
373 return 0;
374}
375
376int req_on_exec_stream (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
377 struct roar_message    m;
378 struct roar_client   * c;
379 struct roar_connection con;
380 int16_t * d = (int16_t*)mes->data;
381 int sock;
382 int i;
383 int r;
384
385 ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): execing stream", client, mes->stream);
386
387
388 if ( mes->stream != -1 ) {
389  if ( streams_is_ready(mes->stream) ) {
390   flags[1] |= COMMAND_FLAG_OUT_CLOSECON;
391  } else {
392   if ( (r = client_stream_exec(client, mes->stream)) == -1 )
393    return -1;
394  }
395
396  ROAR_DBG("req_on_exec_stream(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
397  mes->cmd     = ROAR_CMD_OK;
398  mes->datalen = 0;
399  return 0;
400 }
401
402 ROAR_DBG("req_on_exec_stream(client=%i,...): non stream exec", client);
403
404 if ( mes->datalen < 4*2 )
405  return -1;
406
407 ROAR_DBG("req_on_exec_stream(client=%i,...) = ?", client);
408
409 for (i = 0; i < 4; i++) {
410  d[i] = ROAR_NET2HOST16(d[i]);
411 }
412
413 if ( d[0] != 0 ) // version
414  return -1;
415
416 if ( d[1] != 0 ) // flags
417  return -1;
418
419 if ( clients_get_protohandle(d[2]) == NULL )
420  return -1;
421
422 ROAR_DBG("req_on_exec_stream(client=%i,...) = ?", client);
423
424 memset(&m, 0, sizeof(m));
425
426 m.cmd = ROAR_CMD_OK;
427
428 flags[1] |= COMMAND_FLAG_OUT_NOSEND;
429
430 c = ROAR_CLIENT(g_clients[client]);
431
432 sock = c->fh;
433
434 if ( sock == -1 )
435  return -1;
436
437 ROAR_DBG("req_on_exec_stream(client=%i,...) = ?", client);
438
439 roar_connect_fh(&con, sock);
440
441 roar_send_message(&con, &m, NULL);
442
443 clients_set_fh(client, -1);
444 clients_delete(client);
445
446 ROAR_DBG("req_on_exec_stream(client=%i,...): calling clients_new_from_fh(): sock=%i, d={?, ?, %i, %i}", client, sock, (int)d[2], (int)d[3]);
447 if ( clients_new_from_fh(sock, d[2], d[3], 1) == -1 ) {
448  close(sock);
449 }
450
451 ROAR_DBG("req_on_exec_stream(client=%i,...) = 0", client);
452
453 return 0;
454}
455
456int req_on_con_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
457 char   host[80] = {0};
458 int    port = 0;
459 int    type = ROAR_SOCKET_TYPE_NONE;
460 int    transport_protocol = ROAR_PROTO_NONE;
461 int    fh;
462 int    len;
463
464 if ( mes->datalen < 4 ) {
465  roar_err_set(ROAR_ERROR_BADMSG);
466  return -1;
467 }
468
469 if ( mes->datalen > 80 ) { // we do not support long messages here
470  roar_err_set(ROAR_ERROR_MSGSIZE);
471  return -1;
472 }
473
474 switch (*(mes->data)) {
475  case 0: // version 0 packet:
476    type = (unsigned)mes->data[1];
477    port = ROAR_NET2HOST16(((uint16_t*)mes->data)[1]);
478
479    len = mes->datalen - 4;
480
481    strncpy(host, &(mes->data[4]), len);
482    host[len] = 0;
483   break;
484  default:
485    roar_err_set(ROAR_ERROR_NSVERSION);
486    return -1;
487   break;
488 }
489
490 if ( type > ROAR_SOCKET_TYPE_MAX ) {
491  roar_err_set(ROAR_ERROR_NOENT);
492  return -1;
493 }
494
495 switch (type) {
496  case ROAR_SOCKET_TYPE_NONE:
497  case ROAR_SOCKET_TYPE_FORK:
498  case ROAR_SOCKET_TYPE_GENSTR:
499    roar_err_set(ROAR_ERROR_INVAL);
500    return -1;
501   break;
502  case ROAR_SOCKET_TYPE_FILE:
503    roar_err_set(ROAR_ERROR_PERM);
504    return -1;
505   break;
506  default:
507    // pass.
508   break;
509 }
510
511 switch (transport_protocol) {
512  case ROAR_PROTO_NONE:
513  case ROAR_PROTO_SIMPLE: // same a NONE, plain data over socket. diffrence is only in setup.
514    transport_protocol = ROAR_PROTO_NONE;
515   break;
516  default:
517    roar_err_set(ROAR_ERROR_NOSYS);
518    return -1;
519   break;
520 }
521
522 ROAR_DBG("req_on_con_stream(*): CONNECT(type=%i, host='%s', port=%i)", type, host, port);
523
524 roar_libroar_nowarn();
525 fh = roar_socket_open(ROAR_SOCKET_MODE_CONNECT, type, host, port);
526 roar_libroar_warn();
527
528 if ( fh == -1 )
529  return -1;
530
531 if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
532  close(fh);
533  return 1;
534 }
535
536 mes->datalen = 0;
537 mes->cmd     = ROAR_CMD_OK;
538
539 return 0;
540}
541
542int req_on_passfh      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
543 int sock = clients_get_fh(client);
544 int16_t * d = (int16_t*)mes->data;
545 struct roard_listen * lsock;
546 int listening;
547 int fh;
548 int i;
549
550 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...) = ?", client, mes->stream);
551
552 if ( (fh = roar_socket_recv_fh(sock, NULL, NULL)) == -1 ) {
553  ROAR_WARN("req_on_passfh(client=%i, mes={stream=%i,...},...): was unabled to get filehandle from remote end. bad.", client, mes->stream);
554  ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (ERROR)...", client, mes->stream);
555  return -1;
556 }
557
558 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): fh=%i", client, mes->stream, fh);
559
560 if ( mes->stream != -1 ) { // stream pass:
561  ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): This is a stream passfh", client, mes->stream);
562
563  if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
564   close(fh);
565   ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (ERROR)...", client, mes->stream);
566   return -1;
567  }
568
569  ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
570
571  mes->datalen = 0;
572  mes->cmd     = ROAR_CMD_OK;
573
574  return 0;
575 }
576
577// non-stream pass:
578
579 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): This is a client passfh", client, mes->stream);
580
581/*
582 0: Version,   16
583 1: Flags,     16
584 2: Protocol,  16
585 3: Byteorder, 16
586 Options...
587*/
588
589 if ( mes->datalen < 4*2 )
590  return -1;
591
592 for (i = 0; i < 4; i++) {
593  d[i] = ROAR_NET2HOST16(d[i]);
594 }
595
596 if ( d[0] != 0 ) // version
597  return -1;
598
599 listening = d[1] & ROAR_CLIENTPASS_FLAG_LISTEN;
600
601 if ( listening )
602  d[1] -= ROAR_CLIENTPASS_FLAG_LISTEN;
603
604 if ( d[1] != 0 ) // flags
605  return -1;
606
607 if ( listening ) {
608  if ( get_listen(&lsock, NULL) == -1 ) {
609   close(fh);
610   return -1;
611  }
612
613  roar_vio_open_fh_socket(&(lsock->sock), fh);
614  lsock->used   = 1;
615  lsock->proto  = d[2];
616 } else {
617  if ( clients_new_from_fh(fh, d[2], d[3], 1) == -1 )
618   return -1;
619 }
620
621 ROAR_DBG("req_on_passfh(client=%i, mes={stream=%i,...},...): returning (OK)...", client, mes->stream);
622
623 mes->datalen = 0;
624 mes->cmd     = ROAR_CMD_OK;
625
626 return 0;
627}
628
629#ifdef ROAR_SUPPORT_META
630int req_on_set_meta    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
631 int type;
632 int mode;
633 int namelen, vallen;
634 char   val[255+1];
635 char   name[ROAR_META_MAX_NAMELEN+1];
636
637 if ( mes->datalen < 3 )
638  return -1;
639
640 if ( mes->data[0] != 0 ) // version
641  return -1;
642
643 mode = (unsigned) mes->data[1];
644 type = (unsigned) mes->data[2];
645
646 ROAR_DBG("req_on_set_meta(*): mode=%i, type=%i", mode, type);
647
648 if ( mode == ROAR_META_MODE_CLEAR ) {
649  stream_meta_clear(mes->stream);
650  mes->datalen = 0;
651  mes->cmd     = ROAR_CMD_OK;
652  return 0;
653 } else if ( mode == ROAR_META_MODE_DELETE ) { // unsuppoerted at the moment
654  return -1;
655 } else if ( mode == ROAR_META_MODE_FINALIZE ) {
656  stream_meta_finalize(mes->stream);
657  mes->datalen = 0;
658  mes->cmd     = ROAR_CMD_OK;
659  return 0;
660 } else if ( mode == ROAR_META_MODE_SET || mode == ROAR_META_MODE_ADD ) {
661  if ( mes->datalen < 5 )
662   return -1;
663
664  namelen = (unsigned) mes->data[3];
665  vallen  = (unsigned) mes->data[4];
666
667  ROAR_DBG("req_on_set_meta(*): namelen=%i, vallen=%i", namelen, vallen);
668
669  if ( mes->datalen < (5 + namelen + vallen) )
670   return -1;
671
672  if ( namelen > ROAR_META_MAX_NAMELEN )
673   return -1;
674
675  strncpy(name, &(mes->data[5]), namelen);
676  name[namelen] = 0;
677
678  if ( vallen > 255 )
679   return -1;
680
681  strncpy(val, &(mes->data[5+namelen]), vallen);
682  val[vallen] = 0;
683
684  if ( mode == ROAR_META_MODE_SET ) {
685   if ( stream_meta_set(mes->stream, type, name, val) == -1 )
686    return -1;
687  } else {
688   if ( stream_meta_add(mes->stream, type, name, val) == -1 )
689    return -1;
690  }
691
692  mes->datalen = 0;
693  mes->cmd     = ROAR_CMD_OK;
694  return 0;
695 } else { // unknown mode!
696  return -1;
697 }
698
699 return -1;
700}
701
702int req_on_get_meta    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
703 int vallen;
704 int type;
705 char val[LIBROAR_BUFFER_MSGDATA-1];
706
707 if ( mes->datalen != 2 )
708  return -1;
709
710 if ( mes->data[0] != 0 ) // version
711  return -1;
712
713 type = (unsigned) mes->data[1];
714
715 if ( stream_meta_get(mes->stream, type, NULL, val, LIBROAR_BUFFER_MSGDATA-2) == -1 )
716  return -1;
717
718 vallen = strlen(val);
719
720 mes->cmd     = ROAR_CMD_OK;
721 mes->datalen = 2 + vallen;
722
723 mes->data[0] = 0;
724 mes->data[1] = (unsigned char) vallen;
725
726 val[vallen] = 0;
727
728 strncpy(&(mes->data[2]), val, vallen+1);
729
730 return 0;
731}
732
733int req_on_list_meta   (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
734 int i;
735 int len = 0;
736 int types[ROAR_META_MAX_PER_STREAM];
737
738 if ( mes->datalen != 1 )
739  return -1;
740
741 if ( mes->data[0] != 0 ) // version
742  return -1;
743
744 if ( (len = stream_meta_list(mes->stream, types, ROAR_META_MAX_PER_STREAM)) == -1 )
745  return -1;
746
747 mes->cmd     = ROAR_CMD_OK;
748 mes->datalen = 1 + len;
749 mes->data[0] = 0;
750
751 for (i = 0; i < len; i++)
752  mes->data[i+1] = types[i];
753
754 return 0;
755}
756#endif
757
758int req_on_gettimeofday (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
759 struct roar_time curtime;
760
761 ROAR_DBG("req_on_gettimeofday(client=%i, mes=%p, data=%p, flags=%p) = ?", client, mes, data, flags);
762
763 if ( *data != NULL ) {
764  return -1;
765 }
766
767 ROAR_DBG("req_on_gettimeofday(client=%i, mes=%p, data=%p, flags=%p) = ?", client, mes, data, flags);
768
769 if ( mes->datalen && mes->datalen < 8 ) {
770  return -1;
771 }
772
773 ROAR_DBG("req_on_gettimeofday(client=%i, mes=%p, data=%p, flags=%p) = ?", client, mes, data, flags);
774
775 if ( mes->datalen >= 8 ) {
776  if ( *(uint64_t *)mes->data != 0 ) {
777   return -1;
778  }
779 }
780
781 ROAR_DBG("req_on_gettimeofday(client=%i, mes=%p, data=%p, flags=%p) = ?", client, mes, data, flags);
782
783 if ( roar_clock_gettime(&curtime, ROAR_CLOCK_REALTIME) == -1 )
784  return -1;
785
786 if ( roar_time_to_msg(mes, &curtime) == -1 )
787  return -1;
788
789 mes->cmd = ROAR_CMD_OK;
790
791 ROAR_DBG("req_on_gettimeofday(client=%i, mes=%p, data=%p, flags=%p) = ?", client, mes, data, flags);
792
793 return 0;
794}
795
796int req_on_server_info (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
797#ifdef ROAR_HAVE_UNAME
798 struct utsname utsname;
799#endif
800#ifdef ROAR_HAVE_GETVERSIONEX
801 OSVERSIONINFO osinfo;
802 char buf_release[80];
803#endif
804 struct roar_server_info info;
805 uint16_t * d16;
806#ifdef ROAR_HAVE_GETHOSTID
807 long hostid;
808 char hostidbuf[64];
809#endif
810
811 if ( mes->datalen != 4 )
812  return -1;
813
814 d16 = (uint16_t*)mes->data;
815
816 // check version.
817 if ( ROAR_NET2HOST16(d16[0]) != 0 )
818  return -1;
819
820 switch (ROAR_NET2HOST16(d16[1])) {
821  case ROAR_IT_SERVER:
822   memset(&info, 0, sizeof(info));
823
824   // the following if is optimized out by compiler.
825   if ( ROAR_VERSION_DISTRIBUTION_STRING[0] == 0 ) {
826    info.version = "roard/" ROAR_VERSION_STRING " <" ROAR_DEV_VENDOR_STRING ">";
827   } else {
828    info.version = "roard/" ROAR_VERSION_STRING " <" ROAR_DEV_VENDOR_STRING "> (" ROAR_VERSION_DISTRIBUTION_STRING ")";
829   }
830
831   info.build   = ROAR_BUILD_STAMP;
832   info.license = ROAR_LICENSE_GPLv3_0;
833
834#ifdef ROAR_HAVE_GETHOSTID
835   hostid = gethostid();
836   snprintf(hostidbuf, sizeof(hostidbuf), sizeof(long) == 8 ? "0x%.16lx" : "0x%.8lx", hostid);
837   info.hostid = hostidbuf;
838#endif
839
840   if ( !!strcmp(g_config->location, CONF_DEF_STRING) )
841    info.location = g_config->location;
842
843   if ( !!strcmp(g_config->description, CONF_DEF_STRING) )
844    info.description = g_config->description;
845
846   info.contact = g_config->contact;
847   info.serial  = g_config->serial;
848   info.uiurl   = g_config->uiurl;
849
850#ifdef ROAR_HAVE_UNAME
851   if ( uname(&utsname) == 0 ) {
852    roar_random_salt_nonce(&utsname, sizeof(utsname));
853    info.un.sysname  = utsname.sysname;
854    info.un.release  = utsname.release;
855    info.un.nodename = utsname.nodename;
856    info.un.machine  = utsname.machine;
857   }
858#endif
859#ifdef ROAR_HAVE_GETVERSIONEX
860   osinfo.dwOSVersionInfoSize = sizeof(osinfo);
861   if ( GetVersionEx(&osinfo) ) {
862    info.un.sysname = "Windows";
863    snprintf(buf_release, sizeof(buf_release), "%i.%i.%i",
864               (int)osinfo.dwMajorVersion, (int)osinfo.dwMinorVersion, (int)osinfo.dwBuildNumber);
865    buf_release[sizeof(buf_release)-1] = 0;
866    info.un.release = buf_release;
867   }
868#endif
869
870   *data = NULL;
871
872   if ( roar_server_info_to_mes(mes, &info, (void**)data) == -1 )
873    return -1;
874
875   if ( *data != NULL )
876    flags[1] |= COMMAND_FLAG_OUT_LONGDATA;
877  break;
878  default: /* unknown request */
879    return -1;
880   break;
881 }
882
883 mes->cmd = ROAR_CMD_OK;
884
885 return 0;
886}
887
888int req_on_server_oinfo    (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
889 struct roar_stream s;
890 int dir = ROAR_DIR_PLAY;
891 int subsys;
892//ROAR_DIR_OUTPUT
893
894 if ( mes->datalen != 0 ) {
895  if ( mes->datalen != 2 )
896   return -1;
897
898  if ( mes->data[0] != 0 )
899   return -1;
900
901  dir = mes->data[1];
902 }
903
904 subsys = streams_dir2subsys(dir);
905
906 memset(&s, 0, sizeof(struct roar_stream));
907
908 s.dir           = ROAR_DIR_MIXING;
909 s.pos_rel_id    = -1;
910
911 switch (subsys) {
912  case ROAR_SUBSYS_WAVEFORM:
913    s.info.rate     = g_sa->rate;
914    s.info.bits     = g_sa->bits;
915    s.info.channels = g_sa->channels;
916    s.info.codec    = g_sa->codec;
917    s.pos           = g_pos;
918   break;
919#ifndef ROAR_WITHOUT_DCOMP_MIDI
920  case ROAR_SUBSYS_MIDI:
921   break;
922#endif
923#ifndef ROAR_WITHOUT_DCOMP_LIGHT
924  case ROAR_SUBSYS_LIGHT:
925    s.info.rate     = ROAR_OUTPUT_CFREQ;
926    s.info.bits     = ROAR_LIGHT_BITS;
927    s.info.channels = g_light_state.channels;
928   break;
929#endif
930#ifndef ROAR_WITHOUT_DCOMP_RAW
931  case ROAR_SUBSYS_RAW:
932    // no need to set anything here.
933   break;
934#endif
935#ifndef ROAR_WITHOUT_DCOMP_RDTCS
936  case ROAR_SUBSYS_RDTCS:
937    s.info.rate     = ROAR_OUTPUT_CFREQ;
938    s.info.bits     = ROAR_RDTCS_BITS;
939    s.info.channels = ROAR_RDTCS_CHANNELS;
940    s.info.codec    = ROAR_RDTCS_CODEC;
941   break;
942#endif
943  default:
944    return -1;
945   break;
946 }
947
948 if ( roar_stream_s2m(&s, mes) == -1 )
949  return -1;
950
951 mes->cmd = ROAR_CMD_OK;
952
953 return 0;
954}
955
956int req_on_caps        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
957 struct roar_caps caps;
958 struct roar_stds * stds;
959 size_t i;
960
961 if ( roar_caps_from_msg(&caps, mes, *data) == -1 )
962  return -1;
963
964 // handle the data from the caps command here...
965
966 if ( !(caps.flags & ROAR_CF_REQUEST) ) {
967  mes->cmd = ROAR_CMD_OK;
968  mes->datalen = 0;
969  return 0;
970 }
971
972 mes->datalen = 0;
973
974 switch (caps.type) {
975  case ROAR_CT_STANDARDS:
976    if ( (stds = roar_stds_new(g_caps_stds.stds_len)) == NULL )
977     return -1;
978
979    for ( i = 0; i < stds->stds_len; i++) {
980     stds->stds[i] = ROAR_HOST2NET32(g_caps_stds.stds[i]);
981    }
982
983    caps.data = stds->stds;
984    caps.len  = stds->stds_len * 4;
985    // TODO: add support for **data.
986    if ( roar_caps_to_msg(mes, &caps, NULL) == -1 ) {
987     roar_stds_free(stds);
988     return -1;
989    }
990    roar_stds_free(stds);
991   break;
992  default:
993    return -1;
994   break;
995 }
996
997 mes->cmd = ROAR_CMD_OK;
998
999 return 0;
1000}
1001
1002int req_on_get_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1003 mes->cmd     = ROAR_CMD_OK;
1004 mes->pos     = g_pos;
1005 mes->datalen = 2;
1006
1007 *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby);
1008
1009 return 0;
1010}
1011
1012int req_on_set_standby (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1013 if ( mes->datalen != 2 )
1014  return -1;
1015
1016 g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data));
1017
1018 mes->cmd     = ROAR_CMD_OK;
1019 mes->pos     = g_pos;
1020 mes->datalen = 0;
1021
1022 return 0;
1023}
1024
1025int req_on_exit      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1026 int term = 0;
1027
1028 if ( mes->datalen == 1 )
1029  term = mes->data[0];
1030
1031 mes->cmd     = ROAR_CMD_OK;
1032 mes->pos     = g_pos;
1033 mes->datalen = 0;
1034
1035 ROAR_DBG("req_on_exit(*): term=%i", term);
1036
1037 if ( term ) {
1038  cleanup_listen_socket(1);
1039 } else {
1040  alive = 0;
1041 }
1042
1043 return 0;
1044}
1045
1046int req_on_list_clients(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1047 unsigned char filter, cmp;
1048 uint32_t id;
1049 int clients[ROAR_CLIENTS_MAX];
1050 int i, c = 0;
1051
1052 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
1053  return -1;
1054
1055 // TODO: add code to support filter
1056 if ( filter != ROAR_CTL_FILTER_ANY )
1057  return -1;
1058
1059 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
1060  if ( g_clients[i] != NULL ) {
1061   clients[c++] = i;
1062  }
1063 }
1064
1065 roar_ctl_ia2m(mes, clients, c);
1066
1067 mes->cmd = ROAR_CMD_OK;
1068
1069 return 0;
1070}
1071int req_on_list_streams(int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1072 unsigned char filter, cmp;
1073 uint32_t id;
1074 int streams[ROAR_STREAMS_MAX];
1075 int i, c = 0;
1076 int match;
1077
1078 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
1079  return -1;
1080
1081 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
1082  if ( g_streams[i] == NULL )
1083   continue;
1084
1085  match = 0;
1086
1087  switch (filter) {
1088   case ROAR_CTL_FILTER_ANY:
1089     match = 1;
1090    break;
1091   case ROAR_CTL_FILTER_DIR:
1092     match = roar_filter_match(cmp, id, ROAR_STREAM(g_streams[i])->dir);
1093    break;
1094   default: // unsupported filter...
1095     return -1;
1096    break;
1097  }
1098
1099  if ( match )
1100   streams[c++] = i;
1101 }
1102
1103 roar_ctl_ia2m(mes, streams, c);
1104
1105 mes->cmd = ROAR_CMD_OK;
1106
1107 return 0;
1108}
1109
1110int req_on_get_client  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1111 struct roar_client * c;
1112 int ret;
1113
1114 if ( mes->datalen != 1 )
1115  return -1;
1116
1117 if ( *data != NULL )
1118  roar_mm_free(*data);
1119 *data = NULL;
1120
1121 if ( clients_get(mes->data[0], &c) == -1 )
1122  return -1;
1123
1124 mes->cmd = ROAR_CMD_OK;
1125
1126 ret = roar_ctl_c2m2(mes, c, (void**)data);
1127
1128 if ( *data != NULL )
1129  flags[1] |= COMMAND_FLAG_OUT_LONGDATA;
1130
1131 return ret;
1132}
1133
1134int req_on_get_stream  (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1135 struct roar_stream_server * s;
1136
1137 if ( mes->datalen != 1 )
1138  return -1;
1139
1140 if ( streams_get(mes->data[0], &s) == -1 )
1141  return -1;
1142
1143 mes->cmd = ROAR_CMD_OK;
1144 mes->stream = mes->data[0];
1145
1146 return roar_stream_s2m(ROAR_STREAM(s), mes);
1147}
1148
1149int req_on_get_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1150 struct roar_stream * s;
1151 struct roar_stream_server * ss;
1152 struct roar_audio_info * audio_info;
1153 struct roar_stream_ltm * ltm;
1154 struct roar_stream_rpg   rpg;
1155 uint16_t * d  = (uint16_t *) mes->data;
1156 int64_t * d64 = ( int64_t *) mes->data;
1157 int64_t * d64ptr;
1158 int i, h, k;
1159 char * str;
1160 size_t needed;
1161 int test, bits;
1162
1163 ROAR_DBG("req_on_get_stream_para(client=%i, mes=%p{.stream=%i, .datalen=%i,...}, data=%p, flags=%p) = ?", client, mes, (int)mes->stream, (int)mes->datalen, data, flags);
1164
1165 if ( mes->datalen < 4 )
1166  return -1;
1167
1168 for (i = 0; i < 2; i++) {
1169  d[i] = ROAR_NET2HOST16(d[i]);
1170 }
1171
1172 if ( d[0] != 0 ) {
1173  ROAR_WARN("req_on_get_stream_para(*): unsupported command version: %i, %i", (int)d[0], (int)d[1]);
1174  return -1;
1175 }
1176
1177 switch (d[1]) {
1178  case ROAR_STREAM_PARA_INFO:
1179    if ( streams_get(mes->stream, &ss) == -1 ) {
1180     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
1181     return -1;
1182    }
1183
1184    if ( streams_calc_delay(mes->stream) == -1 ) {
1185     ROAR_WARN("req_on_get_stream_para(*): can not calc delay for stream %i", mes->stream);
1186    }
1187
1188    s = ROAR_STREAM(ss);
1189
1190    audio_info = &(s->info);
1191
1192    mes->datalen = 2*12;
1193
1194    d[ 2] = ROAR_OUTPUT_CALC_OUTBUFSIZE(audio_info);
1195    d[ 3] = ss->pre_underruns;
1196    d[ 4] = ss->post_underruns;
1197    d[ 5] = ss->codec_orgi;
1198    d[ 6] = (ss->flags & 0xFFFF) | (ss->primary ? ROAR_FLAG_PRIMARY : 0) | (ss->driver_id != -1 ? ROAR_FLAG_OUTPUT : 0);
1199    d[ 7] = ss->delay/1000;
1200    d[ 8] = ss->state;
1201    d[ 9] = (ss->flags & 0xFFFF0000) >> 16;
1202    d[10] = ss->mixer_stream;
1203    d[11] = ss->role;
1204
1205    ROAR_DBG("req_on_get_stream_para(*): ss->driver_id=%i", ss->driver_id);
1206
1207    ROAR_DBG("req_on_get_stream_para(*): delay=%i, send delay=%i", ss->delay, d[7]);
1208
1209    for (i = 0; i < mes->datalen/2; i++) {
1210     d[i] = ROAR_HOST2NET16(d[i]);
1211    }
1212
1213    mes->pos = s->pos;
1214   break;
1215
1216  case ROAR_STREAM_PARA_NAME:
1217   str = streams_get_name(mes->stream);
1218
1219   if ( str == NULL )
1220    return -1;
1221
1222    mes->datalen = 4 + strlen(str);
1223
1224    if ( mes->datalen > LIBROAR_BUFFER_MSGDATA )
1225     return -1;
1226
1227    strncpy(((char*)&(mes->data))+4, str, mes->datalen);
1228
1229    d[0] = ROAR_HOST2NET16(d[0]);
1230    d[1] = ROAR_HOST2NET16(d[1]);
1231   break;
1232
1233  case ROAR_STREAM_PARA_CHANMAP:
1234    if ( streams_get(mes->stream, &ss) == -1 ) {
1235     ROAR_WARN("req_on_get_stream_para(*): request on non existing (or other error?) stream %i", mes->stream);
1236     return -1;
1237    }
1238
1239    s = ROAR_STREAM(ss);
1240
1241    memcpy(&(mes->data[4]), ss->chanmap.in, s->info.channels);
1242    mes->datalen = 2*2 + s->info.channels;
1243
1244    d[0] = ROAR_HOST2NET16(d[0]);
1245    d[1] = ROAR_HOST2NET16(d[1]);
1246   break;
1247
1248  case ROAR_STREAM_PARA_LTM:
1249    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request...", client);
1250
1251    if ( mes->datalen < (6 * 2) ) {
1252     ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: message too short", client);
1253     return -1;
1254    }
1255
1256    for (i = 2; i < mes->datalen/2; i++) {
1257     d[i] = ROAR_NET2HOST16(d[i]);
1258    }
1259
1260    if ( d[2] != ROAR_LTM_SST_GET_RAW ) {
1261     ROAR_ERR("req_on_get_stream_para(client=%i, ...): LTM request of client is corruped: unknown LTM subtype: %i", client, (int)d[2]);
1262     return -1;
1263    }
1264
1265    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM request of type GET_RAW", client);
1266
1267    test = d[5];
1268    bits = 0;
1269    while (test) {
1270     if ( test & 0x1 )
1271      bits++;
1272
1273     test >>= 1;
1274    }
1275
1276    needed = 0;
1277
1278    if ( mes->stream == -1 ) {
1279     ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM multi-stream request...", client);
1280
1281     for (i = 6; i < mes->datalen/2; i++) {
1282      if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL )
1283       return -1;
1284
1285      needed += ltm->channels;
1286     }
1287    } else {
1288     ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM single-stream request for stream %i...", client, mes->stream);
1289     if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL )
1290      return -1;
1291
1292     needed = ltm->channels;
1293    }
1294
1295    needed *= bits;
1296
1297    needed += mes->stream == -1 ? (mes->datalen/2) - 6 : 1;
1298
1299    ROAR_DBG("req_on_get_stream_para(client=%i, ...): data size for answer is %i 64 bit sub-packets", client, (int)needed);
1300
1301    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
1302    d64 = _dataspace(mes, data, flags, needed * 8);
1303    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1304
1305    if ( (d = roar_mm_malloc(mes->datalen)) == NULL )
1306     return -1;
1307
1308    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d=%p, data=%p{%p}", client, d, data, *data);
1309
1310
1311    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
1312    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1313    memcpy(d, mes->data, mes->datalen);
1314    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1315
1316    d64ptr = d64;
1317
1318    ROAR_DBG("req_on_get_stream_para(client=%i, ...): mes->datalen=%i, data=%p{%p}", client, (int)mes->datalen, data, *data);
1319    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1320
1321    if ( mes->stream == -1 ) {
1322     for (i = 6; i < mes->datalen/2; i++) {
1323      ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1324
1325      if ( (ltm = streams_ltm_get(d[i], d[5], d[3])) == NULL )
1326       return -1;
1327
1328      *d64ptr = ltm->channels & 0xFFFF;
1329       d64ptr++;
1330
1331      for (h = 0; h < ltm->channels; h++) {
1332       for (k = 0; k < ROAR_LTM_MTBITS; k++) {
1333        if ( d[5] & (1<<k) ) {
1334         switch (1<<k) {
1335          case ROAR_LTM_MT_RMS:
1336            *d64ptr = ltm->cur[h].rms;
1337           break;
1338          default:
1339            ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k);
1340         }
1341         d64ptr++;
1342        }
1343       }
1344      }
1345     }
1346    } else {
1347     if ( (ltm = streams_ltm_get(mes->stream, d[5], d[3])) == NULL )
1348      return -1;
1349
1350     *d64ptr = ltm->channels & 0xFFFF;
1351      d64ptr++;
1352
1353     for (h = 0; h < ltm->channels; h++) {
1354      for (k = 0; k < ROAR_LTM_MTBITS; k++) {
1355       if ( d[5] & (1<<k) ) {
1356        switch (1<<k) {
1357         case ROAR_LTM_MT_RMS:
1358           *d64ptr = ltm->cur[h].rms;
1359           ROAR_DBG("req_on_get_stream_para(client=%i, ...): rms=%lli to %p", client, (long long int)*d64ptr, d64ptr);
1360          break;
1361         default:
1362           ROAR_ERR("req_on_get_stream_para(client=%i, ...): client requets unknown MT for LTM: bit %i", client, k);
1363        }
1364        d64ptr++;
1365       }
1366      }
1367     }
1368    }
1369
1370    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1371
1372    roar_mm_free(d);
1373
1374    for (i = 0; i < needed; i++) {
1375     d64[i] = ROAR_HOST2NET64(d64[i]);
1376    }
1377
1378    mes->datalen = needed * 8;
1379    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM d64=%p, d64ptr=%p", client, d64, d64ptr);
1380    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM final message has %i byte of data", client, (int)mes->datalen);
1381    ROAR_DBG("req_on_get_stream_para(client=%i, ...): d64=%p, data=%p{%p}", client, d64, data, *data);
1382    ROAR_DBG("req_on_get_stream_para(client=%i, ...): LTM GET_RAW request: OK. returning...", client);
1383   break;
1384
1385  case ROAR_STREAM_PARA_RPG:
1386    if ( streams_get_rpg(mes->stream, &rpg) == -1 ) {
1387     return -1;
1388    }
1389
1390    mes->datalen = 2*5;
1391
1392    d[2] = rpg.mode;
1393    d[3] = rpg.mul;
1394    d[4] = rpg.div;
1395
1396    for (i = 0; i < mes->datalen/2; i++) {
1397     d[i] = ROAR_HOST2NET16(d[i]);
1398    }
1399   break;
1400
1401  default:
1402    ROAR_WARN("req_on_get_stream_para(*): unsupported command: %i", d[1]);
1403    return -1;
1404 }
1405
1406 ROAR_DBG("req_on_get_stream_para(client=%i, mes=%p{.stream=%i, .datalen=%i,...}, data=%p, flags=%p) = 0 // returning OK", client, mes, (int)mes->stream, (int)mes->datalen, data, flags);
1407 mes->cmd = ROAR_CMD_OK;
1408 return 0;
1409}
1410
1411int req_on_set_stream_para (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1412 struct roar_stream_server * ss;
1413 struct roar_stream_rpg rpg;
1414 uint16_t * d = (uint16_t *) mes->data;
1415 uint32_t tmp, tmp2, flagstore;
1416 int protect = 0;
1417 int i;
1418
1419 if ( mes->datalen < 2*2 )
1420  return -1;
1421
1422 for (i = 0; i < 2; i++) {
1423  d[i] = ROAR_NET2HOST16(d[i]);
1424 }
1425
1426 if ( d[0] != 0 )
1427  return -1;
1428
1429 switch (d[1]) {
1430  case ROAR_STREAM_PARA_FLAGS:
1431    if ( mes->datalen != 2*4 && mes->datalen != 2*5 )
1432     return -1;
1433
1434    d[2] = ROAR_NET2HOST16(d[2]);
1435    d[3] = ROAR_NET2HOST16(d[3]);
1436
1437    ROAR_DBG("req_on_set_stream_para(*): request seems to be valid");
1438
1439    tmp = 0;
1440
1441    if ( mes->datalen == 2*5 ) {
1442     d[4] = ROAR_NET2HOST16(d[4]);
1443     tmp   = d[4];
1444     tmp <<= 16;
1445    }
1446
1447    tmp |= d[3];
1448
1449    if ( d[2] & ROAR_PROTECT_FLAG ) {
1450     protect = 1;
1451     d[2] -= ROAR_PROTECT_FLAG;
1452    }
1453
1454    switch (d[2]) {
1455     case ROAR_SET_FLAG:
1456       if ( streams_set_flag(mes->stream, tmp) == -1 )
1457        return -1;
1458      break;
1459     case ROAR_RESET_FLAG:
1460       if ( streams_reset_flag(mes->stream, tmp) == -1 )
1461        return -1;
1462      break;
1463     case ROAR_NOOP_FLAG:
1464      break;
1465     case ROAR_TOGGLE_FLAG:
1466       if ( streams_get(mes->stream, &ss) == -1 )
1467        return -1;
1468
1469       flagstore = ss->flags;
1470
1471       tmp2 = flagstore & tmp; // those are the flags we need to reset.
1472       ROAR_DBG("req_on_set_stream_para(*): tmp2=0x%.8lx", (long int)tmp2);
1473       if ( tmp2 )
1474        if ( streams_reset_flag(mes->stream, tmp2) == -1 )
1475         return -1;
1476
1477       tmp2 = (flagstore ^ tmp) & tmp; // those are the flags we need to set.
1478       ROAR_DBG("req_on_set_stream_para(*): tmp2=0x%.8lx", (long int)tmp2);
1479       if ( tmp2 )
1480        if ( streams_set_flag(mes->stream, tmp2) == -1 )
1481         return -1;
1482      break;
1483     default:
1484       return -1;
1485      break;
1486    }
1487
1488   ROAR_DBG("req_on_set_stream_para(*): protect=%i", protect);
1489   if ( protect )
1490    if ( streams_protect_flag(mes->stream, tmp) == -1 )
1491     return -1;
1492
1493   break;
1494  case ROAR_STREAM_PARA_CHANMAP:
1495    if ( streams_set_map(mes->stream, &(mes->data[4]), mes->datalen - 4) == -1 )
1496     return -1;
1497   break;
1498  case ROAR_STREAM_PARA_ROLE:
1499    if ( mes->datalen != 2*3 )
1500     return -1;
1501
1502    d[2] = ROAR_NET2HOST16(d[2]);
1503
1504    if ( streams_set_role(mes->stream, d[2]) == -1 )
1505     return -1;
1506   break;
1507  case ROAR_STREAM_PARA_LTM:
1508    if ( mes->datalen < (6 * 2) )
1509     return -1;
1510
1511    for (i = 2; i < mes->datalen/2; i++) {
1512     d[i] = ROAR_NET2HOST16(d[i]);
1513    }
1514
1515    if ( mes->stream == -1 ) {
1516     for (i = 6; i < mes->datalen/2; i++)
1517      if ( streams_ltm_ctl(d[i], d[5], d[3], d[2]) == -1 )
1518       return -1;
1519    } else {
1520     if ( streams_ltm_ctl(mes->stream, d[5], d[3], d[2]) == -1 )
1521      return -1;
1522    }
1523   break;
1524  case ROAR_STREAM_PARA_RPG:
1525    if ( mes->datalen != (5 * 2) )
1526     return -1;
1527
1528    for (i = 2; i < mes->datalen/2; i++) {
1529     d[i] = ROAR_NET2HOST16(d[i]);
1530    }
1531
1532    rpg.mode = d[2];
1533    rpg.mul  = d[3];
1534    rpg.div  = d[4];
1535
1536    if ( streams_set_rpg(mes->stream, &rpg) == -1 )
1537     return -1;
1538   break;
1539  default:
1540    ROAR_WARN("req_on_set_stream_para(*): unsupported command version: %i, %i", d[0], d[1]);
1541    return -1;
1542   break;
1543 }
1544
1545 mes->cmd     = ROAR_CMD_OK;
1546 mes->datalen = 0;
1547
1548 return 0;
1549}
1550
1551int req_on_kick (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1552 struct roar_stream_server * ss;
1553 uint16_t * info = (uint16_t *) mes->data;
1554 int is_stream = 0;
1555
1556 if ( mes->datalen != 4 )
1557  return -1;
1558
1559 info[0] = ROAR_NET2HOST16(info[0]);
1560 info[1] = ROAR_NET2HOST16(info[1]);
1561
1562 switch (info[0]) {
1563  case ROAR_OT_CLIENT:
1564    clients_delete(info[1]);
1565   break;
1566  case ROAR_OT_STREAM:
1567    is_stream = 1;
1568   break;
1569  case ROAR_OT_SOURCE:
1570    if ( streams_get_flag(info[1], ROAR_FLAG_SOURCE) != 1 )
1571     return -1;
1572    is_stream = 1;
1573   break;
1574  case ROAR_OT_OUTPUT:
1575    if ( streams_get(info[1], &ss) == -1 )
1576     return -1;
1577
1578    if ( ss->driver_id == -1 )
1579     return -1;
1580
1581    is_stream = 1;
1582   break;
1583  case ROAR_OT_MIXER:
1584    if ( streams_get(info[1], &ss) == -1 )
1585     return -1;
1586
1587    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_MIXING )
1588     return -1;
1589
1590    is_stream = 1;
1591   break;
1592  case ROAR_OT_BRIDGE:
1593    if ( streams_get(info[1], &ss) == -1 )
1594     return -1;
1595
1596    if ( ROAR_STREAM(ss)->dir != ROAR_DIR_BRIDGE )
1597     return -1;
1598
1599    is_stream = 1;
1600   break;
1601  default:
1602/* TODO: those types should be handled, too:
1603#define ROAR_OT_SAMPLE    4
1604#define ROAR_OT_LISTEN    8
1605#define ROAR_OT_ACTION    9
1606#define ROAR_OT_MSGQUEUE 10
1607#define ROAR_OT_MSGBUS   11
1608*/
1609    return -1;
1610   break;
1611 }
1612
1613 if ( is_stream ) {
1614  if ( streams_get_flag(info[1], ROAR_FLAG_IMMUTABLE) == 1 )
1615   return -1;
1616
1617  streams_delete(info[1]);
1618 }
1619
1620 mes->cmd     = ROAR_CMD_OK;
1621 mes->datalen = 0;
1622
1623 return 0;
1624}
1625
1626int req_on_attach      (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1627 uint16_t * info = (uint16_t *) mes->data;
1628
1629 if ( mes->datalen < 6 )
1630  return -1;
1631
1632 info[0] = ROAR_NET2HOST16(info[0]);
1633 info[1] = ROAR_NET2HOST16(info[1]);
1634 info[2] = ROAR_NET2HOST16(info[2]);
1635
1636 if ( info[0] != 0 )
1637  return -1;
1638
1639 if ( info[1] == ROAR_ATTACH_SIMPLE ) {
1640  if ( client_stream_move(info[2], mes->stream) == -1 )
1641   return -1;
1642 } else {
1643  return -1;
1644 }
1645
1646 mes->cmd     = ROAR_CMD_OK;
1647 mes->datalen = 0;
1648
1649 return 0;
1650}
1651
1652int req_on_set_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1653 struct roar_mixer_settings tmpmixer;
1654 struct roar_stream_server * s;
1655 uint16_t * info = (uint16_t *) mes->data;
1656 uint16_t   version;
1657 uint16_t   scale = 65535;
1658 int stream;
1659 int i;
1660 int chans;
1661
1662 ROAR_DBG("req_on_set_vol(*) = ?");
1663 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
1664
1665 if ( mes->datalen < (4*2) )
1666  return -1;
1667
1668 version = ROAR_NET2HOST16(info[0]);
1669 ROAR_DBG("req_on_set_vol(*): version=%i", (int)version);
1670
1671 switch (version) {
1672  case 0:
1673    stream = ROAR_NET2HOST16(info[1]);
1674   break;
1675  case 1:
1676    stream = mes->stream;
1677    scale  = ROAR_NET2HOST16(info[1]);
1678   break;
1679  default:
1680    return -1;
1681   break;
1682 }
1683 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
1684
1685 if ( scale == 0 )
1686  return -1;
1687
1688 // TODO: change this code.
1689 //       we should not directly change the stream object but use some stream_*()-func
1690 //       for that job.
1691
1692 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1693  return -1;
1694
1695 s = g_streams[stream];
1696
1697 if ( s == NULL )
1698  return -1;
1699
1700 ROAR_DBG("req_on_set_vol(*): s=%p", s);
1701
1702 info[2] = ROAR_NET2HOST16(info[2]);
1703
1704 switch (info[2]) {
1705  case ROAR_SET_VOL_ALL:
1706    chans = (mes->datalen/2) - 3;
1707    ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
1708
1709    if ( chans >= ROAR_MAX_CHANNELS )
1710     return -1;
1711
1712    ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
1713
1714    for (i = 0; i < chans; i++) {
1715     s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]);
1716     ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3]));
1717    }
1718
1719    s->mixer.scale = scale;
1720
1721    ROAR_DBG("req_on_set_vol(*): mixer changed!");
1722
1723   break;
1724  case ROAR_SET_VOL_ONE:
1725    ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
1726    if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS )
1727     return -1;
1728
1729    s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]);
1730
1731    s->mixer.scale = scale;
1732   break;
1733  case ROAR_SET_VOL_UNMAPPED:
1734    chans = (mes->datalen/2) - 3;
1735
1736    if ( chans >= ROAR_MAX_CHANNELS )
1737     return -1;
1738
1739    memcpy(&tmpmixer, &(s->mixer), sizeof(tmpmixer));
1740
1741    for (i = 0; i < chans; i++) {
1742     tmpmixer.mixer[i] = ROAR_NET2HOST16(info[i+3]);
1743    }
1744
1745    tmpmixer.scale = scale;
1746
1747    if ( roar_conv_volume(&(s->mixer), &tmpmixer, ROAR_STREAM(s)->info.channels, chans) == -1 )
1748     return -1;
1749
1750   break;
1751  case ROAR_SET_VOL_MS:
1752  default:
1753    return -1;
1754 }
1755
1756 if ( streams_set_mixer(stream) == -1 )
1757  return -1;
1758
1759 mes->cmd     = ROAR_CMD_OK;
1760 mes->datalen = 0;
1761
1762 return 0;
1763}
1764
1765int req_on_get_vol (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1766 uint16_t * info = (uint16_t *) mes->data;
1767 uint16_t   version = -1;
1768 int stream;
1769 struct roar_stream_server * s;
1770 int i;
1771 int chans;
1772
1773 ROAR_DBG("req_on_get_vol(*) = ?");
1774 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
1775
1776 if ( mes->datalen < 2 ) {
1777  return -1;
1778 }
1779
1780 version = ROAR_NET2HOST16(info[0]);
1781
1782 switch (version) {
1783  case 0:
1784    if ( mes->datalen < (2*2) )
1785     return -1;
1786
1787    stream = ROAR_NET2HOST16(info[1]);
1788   break;
1789  case 1:
1790    stream = mes->stream;
1791   break;
1792  default:
1793    return -1;
1794   break;
1795 }
1796
1797 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
1798
1799 // TODO: change this code.
1800 //       we should not directly change the stream object but use some stream_*()-func
1801 //       for that job.
1802
1803 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
1804  return -1;
1805
1806 s = g_streams[stream];
1807
1808 if ( s == NULL )
1809  return -1;
1810
1811 ROAR_DBG("req_on_get_vol(*): s=%p", s);
1812
1813 // ok, we have everything
1814
1815 info[0] = ROAR_HOST2NET16(version);
1816
1817 switch (version) {
1818  case 0:
1819    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1820
1821    for (i = 0; i < chans; i++)
1822     info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1823
1824     mes->datalen = (2 + chans)*2;
1825   break;
1826  case 1:
1827    info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels);
1828    info[2] = ROAR_HOST2NET16(s->mixer.scale);
1829    info[3] = ROAR_HOST2NET16(s->mixer.rpg_mul);
1830    info[4] = ROAR_HOST2NET16(s->mixer.rpg_div);
1831
1832    for (i = 0; i < chans; i++)
1833     info[5+i] = ROAR_HOST2NET16(s->mixer.mixer[i]);
1834
1835     mes->datalen = (5 + chans)*2;
1836   break;
1837  default:
1838    return -1;
1839   break;
1840 }
1841
1842 mes->cmd = ROAR_CMD_OK;
1843
1844 return 0;
1845}
1846
1847int req_on_add_data (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1848 struct roar_buffer * b;
1849 void               * buf;
1850
1851 if ( roar_buffer_new_data(&b, mes->datalen, &buf) == -1 ) {
1852  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
1853  ROAR_DBG("req_on_add_data(*) = -1");
1854  return -1;
1855 }
1856
1857 if ( data == NULL ) {
1858  memcpy(buf, mes->data, mes->datalen);
1859 } else {
1860  memcpy(buf, *data, mes->datalen);
1861 }
1862
1863 if ( stream_add_buffer(mes->stream, &b) == -1 ) {
1864  roar_buffer_free(b);
1865  return -1;
1866 }
1867
1868 mes->cmd     = ROAR_CMD_OK_STOP;
1869 mes->datalen = 0;
1870
1871 return 0;
1872}
1873
1874int req_on_beep        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1875 struct roar_beep bs;
1876 int16_t * info = (int16_t*)mes->data;
1877 int stream;
1878
1879 memset(&bs, 0, sizeof(bs));
1880
1881 if ( mes->datalen > 0 ) {
1882  if ( mes->datalen < 2 )
1883   return -1;
1884
1885  if ( ROAR_NET2HOST16(info[0]) != 0 ) /* version */
1886   return -1;
1887
1888  if ( mes->datalen != 8*2 )
1889   return -1;
1890
1891  bs.vol  = ROAR_NET2HOST16(info[1]);
1892  bs.time = ROAR_NET2HOST16(info[2]);
1893  bs.freq = ROAR_NET2HOST16(info[3]);
1894  bs.type = ROAR_NET2HOST16(info[4]);
1895  bs.x    = ROAR_NET2HOST16(info[5]);
1896  bs.y    = ROAR_NET2HOST16(info[6]);
1897  bs.z    = ROAR_NET2HOST16(info[7]);
1898 }
1899
1900 if ( (stream = beep_start(client, &bs)) == -1 )
1901  return -1;
1902
1903 mes->stream  = stream;
1904 mes->cmd     = ROAR_CMD_OK_STOP;
1905 mes->datalen = 0;
1906
1907 return 0;
1908}
1909
1910int req_on_wait        (int client, struct roar_message * mes, char ** data, uint32_t flags[2]) {
1911 uint16_t * u16 = (uint16_t*)mes->data;
1912 struct roar_event events[4];
1913 size_t left, tmp;
1914 size_t num = 0;
1915 void * vp = mes->data;
1916
1917 vp += 4;
1918
1919 // check for complet header...
1920 if ( mes->datalen < 4 )
1921  return -1;
1922
1923 u16[0] = ROAR_NET2HOST16(u16[0]);
1924 u16[1] = ROAR_NET2HOST16(u16[1]);
1925
1926 // do we support version and flags?
1927 if ( u16[0] != 0 || u16[1] != 0 )
1928  return -1;
1929
1930 memset(events, 0, sizeof(events));
1931
1932 left = mes->datalen - 4;
1933
1934 while (left) {
1935  tmp = left;
1936  if ( roar_event_from_blob(&(events[num]), vp, &tmp) == -1 )
1937   return -1;
1938
1939  vp   += tmp;
1940  left -= tmp;
1941  num++;
1942 }
1943
1944 if ( clients_wait(client, events, num) == -1 )
1945  return -1;
1946
1947 flags[1] |= COMMAND_FLAG_OUT_NOSEND;
1948
1949 return 0;
1950}
1951
1952//ll
Note: See TracBrowser for help on using the repository browser.