| | 346 | |
|---|
| | 347 | pool = wi_pool_init(wi_pool_alloc()); |
|---|
| | 348 | |
|---|
| | 349 | while(wd_running) { |
|---|
| | 350 | wi_pool_drain(pool); |
|---|
| | 351 | |
|---|
| | 352 | socket = wi_socket_accept_multiple(wd_control_sockets, 30.0, &address); |
|---|
| | 353 | |
|---|
| | 354 | if(!address) { |
|---|
| | 355 | wi_log_err(WI_STR("Could not accept a connection: %m")); |
|---|
| | 356 | |
|---|
| | 357 | continue; |
|---|
| | 358 | } |
|---|
| | 359 | |
|---|
| | 360 | if(!socket) { |
|---|
| | 361 | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), |
|---|
| | 362 | wi_address_string(address)); |
|---|
| | 363 | |
|---|
| | 364 | continue; |
|---|
| | 365 | } |
|---|
| | 366 | |
|---|
| | 367 | if(!wi_thread_create_thread(wd_control_accept_thread, socket)) { |
|---|
| | 368 | wi_log_err(WI_STR("Could not create a thread for %@: %m"), |
|---|
| | 369 | wi_address_string(address)); |
|---|
| | 370 | |
|---|
| | 371 | continue; |
|---|
| | 372 | } |
|---|
| | 373 | } |
|---|
| | 374 | |
|---|
| | 375 | wi_release(pool); |
|---|
| | 376 | } |
|---|
| | 377 | |
|---|
| | 378 | |
|---|
| | 379 | |
|---|
| | 380 | static void wd_control_accept_thread(wi_runtime_instance_t *argument) { |
|---|
| | 381 | wi_pool_t *pool; |
|---|
| | 382 | wi_socket_t *socket = argument; |
|---|
| 348 | | |
|---|
| 349 | | while(wd_running) { |
|---|
| 350 | | wi_pool_drain(pool); |
|---|
| 351 | | |
|---|
| 352 | | /* accept new user */ |
|---|
| 353 | | socket = wi_socket_accept_multiple(wd_control_sockets, 30.0, &address); |
|---|
| 354 | | |
|---|
| 355 | | if(!address) { |
|---|
| 356 | | wi_log_err(WI_STR("Could not accept a connection: %m")); |
|---|
| 357 | | |
|---|
| 358 | | continue; |
|---|
| 359 | | } |
|---|
| 360 | | |
|---|
| 361 | | ip = wi_address_string(address); |
|---|
| 362 | | |
|---|
| 363 | | if(!socket) { |
|---|
| 364 | | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); |
|---|
| 365 | | |
|---|
| 366 | | continue; |
|---|
| 367 | | } |
|---|
| 368 | | |
|---|
| 369 | | if(!wi_socket_accept_tls(socket, wd_control_socket_tls, 30.0)) { |
|---|
| 370 | | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); |
|---|
| 371 | | |
|---|
| 372 | | continue; |
|---|
| 373 | | } |
|---|
| 374 | | |
|---|
| 375 | | wi_socket_set_direction(socket, WI_SOCKET_READ); |
|---|
| 376 | | |
|---|
| 377 | | wi_log_info(WI_STR("Connect from %@"), ip); |
|---|
| 378 | | |
|---|
| 379 | | /* spawn a user thread */ |
|---|
| 380 | | user = wd_user_with_socket(socket); |
|---|
| 381 | | |
|---|
| 382 | | if(!wi_thread_create_thread(wd_control_thread, user)) |
|---|
| 383 | | wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip); |
|---|
| 384 | | } |
|---|
| 385 | | |
|---|
| | 387 | |
|---|
| | 388 | ip = wi_address_string(wi_socket_address(socket)); |
|---|
| | 389 | |
|---|
| | 390 | if(!wi_socket_accept_tls(socket, wd_control_socket_tls, 30.0)) { |
|---|
| | 391 | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); |
|---|
| | 392 | |
|---|
| | 393 | goto end; |
|---|
| | 394 | } |
|---|
| | 395 | |
|---|
| | 396 | wi_socket_set_direction(socket, WI_SOCKET_READ); |
|---|
| | 397 | |
|---|
| | 398 | wi_log_info(WI_STR("Connect from %@"), ip); |
|---|
| | 399 | |
|---|
| | 400 | user = wd_user_with_socket(socket); |
|---|
| | 401 | |
|---|
| | 402 | wd_users_add_user(user); |
|---|
| | 403 | wd_users_set_user_for_thread(user); |
|---|
| | 404 | |
|---|
| | 405 | wd_command_loop_for_user(user); |
|---|
| | 406 | |
|---|
| | 407 | end: |
|---|
| | 417 | |
|---|
| | 418 | pool = wi_pool_init(wi_pool_alloc()); |
|---|
| | 419 | |
|---|
| | 420 | while(wd_running) { |
|---|
| | 421 | wi_pool_drain(pool); |
|---|
| | 422 | |
|---|
| | 423 | socket = wi_socket_accept_multiple(wd_transfer_sockets, 30.0, &address); |
|---|
| | 424 | |
|---|
| | 425 | if(!address) { |
|---|
| | 426 | wi_log_err(WI_STR("Could not accept a connection: %m")); |
|---|
| | 427 | |
|---|
| | 428 | continue; |
|---|
| | 429 | } |
|---|
| | 430 | |
|---|
| | 431 | if(!socket) { |
|---|
| | 432 | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), |
|---|
| | 433 | wi_address_string(address)); |
|---|
| | 434 | |
|---|
| | 435 | continue; |
|---|
| | 436 | } |
|---|
| | 437 | |
|---|
| | 438 | if(!wi_thread_create_thread(wd_transfer_accept_thread, socket)) { |
|---|
| | 439 | wi_log_err(WI_STR("Could not create a thread for %@: %m"), |
|---|
| | 440 | wi_address_string(address)); |
|---|
| | 441 | |
|---|
| | 442 | continue; |
|---|
| | 443 | } |
|---|
| | 444 | } |
|---|
| | 445 | |
|---|
| | 446 | wi_release(pool); |
|---|
| | 447 | } |
|---|
| | 448 | |
|---|
| | 449 | |
|---|
| | 450 | |
|---|
| | 451 | static void wd_transfer_accept_thread(wi_runtime_instance_t *argument) { |
|---|
| | 452 | wi_pool_t *pool; |
|---|
| | 453 | wi_socket_t *socket = argument; |
|---|
| 400 | | |
|---|
| 401 | | while(wd_running) { |
|---|
| 402 | | wi_pool_drain(pool); |
|---|
| 403 | | |
|---|
| 404 | | /* accept new connection */ |
|---|
| 405 | | socket = wi_socket_accept_multiple(wd_transfer_sockets, 30.0, &address); |
|---|
| 406 | | |
|---|
| 407 | | if(!address) { |
|---|
| 408 | | wi_log_err(WI_STR("Could not accept a connection: %m")); |
|---|
| 409 | | |
|---|
| 410 | | continue; |
|---|
| 411 | | } |
|---|
| 412 | | |
|---|
| 413 | | ip = wi_address_string(address); |
|---|
| 414 | | |
|---|
| 415 | | if(!socket) { |
|---|
| 416 | | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); |
|---|
| 417 | | |
|---|
| 418 | | continue; |
|---|
| 419 | | } |
|---|
| 420 | | |
|---|
| 421 | | if(!wi_socket_accept_tls(socket, wd_transfer_socket_tls, 30.0)) { |
|---|
| 422 | | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); |
|---|
| 423 | | |
|---|
| 424 | | continue; |
|---|
| 425 | | } |
|---|
| 426 | | |
|---|
| 427 | | string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR)); |
|---|
| 428 | | |
|---|
| 429 | | if(!string || wi_string_length(string) == 0) { |
|---|
| 430 | | if(!string) |
|---|
| 431 | | wi_log_warn(WI_STR("Could not read from %@: %m"), ip); |
|---|
| 432 | | |
|---|
| 433 | | continue; |
|---|
| 434 | | } |
|---|
| 435 | | |
|---|
| 436 | | /* parse command */ |
|---|
| 437 | | wi_parse_wired_command(string, &command, &arguments); |
|---|
| 438 | | |
|---|
| 439 | | if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) { |
|---|
| 440 | | /* get transfer by identifier */ |
|---|
| 441 | | transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0)); |
|---|
| 442 | | |
|---|
| 443 | | if(!transfer) |
|---|
| 444 | | continue; |
|---|
| 445 | | |
|---|
| 446 | | if(!wi_is_equal(ip, wd_user_ip(transfer->user))) |
|---|
| 447 | | continue; |
|---|
| 448 | | |
|---|
| 449 | | transfer->socket = wi_retain(socket); |
|---|
| 450 | | |
|---|
| 451 | | /* spawn a transfer thread */ |
|---|
| 452 | | if(!wi_thread_create_thread(wd_transfer_thread, transfer)) |
|---|
| 453 | | wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip); |
|---|
| 454 | | } |
|---|
| 455 | | } |
|---|
| 456 | | |
|---|
| | 459 | |
|---|
| | 460 | ip = wi_address_string(wi_socket_address(socket)); |
|---|
| | 461 | |
|---|
| | 462 | if(!wi_socket_accept_tls(socket, wd_transfer_socket_tls, 30.0)) { |
|---|
| | 463 | wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); |
|---|
| | 464 | |
|---|
| | 465 | goto end; |
|---|
| | 466 | } |
|---|
| | 467 | |
|---|
| | 468 | string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR)); |
|---|
| | 469 | |
|---|
| | 470 | if(!string || wi_string_length(string) == 0) { |
|---|
| | 471 | if(!string) |
|---|
| | 472 | wi_log_warn(WI_STR("Could not read from %@: %m"), ip); |
|---|
| | 473 | |
|---|
| | 474 | goto end; |
|---|
| | 475 | } |
|---|
| | 476 | |
|---|
| | 477 | wi_parse_wired_command(string, &command, &arguments); |
|---|
| | 478 | |
|---|
| | 479 | if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) { |
|---|
| | 480 | transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0)); |
|---|
| | 481 | |
|---|
| | 482 | if(!transfer) |
|---|
| | 483 | goto end; |
|---|
| | 484 | |
|---|
| | 485 | if(!wi_is_equal(ip, wd_user_ip(transfer->user))) |
|---|
| | 486 | goto end; |
|---|
| | 487 | |
|---|
| | 488 | transfer->socket = wi_retain(socket); |
|---|
| | 489 | |
|---|
| | 490 | wd_transfer_loop(transfer); |
|---|
| | 491 | } |
|---|
| | 492 | |
|---|
| | 493 | end: |
|---|