Changeset 0b81cad0 in mainline for uspace/srv/net/tl/udp/udp.c
- Timestamp:
- 2010-11-06T00:19:13Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1712f87
- Parents:
- 0485135 (diff), 0578271 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/udp/udp.c
r0485135 r0b81cad0 51 51 #include <adt/dynamic_fifo.h> 52 52 #include <errno.h> 53 #include <err.h>54 53 55 54 #include <net/socket_codes.h> … … 103 102 int udp_initialize(async_client_conn_t client_connection) 104 103 { 105 ERROR_DECLARE;106 107 104 measured_string_t names[] = { 108 105 { … … 118 115 size_t count = sizeof(names) / sizeof(measured_string_t); 119 116 char *data; 117 int rc; 120 118 121 119 fibril_rwlock_initialize(&udp_globals.lock); … … 124 122 udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 125 123 ICMP_CONNECT_TIMEOUT); 124 126 125 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, 127 126 SERVICE_UDP, client_connection); 128 if (udp_globals.ip_phone < 0) 127 if (udp_globals.ip_phone < 0) { 128 fibril_rwlock_write_unlock(&udp_globals.lock); 129 129 return udp_globals.ip_phone; 130 } 130 131 131 132 // read default packet dimensions 132 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, 133 &udp_globals.packet_dimension)); 134 ERROR_PROPAGATE(socket_ports_initialize(&udp_globals.sockets)); 135 if (ERROR_OCCURRED(packet_dimensions_initialize( 136 &udp_globals.dimensions))) { 133 rc = ip_packet_size_req(udp_globals.ip_phone, -1, 134 &udp_globals.packet_dimension); 135 if (rc != EOK) { 136 fibril_rwlock_write_unlock(&udp_globals.lock); 137 return rc; 138 } 139 140 rc = socket_ports_initialize(&udp_globals.sockets); 141 if (rc != EOK) { 142 fibril_rwlock_write_unlock(&udp_globals.lock); 143 return rc; 144 } 145 146 rc = packet_dimensions_initialize(&udp_globals.dimensions); 147 if (rc != EOK) { 137 148 socket_ports_destroy(&udp_globals.sockets); 138 return ERROR_CODE; 139 } 149 fibril_rwlock_write_unlock(&udp_globals.lock); 150 return rc; 151 } 152 140 153 udp_globals.packet_dimension.prefix += sizeof(udp_header_t); 141 154 udp_globals.packet_dimension.content -= sizeof(udp_header_t); … … 147 160 // get configuration 148 161 configuration = &names[0]; 149 ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, 150 count, &data)); 162 rc = net_get_conf_req(udp_globals.net_phone, &configuration, count, 163 &data); 164 if (rc != EOK) { 165 socket_ports_destroy(&udp_globals.sockets); 166 fibril_rwlock_write_unlock(&udp_globals.lock); 167 return rc; 168 } 169 151 170 if (configuration) { 152 171 if (configuration[0].value) … … 201 220 udp_process_packet(device_id_t device_id, packet_t packet, services_t error) 202 221 { 203 ERROR_DECLARE;204 205 222 size_t length; 206 223 size_t offset; … … 219 236 struct sockaddr *dest; 220 237 packet_dimension_ref packet_dimension; 238 int rc; 221 239 222 240 switch (error) { … … 232 250 return udp_release_and_return(packet, result); 233 251 length = (size_t) result; 234 if (ERROR_OCCURRED(packet_trim(packet, length, 0)))235 return udp_release_and_return(packet,236 ERROR_CODE);252 rc = packet_trim(packet, length, 0); 253 if (rc != EOK) 254 return udp_release_and_return(packet, rc); 237 255 break; 238 256 default: … … 253 271 254 272 // trim all but UDP header 255 if (ERROR_OCCURRED(packet_trim(packet, offset, 0))) 256 return udp_release_and_return(packet, ERROR_CODE); 273 rc = packet_trim(packet, offset, 0); 274 if (rc != EOK) 275 return udp_release_and_return(packet, rc); 257 276 258 277 // get udp header … … 284 303 if (result <= 0) 285 304 return udp_release_and_return(packet, result); 286 287 if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP,288 src, result, dest, result, total_length, &ip_header,289 &length))) {290 return udp_release_and_return(packet, ERROR_CODE);305 306 rc = ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest, 307 result, total_length, &ip_header, &length); 308 if (rc != EOK) { 309 return udp_release_and_return(packet, rc); 291 310 } else { 292 311 checksum = compute_checksum(0, ip_header, length); … … 307 326 308 327 if (total_length < length) { 309 if (ERROR_OCCURRED(packet_trim(next_packet, 0, 310 length - total_length))) { 311 return udp_release_and_return(packet, 312 ERROR_CODE); 313 } 328 rc = packet_trim(next_packet, 0, length - total_length); 329 if (rc != EOK) 330 return udp_release_and_return(packet, rc); 314 331 315 332 // add partial checksum if set … … 360 377 361 378 // queue the received packet 362 if (ERROR_OCCURRED(dyn_fifo_push(&socket->received, 363 packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) || 364 ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone, 365 &udp_globals.dimensions, device_id, &packet_dimension))) { 366 return udp_release_and_return(packet, ERROR_CODE); 367 } 379 rc = dyn_fifo_push(&socket->received, packet_get_id(packet), 380 SOCKET_MAX_RECEIVED_SIZE); 381 if (rc != EOK) 382 return udp_release_and_return(packet, rc); 383 384 rc = tl_get_ip_packet_dimension(udp_globals.ip_phone, 385 &udp_globals.dimensions, device_id, &packet_dimension); 386 if (rc != EOK) 387 return udp_release_and_return(packet, rc); 368 388 369 389 // notify the destination socket … … 436 456 size_t *data_fragment_size, int flags) 437 457 { 438 ERROR_DECLARE;439 440 458 socket_core_ref socket; 441 459 packet_t packet; … … 451 469 device_id_t device_id; 452 470 packet_dimension_ref packet_dimension; 453 454 ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &dest_port)); 471 int rc; 472 473 rc = tl_get_address_port(addr, addrlen, &dest_port); 474 if (rc != EOK) 475 return rc; 455 476 456 477 socket = socket_cores_find(local_sockets, socket_id); … … 460 481 if ((socket->port <= 0) && udp_globals.autobinding) { 461 482 // bind the socket to a random free port if not bound 462 // do { 463 // try to find a free port 464 // fibril_rwlock_read_unlock(&udp_globals.lock); 465 // fibril_rwlock_write_lock(&udp_globals.lock); 466 // might be changed in the meantime 467 // if (socket->port <= 0) { 468 if (ERROR_OCCURRED(socket_bind_free_port( 469 &udp_globals.sockets, socket, 470 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 471 udp_globals.last_used_port))) { 472 // fibril_rwlock_write_unlock( 473 // &udp_globals.lock); 474 // fibril_rwlock_read_lock( 475 // &udp_globals.lock); 476 return ERROR_CODE; 477 } 478 // set the next port as the search starting port 479 // number 480 udp_globals.last_used_port = socket->port; 481 // } 482 // fibril_rwlock_write_unlock(&udp_globals.lock); 483 // fibril_rwlock_read_lock(&udp_globals.lock); 484 // might be changed in the meantime 485 // } while (socket->port <= 0); 483 rc = socket_bind_free_port(&udp_globals.sockets, socket, 484 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 485 udp_globals.last_used_port); 486 if (rc != EOK) 487 return rc; 488 // set the next port as the search starting port number 489 udp_globals.last_used_port = socket->port; 486 490 } 487 491 488 492 if (udp_globals.checksum_computing) { 489 if (ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone, 490 IPPROTO_UDP, addr, addrlen, &device_id, &ip_header, 491 &headerlen))) { 492 return udp_release_and_return(packet, ERROR_CODE); 493 } 493 rc = ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr, 494 addrlen, &device_id, &ip_header, &headerlen); 495 if (rc != EOK) 496 return rc; 494 497 // get the device packet dimension 495 // ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, 496 // &udp_globals.dimensions, device_id, &packet_dimension)); 498 // rc = tl_get_ip_packet_dimension(udp_globals.ip_phone, 499 // &udp_globals.dimensions, device_id, &packet_dimension); 500 // if (rc != EOK) 501 // return rc; 497 502 } 498 503 // } else { 499 504 // do not ask all the time 500 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, 501 &udp_globals.packet_dimension)); 505 rc = ip_packet_size_req(udp_globals.ip_phone, -1, 506 &udp_globals.packet_dimension); 507 if (rc != EOK) 508 return rc; 502 509 packet_dimension = &udp_globals.packet_dimension; 503 510 // } … … 529 536 return udp_release_and_return(packet, result); 530 537 531 if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))) 532 return udp_release_and_return(packet, ERROR_CODE); 538 rc = pq_add(&packet, next_packet, index, 0); 539 if (rc != EOK) 540 return udp_release_and_return(packet, rc); 533 541 534 542 total_length += (size_t) result; … … 547 555 if (udp_globals.checksum_computing) { 548 556 // update the pseudo header 549 if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length( 550 ip_header, headerlen, total_length + UDP_HEADER_SIZE))) { 557 rc = ip_client_set_pseudo_header_data_length(ip_header, 558 headerlen, total_length + UDP_HEADER_SIZE); 559 if (rc != EOK) { 551 560 free(ip_header); 552 return udp_release_and_return(packet, ERROR_CODE);561 return udp_release_and_return(packet, rc); 553 562 } 554 563 … … 565 574 566 575 // prepare the first packet fragment 567 if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 568 0, 0))) { 569 return udp_release_and_return(packet, ERROR_CODE); 570 } 576 rc = ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0); 577 if (rc != EOK) 578 return udp_release_and_return(packet, rc); 579 580 /* Release the UDP global lock on success. */ 581 fibril_rwlock_write_unlock(&udp_globals.lock); 571 582 572 583 // send the packet 573 fibril_rwlock_write_unlock(&udp_globals.lock);574 584 ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0); 575 585 … … 600 610 size_t *addrlen) 601 611 { 602 ERROR_DECLARE;603 604 612 socket_core_ref socket; 605 613 int packet_id; … … 610 618 uint8_t *data; 611 619 int result; 620 int rc; 612 621 613 622 // find the socket … … 620 629 if (packet_id < 0) 621 630 return NO_DATA; 622 623 ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, 624 packet_id)); 631 632 rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id); 633 if (rc != EOK) { 634 (void) dyn_fifo_pop(&socket->received); 635 return rc; 636 } 625 637 626 638 // get udp header 627 639 data = packet_get_data(packet); 628 640 if (!data) { 629 pq_release_remote(udp_globals.net_phone, packet_id);630 return NO_DATA;641 (void) dyn_fifo_pop(&socket->received); 642 return udp_release_and_return(packet, NO_DATA); 631 643 } 632 644 header = (udp_header_ref) data; … … 634 646 // set the source address port 635 647 result = packet_get_addr(packet, (uint8_t **) &addr, NULL); 636 if (ERROR_OCCURRED(tl_set_address_port(addr, result,637 ntohs(header->source_port)))) {638 pq_release_remote(udp_globals.net_phone, packet_id);639 return ERROR_CODE;648 rc = tl_set_address_port(addr, result, ntohs(header->source_port)); 649 if (rc != EOK) { 650 (void) dyn_fifo_pop(&socket->received); 651 return udp_release_and_return(packet, rc); 640 652 } 641 653 *addrlen = (size_t) result; 642 654 643 655 // send the source address 644 ERROR_PROPAGATE(data_reply(addr, *addrlen)); 656 rc = data_reply(addr, *addrlen); 657 switch (rc) { 658 case EOK: 659 break; 660 case EOVERFLOW: 661 return rc; 662 default: 663 (void) dyn_fifo_pop(&socket->received); 664 return udp_release_and_return(packet, rc); 665 } 645 666 646 667 // trim the header 647 ERROR_PROPAGATE(packet_trim(packet, UDP_HEADER_SIZE, 0)); 668 rc = packet_trim(packet, UDP_HEADER_SIZE, 0); 669 if (rc != EOK) { 670 (void) dyn_fifo_pop(&socket->received); 671 return udp_release_and_return(packet, rc); 672 } 648 673 649 674 // reply the packets 650 ERROR_PROPAGATE(socket_reply_packets(packet, &length)); 651 652 // release the packet 653 dyn_fifo_pop(&socket->received); 654 pq_release_remote(udp_globals.net_phone, packet_get_id(packet)); 655 656 // return the total length 657 return (int) length; 675 rc = socket_reply_packets(packet, &length); 676 switch (rc) { 677 case EOK: 678 break; 679 case EOVERFLOW: 680 return rc; 681 default: 682 (void) dyn_fifo_pop(&socket->received); 683 return udp_release_and_return(packet, rc); 684 } 685 686 (void) dyn_fifo_pop(&socket->received); 687 688 // release the packet and return the total length 689 return udp_release_and_return(packet, (int) length); 658 690 } 659 691 … … 826 858 ipc_call_t *answer, int *answer_count) 827 859 { 828 ERROR_DECLARE;829 830 860 packet_t packet; 861 int rc; 831 862 832 863 *answer_count = 0; … … 834 865 switch (IPC_GET_METHOD(*call)) { 835 866 case NET_TL_RECEIVED: 836 if (ERROR_NONE(packet_translate_remote(udp_globals.net_phone, 837 &packet, IPC_GET_PACKET(call)))) { 838 ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call), 839 packet, SERVICE_UDP, IPC_GET_ERROR(call)); 840 } 841 return ERROR_CODE; 842 867 rc = packet_translate_remote(udp_globals.net_phone, &packet, 868 IPC_GET_PACKET(call)); 869 if (rc != EOK) 870 return rc; 871 return udp_received_msg(IPC_GET_DEVICE(call), packet, 872 SERVICE_UDP, IPC_GET_ERROR(call)); 843 873 case IPC_M_CONNECT_TO_ME: 844 874 return udp_process_client_messages(callid, * call); … … 850 880 /** Default thread for new connections. 851 881 * 852 * @param[in] iid The initial message identifier. 853 * @param[in] icall The initial message call structure. 854 * 882 * @param[in] iid The initial message identifier. 883 * @param[in] icall The initial message call structure. 855 884 */ 856 885 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall) … … 898 927 int main(int argc, char *argv[]) 899 928 { 900 ERROR_DECLARE;929 int rc; 901 930 902 931 /* Start the module */ 903 if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection))) 904 return ERROR_CODE; 905 906 return EOK; 932 rc = tl_module_start_standalone(tl_client_connection); 933 return rc; 907 934 } 908 935
Note:
See TracChangeset
for help on using the changeset viewer.