Changes in uspace/srv/net/tl/udp/udp.c [a63ff7d:065d2d5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/udp/udp.c
ra63ff7d r065d2d5 51 51 #include <adt/dynamic_fifo.h> 52 52 #include <errno.h> 53 #include <err.h> 53 54 54 55 #include <net/socket_codes.h> … … 102 103 int udp_initialize(async_client_conn_t client_connection) 103 104 { 105 ERROR_DECLARE; 106 104 107 measured_string_t names[] = { 105 108 { … … 115 118 size_t count = sizeof(names) / sizeof(measured_string_t); 116 119 char *data; 117 int rc;118 120 119 121 fibril_rwlock_initialize(&udp_globals.lock); … … 122 124 udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 123 125 ICMP_CONNECT_TIMEOUT); 124 125 126 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, 126 127 SERVICE_UDP, client_connection); 127 if (udp_globals.ip_phone < 0) { 128 fibril_rwlock_write_unlock(&udp_globals.lock); 128 if (udp_globals.ip_phone < 0) 129 129 return udp_globals.ip_phone; 130 }131 130 132 131 // read default packet 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) { 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))) { 148 137 socket_ports_destroy(&udp_globals.sockets); 149 fibril_rwlock_write_unlock(&udp_globals.lock); 150 return rc; 151 } 152 138 return ERROR_CODE; 139 } 153 140 udp_globals.packet_dimension.prefix += sizeof(udp_header_t); 154 141 udp_globals.packet_dimension.content -= sizeof(udp_header_t); … … 160 147 // get configuration 161 148 configuration = &names[0]; 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 149 ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, 150 count, &data)); 170 151 if (configuration) { 171 152 if (configuration[0].value) … … 220 201 udp_process_packet(device_id_t device_id, packet_t packet, services_t error) 221 202 { 203 ERROR_DECLARE; 204 222 205 size_t length; 223 206 size_t offset; … … 236 219 struct sockaddr *dest; 237 220 packet_dimension_ref packet_dimension; 238 int rc;239 221 240 222 switch (error) { … … 250 232 return udp_release_and_return(packet, result); 251 233 length = (size_t) result; 252 rc = packet_trim(packet, length, 0);253 if (rc != EOK)254 return udp_release_and_return(packet, rc);234 if (ERROR_OCCURRED(packet_trim(packet, length, 0))) 235 return udp_release_and_return(packet, 236 ERROR_CODE); 255 237 break; 256 238 default: … … 271 253 272 254 // trim all but UDP header 273 rc = packet_trim(packet, offset, 0); 274 if (rc != EOK) 275 return udp_release_and_return(packet, rc); 255 if (ERROR_OCCURRED(packet_trim(packet, offset, 0))) 256 return udp_release_and_return(packet, ERROR_CODE); 276 257 277 258 // get udp header … … 303 284 if (result <= 0) 304 285 return udp_release_and_return(packet, result); 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);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); 310 291 } else { 311 292 checksum = compute_checksum(0, ip_header, length); … … 326 307 327 308 if (total_length < length) { 328 rc = packet_trim(next_packet, 0, length - total_length); 329 if (rc != EOK) 330 return udp_release_and_return(packet, rc); 309 if (ERROR_OCCURRED(packet_trim(next_packet, 0, 310 length - total_length))) { 311 return udp_release_and_return(packet, 312 ERROR_CODE); 313 } 331 314 332 315 // add partial checksum if set … … 377 360 378 361 // queue the received packet 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); 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 } 388 368 389 369 // notify the destination socket … … 456 436 size_t *data_fragment_size, int flags) 457 437 { 438 ERROR_DECLARE; 439 458 440 socket_core_ref socket; 459 441 packet_t packet; … … 469 451 device_id_t device_id; 470 452 packet_dimension_ref packet_dimension; 471 int rc; 472 473 rc = tl_get_address_port(addr, addrlen, &dest_port); 474 if (rc != EOK) 475 return rc; 453 454 ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &dest_port)); 476 455 477 456 socket = socket_cores_find(local_sockets, socket_id); … … 481 460 if ((socket->port <= 0) && udp_globals.autobinding) { 482 461 // bind the socket to a random free port if not bound 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; 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); 490 486 } 491 487 492 488 if (udp_globals.checksum_computing) { 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; 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 } 497 494 // get the device 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; 495 // ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, 496 // &udp_globals.dimensions, device_id, &packet_dimension)); 502 497 } 503 498 // } else { 504 499 // do not ask all the time 505 rc = ip_packet_size_req(udp_globals.ip_phone, -1, 506 &udp_globals.packet_dimension); 507 if (rc != EOK) 508 return rc; 500 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, 501 &udp_globals.packet_dimension)); 509 502 packet_dimension = &udp_globals.packet_dimension; 510 503 // } … … 536 529 return udp_release_and_return(packet, result); 537 530 538 rc = pq_add(&packet, next_packet, index, 0); 539 if (rc != EOK) 540 return udp_release_and_return(packet, rc); 531 if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))) 532 return udp_release_and_return(packet, ERROR_CODE); 541 533 542 534 total_length += (size_t) result; … … 555 547 if (udp_globals.checksum_computing) { 556 548 // update the pseudo header 557 rc = ip_client_set_pseudo_header_data_length(ip_header, 558 headerlen, total_length + UDP_HEADER_SIZE); 559 if (rc != EOK) { 549 if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length( 550 ip_header, headerlen, total_length + UDP_HEADER_SIZE))) { 560 551 free(ip_header); 561 return udp_release_and_return(packet, rc);552 return udp_release_and_return(packet, ERROR_CODE); 562 553 } 563 554 … … 574 565 575 566 // prepare the first packet fragment 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. */ 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 } 571 572 // send the packet 581 573 fibril_rwlock_write_unlock(&udp_globals.lock); 582 583 // send the packet584 574 ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0); 585 575 … … 610 600 size_t *addrlen) 611 601 { 602 ERROR_DECLARE; 603 612 604 socket_core_ref socket; 613 605 int packet_id; … … 618 610 uint8_t *data; 619 611 int result; 620 int rc;621 612 622 613 // find the socket … … 626 617 627 618 // get the next received packet 628 packet_id = dyn_fifo_ pop(&socket->received);619 packet_id = dyn_fifo_value(&socket->received); 629 620 if (packet_id < 0) 630 621 return NO_DATA; 631 632 rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id); 633 if (rc != EOK) 634 return rc; 622 623 ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, 624 packet_id)); 635 625 636 626 // get udp header 637 627 data = packet_get_data(packet); 638 if (!data) 639 return udp_release_and_return(packet, NO_DATA); 628 if (!data) { 629 pq_release_remote(udp_globals.net_phone, packet_id); 630 return NO_DATA; 631 } 640 632 header = (udp_header_ref) data; 641 633 642 634 // set the source address port 643 635 result = packet_get_addr(packet, (uint8_t **) &addr, NULL); 644 rc = tl_set_address_port(addr, result, ntohs(header->source_port)); 645 if (rc != EOK) 646 return udp_release_and_return(packet, rc); 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; 640 } 647 641 *addrlen = (size_t) result; 648 642 649 643 // send the source address 650 rc = data_reply(addr, *addrlen); 651 if (rc != EOK) 652 return udp_release_and_return(packet, rc); 644 ERROR_PROPAGATE(data_reply(addr, *addrlen)); 653 645 654 646 // trim the header 655 rc = packet_trim(packet, UDP_HEADER_SIZE, 0); 656 if (rc != EOK) 657 return udp_release_and_return(packet, rc); 647 ERROR_PROPAGATE(packet_trim(packet, UDP_HEADER_SIZE, 0)); 658 648 659 649 // reply the packets 660 rc = socket_reply_packets(packet, &length); 661 if (rc != EOK) 662 return udp_release_and_return(packet, rc); 663 664 // release the packet and return the total length 665 return udp_release_and_return(packet, (int) length); 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; 666 658 } 667 659 … … 834 826 ipc_call_t *answer, int *answer_count) 835 827 { 828 ERROR_DECLARE; 829 836 830 packet_t packet; 837 int rc;838 831 839 832 *answer_count = 0; … … 841 834 switch (IPC_GET_METHOD(*call)) { 842 835 case NET_TL_RECEIVED: 843 rc = packet_translate_remote(udp_globals.net_phone, &packet, 844 IPC_GET_PACKET(call)); 845 if (rc != EOK) 846 return rc; 847 return udp_received_msg(IPC_GET_DEVICE(call), packet, 848 SERVICE_UDP, IPC_GET_ERROR(call)); 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 849 843 case IPC_M_CONNECT_TO_ME: 850 844 return udp_process_client_messages(callid, * call); … … 856 850 /** Default thread for new connections. 857 851 * 858 * @param[in] iid The initial message identifier. 859 * @param[in] icall The initial message call structure. 852 * @param[in] iid The initial message identifier. 853 * @param[in] icall The initial message call structure. 854 * 860 855 */ 861 856 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall) … … 903 898 int main(int argc, char *argv[]) 904 899 { 905 int rc;900 ERROR_DECLARE; 906 901 907 902 /* Start the module */ 908 rc = tl_module_start_standalone(tl_client_connection); 909 return rc; 903 if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection))) 904 return ERROR_CODE; 905 906 return EOK; 910 907 } 911 908
Note:
See TracChangeset
for help on using the changeset viewer.