Changeset 2a786f9 in mainline
- Timestamp:
- 2010-09-22T19:07:02Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ec1bdc8
- Parents:
- dc94cb2 (diff), 921a860 (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. - Location:
- uspace
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/generic/net_remote.c
rdc94cb2 r2a786f9 47 47 #include <net_net_messages.h> 48 48 49 int net_connect_module(services_t service){ 49 int net_connect_module(services_t service) 50 { 50 51 return connect_to_service(SERVICE_NETWORKING); 51 52 } 52 53 53 void net_free_settings(measured_string_ref settings, char * data){ 54 if(settings){ 54 void net_free_settings(measured_string_ref settings, char * data) 55 { 56 if (settings) 55 57 free(settings); 56 } 57 if (data){58 59 if (data) 58 60 free(data); 59 }60 61 } 61 62 62 int net_get_conf_req(int net_phone, measured_string_ref * configuration, size_t count, char ** data){ 63 return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 0, 0, * configuration, count, configuration, data); 63 int 64 net_get_conf_req(int net_phone, measured_string_ref * configuration, 65 size_t count, char ** data) 66 { 67 return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 0, 0, 68 *configuration, count, configuration, data); 64 69 } 65 70 66 int net_get_device_conf_req(int net_phone, device_id_t device_id, measured_string_ref * configuration, size_t count, char ** data){ 67 return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, device_id, 0, * configuration, count, configuration, data); 71 int 72 net_get_device_conf_req(int net_phone, device_id_t device_id, 73 measured_string_ref * configuration, size_t count, char ** data) 74 { 75 return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 76 device_id, 0, *configuration, count, configuration, data); 68 77 } 69 78 -
uspace/lib/net/generic/packet_remote.c
rdc94cb2 r2a786f9 64 64 * 65 65 */ 66 static int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size){ 66 static int 67 packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size) 68 { 67 69 ERROR_DECLARE; 68 70 69 71 ipc_call_t answer; 70 72 aid_t message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer); 73 71 74 *packet = (packet_t) as_get_mappable_page(size); 72 75 if (ERROR_OCCURRED(async_share_in_start_0_0(phone, *packet, size)) || … … 91 94 92 95 *packet = pm_find(packet_id); 93 if (! (*packet)) {96 if (!*packet) { 94 97 ipcarg_t size; 95 98 96 ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id, &size)); 99 ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, 100 packet_id, &size)); 97 101 ERROR_PROPAGATE(packet_return(phone, packet, packet_id, size)); 98 102 } 99 if ((* * packet).next) {103 if ((*packet)->next) { 100 104 packet_t next; 101 105 102 return packet_translate_remote(phone, &next, (* * packet).next);106 return packet_translate_remote(phone, &next, (*packet)->next); 103 107 } 104 108 … … 114 118 ipcarg_t size; 115 119 116 if (ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, max_content,117 addr_len, max_prefix, max_suffix, &packet_id, &size)))120 if (ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, 121 max_content, addr_len, max_prefix, max_suffix, &packet_id, &size))) 118 122 return NULL; 119 123 … … 121 125 packet_t packet = pm_find(packet_id); 122 126 if (!packet) { 123 if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))) 127 if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, 128 size))) 124 129 return NULL; 125 130 } … … 141 146 packet_t packet = pm_find(packet_id); 142 147 if (!packet) { 143 if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))) 148 if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, 149 size))) 144 150 return NULL; 145 151 } -
uspace/lib/net/il/ip_remote.c
rdc94cb2 r2a786f9 127 127 return EINVAL; 128 128 129 if ((!device_id) || ( header) || (headerlen))129 if ((!device_id) || (!header) || (!headerlen)) 130 130 return EBADMEM; 131 131 -
uspace/lib/net/tl/tl_common.c
rdc94cb2 r2a786f9 58 58 DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t); 59 59 60 int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){ 61 const struct sockaddr_in * address_in; 62 const struct sockaddr_in6 * address_in6; 63 64 if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){ 65 return EINVAL; 66 } 67 switch(addr->sa_family){ 68 case AF_INET: 69 if(addrlen != sizeof(struct sockaddr_in)){ 60 int 61 tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port) 62 { 63 const struct sockaddr_in *address_in; 64 const struct sockaddr_in6 *address_in6; 65 66 if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))) 67 return EINVAL; 68 69 switch (addr->sa_family) { 70 case AF_INET: 71 if (addrlen != sizeof(struct sockaddr_in)) 72 return EINVAL; 73 74 address_in = (struct sockaddr_in *) addr; 75 *port = ntohs(address_in->sin_port); 76 break; 77 case AF_INET6: 78 if (addrlen != sizeof(struct sockaddr_in6)) 70 79 return EINVAL; 71 } 72 address_in = (struct sockaddr_in *) addr; 73 *port = ntohs(address_in->sin_port); 74 break; 75 case AF_INET6: 76 if(addrlen != sizeof(struct sockaddr_in6)){ 77 return EINVAL; 78 } 79 address_in6 = (struct sockaddr_in6 *) addr; 80 *port = ntohs(address_in6->sin6_port); 81 break; 82 default: 83 return EAFNOSUPPORT; 84 } 80 81 address_in6 = (struct sockaddr_in6 *) addr; 82 *port = ntohs(address_in6->sin6_port); 83 break; 84 default: 85 return EAFNOSUPPORT; 86 } 87 85 88 return EOK; 86 89 } … … 112 115 return EBADMEM; 113 116 114 *packet_dimension = packet_dimensions_find(packet_dimensions, device_id); 117 *packet_dimension = packet_dimensions_find(packet_dimensions, 118 device_id); 115 119 if (!*packet_dimension) { 116 120 /* Ask for and remember them if not found */ … … 136 140 } 137 141 138 int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){ 142 int 143 tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, 144 device_id_t device_id, size_t content) 145 { 139 146 packet_dimension_ref packet_dimension; 140 147 141 148 packet_dimension = packet_dimensions_find(packet_dimensions, device_id); 142 if (! packet_dimension){149 if (!packet_dimension) 143 150 return ENOENT; 144 }145 151 packet_dimension->content = content; 146 if(device_id != DEVICE_INVALID_ID){ 147 packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID); 148 if(packet_dimension){ 149 if(packet_dimension->content >= content){ 152 153 if (device_id != DEVICE_INVALID_ID) { 154 packet_dimension = packet_dimensions_find(packet_dimensions, 155 DEVICE_INVALID_ID); 156 157 if (packet_dimension) { 158 if (packet_dimension->content >= content) 150 159 packet_dimension->content = content; 151 }else{ 152 packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID); 153 } 160 else 161 packet_dimensions_exclude(packet_dimensions, 162 DEVICE_INVALID_ID); 163 154 164 } 155 165 } 166 156 167 return EOK; 157 168 } … … 159 170 int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port) 160 171 { 161 struct sockaddr_in * 162 struct sockaddr_in6 * 172 struct sockaddr_in *address_in; 173 struct sockaddr_in6 *address_in6; 163 174 size_t length; 164 175 … … 188 199 } 189 200 190 int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){ 201 int 202 tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, 203 services_t error) 204 { 191 205 packet_t next; 192 uint8_t * 206 uint8_t *src; 193 207 int length; 194 208 … … 199 213 200 214 length = packet_get_addr(packet, &src, NULL); 201 if((length > 0) 202 && (! error) 203 && (icmp_phone >= 0) 204 // set both addresses to the source one (avoids the source address deletion before setting the destination one) 205 && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){ 215 if ((length > 0) && (!error) && (icmp_phone >= 0) && 216 // set both addresses to the source one (avoids the source address 217 // deletion before setting the destination one) 218 (packet_set_addr(packet, src, src, (size_t) length) == EOK)) { 206 219 return EOK; 207 } else{220 } else 208 221 pq_release_remote(packet_phone, packet_get_id(packet)); 209 } 222 210 223 return ENOENT; 211 224 } 212 225 213 int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){ 226 int 227 tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, 228 const packet_dimension_ref dimension, const struct sockaddr *addr, 229 socklen_t addrlen) 230 { 214 231 ERROR_DECLARE; 215 232 … … 218 235 void * data; 219 236 220 if (! dimension){221 return EINVAL; 222 } 237 if (!dimension) 238 return EINVAL; 239 223 240 // get the data length 224 if (! async_data_write_receive(&callid, &length)){225 return EINVAL; 226 } 241 if (!async_data_write_receive(&callid, &length)) 242 return EINVAL; 243 227 244 // get a new packet 228 *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix); 229 if(! packet){ 245 *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, 246 prefix + dimension->prefix, dimension->suffix); 247 if (!packet) 230 248 return ENOMEM; 231 } 249 232 250 // allocate space in the packet 233 251 data = packet_suffix(*packet, length); 234 if (! data){252 if (!data) { 235 253 pq_release_remote(packet_phone, packet_get_id(*packet)); 236 254 return ENOMEM; 237 255 } 256 238 257 // read the data into the packet 239 if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) 240 // set the packet destination address 241 || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){ 258 if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) || 259 // set the packet destination address 260 ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, 261 addrlen))) { 242 262 pq_release_remote(packet_phone, packet_get_id(*packet)); 243 263 return ERROR_CODE; 244 264 } 265 245 266 return (int) length; 246 267 } -
uspace/srv/net/tl/udp/udp.c
rdc94cb2 r2a786f9 72 72 #include "udp_module.h" 73 73 74 /** UDP module name. 75 */ 74 /** UDP module name. */ 76 75 #define NAME "UDP protocol" 77 76 78 /** Default UDP checksum computing. 79 */ 77 /** Default UDP checksum computing. */ 80 78 #define NET_DEFAULT_UDP_CHECKSUM_COMPUTING true 81 79 82 /** Default UDP autobind when sending via unbound sockets. 83 */ 80 /** Default UDP autobind when sending via unbound sockets. */ 84 81 #define NET_DEFAULT_UDP_AUTOBINDING true 85 82 86 /** Maximum UDP fragment size. 87 */ 88 #define MAX_UDP_FRAGMENT_SIZE 65535 89 90 /** Free ports pool start. 91 */ 92 #define UDP_FREE_PORTS_START 1025 93 94 /** Free ports pool end. 95 */ 83 /** Maximum UDP fragment size. */ 84 #define MAX_UDP_FRAGMENT_SIZE 65535 85 86 /** Free ports pool start. */ 87 #define UDP_FREE_PORTS_START 1025 88 89 /** Free ports pool end. */ 96 90 #define UDP_FREE_PORTS_END 65535 97 91 98 92 /** Processes the received UDP packet queue. 93 * 99 94 * Is used as an entry point from the underlying IP module. 100 95 * Locks the global lock and calls udp_process_packet() function. 96 * 101 97 * @param[in] device_id The receiving device identifier. 102 98 * @param[in,out] packet The received packet queue. 103 * @param receiver The target service. Ignored parameter. 104 * @param[in] error The packet error reporting service. Prefixes the received packet. 105 * @returns EOK on success. 106 * @returns Other error codes as defined for the udp_process_packet() function. 107 */ 108 int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error); 99 * @param receiver The target service. Ignored parameter. 100 * @param[in] error The packet error reporting service. Prefixes the 101 * received packet. 102 * @returns EOK on success. 103 * @returns Other error codes as defined for the 104 * udp_process_packet() function. 105 */ 106 int 107 udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, 108 services_t error); 109 109 110 110 /** Processes the received UDP packet queue. 111 * 111 112 * Notifies the destination socket application. 112 * Releases the packet on error or sends an ICMP error notification.. 113 * Releases the packet on error or sends an ICMP error notification. 114 * 113 115 * @param[in] device_id The receiving device identifier. 114 116 * @param[in,out] packet The received packet queue. 115 * @param[in] error The packet error reporting service. Prefixes the received packet. 116 * @returns EOK on success. 117 * @returns EINVAL if the packet is not valid. 118 * @returns EINVAL if the stored packet address is not the an_addr_t. 119 * @returns EINVAL if the packet does not contain any data. 120 * @returns NO_DATA if the packet content is shorter than the user datagram header. 121 * @returns ENOMEM if there is not enough memory left. 122 * @returns EADDRNOTAVAIL if the destination socket does not exist. 123 * @returns Other error codes as defined for the ip_client_process_packet() function. 124 */ 125 int udp_process_packet(device_id_t device_id, packet_t packet, services_t error); 117 * @param[in] error The packet error reporting service. Prefixes the 118 * received packet. 119 * @returns EOK on success. 120 * @returns EINVAL if the packet is not valid. 121 * @returns EINVAL if the stored packet address is not the 122 * an_addr_t. 123 * @returns EINVAL if the packet does not contain any data. 124 * @returns NO_DATA if the packet content is shorter than the user 125 * datagram header. 126 * @returns ENOMEM if there is not enough memory left. 127 * @returns EADDRNOTAVAIL if the destination socket does not exist. 128 * @returns Other error codes as defined for the 129 * ip_client_process_packet() function. 130 */ 131 int 132 udp_process_packet(device_id_t device_id, packet_t packet, services_t error); 126 133 127 134 /** Releases the packet and returns the result. 128 * @param[in] packet The packet queue to be released. 129 * @param[in] result The result to be returned. 130 * @return The result parameter. 135 * 136 * @param[in] packet The packet queue to be released. 137 * @param[in] result The result to be returned. 138 * @return The result parameter. 131 139 */ 132 140 int udp_release_and_return(packet_t packet, int result); … … 137 145 138 146 /** Processes the socket client messages. 147 * 139 148 * Runs until the client module disconnects. 140 * @param[in] callid The message identifier. 141 * @param[in] call The message parameters. 142 * @returns EOK on success. 143 * @see socket.h 149 * 150 * @param[in] callid The message identifier. 151 * @param[in] call The message parameters. 152 * @returns EOK on success. 153 * @see socket.h 144 154 */ 145 155 int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call); 146 156 147 157 /** Sends data from the socket to the remote address. 158 * 148 159 * Binds the socket to a free port if not already connected/bound. 149 160 * Handles the NET_SOCKET_SENDTO message. 150 161 * Supports AF_INET and AF_INET6 address families. 162 * 151 163 * @param[in,out] local_sockets The application local sockets. 152 164 * @param[in] socket_id Socket identifier. 153 * @param[in] addr 154 * @param[in] addrlen 165 * @param[in] addr The destination address. 166 * @param[in] addrlen The address length. 155 167 * @param[in] fragments The number of data fragments. 156 168 * @param[out] data_fragment_size The data fragment size in bytes. 157 * @param[in] flags Various send flags. 158 * @returns EOK on success. 159 * @returns EAFNOTSUPPORT if the address family is not supported. 160 * @returns ENOTSOCK if the socket is not found. 161 * @returns EINVAL if the address is invalid. 162 * @returns ENOTCONN if the sending socket is not and cannot be bound. 163 * @returns ENOMEM if there is not enough memory left. 164 * @returns Other error codes as defined for the socket_read_packet_data() function. 165 * @returns Other error codes as defined for the ip_client_prepare_packet() function. 166 * @returns Other error codes as defined for the ip_send_msg() function. 167 */ 168 int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags); 169 * @param[in] flags Various send flags. 170 * @returns EOK on success. 171 * @returns EAFNOTSUPPORT if the address family is not supported. 172 * @returns ENOTSOCK if the socket is not found. 173 * @returns EINVAL if the address is invalid. 174 * @returns ENOTCONN if the sending socket is not and cannot be 175 * bound. 176 * @returns ENOMEM if there is not enough memory left. 177 * @returns Other error codes as defined for the 178 * socket_read_packet_data() function. 179 * @returns Other error codes as defined for the 180 * ip_client_prepare_packet() function. 181 * @returns Other error codes as defined for the ip_send_msg() 182 * function. 183 */ 184 int 185 udp_sendto_message(socket_cores_ref local_sockets, int socket_id, 186 const struct sockaddr * addr, socklen_t addrlen, int fragments, 187 size_t * data_fragment_size, int flags); 169 188 170 189 /** Receives data to the socket. 190 * 171 191 * Handles the NET_SOCKET_RECVFROM message. 172 192 * Replies the source address as well. 193 * 173 194 * @param[in] local_sockets The application local sockets. 174 195 * @param[in] socket_id Socket identifier. 175 * @param[in] flags Various receive flags. 176 * @param[out] addrlen The source address length. 177 * @returns The number of bytes received. 178 * @returns ENOTSOCK if the socket is not found. 179 * @returns NO_DATA if there are no received packets or data. 180 * @returns ENOMEM if there is not enough memory left. 181 * @returns EINVAL if the received address is not an IP address. 182 * @returns Other error codes as defined for the packet_translate() function. 183 * @returns Other error codes as defined for the data_reply() function. 184 */ 185 int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen); 196 * @param[in] flags Various receive flags. 197 * @param[out] addrlen The source address length. 198 * @returns The number of bytes received. 199 * @returns ENOTSOCK if the socket is not found. 200 * @returns NO_DATA if there are no received packets or data. 201 * @returns ENOMEM if there is not enough memory left. 202 * @returns EINVAL if the received address is not an IP address. 203 * @returns Other error codes as defined for the packet_translate() 204 * function. 205 * @returns Other error codes as defined for the data_reply() 206 * function. 207 */ 208 int 209 udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, 210 size_t * addrlen); 186 211 187 212 /*@}*/ … … 189 214 /** UDP global data. 190 215 */ 191 udp_globals_t udp_globals; 192 193 int udp_initialize(async_client_conn_t client_connection){ 216 udp_globals_t udp_globals; 217 218 int udp_initialize(async_client_conn_t client_connection) 219 { 194 220 ERROR_DECLARE; 195 221 196 measured_string_t names[] = {{str_dup("UDP_CHECKSUM_COMPUTING"), 22}, {str_dup("UDP_AUTOBINDING"), 15}}; 222 measured_string_t names[] = { 223 { 224 str_dup("UDP_CHECKSUM_COMPUTING"), 225 22 226 }, 227 { 228 str_dup("UDP_AUTOBINDING"), 229 15 230 } 231 }; 197 232 measured_string_ref configuration; 198 233 size_t count = sizeof(names) / sizeof(measured_string_t); … … 201 236 fibril_rwlock_initialize(&udp_globals.lock); 202 237 fibril_rwlock_write_lock(&udp_globals.lock); 203 udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT); 204 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg); 205 if(udp_globals.ip_phone < 0){ 238 239 udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 240 ICMP_CONNECT_TIMEOUT); 241 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, 242 SERVICE_UDP, client_connection, udp_received_msg); 243 if (udp_globals.ip_phone < 0) 206 244 return udp_globals.ip_phone; 207 } 245 208 246 // read default packet dimensions 209 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension)); 247 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, 248 &udp_globals.packet_dimension)); 210 249 ERROR_PROPAGATE(socket_ports_initialize(&udp_globals.sockets)); 211 if(ERROR_OCCURRED(packet_dimensions_initialize(&udp_globals.dimensions))){ 250 if (ERROR_OCCURRED(packet_dimensions_initialize( 251 &udp_globals.dimensions))) { 212 252 socket_ports_destroy(&udp_globals.sockets); 213 253 return ERROR_CODE; … … 216 256 udp_globals.packet_dimension.content -= sizeof(udp_header_t); 217 257 udp_globals.last_used_port = UDP_FREE_PORTS_START - 1; 258 218 259 // get configuration 219 260 udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING; 220 261 udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING; 221 262 configuration = &names[0]; 222 ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, count, &data)); 223 if(configuration){ 224 if(configuration[0].value){ 225 udp_globals.checksum_computing = (configuration[0].value[0] == 'y'); 226 } 227 if(configuration[1].value){ 228 udp_globals.autobinding = (configuration[1].value[0] == 'y'); 229 } 263 ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, 264 count, &data)); 265 if (configuration) { 266 if (configuration[0].value) 267 udp_globals.checksum_computing = 268 (configuration[0].value[0] == 'y'); 269 270 if (configuration[1].value) 271 udp_globals.autobinding = 272 (configuration[1].value[0] == 'y'); 273 230 274 net_free_settings(configuration, data); 231 275 } 276 232 277 fibril_rwlock_write_unlock(&udp_globals.lock); 233 278 return EOK; 234 279 } 235 280 236 int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){ 281 int 282 udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, 283 services_t error) 284 { 237 285 int result; 238 286 239 287 fibril_rwlock_write_lock(&udp_globals.lock); 240 288 result = udp_process_packet(device_id, packet, error); 241 if (result != EOK){289 if (result != EOK) 242 290 fibril_rwlock_write_unlock(&udp_globals.lock); 243 }244 291 245 292 return result; 246 293 } 247 294 248 int udp_process_packet(device_id_t device_id, packet_t packet, services_t error){ 295 int udp_process_packet(device_id_t device_id, packet_t packet, services_t error) 296 { 249 297 ERROR_DECLARE; 250 298 … … 262 310 icmp_code_t code; 263 311 void *ip_header; 264 struct sockaddr * 265 struct sockaddr * 312 struct sockaddr *src; 313 struct sockaddr *dest; 266 314 packet_dimension_ref packet_dimension; 267 315 268 if(error){ 269 switch(error){ 270 case SERVICE_ICMP: 271 // ignore error 272 // length = icmp_client_header_length(packet); 273 // process error 274 result = icmp_client_process_packet(packet, &type, &code, NULL, NULL); 275 if(result < 0){ 276 return udp_release_and_return(packet, result); 277 } 278 length = (size_t) result; 279 if(ERROR_OCCURRED(packet_trim(packet, length, 0))){ 280 return udp_release_and_return(packet, ERROR_CODE); 281 } 282 break; 283 default: 284 return udp_release_and_return(packet, ENOTSUP); 285 } 286 } 316 if (error) { 317 switch (error) { 318 case SERVICE_ICMP: 319 // ignore error 320 // length = icmp_client_header_length(packet); 321 // process error 322 result = icmp_client_process_packet(packet, &type, 323 &code, NULL, NULL); 324 if (result < 0) 325 return udp_release_and_return(packet, result); 326 length = (size_t) result; 327 if (ERROR_OCCURRED(packet_trim(packet, length, 0))) 328 return udp_release_and_return(packet, 329 ERROR_CODE); 330 break; 331 default: 332 return udp_release_and_return(packet, ENOTSUP); 333 } 334 } 335 287 336 // TODO process received ipopts? 288 337 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL); 289 if (result < 0){338 if (result < 0) 290 339 return udp_release_and_return(packet, result); 291 }292 340 offset = (size_t) result; 293 341 294 342 length = packet_get_data_length(packet); 295 if (length <= 0){343 if (length <= 0) 296 344 return udp_release_and_return(packet, EINVAL); 297 } 298 if(length < UDP_HEADER_SIZE + offset){ 345 if (length < UDP_HEADER_SIZE + offset) 299 346 return udp_release_and_return(packet, NO_DATA); 300 }301 347 302 348 // trim all but UDP header 303 if (ERROR_OCCURRED(packet_trim(packet, offset, 0))){349 if (ERROR_OCCURRED(packet_trim(packet, offset, 0))) 304 350 return udp_release_and_return(packet, ERROR_CODE); 305 }306 351 307 352 // get udp header 308 353 header = (udp_header_ref) packet_get_data(packet); 309 if (! header){354 if (!header) 310 355 return udp_release_and_return(packet, NO_DATA); 311 } 356 312 357 // find the destination socket 313 socket = socket_port_find(&udp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0); 314 if(! socket){ 315 if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){ 316 icmp_destination_unreachable_msg(udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet); 358 socket = socket_port_find(&udp_globals.sockets, 359 ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0); 360 if (!socket) { 361 if (tl_prepare_icmp_packet(udp_globals.net_phone, 362 udp_globals.icmp_phone, packet, error) == EOK) { 363 icmp_destination_unreachable_msg(udp_globals.icmp_phone, 364 ICMP_PORT_UNREACH, 0, packet); 317 365 } 318 366 return EADDRNOTAVAIL; … … 323 371 fragments = 0; 324 372 total_length = ntohs(header->total_length); 373 325 374 // compute header checksum if set 326 if(header->checksum && (! error)){ 327 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest); 328 if(result <= 0){ 375 if (header->checksum && (!error)) { 376 result = packet_get_addr(packet, (uint8_t **) &src, 377 (uint8_t **) &dest); 378 if( result <= 0) 329 379 return udp_release_and_return(packet, result); 330 } 331 if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest, result, total_length, &ip_header, &length))){ 380 381 if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP, 382 src, result, dest, result, total_length, &ip_header, 383 &length))) { 332 384 return udp_release_and_return(packet, ERROR_CODE); 333 } else{385 } else { 334 386 checksum = compute_checksum(0, ip_header, length); 335 // the udp header checksum will be added with the first fragment later 387 // the udp header checksum will be added with the first 388 // fragment later 336 389 free(ip_header); 337 390 } 338 } else{391 } else { 339 392 header->checksum = 0; 340 393 checksum = 0; 341 394 } 342 395 343 do {396 do { 344 397 ++ fragments; 345 398 length = packet_get_data_length(next_packet); 346 if (length <= 0){399 if (length <= 0) 347 400 return udp_release_and_return(packet, NO_DATA); 348 } 349 if(total_length < length){ 350 if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - total_length))){ 351 return udp_release_and_return(packet, ERROR_CODE); 401 402 if (total_length < length) { 403 if (ERROR_OCCURRED(packet_trim(next_packet, 0, 404 length - total_length))) { 405 return udp_release_and_return(packet, 406 ERROR_CODE); 352 407 } 408 353 409 // add partial checksum if set 354 if(header->checksum){ 355 checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet)); 410 if (header->checksum) { 411 checksum = compute_checksum(checksum, 412 packet_get_data(packet), 413 packet_get_data_length(packet)); 356 414 } 415 357 416 // relese the rest of the packet fragments 358 417 tmp_packet = pq_next(next_packet); 359 while (tmp_packet){418 while (tmp_packet) { 360 419 next_packet = pq_detach(tmp_packet); 361 pq_release_remote(udp_globals.net_phone, packet_get_id(tmp_packet)); 420 pq_release_remote(udp_globals.net_phone, 421 packet_get_id(tmp_packet)); 362 422 tmp_packet = next_packet; 363 423 } 424 364 425 // exit the loop 365 426 break; 366 427 } 367 428 total_length -= length; 429 368 430 // add partial checksum if set 369 if(header->checksum){ 370 checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet)); 371 } 372 }while((next_packet = pq_next(next_packet)) && (total_length > 0)); 431 if (header->checksum) { 432 checksum = compute_checksum(checksum, 433 packet_get_data(packet), 434 packet_get_data_length(packet)); 435 } 436 437 } while ((next_packet = pq_next(next_packet)) && (total_length > 0)); 373 438 374 439 // check checksum 375 if(header->checksum){ 376 if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){ 377 if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){ 440 if (header->checksum) { 441 if (flip_checksum(compact_checksum(checksum)) != 442 IP_CHECKSUM_ZERO) { 443 if (tl_prepare_icmp_packet(udp_globals.net_phone, 444 udp_globals.icmp_phone, packet, error) == EOK) { 378 445 // checksum error ICMP 379 icmp_parameter_problem_msg(udp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet); 446 icmp_parameter_problem_msg( 447 udp_globals.icmp_phone, ICMP_PARAM_POINTER, 448 ((size_t) ((void *) &header->checksum)) - 449 ((size_t) ((void *) header)), packet); 380 450 } 381 451 return EINVAL; … … 384 454 385 455 // queue the received packet 386 if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) 387 || ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension))){ 456 if (ERROR_OCCURRED(dyn_fifo_push(&socket->received, 457 packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) || 458 ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone, 459 &udp_globals.dimensions, device_id, &packet_dimension))) { 388 460 return udp_release_and_return(packet, ERROR_CODE); 389 461 } … … 391 463 // notify the destination socket 392 464 fibril_rwlock_write_unlock(&udp_globals.lock); 393 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0, (ipcarg_t) fragments); 465 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 466 (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0, 467 (ipcarg_t) fragments); 468 394 469 return EOK; 395 470 } 396 471 397 int udp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){ 472 int 473 udp_message_standalone(ipc_callid_t callid, ipc_call_t * call, 474 ipc_call_t * answer, int * answer_count) 475 { 398 476 ERROR_DECLARE; 399 477 … … 401 479 402 480 *answer_count = 0; 403 switch(IPC_GET_METHOD(*call)){ 404 case NET_TL_RECEIVED: 405 if(! ERROR_OCCURRED(packet_translate_remote(udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){ 406 ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_UDP, IPC_GET_ERROR(call)); 407 } 408 return ERROR_CODE; 409 case IPC_M_CONNECT_TO_ME: 410 return udp_process_client_messages(callid, * call); 411 } 481 482 switch (IPC_GET_METHOD(*call)) { 483 case NET_TL_RECEIVED: 484 if (!ERROR_OCCURRED(packet_translate_remote( 485 udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))) { 486 ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call), 487 packet, SERVICE_UDP, IPC_GET_ERROR(call)); 488 } 489 return ERROR_CODE; 490 491 case IPC_M_CONNECT_TO_ME: 492 return udp_process_client_messages(callid, * call); 493 } 494 412 495 return ENOTSUP; 413 496 } 414 497 415 int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call){ 498 int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call) 499 { 416 500 int res; 417 501 bool keep_on_going = true; 418 502 socket_cores_t local_sockets; 419 503 int app_phone = IPC_GET_PHONE(&call); 420 struct sockaddr * 504 struct sockaddr *addr; 421 505 int socket_id; 422 506 size_t addrlen; … … 433 517 answer_count = 0; 434 518 435 // The client connection is only in one fibril and therefore no additional locks are needed. 519 // The client connection is only in one fibril and therefore no 520 // additional locks are needed. 436 521 437 522 socket_cores_initialize(&local_sockets); 438 523 439 while (keep_on_going){524 while (keep_on_going) { 440 525 441 526 // answer the call … … 449 534 450 535 // process the call 451 switch(IPC_GET_METHOD(call)){ 452 case IPC_M_PHONE_HUNGUP: 453 keep_on_going = false; 454 res = EHANGUP; 536 switch (IPC_GET_METHOD(call)) { 537 case IPC_M_PHONE_HUNGUP: 538 keep_on_going = false; 539 res = EHANGUP; 540 break; 541 542 case NET_SOCKET: 543 socket_id = SOCKET_GET_SOCKET_ID(call); 544 res = socket_create(&local_sockets, app_phone, NULL, 545 &socket_id); 546 SOCKET_SET_SOCKET_ID(answer, socket_id); 547 548 if (res != EOK) 455 549 break; 456 case NET_SOCKET: 457 socket_id = SOCKET_GET_SOCKET_ID(call); 458 res = socket_create(&local_sockets, app_phone, NULL, &socket_id); 459 SOCKET_SET_SOCKET_ID(answer, socket_id); 460 461 if(res == EOK){ 462 if (tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){ 463 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, packet_dimension->content); 464 } 465 // SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_UDP_FRAGMENT_SIZE); 466 SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE); 467 answer_count = 3; 468 } 550 551 if (tl_get_ip_packet_dimension(udp_globals.ip_phone, 552 &udp_globals.dimensions, DEVICE_INVALID_ID, 553 &packet_dimension) == EOK) { 554 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, 555 packet_dimension->content); 556 } 557 558 // SOCKET_SET_DATA_FRAGMENT_SIZE(answer, 559 // MAX_UDP_FRAGMENT_SIZE); 560 SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE); 561 answer_count = 3; 562 break; 563 564 case NET_SOCKET_BIND: 565 res = data_receive((void **) &addr, &addrlen); 566 if (res != EOK) 469 567 break; 470 case NET_SOCKET_BIND: 471 res = data_receive((void **) &addr, &addrlen); 472 if(res == EOK){ 473 fibril_rwlock_write_lock(&udp_globals.lock); 474 res = socket_bind(&local_sockets, &udp_globals.sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port); 475 fibril_rwlock_write_unlock(&udp_globals.lock); 476 free(addr); 477 } 568 fibril_rwlock_write_lock(&udp_globals.lock); 569 res = socket_bind(&local_sockets, &udp_globals.sockets, 570 SOCKET_GET_SOCKET_ID(call), addr, addrlen, 571 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 572 udp_globals.last_used_port); 573 fibril_rwlock_write_unlock(&udp_globals.lock); 574 free(addr); 575 break; 576 577 case NET_SOCKET_SENDTO: 578 res = data_receive((void **) &addr, &addrlen); 579 if (res != EOK) 478 580 break; 479 case NET_SOCKET_SENDTO: 480 res = data_receive((void **) &addr, &addrlen); 481 if(res == EOK){ 482 fibril_rwlock_write_lock(&udp_globals.lock); 483 res = udp_sendto_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call)); 484 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 485 if(res != EOK){ 486 fibril_rwlock_write_unlock(&udp_globals.lock); 487 }else{ 488 answer_count = 2; 489 } 490 free(addr); 491 } 581 582 fibril_rwlock_write_lock(&udp_globals.lock); 583 res = udp_sendto_message(&local_sockets, 584 SOCKET_GET_SOCKET_ID(call), addr, addrlen, 585 SOCKET_GET_DATA_FRAGMENTS(call), &size, 586 SOCKET_GET_FLAGS(call)); 587 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 588 589 if (res != EOK) 590 fibril_rwlock_write_unlock(&udp_globals.lock); 591 else 592 answer_count = 2; 593 594 free(addr); 595 break; 596 597 case NET_SOCKET_RECVFROM: 598 fibril_rwlock_write_lock(&udp_globals.lock); 599 res = udp_recvfrom_message(&local_sockets, 600 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), 601 &addrlen); 602 fibril_rwlock_write_unlock(&udp_globals.lock); 603 604 if (res <= 0) 492 605 break; 493 case NET_SOCKET_RECVFROM: 494 fibril_rwlock_write_lock(&udp_globals.lock); 495 res = udp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen); 496 fibril_rwlock_write_unlock(&udp_globals.lock); 497 if(res > 0){ 498 SOCKET_SET_READ_DATA_LENGTH(answer, res); 499 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen); 500 answer_count = 3; 501 res = EOK; 502 } 503 break; 504 case NET_SOCKET_CLOSE: 505 fibril_rwlock_write_lock(&udp_globals.lock); 506 res = socket_destroy(udp_globals.net_phone, SOCKET_GET_SOCKET_ID(call), &local_sockets, &udp_globals.sockets, NULL); 507 fibril_rwlock_write_unlock(&udp_globals.lock); 508 break; 509 case NET_SOCKET_GETSOCKOPT: 510 case NET_SOCKET_SETSOCKOPT: 511 default: 512 res = ENOTSUP; 513 break; 606 607 SOCKET_SET_READ_DATA_LENGTH(answer, res); 608 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen); 609 answer_count = 3; 610 res = EOK; 611 break; 612 613 case NET_SOCKET_CLOSE: 614 fibril_rwlock_write_lock(&udp_globals.lock); 615 res = socket_destroy(udp_globals.net_phone, 616 SOCKET_GET_SOCKET_ID(call), &local_sockets, 617 &udp_globals.sockets, NULL); 618 fibril_rwlock_write_unlock(&udp_globals.lock); 619 break; 620 621 case NET_SOCKET_GETSOCKOPT: 622 case NET_SOCKET_SETSOCKOPT: 623 default: 624 res = ENOTSUP; 625 break; 514 626 } 515 627 } … … 519 631 520 632 // release all local sockets 521 socket_cores_release(udp_globals.net_phone, &local_sockets, &udp_globals.sockets, NULL); 633 socket_cores_release(udp_globals.net_phone, &local_sockets, 634 &udp_globals.sockets, NULL); 522 635 523 636 return res; 524 637 } 525 638 526 int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags){ 639 int 640 udp_sendto_message(socket_cores_ref local_sockets, int socket_id, 641 const struct sockaddr *addr, socklen_t addrlen, int fragments, 642 size_t *data_fragment_size, int flags) 643 { 527 644 ERROR_DECLARE; 528 645 … … 544 661 545 662 socket = socket_cores_find(local_sockets, socket_id); 546 if (! socket){663 if (!socket) 547 664 return ENOTSOCK; 548 } 549 550 if((socket->port <= 0) && udp_globals.autobinding){ 665 666 if ((socket->port <= 0) && udp_globals.autobinding) { 551 667 // bind the socket to a random free port if not bound 552 // do {668 // do { 553 669 // try to find a free port 554 670 // fibril_rwlock_read_unlock(&udp_globals.lock); 555 671 // fibril_rwlock_write_lock(&udp_globals.lock); 556 672 // might be changed in the meantime 557 // if(socket->port <= 0){ 558 if(ERROR_OCCURRED(socket_bind_free_port(&udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port))){ 559 // fibril_rwlock_write_unlock(&udp_globals.lock); 560 // fibril_rwlock_read_lock(&udp_globals.lock); 673 // if (socket->port <= 0) { 674 if (ERROR_OCCURRED(socket_bind_free_port( 675 &udp_globals.sockets, socket, 676 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 677 udp_globals.last_used_port))) { 678 // fibril_rwlock_write_unlock( 679 // &udp_globals.lock); 680 // fibril_rwlock_read_lock( 681 // &udp_globals.lock); 561 682 return ERROR_CODE; 562 683 } 563 // set the next port as the search starting port number 684 // set the next port as the search starting port 685 // number 564 686 udp_globals.last_used_port = socket->port; 565 687 // } … … 567 689 // fibril_rwlock_read_lock(&udp_globals.lock); 568 690 // might be changed in the meantime 569 // }while(socket->port <= 0); 570 } 571 572 if(udp_globals.checksum_computing){ 573 if(ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, &device_id, &ip_header, &headerlen))){ 691 // } while (socket->port <= 0); 692 } 693 694 if (udp_globals.checksum_computing) { 695 if (ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone, 696 IPPROTO_UDP, addr, addrlen, &device_id, &ip_header, 697 &headerlen))) { 574 698 return udp_release_and_return(packet, ERROR_CODE); 575 699 } 576 700 // get the device packet dimension 577 // ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension)); 578 } 579 // }else{ 701 // ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, 702 // &udp_globals.dimensions, device_id, &packet_dimension)); 703 } 704 // } else { 580 705 // do not ask all the time 581 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension)); 706 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, 707 &udp_globals.packet_dimension)); 582 708 packet_dimension = &udp_globals.packet_dimension; 583 709 // } 584 710 585 711 // read the first packet fragment 586 result = tl_socket_read_packet_data(udp_globals.net_phone, &packet, UDP_HEADER_SIZE, packet_dimension, addr, addrlen); 587 if(result < 0){ 712 result = tl_socket_read_packet_data(udp_globals.net_phone, &packet, 713 UDP_HEADER_SIZE, packet_dimension, addr, addrlen); 714 if (result < 0) 588 715 return result; 589 } 716 590 717 total_length = (size_t) result; 591 if(udp_globals.checksum_computing){ 592 checksum = compute_checksum(0, packet_get_data(packet), packet_get_data_length(packet)); 593 }else{ 718 if (udp_globals.checksum_computing) 719 checksum = compute_checksum(0, packet_get_data(packet), 720 packet_get_data_length(packet)); 721 else 594 722 checksum = 0; 595 } 723 596 724 // prefix the udp header 597 725 header = PACKET_PREFIX(packet, udp_header_t); 598 if(! header) {726 if(! header) 599 727 return udp_release_and_return(packet, ENOMEM); 600 } 728 601 729 bzero(header, sizeof(*header)); 602 730 // read the rest of the packet fragments 603 for(index = 1; index < fragments; ++ index){ 604 result = tl_socket_read_packet_data(udp_globals.net_phone, &next_packet, 0, packet_dimension, addr, addrlen); 605 if(result < 0){ 731 for (index = 1; index < fragments; ++ index) { 732 result = tl_socket_read_packet_data(udp_globals.net_phone, 733 &next_packet, 0, packet_dimension, addr, addrlen); 734 if (result < 0) 606 735 return udp_release_and_return(packet, result); 607 } 608 if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))){736 737 if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))) 609 738 return udp_release_and_return(packet, ERROR_CODE); 610 } 739 611 740 total_length += (size_t) result; 612 if(udp_globals.checksum_computing){ 613 checksum = compute_checksum(checksum, packet_get_data(next_packet), packet_get_data_length(next_packet)); 614 } 615 } 741 if (udp_globals.checksum_computing) { 742 checksum = compute_checksum(checksum, 743 packet_get_data(next_packet), 744 packet_get_data_length(next_packet)); 745 } 746 } 747 616 748 // set the udp header 617 749 header->source_port = htons((socket->port > 0) ? socket->port : 0); … … 619 751 header->total_length = htons(total_length + sizeof(*header)); 620 752 header->checksum = 0; 621 if (udp_globals.checksum_computing){753 if (udp_globals.checksum_computing) { 622 754 // update the pseudo header 623 if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(ip_header, headerlen, total_length + UDP_HEADER_SIZE))){ 755 if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length( 756 ip_header, headerlen, total_length + UDP_HEADER_SIZE))) { 624 757 free(ip_header); 625 758 return udp_release_and_return(packet, ERROR_CODE); 626 759 } 760 627 761 // finish the checksum computation 628 762 checksum = compute_checksum(checksum, ip_header, headerlen); 629 checksum = compute_checksum(checksum, (uint8_t *) header, sizeof(*header)); 630 header->checksum = htons(flip_checksum(compact_checksum(checksum))); 763 checksum = compute_checksum(checksum, (uint8_t *) header, 764 sizeof(*header)); 765 header->checksum = 766 htons(flip_checksum(compact_checksum(checksum))); 631 767 free(ip_header); 632 } else{768 } else { 633 769 device_id = DEVICE_INVALID_ID; 634 770 } 771 635 772 // prepare the first packet fragment 636 if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0))){ 773 if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 774 0, 0))) { 637 775 return udp_release_and_return(packet, ERROR_CODE); 638 776 } 777 639 778 // send the packet 640 779 fibril_rwlock_write_unlock(&udp_globals.lock); 641 780 ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0); 781 642 782 return EOK; 643 783 } 644 784 645 int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){ 785 int 786 udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, 787 size_t *addrlen) 788 { 646 789 ERROR_DECLARE; 647 790 … … 650 793 packet_t packet; 651 794 udp_header_ref header; 652 struct sockaddr * 795 struct sockaddr *addr; 653 796 size_t length; 654 uint8_t * 797 uint8_t *data; 655 798 int result; 656 799 657 800 // find the socket 658 801 socket = socket_cores_find(local_sockets, socket_id); 659 if (! socket){802 if (!socket) 660 803 return ENOTSOCK; 661 } 804 662 805 // get the next received packet 663 806 packet_id = dyn_fifo_value(&socket->received); 664 if (packet_id < 0){807 if (packet_id < 0) 665 808 return NO_DATA; 666 } 667 ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, packet_id)); 809 810 ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, 811 packet_id)); 812 668 813 // get udp header 669 814 data = packet_get_data(packet); 670 if (! data){815 if (!data) { 671 816 pq_release_remote(udp_globals.net_phone, packet_id); 672 817 return NO_DATA; … … 676 821 // set the source address port 677 822 result = packet_get_addr(packet, (uint8_t **) &addr, NULL); 678 if(ERROR_OCCURRED(tl_set_address_port(addr, result, ntohs(header->source_port)))){ 823 if (ERROR_OCCURRED(tl_set_address_port(addr, result, 824 ntohs(header->source_port)))) { 679 825 pq_release_remote(udp_globals.net_phone, packet_id); 680 826 return ERROR_CODE; 681 827 } 682 828 *addrlen = (size_t) result; 829 683 830 // send the source address 684 831 ERROR_PROPAGATE(data_reply(addr, * addrlen)); … … 693 840 dyn_fifo_pop(&socket->received); 694 841 pq_release_remote(udp_globals.net_phone, packet_get_id(packet)); 842 695 843 // return the total length 696 844 return (int) length; 697 845 } 698 846 699 int udp_release_and_return(packet_t packet, int result){ 847 int udp_release_and_return(packet_t packet, int result) 848 { 700 849 pq_release_remote(udp_globals.net_phone, packet_get_id(packet)); 701 850 return result; … … 704 853 /** Default thread for new connections. 705 854 * 706 * @param[in] iid 707 * @param[in] icall 855 * @param[in] iid The initial message identifier. 856 * @param[in] icall The initial message call structure. 708 857 * 709 858 */ … … 716 865 ipc_answer_0(iid, EOK); 717 866 718 while (true) {867 while (true) { 719 868 ipc_call_t answer; 720 869 int answer_count; … … 731 880 &answer_count); 732 881 733 /* End if said to either by the message or the processing result */ 734 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP)) 882 /* 883 * End if said to either by the message or the processing result 884 */ 885 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || 886 (res == EHANGUP)) 735 887 return; 736 888 … … 742 894 /** Starts the module. 743 895 * 744 * @param argc The count of the command line arguments. Ignored parameter. 745 * @param argv The command line parameters. Ignored parameter. 746 * 747 * @returns EOK on success. 748 * @returns Other error codes as defined for each specific module start function. 749 * 896 * @param argc The count of the command line arguments. Ignored 897 * parameter. 898 * @param argv The command line parameters. Ignored parameter. 899 * 900 * @returns EOK on success. 901 * @returns Other error codes as defined for each specific module 902 * start function. 750 903 */ 751 904 int main(int argc, char *argv[])
Note:
See TracChangeset
for help on using the changeset viewer.