1 | //req.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int req_on_noop (int client, struct roar_message * mes, char * data) { |
---|
6 | mes->cmd = ROAR_CMD_OK; |
---|
7 | mes->datalen = 0; |
---|
8 | return 0; |
---|
9 | } |
---|
10 | |
---|
11 | int req_on_identify (int client, struct roar_message * mes, char * data) { |
---|
12 | struct roar_client * c; |
---|
13 | int max_len; |
---|
14 | #ifdef SO_PEERCRED |
---|
15 | struct ucred cred; |
---|
16 | #endif |
---|
17 | |
---|
18 | if ( mes->datalen < 1 ) |
---|
19 | return -1; |
---|
20 | |
---|
21 | clients_get(client, &c); |
---|
22 | |
---|
23 | if ( mes->data[0] == 1 ) { |
---|
24 | #ifdef SO_PEERCRED |
---|
25 | if (getsockopt(c->fh, SOL_SOCKET, SO_PEERCRED, &cred, (socklen_t) sizeof(struct ucred)) != -1) { |
---|
26 | c->pid = cred.pid; |
---|
27 | } |
---|
28 | #endif |
---|
29 | if ( c->pid == -1 ) { |
---|
30 | c->pid = ROAR_NET2HOST32(*(uint32_t*)((mes->data)+1)); |
---|
31 | } |
---|
32 | |
---|
33 | max_len = (mes->datalen - 5) < (ROAR_BUFFER_NAME-1) ? (mes->datalen - 5) : (ROAR_BUFFER_NAME-1); |
---|
34 | |
---|
35 | strncpy(c->name, mes->data + 5, max_len); |
---|
36 | c->name[max_len] = 0; |
---|
37 | |
---|
38 | mes->cmd = ROAR_CMD_OK; |
---|
39 | mes->datalen = 0; |
---|
40 | |
---|
41 | ROAR_DBG("req_on_identify(*): client=%i, pid=%i", client, c->pid); |
---|
42 | ROAR_DBG("req_on_identify(*) = 0"); |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | return -1; |
---|
47 | } |
---|
48 | |
---|
49 | int req_on_auth (int client, struct roar_message * mes, char * data) { |
---|
50 | // TODO: add code to support some auth. |
---|
51 | mes->cmd = ROAR_CMD_OK; |
---|
52 | mes->datalen = 0; |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | int req_on_new_stream (int client, struct roar_message * mes, char * data) { |
---|
58 | int stream; |
---|
59 | struct roar_stream * s; |
---|
60 | |
---|
61 | if ((stream = streams_new()) == -1 ) |
---|
62 | return -1; |
---|
63 | |
---|
64 | if ( streams_get(stream, (struct roar_stream_server **)&s) == -1 ) { |
---|
65 | streams_delete(stream); |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | if ( client_stream_add(client, stream) == -1 ) { |
---|
70 | streams_delete(stream); |
---|
71 | return -1; |
---|
72 | } |
---|
73 | |
---|
74 | if ( roar_stream_m2s(s, mes) == -1 ) { |
---|
75 | streams_delete(stream); |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | mes->cmd = ROAR_CMD_OK; |
---|
80 | mes->stream = stream; |
---|
81 | mes->datalen = 0; |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | int req_on_exec_stream (int client, struct roar_message * mes, char * data) { |
---|
87 | int r; |
---|
88 | |
---|
89 | if ( (r = client_stream_exec(client, mes->stream)) == -1 ) |
---|
90 | return -1; |
---|
91 | |
---|
92 | mes->cmd = ROAR_CMD_OK; |
---|
93 | mes->datalen = 0; |
---|
94 | |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | int req_on_con_stream (int client, struct roar_message * mes, char * data) { |
---|
99 | char host[80] = {0}; |
---|
100 | int port = 0; |
---|
101 | int type; |
---|
102 | int fh; |
---|
103 | int len; |
---|
104 | |
---|
105 | if ( mes->datalen < 4 ) |
---|
106 | return -1; |
---|
107 | |
---|
108 | if ( *(mes->data) != 0 ) |
---|
109 | return -1; |
---|
110 | |
---|
111 | if ( mes->datalen > 80 ) // we do not support long messages here |
---|
112 | return -1; |
---|
113 | |
---|
114 | type = (unsigned)mes->data[1]; |
---|
115 | port = ROAR_NET2HOST16(((uint16_t*)mes->data)[1]); |
---|
116 | |
---|
117 | len = mes->datalen - 4; |
---|
118 | |
---|
119 | strncpy(host, &(mes->data[4]), len); |
---|
120 | host[len] = 0; |
---|
121 | |
---|
122 | if ( type > ROAR_SOCKET_TYPE_MAX ) |
---|
123 | return -1; |
---|
124 | |
---|
125 | if ( type == ROAR_SOCKET_TYPE_FILE ) // disabled because of security resons |
---|
126 | return -1; |
---|
127 | |
---|
128 | if ( type == ROAR_SOCKET_TYPE_FORK ) // why should we connect to ourself? |
---|
129 | return -1; |
---|
130 | |
---|
131 | if ( (fh = roar_socket_open(ROAR_SOCKET_MODE_CONNECT, type, host, port)) == -1 ) |
---|
132 | return -1; |
---|
133 | |
---|
134 | if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) { |
---|
135 | close(fh); |
---|
136 | return 1; |
---|
137 | } |
---|
138 | |
---|
139 | return 0; |
---|
140 | |
---|
141 | mes->datalen = 0; |
---|
142 | mes->cmd = ROAR_CMD_OK; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | int req_on_set_meta (int client, struct roar_message * mes, char * data) { |
---|
147 | int type; |
---|
148 | int mode; |
---|
149 | int namelen, vallen; |
---|
150 | char val[255+1]; |
---|
151 | char name[ROAR_META_MAX_NAMELEN+1]; |
---|
152 | |
---|
153 | if ( mes->datalen < 3 ) |
---|
154 | return -1; |
---|
155 | |
---|
156 | if ( mes->data[0] != 0 ) // version |
---|
157 | return -1; |
---|
158 | |
---|
159 | mode = (unsigned) mes->data[1]; |
---|
160 | type = (unsigned) mes->data[2]; |
---|
161 | |
---|
162 | ROAR_DBG("req_on_set_meta(*): mode=%i, type=%i", mode, type); |
---|
163 | |
---|
164 | if ( mode == ROAR_META_MODE_CLEAR ) { |
---|
165 | stream_meta_clear(mes->stream); |
---|
166 | mes->datalen = 0; |
---|
167 | mes->cmd = ROAR_CMD_OK; |
---|
168 | return 0; |
---|
169 | } else if ( mode == ROAR_META_MODE_DELETE ) { // unsuppoerted at the moment |
---|
170 | } else if ( mode == ROAR_META_MODE_SET || mode == ROAR_META_MODE_ADD ) { |
---|
171 | if ( mes->datalen < 5 ) |
---|
172 | return -1; |
---|
173 | |
---|
174 | namelen = (unsigned) mes->data[3]; |
---|
175 | vallen = (unsigned) mes->data[4]; |
---|
176 | |
---|
177 | ROAR_DBG("req_on_set_meta(*): namelen=%i, vallen=%i", namelen, vallen); |
---|
178 | |
---|
179 | if ( mes->datalen < (5 + namelen + vallen) ) |
---|
180 | return -1; |
---|
181 | |
---|
182 | if ( namelen > ROAR_META_MAX_NAMELEN ) |
---|
183 | return -1; |
---|
184 | |
---|
185 | strncpy(name, &(mes->data[5]), namelen); |
---|
186 | name[namelen] = 0; |
---|
187 | |
---|
188 | if ( vallen > 255 ) |
---|
189 | return -1; |
---|
190 | |
---|
191 | strncpy(val, &(mes->data[5+namelen]), vallen); |
---|
192 | val[vallen] = 0; |
---|
193 | |
---|
194 | if ( mode == ROAR_META_MODE_SET ) { |
---|
195 | if ( stream_meta_set(mes->stream, type, name, val) == -1 ) |
---|
196 | return -1; |
---|
197 | } else { |
---|
198 | if ( stream_meta_add(mes->stream, type, name, val) == -1 ) |
---|
199 | return -1; |
---|
200 | } |
---|
201 | |
---|
202 | mes->datalen = 0; |
---|
203 | mes->cmd = ROAR_CMD_OK; |
---|
204 | return 0; |
---|
205 | } else { // unknown mode! |
---|
206 | return -1; |
---|
207 | } |
---|
208 | |
---|
209 | return -1; |
---|
210 | } |
---|
211 | |
---|
212 | int req_on_get_meta (int client, struct roar_message * mes, char * data) { |
---|
213 | int vallen; |
---|
214 | int type; |
---|
215 | char val[LIBROAR_BUFFER_MSGDATA-1]; |
---|
216 | |
---|
217 | if ( mes->datalen != 2 ) |
---|
218 | return -1; |
---|
219 | |
---|
220 | if ( mes->data[0] != 0 ) // version |
---|
221 | return -1; |
---|
222 | |
---|
223 | type = (unsigned) mes->data[1]; |
---|
224 | |
---|
225 | if ( stream_meta_get(mes->stream, type, NULL, val, LIBROAR_BUFFER_MSGDATA-2) == -1 ) |
---|
226 | return -1; |
---|
227 | |
---|
228 | vallen = strlen(val); |
---|
229 | |
---|
230 | mes->cmd = ROAR_CMD_OK; |
---|
231 | mes->datalen = 2 + vallen; |
---|
232 | |
---|
233 | mes->data[0] = 0; |
---|
234 | mes->data[1] = (unsigned char) vallen; |
---|
235 | |
---|
236 | val[vallen] = 0; |
---|
237 | |
---|
238 | strncpy(&(mes->data[2]), val, vallen+1); |
---|
239 | |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | int req_on_list_meta (int client, struct roar_message * mes, char * data) { |
---|
244 | int i; |
---|
245 | int len = 0; |
---|
246 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
247 | |
---|
248 | if ( mes->datalen != 1 ) |
---|
249 | return -1; |
---|
250 | |
---|
251 | if ( mes->data[0] != 0 ) // version |
---|
252 | return -1; |
---|
253 | |
---|
254 | if ( (len = stream_meta_list(mes->stream, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
255 | return -1; |
---|
256 | |
---|
257 | mes->cmd = ROAR_CMD_OK; |
---|
258 | mes->datalen = 1 + len; |
---|
259 | mes->data[0] = 0; |
---|
260 | |
---|
261 | for (i = 0; i < len; i++) |
---|
262 | mes->data[i+1] = types[i]; |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|
267 | int req_on_server_oinfo (int client, struct roar_message * mes, char * data) { |
---|
268 | struct roar_stream s; |
---|
269 | //ROAR_DIR_OUTPUT |
---|
270 | |
---|
271 | s.dir = ROAR_DIR_OUTPUT; |
---|
272 | s.pos_rel_id = -1; |
---|
273 | s.info.rate = g_sa->rate; |
---|
274 | s.info.bits = g_sa->bits; |
---|
275 | s.info.channels = g_sa->channels; |
---|
276 | s.info.codec = g_sa->codec; |
---|
277 | |
---|
278 | if ( roar_stream_s2m(&s, mes) == -1 ) |
---|
279 | return -1; |
---|
280 | |
---|
281 | mes->cmd = ROAR_CMD_OK; |
---|
282 | |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | int req_on_get_standby (int client, struct roar_message * mes, char * data) { |
---|
288 | mes->cmd = ROAR_CMD_OK; |
---|
289 | mes->datalen = 2; |
---|
290 | |
---|
291 | *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby); |
---|
292 | |
---|
293 | return 0; |
---|
294 | } |
---|
295 | |
---|
296 | int req_on_set_standby (int client, struct roar_message * mes, char * data) { |
---|
297 | if ( mes->datalen != 2 ) |
---|
298 | return -1; |
---|
299 | |
---|
300 | g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data)); |
---|
301 | |
---|
302 | mes->cmd = ROAR_CMD_OK; |
---|
303 | mes->datalen = 0; |
---|
304 | |
---|
305 | return 0; |
---|
306 | } |
---|
307 | |
---|
308 | int req_on_exit (int client, struct roar_message * mes, char * data) { |
---|
309 | mes->cmd = ROAR_CMD_OK; |
---|
310 | mes->datalen = 0; |
---|
311 | |
---|
312 | alive = 0; |
---|
313 | |
---|
314 | return 0; |
---|
315 | } |
---|
316 | |
---|
317 | int req_on_list_clients(int client, struct roar_message * mes, char * data) { |
---|
318 | unsigned char filter, cmp; |
---|
319 | uint32_t id; |
---|
320 | int clients[ROAR_CLIENTS_MAX]; |
---|
321 | int i, c = 0; |
---|
322 | |
---|
323 | if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 ) |
---|
324 | return -1; |
---|
325 | |
---|
326 | // TODO: add code to support filter |
---|
327 | if ( filter != ROAR_CTL_FILTER_ANY ) |
---|
328 | return -1; |
---|
329 | |
---|
330 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
331 | if ( g_clients[i] != NULL ) { |
---|
332 | clients[c++] = i; |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | roar_ctl_ia2m(mes, clients, c); |
---|
337 | |
---|
338 | mes->cmd = ROAR_CMD_OK; |
---|
339 | |
---|
340 | return 0; |
---|
341 | } |
---|
342 | int req_on_list_streams(int client, struct roar_message * mes, char * data) { |
---|
343 | unsigned char filter, cmp; |
---|
344 | uint32_t id; |
---|
345 | int streams[ROAR_STREAMS_MAX]; |
---|
346 | int i, c = 0; |
---|
347 | |
---|
348 | if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 ) |
---|
349 | return -1; |
---|
350 | |
---|
351 | // TODO: add code to support filter |
---|
352 | if ( filter != ROAR_CTL_FILTER_ANY ) |
---|
353 | return -1; |
---|
354 | |
---|
355 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
356 | if ( g_streams[i] != NULL ) { |
---|
357 | streams[c++] = i; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | roar_ctl_ia2m(mes, streams, c); |
---|
362 | |
---|
363 | mes->cmd = ROAR_CMD_OK; |
---|
364 | |
---|
365 | return 0; |
---|
366 | } |
---|
367 | |
---|
368 | int req_on_get_client (int client, struct roar_message * mes, char * data) { |
---|
369 | struct roar_client * c; |
---|
370 | |
---|
371 | if ( mes->datalen != 1 ) |
---|
372 | return -1; |
---|
373 | |
---|
374 | if ( clients_get(mes->data[0], &c) == -1 ) |
---|
375 | return -1; |
---|
376 | |
---|
377 | mes->cmd = ROAR_CMD_OK; |
---|
378 | |
---|
379 | return roar_ctl_c2m(mes, c); |
---|
380 | } |
---|
381 | |
---|
382 | int req_on_get_stream (int client, struct roar_message * mes, char * data) { |
---|
383 | struct roar_stream_server * s; |
---|
384 | |
---|
385 | if ( mes->datalen != 1 ) |
---|
386 | return -1; |
---|
387 | |
---|
388 | if ( streams_get(mes->data[0], &s) == -1 ) |
---|
389 | return -1; |
---|
390 | |
---|
391 | mes->cmd = ROAR_CMD_OK; |
---|
392 | |
---|
393 | return roar_stream_s2m(ROAR_STREAM(s), mes); |
---|
394 | } |
---|
395 | |
---|
396 | int req_on_kick (int client, struct roar_message * mes, char * data) { |
---|
397 | uint16_t * info = (uint16_t *) mes->data; |
---|
398 | |
---|
399 | if ( mes->datalen != 4 ) |
---|
400 | return -1; |
---|
401 | |
---|
402 | info[0] = ROAR_NET2HOST16(info[0]); |
---|
403 | info[1] = ROAR_NET2HOST16(info[1]); |
---|
404 | |
---|
405 | if ( info[0] == ROAR_OT_CLIENT ) { |
---|
406 | clients_delete(info[1]); |
---|
407 | } else if ( info[0] == ROAR_OT_STREAM ) { |
---|
408 | streams_delete(info[1]); |
---|
409 | } else { |
---|
410 | return -1; |
---|
411 | } |
---|
412 | |
---|
413 | mes->cmd = ROAR_CMD_OK; |
---|
414 | mes->datalen = 0; |
---|
415 | |
---|
416 | return 0; |
---|
417 | } |
---|
418 | |
---|
419 | int req_on_set_vol (int client, struct roar_message * mes, char * data) { |
---|
420 | uint16_t * info = (uint16_t *) mes->data; |
---|
421 | int stream; |
---|
422 | struct roar_stream_server * s; |
---|
423 | int i; |
---|
424 | int chans; |
---|
425 | |
---|
426 | ROAR_DBG("req_on_set_vol(*) = ?"); |
---|
427 | ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen); |
---|
428 | |
---|
429 | if ( mes->datalen < (4*2) ) |
---|
430 | return -1; |
---|
431 | |
---|
432 | if ( info[0] != 0 ) // version |
---|
433 | return -1; |
---|
434 | |
---|
435 | stream = ROAR_NET2HOST16(info[1]); |
---|
436 | ROAR_DBG("req_on_set_vol(*): stream=%i", stream); |
---|
437 | |
---|
438 | // TODO: change this code. |
---|
439 | // we should not directly change the stream object but use some stream_*()-func |
---|
440 | // for that job. |
---|
441 | |
---|
442 | if ( stream < 0 || stream >= ROAR_STREAMS_MAX ) |
---|
443 | return -1; |
---|
444 | |
---|
445 | s = g_streams[stream]; |
---|
446 | |
---|
447 | if ( s == NULL ) |
---|
448 | return -1; |
---|
449 | |
---|
450 | ROAR_DBG("req_on_set_vol(*): s=%p", s); |
---|
451 | |
---|
452 | info[2] = ROAR_NET2HOST16(info[2]); |
---|
453 | |
---|
454 | if ( info[2] == ROAR_SET_VOL_ALL ) { |
---|
455 | chans = (mes->datalen/2) - 3; |
---|
456 | ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans); |
---|
457 | |
---|
458 | if ( chans >= ROAR_MAX_CHANNELS ) |
---|
459 | return -1; |
---|
460 | |
---|
461 | ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer); |
---|
462 | |
---|
463 | for (i = 0; i < chans; i++) { |
---|
464 | s->mixer.mixer[i] = ROAR_NET2HOST16(info[i+3]); |
---|
465 | ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, ROAR_NET2HOST16(info[i+3])); |
---|
466 | } |
---|
467 | |
---|
468 | ROAR_DBG("req_on_set_vol(*): mixer changed!"); |
---|
469 | |
---|
470 | } else if ( info[2] == ROAR_SET_VOL_ONE ) { |
---|
471 | ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE"); |
---|
472 | if ( ROAR_NET2HOST16(info[3]) >= ROAR_MAX_CHANNELS ) |
---|
473 | return -1; |
---|
474 | |
---|
475 | s->mixer.mixer[ROAR_NET2HOST16(info[3])] = ROAR_NET2HOST16(info[4]); |
---|
476 | } else { |
---|
477 | return -1; |
---|
478 | } |
---|
479 | |
---|
480 | mes->cmd = ROAR_CMD_OK; |
---|
481 | mes->datalen = 0; |
---|
482 | |
---|
483 | return 0; |
---|
484 | } |
---|
485 | |
---|
486 | int req_on_get_vol (int client, struct roar_message * mes, char * data) { |
---|
487 | uint16_t * info = (uint16_t *) mes->data; |
---|
488 | int stream; |
---|
489 | struct roar_stream_server * s; |
---|
490 | int i; |
---|
491 | int chans; |
---|
492 | |
---|
493 | ROAR_DBG("req_on_get_vol(*) = ?"); |
---|
494 | ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen); |
---|
495 | |
---|
496 | if ( mes->datalen < (2*2) ) |
---|
497 | return -1; |
---|
498 | |
---|
499 | if ( info[0] != 0 ) // version |
---|
500 | return -1; |
---|
501 | |
---|
502 | stream = ROAR_NET2HOST16(info[1]); |
---|
503 | ROAR_DBG("req_on_get_vol(*): stream=%i", stream); |
---|
504 | |
---|
505 | // TODO: change this code. |
---|
506 | // we should not directly change the stream object but use some stream_*()-func |
---|
507 | // for that job. |
---|
508 | |
---|
509 | if ( stream < 0 || stream >= ROAR_STREAMS_MAX ) |
---|
510 | return -1; |
---|
511 | |
---|
512 | s = g_streams[stream]; |
---|
513 | |
---|
514 | if ( s == NULL ) |
---|
515 | return -1; |
---|
516 | |
---|
517 | ROAR_DBG("req_on_get_vol(*): s=%p", s); |
---|
518 | |
---|
519 | // ok, we have everything |
---|
520 | |
---|
521 | info[0] = 0; |
---|
522 | info[1] = ROAR_HOST2NET16(chans = ROAR_STREAM(s)->info.channels); |
---|
523 | |
---|
524 | for (i = 0; i < chans; i++) |
---|
525 | info[2+i] = ROAR_HOST2NET16(s->mixer.mixer[i]); |
---|
526 | |
---|
527 | mes->datalen = (2 + chans)*2; |
---|
528 | mes->cmd = ROAR_CMD_OK; |
---|
529 | |
---|
530 | return 0; |
---|
531 | } |
---|
532 | |
---|
533 | int req_on_add_data (int client, struct roar_message * mes, char * data) { |
---|
534 | struct roar_buffer * b; |
---|
535 | char * buf; |
---|
536 | |
---|
537 | if ( roar_buffer_new(&b, mes->datalen) == -1 ) { |
---|
538 | ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!"); |
---|
539 | ROAR_DBG("req_on_add_data(*) = -1"); |
---|
540 | return -1; |
---|
541 | } |
---|
542 | |
---|
543 | roar_buffer_get_data(b, (void **)&buf); |
---|
544 | |
---|
545 | if ( data == NULL ) { |
---|
546 | memcpy(buf, mes->data, mes->datalen); |
---|
547 | } else { |
---|
548 | memcpy(buf, data, mes->datalen); |
---|
549 | } |
---|
550 | |
---|
551 | if ( stream_add_buffer(mes->stream, b) == -1 ) { |
---|
552 | roar_buffer_free(b); |
---|
553 | return -1; |
---|
554 | } |
---|
555 | |
---|
556 | mes->cmd = ROAR_CMD_OK; |
---|
557 | mes->datalen = 0; |
---|
558 | |
---|
559 | return 0; |
---|
560 | } |
---|
561 | |
---|
562 | //ll |
---|