Changeset 849ed54 in mainline for uspace/lib/socket/generic/socket_core.c
- Timestamp:
- 2010-03-30T18:39:04Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7553689
- Parents:
- 7d6fe4db
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/socket/generic/socket_core.c
r7d6fe4db r849ed54 38 38 #include <stdlib.h> 39 39 40 #include "../err.h" 41 42 #include "../include/in.h" 43 #include "../include/inet.h" 44 45 #include "../include/socket_codes.h" 46 #include "../include/socket_errno.h" 47 48 #include "../structures/dynamic_fifo.h" 49 #include "../structures/int_map.h" 50 #include "../structures/packet/packet.h" 51 #include "../structures/packet/packet_client.h" 52 53 #include "../modules.h" 54 55 #include "socket_core.h" 40 #include <net_err.h> 41 #include <in.h> 42 #include <inet.h> 43 #include <socket_codes.h> 44 #include <socket_errno.h> 45 #include <adt/dynamic_fifo.h> 46 #include <adt/int_map.h> 47 #include <packet/packet.h> 48 #include <packet/packet_client.h> 49 #include <net_modules.h> 50 #include <socket_core.h> 56 51 57 52 /** Maximum number of random attempts to find a new socket identifier before switching to the sequence. … … 70 65 }; 71 66 72 /** Binds the socket to the port. 73 * The SOCKET_MAP_KEY_LISTENING key identifier is used. 74 * @param[in] global_sockets The global sockets to be updated. 75 * @param[in] socket The socket to be added. 76 * @param[in] port The port number to be bound to. 77 * @returns EOK on success. 78 * @returns ENOMEM if there is not enough memory left. 79 * @returns Other error codes as defined for the socket_ports_add() function. 80 */ 81 int socket_bind_insert(socket_ports_ref global_sockets, socket_core_ref socket, int port); 67 INT_MAP_IMPLEMENT(socket_cores, socket_core_t); 68 69 GENERIC_CHAR_MAP_IMPLEMENT(socket_port_map, socket_core_ref); 70 71 INT_MAP_IMPLEMENT(socket_ports, socket_port_t); 82 72 83 73 /** Destroys the socket. … … 90 80 * @param[in] socket_release The client release callback function. 91 81 */ 92 void socket_destroy_core(int packet_phone, socket_core_ref socket, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)); 93 94 /** Adds the socket to a socket port. 95 * @param[in,out] socket_port The socket port structure. 96 * @param[in] socket The socket to be added. 97 * @param[in] key The socket key identifier. 98 * @param[in] key_length The socket key length. 99 * @returns EOK on success. 100 * @returns ENOMEM if there is not enough memory left. 101 */ 102 int socket_port_add_core(socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length); 103 104 /** Tries to find a new free socket identifier. 105 * @param[in] local_sockets The local sockets to be searched. 106 * @param[in] positive A value indicating whether a positive identifier is requested. A negative identifier is requested if set to false. 107 * @returns The new socket identifier. 108 * @returns ELIMIT if there is no socket identifier available. 109 */ 110 static int socket_generate_new_id(socket_cores_ref local_sockets, int positive); 111 112 INT_MAP_IMPLEMENT(socket_cores, socket_core_t); 113 114 GENERIC_CHAR_MAP_IMPLEMENT(socket_port_map, socket_core_ref); 115 116 INT_MAP_IMPLEMENT(socket_ports, socket_port_t); 82 static void socket_destroy_core(int packet_phone, socket_core_ref socket, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)){ 83 int packet_id; 84 85 // if bound 86 if(socket->port){ 87 // release the port 88 socket_port_release(global_sockets, socket); 89 } 90 // release all received packets 91 while((packet_id = dyn_fifo_pop(&socket->received)) >= 0){ 92 pq_release(packet_phone, packet_id); 93 } 94 dyn_fifo_destroy(&socket->received); 95 dyn_fifo_destroy(&socket->accepted); 96 if(socket_release){ 97 socket_release(socket); 98 } 99 socket_cores_exclude(local_sockets, socket->socket_id); 100 } 117 101 118 102 void socket_cores_release(int packet_phone, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)){ … … 135 119 } 136 120 137 void socket_destroy_core(int packet_phone, socket_core_ref socket, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)){ 138 int packet_id; 139 140 // if bound 141 if(socket->port){ 142 // release the port 143 socket_port_release(global_sockets, socket); 144 } 145 // release all received packets 146 while((packet_id = dyn_fifo_pop(&socket->received)) >= 0){ 147 pq_release(packet_phone, packet_id); 148 } 149 dyn_fifo_destroy(&socket->received); 150 dyn_fifo_destroy(&socket->accepted); 151 if(socket_release){ 152 socket_release(socket); 153 } 154 socket_cores_exclude(local_sockets, socket->socket_id); 121 /** Adds the socket to a socket port. 122 * @param[in,out] socket_port The socket port structure. 123 * @param[in] socket The socket to be added. 124 * @param[in] key The socket key identifier. 125 * @param[in] key_length The socket key length. 126 * @returns EOK on success. 127 * @returns ENOMEM if there is not enough memory left. 128 */ 129 static int socket_port_add_core(socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length){ 130 ERROR_DECLARE; 131 132 socket_core_ref * socket_ref; 133 134 // create a wrapper 135 socket_ref = malloc(sizeof(*socket_ref)); 136 if(! socket_ref){ 137 return ENOMEM; 138 } 139 *socket_ref = socket; 140 // add the wrapper 141 if(ERROR_OCCURRED(socket_port_map_add(&socket_port->map, key, key_length, socket_ref))){ 142 free(socket_ref); 143 return ERROR_CODE; 144 } 145 ++ socket_port->count; 146 socket->key = key; 147 socket->key_length = key_length; 148 return EOK; 149 } 150 151 /** Binds the socket to the port. 152 * The SOCKET_MAP_KEY_LISTENING key identifier is used. 153 * @param[in] global_sockets The global sockets to be updated. 154 * @param[in] socket The socket to be added. 155 * @param[in] port The port number to be bound to. 156 * @returns EOK on success. 157 * @returns ENOMEM if there is not enough memory left. 158 * @returns Other error codes as defined for the socket_ports_add() function. 159 */ 160 static int socket_bind_insert(socket_ports_ref global_sockets, socket_core_ref socket, int port){ 161 ERROR_DECLARE; 162 163 socket_port_ref socket_port; 164 165 // create a wrapper 166 socket_port = malloc(sizeof(*socket_port)); 167 if(! socket_port){ 168 return ENOMEM; 169 } 170 socket_port->count = 0; 171 if(ERROR_OCCURRED(socket_port_map_initialize(&socket_port->map)) 172 || ERROR_OCCURRED(socket_port_add_core(socket_port, socket, SOCKET_MAP_KEY_LISTENING, 0))){ 173 socket_port_map_destroy(&socket_port->map); 174 free(socket_port); 175 return ERROR_CODE; 176 } 177 // register the incomming port 178 ERROR_CODE = socket_ports_add(global_sockets, port, socket_port); 179 if(ERROR_CODE < 0){ 180 socket_port_map_destroy(&socket_port->map); 181 free(socket_port); 182 return ERROR_CODE; 183 } 184 socket->port = port; 185 return EOK; 155 186 } 156 187 … … 225 256 } 226 257 227 int socket_bind_insert(socket_ports_ref global_sockets, socket_core_ref socket, int port){ 228 ERROR_DECLARE; 229 230 socket_port_ref socket_port; 231 232 // create a wrapper 233 socket_port = malloc(sizeof(*socket_port)); 234 if(! socket_port){ 235 return ENOMEM; 236 } 237 socket_port->count = 0; 238 if(ERROR_OCCURRED(socket_port_map_initialize(&socket_port->map)) 239 || ERROR_OCCURRED(socket_port_add_core(socket_port, socket, SOCKET_MAP_KEY_LISTENING, 0))){ 240 socket_port_map_destroy(&socket_port->map); 241 free(socket_port); 242 return ERROR_CODE; 243 } 244 // register the incomming port 245 ERROR_CODE = socket_ports_add(global_sockets, port, socket_port); 246 if(ERROR_CODE < 0){ 247 socket_port_map_destroy(&socket_port->map); 248 free(socket_port); 249 return ERROR_CODE; 250 } 251 socket->port = port; 252 return EOK; 253 } 254 255 258 /** Tries to find a new free socket identifier. 259 * @param[in] local_sockets The local sockets to be searched. 260 * @param[in] positive A value indicating whether a positive identifier is requested. A negative identifier is requested if set to false. 261 * @returns The new socket identifier. 262 * @returns ELIMIT if there is no socket identifier available. 263 */ 256 264 static int socket_generate_new_id(socket_cores_ref local_sockets, int positive){ 257 265 int socket_id; … … 465 473 } 466 474 467 int socket_port_add_core(socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length){468 ERROR_DECLARE;469 470 socket_core_ref * socket_ref;471 472 // create a wrapper473 socket_ref = malloc(sizeof(*socket_ref));474 if(! socket_ref){475 return ENOMEM;476 }477 *socket_ref = socket;478 // add the wrapper479 if(ERROR_OCCURRED(socket_port_map_add(&socket_port->map, key, key_length, socket_ref))){480 free(socket_ref);481 return ERROR_CODE;482 }483 ++ socket_port->count;484 socket->key = key;485 socket->key_length = key_length;486 return EOK;487 }488 489 475 /** @} 490 476 */
Note:
See TracChangeset
for help on using the changeset viewer.