Changes in uspace/srv/net/udp/sock.c [6b0b508:695b6ff] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/udp/sock.c
r6b0b508 r695b6ff 1 1 /* 2 2 * Copyright (c) 2008 Lukas Mejdrech 3 * Copyright (c) 201 2Jiri Svoboda3 * Copyright (c) 2013 Jiri Svoboda 4 4 * All rights reserved. 5 5 * … … 159 159 static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 160 160 { 161 int rc;162 struct sockaddr_in *addr;163 size_t addr_size;164 socket_core_t *sock_core;165 udp_sockdata_t *socket;166 udp_sock_t fsock;167 udp_error_t urc;168 169 161 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()"); 170 162 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept"); 171 172 addr= NULL;173 174 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);163 164 struct sockaddr_in6 *addr6 = NULL; 165 size_t addr_len; 166 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len); 175 167 if (rc != EOK) { 176 168 async_answer_0(callid, rc); 177 goto out; 178 } 179 180 if (addr_size != sizeof(struct sockaddr_in)) { 169 return; 170 } 171 172 if ((addr_len != sizeof(struct sockaddr_in)) && 173 (addr_len != sizeof(struct sockaddr_in6))) { 181 174 async_answer_0(callid, EINVAL); 182 175 goto out; 183 176 } 184 177 178 struct sockaddr_in *addr = (struct sockaddr_in *) addr6; 179 185 180 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind"); 181 186 182 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 187 addr , addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,183 addr6, addr_len, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 188 184 last_used_port); 189 185 if (rc != EOK) { … … 193 189 194 190 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find"); 195 sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call)); 191 192 socket_core_t *sock_core = socket_cores_find(&client->sockets, 193 SOCKET_GET_SOCKET_ID(call)); 196 194 if (sock_core == NULL) { 197 195 async_answer_0(callid, ENOENT); 198 196 goto out; 199 197 } 200 201 socket = (udp_sockdata_t *)sock_core->specific_data; 202 203 fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 204 fsock.port = sock_core->port; 205 urc = udp_uc_set_local(socket->assoc, &fsock); 206 198 199 udp_sockdata_t *socket = 200 (udp_sockdata_t *) sock_core->specific_data; 201 202 udp_sock_t fsocket; 203 204 fsocket.port = sock_core->port; 205 206 switch (addr->sin_family) { 207 case AF_INET: 208 inet_sockaddr_in_addr(addr, &fsocket.addr); 209 break; 210 case AF_INET6: 211 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 212 break; 213 default: 214 async_answer_0(callid, EINVAL); 215 goto out; 216 } 217 218 udp_error_t urc = udp_uc_set_local(socket->assoc, &fsocket); 219 207 220 switch (urc) { 208 221 case UDP_EOK: … … 221 234 assert(false); 222 235 } 223 236 224 237 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success"); 225 238 async_answer_0(callid, rc); 239 226 240 out: 227 if (addr != NULL)228 free(addr );241 if (addr6 != NULL) 242 free(addr6); 229 243 } 230 244 … … 251 265 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()"); 252 266 253 struct sockaddr_in *addr = NULL; 254 udp_sock_t fsock; 255 udp_sock_t *fsock_ptr; 267 uint8_t *buffer = calloc(UDP_FRAGMENT_SIZE, 1); 268 if (buffer == NULL) { 269 async_answer_0(callid, ENOMEM); 270 return; 271 } 272 273 struct sockaddr_in6 *addr6 = NULL; 274 struct sockaddr_in *addr; 275 udp_sock_t fsocket; 276 udp_sock_t *fsocket_ptr; 256 277 257 278 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) { 258 size_t addr_ size;259 int rc = async_data_write_accept((void **) &addr , false,260 0, 0, 0, &addr_ size);279 size_t addr_len; 280 int rc = async_data_write_accept((void **) &addr6, false, 281 0, 0, 0, &addr_len); 261 282 if (rc != EOK) { 262 283 async_answer_0(callid, rc); … … 264 285 } 265 286 266 if (addr_size != sizeof(struct sockaddr_in)) { 287 if ((addr_len != sizeof(struct sockaddr_in)) && 288 (addr_len != sizeof(struct sockaddr_in6))) { 267 289 async_answer_0(callid, EINVAL); 268 290 goto out; 269 291 } 270 292 271 fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 272 fsock.port = uint16_t_be2host(addr->sin_port); 273 fsock_ptr = &fsock; 293 addr = (struct sockaddr_in *) addr6; 294 295 switch (addr->sin_family) { 296 case AF_INET: 297 inet_sockaddr_in_addr(addr, &fsocket.addr); 298 break; 299 case AF_INET6: 300 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 301 break; 302 default: 303 async_answer_0(callid, EINVAL); 304 goto out; 305 } 306 307 fsocket.port = uint16_t_be2host(addr->sin_port); 308 fsocket_ptr = &fsocket; 274 309 } else 275 fsock _ptr = NULL;310 fsocket_ptr = NULL; 276 311 277 312 int socket_id = SOCKET_GET_SOCKET_ID(call); … … 314 349 fibril_mutex_lock(&socket->lock); 315 350 316 if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) { 351 if (inet_addr_is_any(&socket->assoc->ident.local.addr) && 352 socket->assoc->ident.iplink == 0) { 317 353 /* Determine local IP address */ 318 inet_addr_t loc_addr, rem_addr; 319 320 rem_addr.ipv4 = fsock_ptr ? fsock.addr.ipv4 : 321 socket->assoc->ident.foreign.addr.ipv4; 354 inet_addr_t loc_addr; 355 inet_addr_t rem_addr; 356 357 rem_addr = fsocket_ptr ? fsocket.addr : 358 socket->assoc->ident.foreign.addr; 322 359 323 360 int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); … … 327 364 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to " 328 365 "determine local address."); 329 return; 330 } 331 332 socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4; 333 log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x", 334 socket->assoc->ident.local.addr.ipv4); 366 goto out; 367 } 368 369 socket->assoc->ident.local.addr = loc_addr; 335 370 } 336 371 … … 351 386 length = UDP_FRAGMENT_SIZE; 352 387 353 uint8_t buffer[UDP_FRAGMENT_SIZE];354 388 int rc = async_data_write_finalize(wcallid, buffer, length); 355 389 if (rc != EOK) { … … 360 394 361 395 udp_error_t urc = 362 udp_uc_send(socket->assoc, fsock _ptr, buffer, length, 0);396 udp_uc_send(socket->assoc, fsocket_ptr, buffer, length, 0); 363 397 364 398 switch (urc) { … … 395 429 396 430 out: 397 if (addr != NULL) 398 free(addr); 431 if (addr6 != NULL) 432 free(addr6); 433 434 free(buffer); 399 435 } 400 436 401 437 static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 402 438 { 403 int socket_id;404 int flags;405 size_t addr_length, length;406 socket_core_t *sock_core;407 udp_sockdata_t *socket;408 ipc_call_t answer;409 ipc_callid_t rcallid;410 size_t data_len;411 udp_error_t urc;412 udp_sock_t rsock;413 struct sockaddr_in addr;414 int rc;415 416 439 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client); 417 418 socket_id = SOCKET_GET_SOCKET_ID(call);419 flags = SOCKET_GET_FLAGS(call);420 421 sock_core =socket_cores_find(&client->sockets, socket_id);440 441 int socket_id = SOCKET_GET_SOCKET_ID(call); 442 443 socket_core_t *sock_core = 444 socket_cores_find(&client->sockets, socket_id); 422 445 if (sock_core == NULL) { 423 446 async_answer_0(callid, ENOTSOCK); 424 447 return; 425 448 } 426 427 socket = (udp_sockdata_t *)sock_core->specific_data; 449 450 udp_sockdata_t *socket = 451 (udp_sockdata_t *) sock_core->specific_data; 452 428 453 fibril_mutex_lock(&socket->lock); 429 454 430 455 if (socket->assoc == NULL) { 431 456 fibril_mutex_unlock(&socket->lock); … … 433 458 return; 434 459 } 435 436 (void)flags; 437 460 438 461 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock"); 462 439 463 fibril_mutex_lock(&socket->recv_buffer_lock); 440 while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) { 464 465 while ((socket->recv_buffer_used == 0) && 466 (socket->recv_error == UDP_EOK)) { 441 467 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv"); 442 468 fibril_condvar_wait(&socket->recv_buffer_cv, 443 469 &socket->recv_buffer_lock); 444 470 } 445 471 446 472 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer"); 447 448 rsock = socket->recv_fsock; 449 data_len = socket->recv_buffer_used; 450 urc = socket->recv_error; 451 473 474 size_t data_len = socket->recv_buffer_used; 475 udp_error_t urc = socket->recv_error; 476 452 477 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len); 453 478 479 int rc; 480 454 481 switch (urc) { 455 482 case UDP_EOK: … … 466 493 assert(false); 467 494 } 468 495 469 496 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc); 497 470 498 if (rc != EOK) { 471 499 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 474 502 return; 475 503 } 476 504 505 ipc_callid_t rcallid; 506 size_t addr_size = 0; 507 477 508 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 478 /* Fill addr */ 479 addr.sin_family = AF_INET; 480 addr.sin_addr.s_addr = host2uint32_t_be(rsock.addr.ipv4); 481 addr.sin_port = host2uint16_t_be(rsock.port); 482 483 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 484 if (!async_data_read_receive(&rcallid, &addr_length)) { 509 /* Fill address */ 510 udp_sock_t *rsock = &socket->recv_fsock; 511 struct sockaddr_in addr; 512 struct sockaddr_in6 addr6; 513 size_t addr_length; 514 515 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr, 516 &addr6); 517 518 switch (addr_af) { 519 case AF_INET: 520 addr.sin_port = host2uint16_t_be(rsock->port); 521 522 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 523 if (!async_data_read_receive(&rcallid, &addr_length)) { 524 fibril_mutex_unlock(&socket->recv_buffer_lock); 525 fibril_mutex_unlock(&socket->lock); 526 async_answer_0(callid, EINVAL); 527 return; 528 } 529 530 if (addr_length > sizeof(addr)) 531 addr_length = sizeof(addr); 532 533 addr_size = sizeof(addr); 534 535 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 536 rc = async_data_read_finalize(rcallid, &addr, addr_length); 537 if (rc != EOK) { 538 fibril_mutex_unlock(&socket->recv_buffer_lock); 539 fibril_mutex_unlock(&socket->lock); 540 async_answer_0(callid, EINVAL); 541 return; 542 } 543 544 break; 545 case AF_INET6: 546 addr6.sin6_port = host2uint16_t_be(rsock->port); 547 548 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive"); 549 if (!async_data_read_receive(&rcallid, &addr_length)) { 550 fibril_mutex_unlock(&socket->recv_buffer_lock); 551 fibril_mutex_unlock(&socket->lock); 552 async_answer_0(callid, EINVAL); 553 return; 554 } 555 556 if (addr_length > sizeof(addr6)) 557 addr_length = sizeof(addr6); 558 559 addr_size = sizeof(addr6); 560 561 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize"); 562 rc = async_data_read_finalize(rcallid, &addr6, addr_length); 563 if (rc != EOK) { 564 fibril_mutex_unlock(&socket->recv_buffer_lock); 565 fibril_mutex_unlock(&socket->lock); 566 async_answer_0(callid, EINVAL); 567 return; 568 } 569 570 break; 571 default: 485 572 fibril_mutex_unlock(&socket->recv_buffer_lock); 486 573 fibril_mutex_unlock(&socket->lock); … … 488 575 return; 489 576 } 490 491 if (addr_length > sizeof(addr)) 492 addr_length = sizeof(addr); 493 494 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 495 rc = async_data_read_finalize(rcallid, &addr, addr_length); 496 if (rc != EOK) { 497 fibril_mutex_unlock(&socket->recv_buffer_lock); 498 fibril_mutex_unlock(&socket->lock); 499 async_answer_0(callid, EINVAL); 500 return; 501 } 502 } 503 577 } 578 504 579 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive"); 580 581 size_t length; 505 582 if (!async_data_read_receive(&rcallid, &length)) { 506 583 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 509 586 return; 510 587 } 511 588 512 589 if (length > data_len) 513 590 length = data_len; 514 591 515 592 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize"); 593 516 594 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 517 518 if ( length < data_len && rc == EOK)595 596 if ((length < data_len) && (rc == EOK)) 519 597 rc = EOVERFLOW; 520 598 521 599 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length); 600 601 ipc_call_t answer; 602 522 603 IPC_SET_ARG2(answer, 0); 523 604 SOCKET_SET_READ_DATA_LENGTH(answer, length); 524 SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));605 SOCKET_SET_ADDRESS_LENGTH(answer, addr_size); 525 606 async_answer_3(callid, EOK, IPC_GET_ARG1(answer), 526 607 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer)); 527 608 528 609 socket->recv_buffer_used = 0; 529 610 530 611 fibril_condvar_broadcast(&socket->recv_buffer_cv); 531 612 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 537 618 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()"); 538 619 int socket_id = SOCKET_GET_SOCKET_ID(call); 539 620 621 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - find core"); 540 622 socket_core_t *sock_core = 541 623 socket_cores_find(&client->sockets, socket_id); 542 624 if (sock_core == NULL) { 625 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - core not found"); 543 626 async_answer_0(callid, ENOTSOCK); 544 627 return; 545 628 } 546 629 630 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - spec data"); 547 631 udp_sockdata_t *socket = 548 632 (udp_sockdata_t *) sock_core->specific_data; 633 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket"); 549 634 fibril_mutex_lock(&socket->lock); 550 635 636 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket buffer"); 637 fibril_mutex_lock(&socket->recv_buffer_lock); 638 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - set socket->sock_core = NULL"); 639 socket->sock_core = NULL; 640 fibril_mutex_unlock(&socket->recv_buffer_lock); 641 642 udp_uc_reset(socket->assoc); 643 551 644 int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock, 552 645 udp_free_sock_data); 553 646 if (rc != EOK) { 647 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - socket_destroy failed"); 554 648 fibril_mutex_unlock(&socket->lock); 555 649 async_answer_0(callid, rc); 556 650 return; 557 651 } 558 652 653 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - broadcast recv_buffer_cv"); 654 fibril_condvar_broadcast(&socket->recv_buffer_cv); 655 559 656 fibril_mutex_unlock(&socket->lock); 560 657 async_answer_0(callid, EOK); … … 569 666 static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 570 667 { 571 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt()"); 572 async_answer_0(callid, ENOTSUP); 573 } 668 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt)"); 669 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept"); 670 671 void *data = NULL; 672 size_t data_len; 673 int rc = async_data_write_accept(&data, false, 0, 0, 0, &data_len); 674 if (rc != EOK) { 675 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed accepting data"); 676 async_answer_0(callid, rc); 677 return; 678 } 679 680 sysarg_t opt_level = SOL_SOCKET; 681 sysarg_t opt_name = SOCKET_GET_OPT_NAME(call); 682 683 if (opt_level != SOL_SOCKET || opt_name != SO_IPLINK || 684 data_len != sizeof(service_id_t)) { 685 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed opt_level/name/len"); 686 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed opt_level=%d, " 687 "opt_name=%d, data_len=%zu", (int)opt_level, (int)opt_name, 688 data_len); 689 async_answer_0(callid, EINVAL); 690 return; 691 } 692 693 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find"); 694 695 socket_core_t *sock_core = socket_cores_find(&client->sockets, 696 SOCKET_GET_SOCKET_ID(call)); 697 if (sock_core == NULL) { 698 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed getting sock_core"); 699 async_answer_0(callid, ENOENT); 700 return; 701 } 702 703 udp_sockdata_t *socket = 704 (udp_sockdata_t *) sock_core->specific_data; 705 706 service_id_t iplink = *(service_id_t *)data; 707 udp_uc_set_iplink(socket->assoc, iplink); 708 709 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success"); 710 async_answer_0(callid, EOK); 711 } 712 574 713 575 714 static int udp_sock_recv_fibril(void *arg) … … 582 721 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()"); 583 722 723 fibril_mutex_lock(&sock->recv_buffer_lock); 724 584 725 while (true) { 585 726 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()"); 586 fibril_mutex_lock(&sock->recv_buffer_lock); 587 while (sock->recv_buffer_used != 0) { 727 while ((sock->recv_buffer_used != 0) && (sock->sock_core != NULL)) { 588 728 fibril_condvar_wait(&sock->recv_buffer_cv, 589 729 &sock->recv_buffer_lock); 590 730 } 591 731 732 fibril_mutex_unlock(&sock->recv_buffer_lock); 733 592 734 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()"); 593 735 urc = udp_uc_receive(sock->assoc, sock->recv_buffer, 594 736 UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock); 737 fibril_mutex_lock(&sock->recv_buffer_lock); 595 738 sock->recv_error = urc; 596 597 udp_sock_notify_data(sock->sock_core); 598 739 740 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] udp_uc_receive -> %d", urc); 741 742 if (sock->sock_core != NULL) 743 udp_sock_notify_data(sock->sock_core); 744 599 745 if (urc != UDP_EOK) { 746 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break"); 600 747 fibril_condvar_broadcast(&sock->recv_buffer_cv); 601 748 fibril_mutex_unlock(&sock->recv_buffer_lock); 602 749 break; 603 750 } 604 751 605 752 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv"); 606 753 607 754 sock->recv_buffer_used = rcvd; 608 fibril_mutex_unlock(&sock->recv_buffer_lock);609 755 fibril_condvar_broadcast(&sock->recv_buffer_cv); 610 756 } 611 757 758 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() exited loop"); 612 759 udp_uc_destroy(sock->assoc); 760 761 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() terminated"); 613 762 614 763 return 0; … … 623 772 /* Accept the connection */ 624 773 async_answer_0(iid, EOK); 774 775 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: begin"); 625 776 626 777 client.sess = async_callback_receive(EXCHANGE_SERIALIZE);
Note:
See TracChangeset
for help on using the changeset viewer.