Changes in uspace/srv/net/tcp/sock.c [b243da3:05bfce7] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tcp/sock.c
rb243da3 r05bfce7 179 179 } 180 180 181 inet_addr_any(&sock->laddr);181 sock->laddr.ipv4 = TCP_IPV4_ANY; 182 182 sock->lconn = NULL; 183 183 sock->backlog = 0; … … 314 314 log_msg(LOG_DEFAULT, LVL_DEBUG, " - open connections"); 315 315 316 inet_addr_any(&lsocket.addr);316 lsocket.addr.ipv4 = TCP_IPV4_ANY; 317 317 lsocket.port = sock_core->port; 318 319 inet_addr_any(&fsocket.addr); 318 fsocket.addr.ipv4 = TCP_IPV4_ANY; 320 319 fsocket.port = TCP_PORT_ANY; 321 320 … … 354 353 } 355 354 356 static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, 357 ipc_call_t call) 358 { 355 static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 356 { 357 int rc; 358 struct sockaddr_in *addr; 359 int socket_id; 360 size_t addr_len; 361 socket_core_t *sock_core; 362 tcp_sockdata_t *socket; 363 tcp_error_t trc; 364 tcp_sock_t lsocket; 365 tcp_sock_t fsocket; 366 359 367 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()"); 360 361 struct sockaddr_in6 *addr6 = NULL; 362 size_t addr_len; 363 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len); 364 if (rc != EOK) { 368 369 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len); 370 if (rc != EOK || addr_len != sizeof(struct sockaddr_in)) { 365 371 async_answer_0(callid, rc); 366 372 return; 367 373 } 368 369 if ((addr_len != sizeof(struct sockaddr_in)) && 370 (addr_len != sizeof(struct sockaddr_in6))) { 371 async_answer_0(callid, EINVAL); 372 goto out; 373 } 374 375 struct sockaddr_in *addr = (struct sockaddr_in *) addr6; 376 377 int socket_id = SOCKET_GET_SOCKET_ID(call); 378 socket_core_t *sock_core = socket_cores_find(&client->sockets, 379 socket_id); 374 375 socket_id = SOCKET_GET_SOCKET_ID(call); 376 377 sock_core = socket_cores_find(&client->sockets, socket_id); 380 378 if (sock_core == NULL) { 381 379 async_answer_0(callid, ENOTSOCK); 382 goto out; 383 } 384 385 tcp_sockdata_t *socket = 386 (tcp_sockdata_t *) sock_core->specific_data; 387 380 return; 381 } 382 383 socket = (tcp_sockdata_t *)sock_core->specific_data; 388 384 if (sock_core->port <= 0) { 389 385 rc = socket_bind_free_port(&gsock, sock_core, … … 392 388 if (rc != EOK) { 393 389 async_answer_0(callid, rc); 394 goto out;390 return; 395 391 } 396 392 397 393 last_used_port = sock_core->port; 398 394 } 399 395 400 396 fibril_mutex_lock(&socket->lock); 401 402 if ( inet_addr_is_any(&socket->laddr)) {397 398 if (socket->laddr.ipv4 == TCP_IPV4_ANY) { 403 399 /* Determine local IP address */ 404 inet_addr_t loc_addr; 405 inet_addr_t rem_addr; 406 407 switch (addr->sin_family) { 408 case AF_INET: 409 inet_sockaddr_in_addr(addr, &rem_addr); 410 break; 411 case AF_INET6: 412 inet_sockaddr_in6_addr(addr6, &rem_addr); 413 break; 414 default: 415 fibril_mutex_unlock(&socket->lock); 416 async_answer_0(callid, EINVAL); 417 goto out; 418 } 419 400 inet_addr_t loc_addr, rem_addr; 401 402 rem_addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 420 403 rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); 421 404 if (rc != EOK) { … … 424 407 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect: Failed to " 425 408 "determine local address."); 426 goto out; 427 } 428 429 socket->laddr = loc_addr; 430 } 431 432 tcp_sock_t lsocket; 433 tcp_sock_t fsocket; 434 435 lsocket.addr = socket->laddr; 409 return; 410 } 411 412 socket->laddr.ipv4 = loc_addr.ipv4; 413 log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x", socket->laddr.ipv4); 414 } 415 416 lsocket.addr.ipv4 = socket->laddr.ipv4; 436 417 lsocket.port = sock_core->port; 437 438 switch (addr->sin_family) { 439 case AF_INET: 440 inet_sockaddr_in_addr(addr, &fsocket.addr); 441 break; 442 case AF_INET6: 443 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 444 break; 445 default: 446 fibril_mutex_unlock(&socket->lock); 447 async_answer_0(callid, EINVAL); 448 goto out; 449 } 450 418 fsocket.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 451 419 fsocket.port = uint16_t_be2host(addr->sin_port); 452 453 tcp_error_t trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, 454 &socket->conn); 455 420 421 trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, &socket->conn); 422 456 423 if (socket->conn != NULL) 457 socket->conn->name = (char *) 458 424 socket->conn->name = (char *)"C"; 425 459 426 fibril_mutex_unlock(&socket->lock); 460 427 461 428 switch (trc) { 462 429 case TCP_EOK: … … 469 436 assert(false); 470 437 } 471 438 472 439 if (rc == EOK) 473 440 fibril_add_ready(socket->recv_fibril); 474 441 475 442 async_answer_0(callid, rc); 476 477 out:478 if (addr6 != NULL)479 free(addr6);480 443 } 481 444 … … 544 507 /* Replenish listening connection */ 545 508 546 inet_addr_any(&lsocket.addr);509 lsocket.addr.ipv4 = TCP_IPV4_ANY; 547 510 lsocket.port = sock_core->port; 548 549 inet_addr_any(&fsocket.addr); 511 fsocket.addr.ipv4 = TCP_IPV4_ANY; 550 512 fsocket.port = TCP_PORT_ANY; 551 513 … … 613 575 ipc_callid_t wcallid; 614 576 size_t length; 577 uint8_t buffer[TCP_SOCK_FRAGMENT_SIZE]; 615 578 tcp_error_t trc; 616 579 int rc; 617 618 uint8_t *buffer = calloc(TCP_SOCK_FRAGMENT_SIZE, 1);619 if (buffer == NULL) {620 async_answer_0(callid, ENOMEM);621 return;622 }623 580 624 581 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_send()"); … … 630 587 if (sock_core == NULL) { 631 588 async_answer_0(callid, ENOTSOCK); 632 goto out;589 return; 633 590 } 634 591 … … 646 603 fibril_mutex_unlock(&socket->lock); 647 604 async_answer_0(callid, EINVAL); 648 goto out;605 return; 649 606 } 650 607 … … 656 613 fibril_mutex_unlock(&socket->lock); 657 614 async_answer_0(callid, rc); 658 goto out;615 return; 659 616 } 660 617 … … 681 638 fibril_mutex_unlock(&socket->lock); 682 639 async_answer_0(callid, rc); 683 goto out;640 return; 684 641 } 685 642 } … … 690 647 IPC_GET_ARG2(answer)); 691 648 fibril_mutex_unlock(&socket->lock); 692 693 out:694 free(buffer);695 649 } 696 650 … … 703 657 static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 704 658 { 659 int socket_id; 660 int flags; 661 size_t addr_length, length; 662 socket_core_t *sock_core; 663 tcp_sockdata_t *socket; 664 ipc_call_t answer; 665 ipc_callid_t rcallid; 666 size_t data_len; 667 struct sockaddr_in addr; 668 tcp_sock_t *rsock; 669 int rc; 670 705 671 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: tcp_sock_recv[from]()", client); 706 707 intsocket_id = SOCKET_GET_SOCKET_ID(call);708 709 socket_core_t *sock_core = 710 672 673 socket_id = SOCKET_GET_SOCKET_ID(call); 674 flags = SOCKET_GET_FLAGS(call); 675 676 sock_core = socket_cores_find(&client->sockets, socket_id); 711 677 if (sock_core == NULL) { 712 678 async_answer_0(callid, ENOTSOCK); 713 679 return; 714 680 } 715 716 tcp_sockdata_t *socket = 717 (tcp_sockdata_t *) sock_core->specific_data; 718 681 682 socket = (tcp_sockdata_t *)sock_core->specific_data; 719 683 fibril_mutex_lock(&socket->lock); 720 684 721 685 if (socket->conn == NULL) { 722 686 fibril_mutex_unlock(&socket->lock); … … 724 688 return; 725 689 } 726 690 691 (void)flags; 692 727 693 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom(): lock recv_buffer_lock"); 728 729 694 fibril_mutex_lock(&socket->recv_buffer_lock); 730 while ((socket->recv_buffer_used == 0) && 731 (socket->recv_error == TCP_EOK)) { 695 while (socket->recv_buffer_used == 0 && socket->recv_error == TCP_EOK) { 732 696 log_msg(LOG_DEFAULT, LVL_DEBUG, "wait for recv_buffer_cv + recv_buffer_used != 0"); 733 697 fibril_condvar_wait(&socket->recv_buffer_cv, 734 698 &socket->recv_buffer_lock); 735 699 } 736 700 737 701 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer"); 738 739 size_t data_len = socket->recv_buffer_used; 740 tcp_error_t trc = socket->recv_error; 741 int rc; 742 743 switch (trc) { 702 703 data_len = socket->recv_buffer_used; 704 rc = socket->recv_error; 705 706 switch (socket->recv_error) { 744 707 case TCP_EOK: 745 708 rc = EOK; … … 755 718 assert(false); 756 719 } 757 720 758 721 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv result -> %d", rc); 759 760 722 if (rc != EOK) { 761 723 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 764 726 return; 765 727 } 766 767 ipc_callid_t rcallid; 768 728 769 729 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 770 /* Fill address */ 771 tcp_sock_t *rsock = &socket->conn->ident.foreign; 772 struct sockaddr_in addr; 773 struct sockaddr_in6 addr6; 774 size_t addr_length; 775 776 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr, 777 &addr6); 778 779 switch (addr_af) { 780 case AF_INET: 781 addr.sin_port = host2uint16_t_be(rsock->port); 782 783 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 784 if (!async_data_read_receive(&rcallid, &addr_length)) { 785 fibril_mutex_unlock(&socket->recv_buffer_lock); 786 fibril_mutex_unlock(&socket->lock); 787 async_answer_0(callid, EINVAL); 788 return; 789 } 790 791 if (addr_length > sizeof(addr)) 792 addr_length = sizeof(addr); 793 794 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 795 rc = async_data_read_finalize(rcallid, &addr, addr_length); 796 if (rc != EOK) { 797 fibril_mutex_unlock(&socket->recv_buffer_lock); 798 fibril_mutex_unlock(&socket->lock); 799 async_answer_0(callid, EINVAL); 800 return; 801 } 802 803 break; 804 case AF_INET6: 805 addr6.sin6_port = host2uint16_t_be(rsock->port); 806 807 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive"); 808 if (!async_data_read_receive(&rcallid, &addr_length)) { 809 fibril_mutex_unlock(&socket->recv_buffer_lock); 810 fibril_mutex_unlock(&socket->lock); 811 async_answer_0(callid, EINVAL); 812 return; 813 } 814 815 if (addr_length > sizeof(addr6)) 816 addr_length = sizeof(addr6); 817 818 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize"); 819 rc = async_data_read_finalize(rcallid, &addr6, addr_length); 820 if (rc != EOK) { 821 fibril_mutex_unlock(&socket->recv_buffer_lock); 822 fibril_mutex_unlock(&socket->lock); 823 async_answer_0(callid, EINVAL); 824 return; 825 } 826 827 break; 828 default: 730 /* Fill addr */ 731 rsock = &socket->conn->ident.foreign; 732 addr.sin_family = AF_INET; 733 addr.sin_addr.s_addr = host2uint32_t_be(rsock->addr.ipv4); 734 addr.sin_port = host2uint16_t_be(rsock->port); 735 736 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 737 if (!async_data_read_receive(&rcallid, &addr_length)) { 829 738 fibril_mutex_unlock(&socket->recv_buffer_lock); 830 739 fibril_mutex_unlock(&socket->lock); … … 832 741 return; 833 742 } 834 } 835 743 744 if (addr_length > sizeof(addr)) 745 addr_length = sizeof(addr); 746 747 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 748 rc = async_data_read_finalize(rcallid, &addr, addr_length); 749 if (rc != EOK) { 750 fibril_mutex_unlock(&socket->recv_buffer_lock); 751 fibril_mutex_unlock(&socket->lock); 752 async_answer_0(callid, EINVAL); 753 return; 754 } 755 } 756 836 757 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive"); 837 838 size_t length;839 758 if (!async_data_read_receive(&rcallid, &length)) { 840 759 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 843 762 return; 844 763 } 845 764 846 765 if (length > data_len) 847 766 length = data_len; 848 767 849 768 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize"); 850 851 769 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 852 770 853 771 socket->recv_buffer_used -= length; 854 855 772 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom: %zu left in buffer", 856 773 socket->recv_buffer_used); 857 858 774 if (socket->recv_buffer_used > 0) { 859 775 memmove(socket->recv_buffer, socket->recv_buffer + length, … … 861 777 tcp_sock_notify_data(socket->sock_core); 862 778 } 863 779 864 780 fibril_condvar_broadcast(&socket->recv_buffer_cv); 865 866 if ( (length < data_len) && (rc == EOK))781 782 if (length < data_len && rc == EOK) 867 783 rc = EOVERFLOW; 868 869 ipc_call_t answer; 870 784 871 785 SOCKET_SET_READ_DATA_LENGTH(answer, length); 872 786 async_answer_1(callid, EOK, IPC_GET_ARG1(answer)); 873 787 874 788 fibril_mutex_unlock(&socket->recv_buffer_lock); 875 789 fibril_mutex_unlock(&socket->lock); … … 882 796 tcp_sockdata_t *socket; 883 797 tcp_error_t trc; 884 int i;885 798 int rc; 886 799 … … 898 811 899 812 if (socket->conn != NULL) { 900 /* Close connection */901 813 trc = tcp_uc_close(socket->conn); 902 814 if (trc != TCP_EOK && trc != TCP_ENOTEXIST) { … … 904 816 async_answer_0(callid, EBADF); 905 817 return; 906 }907 }908 909 if (socket->lconn != NULL) {910 /* Close listening connections */911 for (i = 0; i < socket->backlog; i++) {912 tcp_uc_set_cstate_cb(socket->lconn[i]->conn, NULL, NULL);913 trc = tcp_uc_close(socket->lconn[i]->conn);914 if (trc != TCP_EOK && trc != TCP_ENOTEXIST) {915 fibril_mutex_unlock(&socket->lock);916 async_answer_0(callid, EBADF);917 return;918 }919 920 free(socket->lconn[i]);921 socket->lconn[i] = NULL;922 818 } 923 819 }
Note:
See TracChangeset
for help on using the changeset viewer.