[0] | 1 | //clients.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 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, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
| 27 | int clients_init (void) { |
---|
| 28 | int i; |
---|
| 29 | |
---|
| 30 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 31 | g_clients[i] = NULL; |
---|
| 32 | |
---|
| 33 | return 0; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | int clients_free (void) { |
---|
| 37 | int i; |
---|
| 38 | |
---|
| 39 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 40 | if ( g_clients[i] ) |
---|
| 41 | clients_delete(i); |
---|
| 42 | |
---|
| 43 | return 0; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | int clients_new (void) { |
---|
| 47 | int i; |
---|
| 48 | int s; |
---|
| 49 | struct roar_client * n; |
---|
| 50 | |
---|
| 51 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 52 | if ( g_clients[i] == NULL ) { |
---|
| 53 | n = malloc(sizeof(struct roar_client)); |
---|
| 54 | if ( n != NULL ) { |
---|
| 55 | n->pid = -1; |
---|
[437] | 56 | n->uid = -1; |
---|
| 57 | n->gid = -1; |
---|
[0] | 58 | n->fh = -1; |
---|
| 59 | |
---|
| 60 | *n->name = 0; |
---|
| 61 | *n->host = 0; |
---|
| 62 | |
---|
[346] | 63 | n->acl = NULL; |
---|
| 64 | |
---|
[0] | 65 | n->execed = -1; |
---|
| 66 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
| 67 | n->streams[s] = -1; |
---|
| 68 | |
---|
| 69 | g_clients[i] = n; |
---|
| 70 | |
---|
| 71 | ROAR_DBG("clients_new(void) = %i", i); |
---|
| 72 | return i; |
---|
| 73 | } else { |
---|
| 74 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
| 75 | ROAR_ERR("clients_new(void) = -1"); |
---|
| 76 | return -1; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | return -1; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int clients_delete (int id) { |
---|
| 85 | int i; |
---|
| 86 | |
---|
| 87 | if ( g_clients[id] == NULL ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
| 90 | if (g_clients[id]->execed != -1) { |
---|
| 91 | // return streams_delete(g_clients[id]->execed); |
---|
| 92 | g_clients[id]->execed = -1; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 96 | streams_delete(g_clients[id]->streams[i]); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if ( g_clients[id]->fh != -1 ) |
---|
| 100 | close(g_clients[id]->fh); |
---|
| 101 | |
---|
| 102 | free(g_clients[id]); |
---|
| 103 | g_clients[id] = NULL; |
---|
| 104 | |
---|
| 105 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
| 106 | return 0; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | int clients_get (int id, struct roar_client ** client) { |
---|
| 110 | *client = g_clients[id]; |
---|
| 111 | |
---|
| 112 | if ( *client == NULL ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | return 0; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | int clients_set_fh (int id, int fh) { |
---|
[501] | 119 | |
---|
[0] | 120 | if ( g_clients[id] == NULL ) |
---|
| 121 | return -1; |
---|
| 122 | |
---|
| 123 | g_clients[id]->fh = fh; |
---|
| 124 | |
---|
| 125 | return 0; |
---|
| 126 | } |
---|
| 127 | |
---|
[755] | 128 | int clients_get_fh (int id) { |
---|
| 129 | if ( g_clients[id] == NULL ) |
---|
| 130 | return -1; |
---|
| 131 | |
---|
| 132 | return g_clients[id]->fh; |
---|
| 133 | } |
---|
| 134 | |
---|
[0] | 135 | int clients_set_pid (int id, int pid) { |
---|
| 136 | if ( g_clients[id] == NULL ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
| 139 | g_clients[id]->pid = pid; |
---|
| 140 | |
---|
| 141 | return 0; |
---|
| 142 | } |
---|
| 143 | |
---|
[439] | 144 | int clients_set_uid (int id, int uid) { |
---|
| 145 | if ( g_clients[id] == NULL ) |
---|
| 146 | return -1; |
---|
| 147 | |
---|
| 148 | g_clients[id]->uid = uid; |
---|
| 149 | |
---|
| 150 | return 0; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | int clients_set_gid (int id, int gid) { |
---|
| 154 | if ( g_clients[id] == NULL ) |
---|
| 155 | return -1; |
---|
| 156 | |
---|
| 157 | g_clients[id]->gid = gid; |
---|
| 158 | |
---|
| 159 | return 0; |
---|
| 160 | } |
---|
| 161 | |
---|
[498] | 162 | #define MAX_STREAMLESS 8 |
---|
| 163 | |
---|
[0] | 164 | int clients_check_all (void) { |
---|
| 165 | struct timeval tv; |
---|
| 166 | fd_set r, e; |
---|
[66] | 167 | int i, j; |
---|
[0] | 168 | int ret; |
---|
| 169 | int fh; |
---|
| 170 | int max_fh = -1; |
---|
[71] | 171 | int have = 0; |
---|
[498] | 172 | struct { |
---|
| 173 | int id; |
---|
| 174 | int fh; |
---|
| 175 | } streamless[MAX_STREAMLESS]; |
---|
| 176 | int have_streamless = 0; |
---|
| 177 | int have_stream; |
---|
[0] | 178 | |
---|
| 179 | FD_ZERO(&r); |
---|
| 180 | FD_ZERO(&e); |
---|
| 181 | |
---|
| 182 | tv.tv_sec = 0; |
---|
| 183 | tv.tv_usec = 1; |
---|
| 184 | |
---|
| 185 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 186 | if ( g_clients[i] == NULL ) |
---|
| 187 | continue; |
---|
| 188 | |
---|
[66] | 189 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
[71] | 190 | have++; |
---|
| 191 | |
---|
[610] | 192 | ROAR_DBG("clients_check_all(*): fh=%i", fh); |
---|
| 193 | |
---|
[66] | 194 | FD_SET(fh, &r); |
---|
| 195 | FD_SET(fh, &e); |
---|
| 196 | |
---|
| 197 | if ( fh > max_fh ) |
---|
| 198 | max_fh = fh; |
---|
[84] | 199 | } |
---|
[0] | 200 | |
---|
[498] | 201 | have_stream = 0; |
---|
| 202 | |
---|
[84] | 203 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 204 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 205 | FD_SET(fh, &r); |
---|
[0] | 206 | |
---|
[84] | 207 | if ( fh > max_fh ) |
---|
| 208 | max_fh = fh; |
---|
[498] | 209 | |
---|
| 210 | have_stream = 1; |
---|
[66] | 211 | } |
---|
[84] | 212 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
[66] | 213 | } |
---|
| 214 | |
---|
[498] | 215 | if ( !have_stream && have_streamless < MAX_STREAMLESS ) { |
---|
| 216 | streamless[have_streamless ].id = i; |
---|
| 217 | if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 ) |
---|
| 218 | have_streamless--; |
---|
| 219 | } |
---|
[0] | 220 | } |
---|
| 221 | |
---|
[256] | 222 | if ( max_fh == -1 ) |
---|
| 223 | return 0; |
---|
| 224 | |
---|
[0] | 225 | if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) { |
---|
[71] | 226 | return ret < 0 ? ret : have; |
---|
[0] | 227 | } |
---|
| 228 | |
---|
| 229 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 230 | if ( g_clients[i] == NULL ) |
---|
| 231 | continue; |
---|
| 232 | |
---|
[66] | 233 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
| 234 | if ( FD_ISSET(fh, &r) ) { |
---|
| 235 | if ( g_clients[i]->execed == -1 ) { |
---|
| 236 | clients_check(i); |
---|
[255] | 237 | /* |
---|
[66] | 238 | } else { |
---|
| 239 | streams_check(g_clients[i]->execed); |
---|
[255] | 240 | */ |
---|
[66] | 241 | } |
---|
| 242 | } |
---|
[0] | 243 | |
---|
[84] | 244 | if ( FD_ISSET(fh, &e) ) { |
---|
[66] | 245 | clients_delete(i); |
---|
[84] | 246 | continue; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | if ( g_clients[i] == NULL ) |
---|
| 251 | continue; |
---|
| 252 | |
---|
| 253 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 254 | //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]); |
---|
[136] | 255 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
| 256 | break; |
---|
[84] | 257 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 258 | if ( FD_ISSET(fh, &r) ) { |
---|
| 259 | streams_check(g_clients[i]->streams[j]); |
---|
[66] | 260 | } |
---|
[0] | 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[498] | 265 | if ( have_streamless ) { |
---|
| 266 | FD_ZERO(&r); |
---|
| 267 | |
---|
| 268 | tv.tv_sec = 0; |
---|
| 269 | tv.tv_usec = 1; |
---|
| 270 | |
---|
| 271 | max_fh = -1; |
---|
| 272 | |
---|
| 273 | for (i = 0; i < have_streamless; i++) { |
---|
[607] | 274 | if ( ! g_clients[j = streamless[i].id] ) |
---|
| 275 | continue; |
---|
| 276 | |
---|
| 277 | if ( g_clients[j]->execed != -1 ) |
---|
[601] | 278 | continue; |
---|
| 279 | |
---|
[498] | 280 | fh = streamless[i].fh; |
---|
| 281 | |
---|
| 282 | ROAR_DBG("clients_check_all(void): fh=%i", fh); |
---|
| 283 | FD_SET(fh, &r); |
---|
| 284 | |
---|
| 285 | if ( fh > max_fh ) |
---|
| 286 | max_fh = fh; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) { |
---|
| 290 | return ret; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | for (i = 0; i < have_streamless; i++) { |
---|
| 294 | if ( FD_ISSET(streamless[i].fh, &r) ) { |
---|
| 295 | clients_check(streamless[i].id); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
[71] | 300 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
| 301 | return have; |
---|
[0] | 302 | } |
---|
| 303 | |
---|
| 304 | int clients_check (int id) { |
---|
| 305 | struct roar_message m; |
---|
| 306 | struct roar_connection con; |
---|
| 307 | char * data = NULL; |
---|
| 308 | int oldcmd; |
---|
| 309 | int r; |
---|
[498] | 310 | int rv = 0; |
---|
[0] | 311 | |
---|
| 312 | if ( g_clients[id] == NULL ) |
---|
| 313 | return -1; |
---|
| 314 | if ( g_clients[id]->fh == -1 ) |
---|
| 315 | return -1; |
---|
| 316 | |
---|
| 317 | con.fh = g_clients[id]->fh; |
---|
| 318 | |
---|
| 319 | r = roar_recv_message(&con, &m, &data); |
---|
| 320 | |
---|
| 321 | if ( r == -1 ) { // should we drop the client? |
---|
| 322 | clients_delete(id); |
---|
| 323 | return -1; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | roar_debug_message_print(&m); |
---|
| 327 | |
---|
| 328 | oldcmd = m.cmd; |
---|
| 329 | |
---|
| 330 | if ( (r = command_exec(id, &m, data)) == -1 ) { |
---|
| 331 | m.cmd = ROAR_CMD_ERROR; |
---|
| 332 | m.datalen = 0; |
---|
| 333 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
| 334 | } else { |
---|
| 335 | if ( m.cmd == oldcmd ) { |
---|
| 336 | m.cmd = ROAR_CMD_OK; |
---|
| 337 | m.datalen = 0; |
---|
[498] | 338 | } else if ( m.cmd == ROAR_CMD_OK_STOP ) { |
---|
| 339 | m.cmd = ROAR_CMD_OK; |
---|
| 340 | rv = 1; |
---|
[0] | 341 | } |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | roar_send_message(&con, &m, NULL); |
---|
| 345 | |
---|
| 346 | if ( data ) |
---|
| 347 | free(data); |
---|
| 348 | |
---|
| 349 | ROAR_DBG("clients_check(id=%i) = 0", id); |
---|
[498] | 350 | return rv; |
---|
[0] | 351 | } |
---|
| 352 | |
---|
| 353 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
| 354 | int i; |
---|
[934] | 355 | // int fh; |
---|
| 356 | int j; |
---|
[0] | 357 | |
---|
| 358 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 359 | if ( g_clients[i] == NULL ) |
---|
| 360 | continue; |
---|
| 361 | |
---|
[934] | 362 | /* |
---|
[0] | 363 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 364 | continue; |
---|
[934] | 365 | */ |
---|
[0] | 366 | |
---|
| 367 | if ( g_clients[i]->execed == -1 ) { |
---|
| 368 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
[934] | 369 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 370 | //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 371 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]); |
---|
| 372 | if ( g_clients[i]->streams[j] != -1 ) |
---|
| 373 | streams_send_mon(g_clients[i]->streams[j]); |
---|
| 374 | } |
---|
[0] | 375 | } else { |
---|
| 376 | // streams_check(g_clients[i]->execed); |
---|
| 377 | streams_send_mon(g_clients[i]->execed); |
---|
| 378 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 379 | // clients_delete(i); // delete client in case we could not write |
---|
| 380 | } |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | return -1; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
| 387 | int i; |
---|
| 388 | int fh; |
---|
| 389 | |
---|
| 390 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 391 | if ( g_clients[i] == NULL ) |
---|
| 392 | continue; |
---|
| 393 | |
---|
| 394 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 395 | continue; |
---|
| 396 | |
---|
| 397 | if ( g_clients[i]->execed == -1 ) { |
---|
| 398 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 399 | } else { |
---|
| 400 | // streams_check(g_clients[i]->execed); |
---|
| 401 | streams_send_filter(g_clients[i]->execed); |
---|
| 402 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 403 | // clients_delete(i); // delete client in case we could not write |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | return -1; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | int client_stream_exec (int client, int stream) { |
---|
| 411 | int i; |
---|
| 412 | |
---|
| 413 | if ( g_clients[client] == NULL ) |
---|
| 414 | return -1; |
---|
| 415 | |
---|
| 416 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 417 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 418 | g_clients[client]->execed = stream; |
---|
| 419 | streams_set_fh(stream, g_clients[client]->fh); |
---|
[378] | 420 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
[0] | 421 | return 0; |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | return -1; |
---|
| 426 | } |
---|
| 427 | |
---|
[78] | 428 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
| 429 | int i; |
---|
| 430 | |
---|
| 431 | if ( g_clients[client] == NULL ) |
---|
| 432 | return -1; |
---|
| 433 | |
---|
| 434 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 435 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 436 | streams_set_fh(stream, fh); |
---|
| 437 | return 0; |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | return -1; |
---|
| 442 | } |
---|
| 443 | |
---|
[0] | 444 | int client_stream_add (int client, int stream) { |
---|
| 445 | int i; |
---|
| 446 | |
---|
| 447 | if ( g_clients[client] == NULL ) |
---|
| 448 | return -1; |
---|
| 449 | |
---|
| 450 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 451 | if ( g_clients[client]->streams[i] == -1 ) { |
---|
| 452 | g_clients[client]->streams[i] = stream; |
---|
| 453 | streams_set_client(stream, client); |
---|
| 454 | return 0; |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | return -1; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | int client_stream_delete (int client, int stream) { |
---|
| 462 | int i; |
---|
| 463 | |
---|
| 464 | if ( g_clients[client] == NULL ) |
---|
| 465 | return -1; |
---|
| 466 | |
---|
| 467 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 468 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 469 | g_clients[client]->streams[i] = -1; |
---|
| 470 | |
---|
| 471 | if ( stream == g_clients[client]->execed ) { |
---|
| 472 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
| 473 | clients_delete(client); |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
| 477 | return 0; |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
| 482 | return -1; |
---|
| 483 | } |
---|
| 484 | |
---|
[767] | 485 | int client_stream_move (int client, int stream) { |
---|
| 486 | int old_client = streams_get_client(stream); |
---|
| 487 | |
---|
[771] | 488 | ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client); |
---|
| 489 | |
---|
[767] | 490 | if ( old_client != -1 ) |
---|
| 491 | if ( client_stream_delete(old_client, stream) == -1 ) |
---|
| 492 | return -1; |
---|
| 493 | |
---|
| 494 | return client_stream_add(client, stream); |
---|
| 495 | } |
---|
| 496 | |
---|
[0] | 497 | //ll |
---|