Changes in uspace/srv/net/tcp/sock.c [05bfce7:b243da3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tcp/sock.c
r05bfce7 rb243da3 179 179 } 180 180 181 sock->laddr.ipv4 = TCP_IPV4_ANY;181 inet_addr_any(&sock->laddr); 182 182 sock->lconn = NULL; 183 183 sock->backlog = 0; … … 314 314 log_msg(LOG_DEFAULT, LVL_DEBUG, " - open connections"); 315 315 316 lsocket.addr.ipv4 = TCP_IPV4_ANY;316 inet_addr_any(&lsocket.addr); 317 317 lsocket.port = sock_core->port; 318 fsocket.addr.ipv4 = TCP_IPV4_ANY; 318 319 inet_addr_any(&fsocket.addr); 319 320 fsocket.port = TCP_PORT_ANY; 320 321 … … 353 354 } 354 355 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; 356 static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, 357 ipc_call_t call) 358 { 359 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()"); 360 361 struct sockaddr_in6 *addr6 = NULL; 360 362 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 367 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()"); 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)) { 363 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len); 364 if (rc != EOK) { 371 365 async_answer_0(callid, rc); 372 366 return; 373 367 } 374 375 socket_id = SOCKET_GET_SOCKET_ID(call); 376 377 sock_core = socket_cores_find(&client->sockets, socket_id); 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); 378 380 if (sock_core == NULL) { 379 381 async_answer_0(callid, ENOTSOCK); 380 return; 381 } 382 383 socket = (tcp_sockdata_t *)sock_core->specific_data; 382 goto out; 383 } 384 385 tcp_sockdata_t *socket = 386 (tcp_sockdata_t *) sock_core->specific_data; 387 384 388 if (sock_core->port <= 0) { 385 389 rc = socket_bind_free_port(&gsock, sock_core, … … 388 392 if (rc != EOK) { 389 393 async_answer_0(callid, rc); 390 return;394 goto out; 391 395 } 392 396 393 397 last_used_port = sock_core->port; 394 398 } 395 399 396 400 fibril_mutex_lock(&socket->lock); 397 398 if ( socket->laddr.ipv4 == TCP_IPV4_ANY) {401 402 if (inet_addr_is_any(&socket->laddr)) { 399 403 /* Determine local IP address */ 400 inet_addr_t loc_addr, rem_addr; 401 402 rem_addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 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 403 420 rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); 404 421 if (rc != EOK) { … … 407 424 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect: Failed to " 408 425 "determine local address."); 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; 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; 417 436 lsocket.port = sock_core->port; 418 fsocket.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 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 419 451 fsocket.port = uint16_t_be2host(addr->sin_port); 420 421 trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, &socket->conn); 422 452 453 tcp_error_t trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, 454 &socket->conn); 455 423 456 if (socket->conn != NULL) 424 socket->conn->name = (char *) "C";425 457 socket->conn->name = (char *) "C"; 458 426 459 fibril_mutex_unlock(&socket->lock); 427 460 428 461 switch (trc) { 429 462 case TCP_EOK: … … 436 469 assert(false); 437 470 } 438 471 439 472 if (rc == EOK) 440 473 fibril_add_ready(socket->recv_fibril); 441 474 442 475 async_answer_0(callid, rc); 476 477 out: 478 if (addr6 != NULL) 479 free(addr6); 443 480 } 444 481 … … 507 544 /* Replenish listening connection */ 508 545 509 lsocket.addr.ipv4 = TCP_IPV4_ANY;546 inet_addr_any(&lsocket.addr); 510 547 lsocket.port = sock_core->port; 511 fsocket.addr.ipv4 = TCP_IPV4_ANY; 548 549 inet_addr_any(&fsocket.addr); 512 550 fsocket.port = TCP_PORT_ANY; 513 551 … … 575 613 ipc_callid_t wcallid; 576 614 size_t length; 577 uint8_t buffer[TCP_SOCK_FRAGMENT_SIZE];578 615 tcp_error_t trc; 579 616 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 } 580 623 581 624 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_send()"); … … 587 630 if (sock_core == NULL) { 588 631 async_answer_0(callid, ENOTSOCK); 589 return;632 goto out; 590 633 } 591 634 … … 603 646 fibril_mutex_unlock(&socket->lock); 604 647 async_answer_0(callid, EINVAL); 605 return;648 goto out; 606 649 } 607 650 … … 613 656 fibril_mutex_unlock(&socket->lock); 614 657 async_answer_0(callid, rc); 615 return;658 goto out; 616 659 } 617 660 … … 638 681 fibril_mutex_unlock(&socket->lock); 639 682 async_answer_0(callid, rc); 640 return;683 goto out; 641 684 } 642 685 } … … 647 690 IPC_GET_ARG2(answer)); 648 691 fibril_mutex_unlock(&socket->lock); 692 693 out: 694 free(buffer); 649 695 } 650 696 … … 657 703 static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 658 704 { 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 671 705 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: tcp_sock_recv[from]()", client); 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);706 707 int socket_id = SOCKET_GET_SOCKET_ID(call); 708 709 socket_core_t *sock_core = 710 socket_cores_find(&client->sockets, socket_id); 677 711 if (sock_core == NULL) { 678 712 async_answer_0(callid, ENOTSOCK); 679 713 return; 680 714 } 681 682 socket = (tcp_sockdata_t *)sock_core->specific_data; 715 716 tcp_sockdata_t *socket = 717 (tcp_sockdata_t *) sock_core->specific_data; 718 683 719 fibril_mutex_lock(&socket->lock); 684 720 685 721 if (socket->conn == NULL) { 686 722 fibril_mutex_unlock(&socket->lock); … … 688 724 return; 689 725 } 690 691 (void)flags; 692 726 693 727 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom(): lock recv_buffer_lock"); 728 694 729 fibril_mutex_lock(&socket->recv_buffer_lock); 695 while (socket->recv_buffer_used == 0 && socket->recv_error == TCP_EOK) { 730 while ((socket->recv_buffer_used == 0) && 731 (socket->recv_error == TCP_EOK)) { 696 732 log_msg(LOG_DEFAULT, LVL_DEBUG, "wait for recv_buffer_cv + recv_buffer_used != 0"); 697 733 fibril_condvar_wait(&socket->recv_buffer_cv, 698 734 &socket->recv_buffer_lock); 699 735 } 700 736 701 737 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer"); 702 703 data_len = socket->recv_buffer_used; 704 rc = socket->recv_error; 705 706 switch (socket->recv_error) { 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) { 707 744 case TCP_EOK: 708 745 rc = EOK; … … 718 755 assert(false); 719 756 } 720 757 721 758 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv result -> %d", rc); 759 722 760 if (rc != EOK) { 723 761 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 726 764 return; 727 765 } 728 766 767 ipc_callid_t rcallid; 768 729 769 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 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)) { 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: 738 829 fibril_mutex_unlock(&socket->recv_buffer_lock); 739 830 fibril_mutex_unlock(&socket->lock); … … 741 832 return; 742 833 } 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 834 } 835 757 836 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive"); 837 838 size_t length; 758 839 if (!async_data_read_receive(&rcallid, &length)) { 759 840 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 762 843 return; 763 844 } 764 845 765 846 if (length > data_len) 766 847 length = data_len; 767 848 768 849 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize"); 850 769 851 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 770 852 771 853 socket->recv_buffer_used -= length; 854 772 855 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom: %zu left in buffer", 773 856 socket->recv_buffer_used); 857 774 858 if (socket->recv_buffer_used > 0) { 775 859 memmove(socket->recv_buffer, socket->recv_buffer + length, … … 777 861 tcp_sock_notify_data(socket->sock_core); 778 862 } 779 863 780 864 fibril_condvar_broadcast(&socket->recv_buffer_cv); 781 782 if ( length < data_len && rc == EOK)865 866 if ((length < data_len) && (rc == EOK)) 783 867 rc = EOVERFLOW; 784 868 869 ipc_call_t answer; 870 785 871 SOCKET_SET_READ_DATA_LENGTH(answer, length); 786 872 async_answer_1(callid, EOK, IPC_GET_ARG1(answer)); 787 873 788 874 fibril_mutex_unlock(&socket->recv_buffer_lock); 789 875 fibril_mutex_unlock(&socket->lock); … … 796 882 tcp_sockdata_t *socket; 797 883 tcp_error_t trc; 884 int i; 798 885 int rc; 799 886 … … 811 898 812 899 if (socket->conn != NULL) { 900 /* Close connection */ 813 901 trc = tcp_uc_close(socket->conn); 814 902 if (trc != TCP_EOK && trc != TCP_ENOTEXIST) { … … 819 907 } 820 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 } 923 } 924 821 925 /* Grab recv_buffer_lock because of CV wait in tcp_sock_recv_fibril() */ 822 926 fibril_mutex_lock(&socket->recv_buffer_lock);
Note:
See TracChangeset
for help on using the changeset viewer.