Changes in uspace/lib/net/tl/socket_core.c [61bfc370:6b82009] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/tl/socket_core.c
r61bfc370 r6b82009 38 38 #include <packet_client.h> 39 39 #include <packet_remote.h> 40 41 40 #include <net/socket_codes.h> 42 41 #include <net/in.h> … … 44 43 #include <net/packet.h> 45 44 #include <net/modules.h> 46 47 45 #include <stdint.h> 48 46 #include <stdlib.h> 49 47 #include <errno.h> 50 51 48 #include <adt/dynamic_fifo.h> 52 49 #include <adt/int_map.h> … … 56 53 * switching to the sequence. 57 54 */ 58 #define SOCKET_ID_TRIES 55 #define SOCKET_ID_TRIES 100 59 56 60 57 /** Bound port sockets.*/ … … 72 69 INT_MAP_IMPLEMENT(socket_ports, socket_port_t); 73 70 74 /** Destroy sthe socket.71 /** Destroy the socket. 75 72 * 76 73 * If the socket is bound, the port is released. 77 * Release s all buffered packets, calls the release function and removesthe74 * Release all buffered packets, call the release function and remove the 78 75 * socket from the local sockets. 79 76 * 80 * @param[in] packet_phone The packet server phone to release buffered packets.81 * @param[in] socket The socket to be destroyed.82 * @param[in,out] local_sockets The local sockets to be updated.83 * @param[in,out] global_sockets The global sockets to be updated.84 * @param[in] socket_release The client release callback function.85 * /86 static void 87 s ocket_destroy_core(int packet_phone, socket_core_t *socket,77 * @param[in] sess Packet server session. 78 * @param[in] socket Socket to be destroyed. 79 * @param[in,out] local_sockets Local sockets to be updated. 80 * @param[in,out] global_sockets Global sockets to be updated. 81 * @param[in] socket_release Client release callback function. 82 * 83 */ 84 static void socket_destroy_core(async_sess_t *sess, socket_core_t *socket, 88 85 socket_cores_t *local_sockets, socket_ports_t *global_sockets, 89 86 void (* socket_release)(socket_core_t *socket)) 90 87 { 88 /* If bound */ 89 if (socket->port) { 90 /* Release the port */ 91 socket_port_release(global_sockets, socket); 92 } 93 94 /* Release all received packets */ 91 95 int packet_id; 92 93 // if bound94 if (socket->port) {95 // release the port96 socket_port_release(global_sockets, socket);97 }98 99 // release all received packets100 96 while ((packet_id = dyn_fifo_pop(&socket->received)) >= 0) 101 pq_release_remote( packet_phone, packet_id);102 97 pq_release_remote(sess, packet_id); 98 103 99 dyn_fifo_destroy(&socket->received); 104 100 dyn_fifo_destroy(&socket->accepted); 105 101 106 102 if (socket_release) 107 103 socket_release(socket); 108 109 socket_cores_exclude(local_sockets, socket->socket_id );110 } 111 112 /** Destroy slocal sockets.113 * 114 * Release s all buffered packets and callsthe release function for each of the104 105 socket_cores_exclude(local_sockets, socket->socket_id, free); 106 } 107 108 /** Destroy local sockets. 109 * 110 * Release all buffered packets and call the release function for each of the 115 111 * sockets. 116 112 * 117 * @param[in] packet_phone The packet server phone to release buffered packets.118 * @param[in] local_sockets The local sockets to be destroyed.119 * @param[in,out] global_sockets The global sockets to be updated.120 * @param[in] socket_release The client release callback function.121 * /122 void 123 socket_cores_release(int packet_phone, socket_cores_t *local_sockets,113 * @param[in] sess Packet server session. 114 * @param[in] local_sockets Local sockets to be destroyed. 115 * @param[in,out] global_sockets Global sockets to be updated. 116 * @param[in] socket_release Client release callback function. 117 * 118 */ 119 void socket_cores_release(async_sess_t *sess, socket_cores_t *local_sockets, 124 120 socket_ports_t *global_sockets, 125 121 void (* socket_release)(socket_core_t *socket)) 126 122 { 127 int index;128 129 123 if (!socket_cores_is_valid(local_sockets)) 130 124 return; 131 125 132 126 local_sockets->magic = 0; 133 127 128 int index; 134 129 for (index = 0; index < local_sockets->next; ++index) { 135 130 if (socket_cores_item_is_valid(&local_sockets->items[index])) { 136 131 local_sockets->items[index].magic = 0; 137 132 138 133 if (local_sockets->items[index].value) { 139 socket_destroy_core( packet_phone,134 socket_destroy_core(sess, 140 135 local_sockets->items[index].value, 141 136 local_sockets, global_sockets, … … 146 141 } 147 142 } 148 143 149 144 free(local_sockets->items); 150 145 } … … 166 161 int rc; 167 162 168 / / create a wrapper163 /* Create a wrapper */ 169 164 socket_ref = malloc(sizeof(*socket_ref)); 170 165 if (!socket_ref) … … 172 167 173 168 *socket_ref = socket; 174 / / add the wrapper169 /* Add the wrapper */ 175 170 rc = socket_port_map_add(&socket_port->map, key, key_length, 176 171 socket_ref); … … 206 201 int rc; 207 202 208 / / create a wrapper203 /* Create a wrapper */ 209 204 socket_port = malloc(sizeof(*socket_port)); 210 205 if (!socket_port) … … 221 216 goto fail; 222 217 223 / / register the incomming port218 /* Register the incoming port */ 224 219 rc = socket_ports_add(global_sockets, port, socket_port); 225 220 if (rc < 0) … … 230 225 231 226 fail: 232 socket_port_map_destroy(&socket_port->map );227 socket_port_map_destroy(&socket_port->map, free); 233 228 free(socket_port); 234 229 return rc; … … 277 272 278 273 address_in = (struct sockaddr_in *) addr; 279 / / find the socket274 /* Find the socket */ 280 275 socket = socket_cores_find(local_sockets, socket_id); 281 276 if (!socket) 282 277 return ENOTSOCK; 283 278 284 / / bind a free port?279 /* Bind a free port? */ 285 280 if (address_in->sin_port <= 0) 286 281 return socket_bind_free_port(global_sockets, socket, 287 282 free_ports_start, free_ports_end, last_used_port); 288 283 289 / / try to find the port284 /* Try to find the port */ 290 285 socket_port = socket_ports_find(global_sockets, 291 286 ntohs(address_in->sin_port)); 292 287 if (socket_port) { 293 / / already used288 /* Already used */ 294 289 return EADDRINUSE; 295 290 } 296 291 297 / / if bound292 /* If bound */ 298 293 if (socket->port) { 299 / / release the port294 /* Release the port */ 300 295 socket_port_release(global_sockets, socket); 301 296 } … … 333 328 int index; 334 329 335 / / from the last used one330 /* From the last used one */ 336 331 index = last_used_port; 337 332 … … 339 334 ++index; 340 335 341 / / til the range end336 /* Till the range end */ 342 337 if (index >= free_ports_end) { 343 / / start from the range beginning338 /* Start from the range beginning */ 344 339 index = free_ports_start - 1; 345 340 do { 346 341 ++index; 347 / / til the last used one342 /* Till the last used one */ 348 343 if (index >= last_used_port) { 349 / / none found344 /* None found */ 350 345 return ENOTCONN; 351 346 } 352 347 } while (socket_ports_find(global_sockets, index)); 353 348 354 / / found, break immediately349 /* Found, break immediately */ 355 350 break; 356 351 } … … 376 371 377 372 count = 0; 378 // socket_id = socket_globals.last_id; 373 #if 0 374 socket_id = socket_globals.last_id; 375 #endif 379 376 do { 380 377 if (count < SOCKET_ID_TRIES) { … … 384 381 socket_id = 1; 385 382 ++count; 386 / / only this branch for last_id383 /* Only this branch for last_id */ 387 384 } else { 388 385 if (socket_id < INT_MAX) { 389 386 ++ socket_id; 390 /* } else if(socket_globals.last_id) { 391 * socket_globals.last_id = 0; 392 * socket_id = 1; 393 */ } else { 387 #if 0 388 } else if(socket_globals.last_id) { 389 socket_globals.last_id = 0; 390 socket_id = 1; 391 #endif 392 } else { 394 393 return ELIMIT; 395 394 } … … 402 401 } 403 402 404 /** Creates a new socket. 405 * 406 * @param[in,out] local_sockets The local sockets to be updated. 407 * @param[in] app_phone The application phone. 408 * @param[in] specific_data The socket specific data. 409 * @param[in,out] socket_id The new socket identifier. A new identifier is 410 * chosen if set to zero or negative. A negative identifier 411 * is chosen if set to negative. 412 * @return EOK on success. 413 * @return EINVAL if the socket_id parameter is NULL. 414 * @return ENOMEM if there is not enough memory left. 415 */ 416 int 417 socket_create(socket_cores_t *local_sockets, int app_phone, 403 /** Create a new socket. 404 * 405 * @param[in,out] local_sockets Local sockets to be updated. 406 * @param[in] sess Application session. 407 * @param[in] specific_data Socket specific data. 408 * @param[in,out] socket_id New socket identifier. A new identifier 409 * is chosen if set to zero or negative. 410 * A negative identifier is chosen if set 411 * to negative. 412 * 413 * @return EOK on success. 414 * @return EINVAL if the socket_id parameter is NULL. 415 * @return ENOMEM if there is not enough memory left. 416 * 417 */ 418 int socket_create(socket_cores_t *local_sockets, async_sess_t* sess, 418 419 void *specific_data, int *socket_id) 419 420 { … … 425 426 return EINVAL; 426 427 427 / / store the socket428 /* Store the socket */ 428 429 if (*socket_id <= 0) { 429 430 positive = (*socket_id == 0); … … 441 442 return ENOMEM; 442 443 443 / / initialize444 socket-> phone = app_phone;444 /* Initialize */ 445 socket->sess = sess; 445 446 socket->port = -1; 446 447 socket->key = NULL; … … 471 472 } 472 473 473 /** Destroy sthe socket.474 /** Destroy the socket. 474 475 * 475 476 * If the socket is bound, the port is released. 476 * Release s all buffered packets, calls the release function and removesthe477 * Release all buffered packets, call the release function and remove the 477 478 * socket from the local sockets. 478 479 * 479 * @param[in] packet_phone The packet server phone to release buffered packets. 480 * @param[in] socket_id The socket identifier. 481 * @param[in,out] local_sockets The local sockets to be updated. 482 * @param[in,out] global_sockets The global sockets to be updated. 483 * @param[in] socket_release The client release callback function. 484 * @return EOK on success. 485 * @return ENOTSOCK if the socket is not found. 480 * @param[in] sess Packet server session. 481 * @param[in] socket_id Socket identifier. 482 * @param[in,out] local_sockets Local sockets to be updated. 483 * @param[in,out] global_sockets Global sockets to be updated. 484 * @param[in] socket_release Client release callback function. 485 * 486 * @return EOK on success. 487 * @return ENOTSOCK if the socket is not found. 488 * 486 489 */ 487 490 int 488 socket_destroy( int packet_phone, int socket_id, socket_cores_t *local_sockets,491 socket_destroy(async_sess_t *sess, int socket_id, socket_cores_t *local_sockets, 489 492 socket_ports_t *global_sockets, 490 493 void (*socket_release)(socket_core_t *socket)) 491 494 { 492 socket_core_t *socket; 493 int accepted_id; 494 495 // find the socket 496 socket = socket_cores_find(local_sockets, socket_id); 495 /* Find the socket */ 496 socket_core_t *socket = socket_cores_find(local_sockets, socket_id); 497 497 if (!socket) 498 498 return ENOTSOCK; 499 499 500 // destroy all accepted sockets 500 /* Destroy all accepted sockets */ 501 int accepted_id; 501 502 while ((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0) 502 socket_destroy( packet_phone, accepted_id, local_sockets,503 socket_destroy(sess, accepted_id, local_sockets, 503 504 global_sockets, socket_release); 504 505 505 socket_destroy_core( packet_phone, socket, local_sockets, global_sockets,506 socket_destroy_core(sess, socket, local_sockets, global_sockets, 506 507 socket_release); 507 508 … … 535 536 next_packet = pq_next(packet); 536 537 if (!next_packet) { 537 / / write all if only one fragment538 /* Write all if only one fragment */ 538 539 rc = data_reply(packet_get_data(packet), 539 540 packet_get_data_length(packet)); 540 541 if (rc != EOK) 541 542 return rc; 542 / / store the total length543 /* Store the total length */ 543 544 *length = packet_get_data_length(packet); 544 545 } else { 545 / / count the packet fragments546 /* Count the packet fragments */ 546 547 fragments = 1; 547 548 next_packet = pq_next(packet); … … 549 550 ++fragments; 550 551 551 / / compute and store the fragment lengths552 /* Compute and store the fragment lengths */ 552 553 lengths = (size_t *) malloc(sizeof(size_t) * fragments + 553 554 sizeof(size_t)); … … 565 566 } 566 567 567 / / write the fragment lengths568 /* Write the fragment lengths */ 568 569 rc = data_reply(lengths, sizeof(int) * (fragments + 1)); 569 570 if (rc != EOK) { … … 573 574 next_packet = packet; 574 575 575 / / write the fragments576 /* Write the fragments */ 576 577 for (index = 0; index < fragments; ++index) { 577 578 rc = data_reply(packet_get_data(next_packet), … … 584 585 } 585 586 586 / / store the total length587 /* Store the total length */ 587 588 *length = lengths[fragments]; 588 589 free(lengths); … … 636 637 return; 637 638 638 / / find ports639 /* Find ports */ 639 640 socket_port = socket_ports_find(global_sockets, socket->port); 640 641 if (socket_port) { 641 / / find the socket642 /* Find the socket */ 642 643 socket_ref = socket_port_map_find(&socket_port->map, 643 644 socket->key, socket->key_length); … … 646 647 --socket_port->count; 647 648 648 / / release if empty649 /* Release if empty */ 649 650 if (socket_port->count <= 0) { 650 / / destroy the map651 socket_port_map_destroy(&socket_port->map );652 / / release the port651 /* Destroy the map */ 652 socket_port_map_destroy(&socket_port->map, free); 653 /* Release the port */ 653 654 socket_ports_exclude(global_sockets, 654 socket->port );655 socket->port, free); 655 656 } else { 656 / / remove657 /* Remove */ 657 658 socket_port_map_exclude(&socket_port->map, 658 socket->key, socket->key_length );659 socket->key, socket->key_length, free); 659 660 } 660 661 } … … 685 686 int rc; 686 687 687 / / find ports688 /* Find ports */ 688 689 socket_port = socket_ports_find(global_sockets, port); 689 690 if (!socket_port) 690 691 return ENOENT; 691 692 692 / / add the socket693 /* Add the socket */ 693 694 rc = socket_port_add_core(socket_port, socket, key, key_length); 694 695 if (rc != EOK)
Note:
See TracChangeset
for help on using the changeset viewer.