[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup udp
|
---|
[457a6f5] | 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[457a6f5] | 34 | * UDP module implementation.
|
---|
| 35 | * @see udp.h
|
---|
[21580dd] | 36 | */
|
---|
| 37 |
|
---|
[457a6f5] | 38 | #include "udp.h"
|
---|
| 39 | #include "udp_header.h"
|
---|
| 40 | #include "udp_module.h"
|
---|
| 41 |
|
---|
[21580dd] | 42 | #include <async.h>
|
---|
| 43 | #include <fibril_synch.h>
|
---|
| 44 | #include <malloc.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 | #include <ipc/ipc.h>
|
---|
| 47 | #include <ipc/services.h>
|
---|
[514ee46] | 48 | #include <ipc/net.h>
|
---|
[8e3a65c] | 49 | #include <ipc/tl.h>
|
---|
[88e127ee] | 50 | #include <ipc/socket.h>
|
---|
[457a6f5] | 51 | #include <adt/dynamic_fifo.h>
|
---|
[e98b1d5] | 52 | #include <errno.h>
|
---|
[21580dd] | 53 |
|
---|
[058edb6] | 54 | #include <net/socket_codes.h>
|
---|
[fe5d3c1b] | 55 | #include <net/ip_protocols.h>
|
---|
[e4554d4] | 56 | #include <net/in.h>
|
---|
| 57 | #include <net/in6.h>
|
---|
| 58 | #include <net/inet.h>
|
---|
[c7a8442] | 59 | #include <net/modules.h>
|
---|
[058edb6] | 60 |
|
---|
[0a866eeb] | 61 | #include <packet_client.h>
|
---|
[14f1db0] | 62 | #include <packet_remote.h>
|
---|
[849ed54] | 63 | #include <net_checksum.h>
|
---|
| 64 | #include <ip_client.h>
|
---|
| 65 | #include <ip_interface.h>
|
---|
| 66 | #include <icmp_client.h>
|
---|
| 67 | #include <icmp_interface.h>
|
---|
| 68 | #include <net_interface.h>
|
---|
| 69 | #include <socket_core.h>
|
---|
| 70 | #include <tl_common.h>
|
---|
[14f1db0] | 71 | #include <tl_local.h>
|
---|
| 72 | #include <tl_interface.h>
|
---|
[21580dd] | 73 |
|
---|
[7282582] | 74 | /** UDP module name. */
|
---|
[849ed54] | 75 | #define NAME "UDP protocol"
|
---|
| 76 |
|
---|
[7282582] | 77 | /** Default UDP checksum computing. */
|
---|
[21580dd] | 78 | #define NET_DEFAULT_UDP_CHECKSUM_COMPUTING true
|
---|
| 79 |
|
---|
[7282582] | 80 | /** Default UDP autobind when sending via unbound sockets. */
|
---|
[21580dd] | 81 | #define NET_DEFAULT_UDP_AUTOBINDING true
|
---|
| 82 |
|
---|
[7282582] | 83 | /** Maximum UDP fragment size. */
|
---|
| 84 | #define MAX_UDP_FRAGMENT_SIZE 65535
|
---|
[21580dd] | 85 |
|
---|
[7282582] | 86 | /** Free ports pool start. */
|
---|
| 87 | #define UDP_FREE_PORTS_START 1025
|
---|
[21580dd] | 88 |
|
---|
[7282582] | 89 | /** Free ports pool end. */
|
---|
[21580dd] | 90 | #define UDP_FREE_PORTS_END 65535
|
---|
| 91 |
|
---|
[457a6f5] | 92 | /** UDP global data. */
|
---|
| 93 | udp_globals_t udp_globals;
|
---|
[21580dd] | 94 |
|
---|
[457a6f5] | 95 | /** Initializes the UDP module.
|
---|
[7282582] | 96 | *
|
---|
[457a6f5] | 97 | * @param[in] client_connection The client connection processing function. The
|
---|
| 98 | * module skeleton propagates its own one.
|
---|
[1bfd3d3] | 99 | * @return EOK on success.
|
---|
| 100 | * @return ENOMEM if there is not enough memory left.
|
---|
[21580dd] | 101 | */
|
---|
[7282582] | 102 | int udp_initialize(async_client_conn_t client_connection)
|
---|
| 103 | {
|
---|
| 104 | measured_string_t names[] = {
|
---|
| 105 | {
|
---|
[065d2d5] | 106 | (char *) "UDP_CHECKSUM_COMPUTING",
|
---|
[7282582] | 107 | 22
|
---|
| 108 | },
|
---|
| 109 | {
|
---|
[065d2d5] | 110 | (char *) "UDP_AUTOBINDING",
|
---|
[7282582] | 111 | 15
|
---|
| 112 | }
|
---|
| 113 | };
|
---|
[4eca056] | 114 | measured_string_t *configuration;
|
---|
[aadf01e] | 115 | size_t count = sizeof(names) / sizeof(measured_string_t);
|
---|
[457a6f5] | 116 | char *data;
|
---|
[46ae62c] | 117 | int rc;
|
---|
[21580dd] | 118 |
|
---|
[aadf01e] | 119 | fibril_rwlock_initialize(&udp_globals.lock);
|
---|
| 120 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
[7282582] | 121 |
|
---|
| 122 | udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
|
---|
| 123 | ICMP_CONNECT_TIMEOUT);
|
---|
[46ae62c] | 124 |
|
---|
[7282582] | 125 | udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
|
---|
[5a868d7] | 126 | SERVICE_UDP, client_connection);
|
---|
[46ae62c] | 127 | if (udp_globals.ip_phone < 0) {
|
---|
| 128 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[21580dd] | 129 | return udp_globals.ip_phone;
|
---|
[46ae62c] | 130 | }
|
---|
[7282582] | 131 |
|
---|
[fb04cba8] | 132 | /* Read default packet dimensions */
|
---|
[46ae62c] | 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) {
|
---|
[aadf01e] | 148 | socket_ports_destroy(&udp_globals.sockets);
|
---|
[46ae62c] | 149 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 150 | return rc;
|
---|
[21580dd] | 151 | }
|
---|
[46ae62c] | 152 |
|
---|
[aadf01e] | 153 | udp_globals.packet_dimension.prefix += sizeof(udp_header_t);
|
---|
| 154 | udp_globals.packet_dimension.content -= sizeof(udp_header_t);
|
---|
[21580dd] | 155 | udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
|
---|
[7282582] | 156 |
|
---|
[21580dd] | 157 | udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
|
---|
| 158 | udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
|
---|
[065d2d5] | 159 |
|
---|
[fb04cba8] | 160 | /* Get configuration */
|
---|
[aadf01e] | 161 | configuration = &names[0];
|
---|
[46ae62c] | 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 |
|
---|
[7282582] | 170 | if (configuration) {
|
---|
| 171 | if (configuration[0].value)
|
---|
| 172 | udp_globals.checksum_computing =
|
---|
| 173 | (configuration[0].value[0] == 'y');
|
---|
| 174 |
|
---|
| 175 | if (configuration[1].value)
|
---|
| 176 | udp_globals.autobinding =
|
---|
| 177 | (configuration[1].value[0] == 'y');
|
---|
| 178 |
|
---|
[aadf01e] | 179 | net_free_settings(configuration, data);
|
---|
[21580dd] | 180 | }
|
---|
[7282582] | 181 |
|
---|
[aadf01e] | 182 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[21580dd] | 183 | return EOK;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[457a6f5] | 186 | /** Releases the packet and returns the result.
|
---|
| 187 | *
|
---|
| 188 | * @param[in] packet The packet queue to be released.
|
---|
| 189 | * @param[in] result The result to be returned.
|
---|
| 190 | * @return The result parameter.
|
---|
| 191 | */
|
---|
| 192 | static int udp_release_and_return(packet_t packet, int result)
|
---|
[7282582] | 193 | {
|
---|
[457a6f5] | 194 | pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
|
---|
[21580dd] | 195 | return result;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[457a6f5] | 198 | /** Processes the received UDP packet queue.
|
---|
| 199 | *
|
---|
| 200 | * Notifies the destination socket application.
|
---|
| 201 | * Releases the packet on error or sends an ICMP error notification.
|
---|
| 202 | *
|
---|
| 203 | * @param[in] device_id The receiving device identifier.
|
---|
| 204 | * @param[in,out] packet The received packet queue.
|
---|
| 205 | * @param[in] error The packet error reporting service. Prefixes the
|
---|
| 206 | * received packet.
|
---|
[1bfd3d3] | 207 | * @return EOK on success.
|
---|
| 208 | * @return EINVAL if the packet is not valid.
|
---|
| 209 | * @return EINVAL if the stored packet address is not the
|
---|
[457a6f5] | 210 | * an_addr_t.
|
---|
[1bfd3d3] | 211 | * @return EINVAL if the packet does not contain any data.
|
---|
| 212 | * @return NO_DATA if the packet content is shorter than the user
|
---|
[457a6f5] | 213 | * datagram header.
|
---|
[1bfd3d3] | 214 | * @return ENOMEM if there is not enough memory left.
|
---|
| 215 | * @return EADDRNOTAVAIL if the destination socket does not exist.
|
---|
| 216 | * @return Other error codes as defined for the
|
---|
[457a6f5] | 217 | * ip_client_process_packet() function.
|
---|
| 218 | */
|
---|
[fb04cba8] | 219 | static int udp_process_packet(device_id_t device_id, packet_t packet,
|
---|
| 220 | services_t error)
|
---|
[7282582] | 221 | {
|
---|
[aadf01e] | 222 | size_t length;
|
---|
| 223 | size_t offset;
|
---|
| 224 | int result;
|
---|
[4e5c7ba] | 225 | udp_header_t *header;
|
---|
[88a1bb9] | 226 | socket_core_t *socket;
|
---|
[aadf01e] | 227 | packet_t next_packet;
|
---|
| 228 | size_t total_length;
|
---|
| 229 | uint32_t checksum;
|
---|
| 230 | int fragments;
|
---|
| 231 | packet_t tmp_packet;
|
---|
| 232 | icmp_type_t type;
|
---|
| 233 | icmp_code_t code;
|
---|
[14f1db0] | 234 | void *ip_header;
|
---|
[7282582] | 235 | struct sockaddr *src;
|
---|
| 236 | struct sockaddr *dest;
|
---|
[f772bc55] | 237 | packet_dimension_t *packet_dimension;
|
---|
[46ae62c] | 238 | int rc;
|
---|
[aadf01e] | 239 |
|
---|
[457a6f5] | 240 | switch (error) {
|
---|
| 241 | case SERVICE_NONE:
|
---|
| 242 | break;
|
---|
| 243 | case SERVICE_ICMP:
|
---|
[fb04cba8] | 244 | /* Ignore error */
|
---|
[457a6f5] | 245 | // length = icmp_client_header_length(packet);
|
---|
[fb04cba8] | 246 |
|
---|
| 247 | /* Process error */
|
---|
[457a6f5] | 248 | result = icmp_client_process_packet(packet, &type,
|
---|
| 249 | &code, NULL, NULL);
|
---|
| 250 | if (result < 0)
|
---|
| 251 | return udp_release_and_return(packet, result);
|
---|
| 252 | length = (size_t) result;
|
---|
[46ae62c] | 253 | rc = packet_trim(packet, length, 0);
|
---|
| 254 | if (rc != EOK)
|
---|
| 255 | return udp_release_and_return(packet, rc);
|
---|
[457a6f5] | 256 | break;
|
---|
| 257 | default:
|
---|
| 258 | return udp_release_and_return(packet, ENOTSUP);
|
---|
[21580dd] | 259 | }
|
---|
[7282582] | 260 |
|
---|
[fb04cba8] | 261 | /* TODO process received ipopts? */
|
---|
[aadf01e] | 262 | result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
|
---|
[7282582] | 263 | if (result < 0)
|
---|
[aadf01e] | 264 | return udp_release_and_return(packet, result);
|
---|
| 265 | offset = (size_t) result;
|
---|
[21580dd] | 266 |
|
---|
[aadf01e] | 267 | length = packet_get_data_length(packet);
|
---|
[7282582] | 268 | if (length <= 0)
|
---|
[aadf01e] | 269 | return udp_release_and_return(packet, EINVAL);
|
---|
[7282582] | 270 | if (length < UDP_HEADER_SIZE + offset)
|
---|
[aadf01e] | 271 | return udp_release_and_return(packet, NO_DATA);
|
---|
[21580dd] | 272 |
|
---|
[fb04cba8] | 273 | /* Trim all but UDP header */
|
---|
[46ae62c] | 274 | rc = packet_trim(packet, offset, 0);
|
---|
| 275 | if (rc != EOK)
|
---|
| 276 | return udp_release_and_return(packet, rc);
|
---|
[21580dd] | 277 |
|
---|
[fb04cba8] | 278 | /* Get UDP header */
|
---|
[4e5c7ba] | 279 | header = (udp_header_t *) packet_get_data(packet);
|
---|
[7282582] | 280 | if (!header)
|
---|
[aadf01e] | 281 | return udp_release_and_return(packet, NO_DATA);
|
---|
[7282582] | 282 |
|
---|
[fb04cba8] | 283 | /* Find the destination socket */
|
---|
[7282582] | 284 | socket = socket_port_find(&udp_globals.sockets,
|
---|
| 285 | ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
|
---|
| 286 | if (!socket) {
|
---|
| 287 | if (tl_prepare_icmp_packet(udp_globals.net_phone,
|
---|
| 288 | udp_globals.icmp_phone, packet, error) == EOK) {
|
---|
| 289 | icmp_destination_unreachable_msg(udp_globals.icmp_phone,
|
---|
| 290 | ICMP_PORT_UNREACH, 0, packet);
|
---|
[21580dd] | 291 | }
|
---|
| 292 | return EADDRNOTAVAIL;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[fb04cba8] | 295 | /* Count the received packet fragments */
|
---|
[21580dd] | 296 | next_packet = packet;
|
---|
| 297 | fragments = 0;
|
---|
[aadf01e] | 298 | total_length = ntohs(header->total_length);
|
---|
[7282582] | 299 |
|
---|
[fb04cba8] | 300 | /* Compute header checksum if set */
|
---|
[457a6f5] | 301 | if (header->checksum && !error) {
|
---|
[7282582] | 302 | result = packet_get_addr(packet, (uint8_t **) &src,
|
---|
| 303 | (uint8_t **) &dest);
|
---|
[457a6f5] | 304 | if (result <= 0)
|
---|
[aadf01e] | 305 | return udp_release_and_return(packet, result);
|
---|
[46ae62c] | 306 |
|
---|
| 307 | rc = ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest,
|
---|
| 308 | result, total_length, &ip_header, &length);
|
---|
| 309 | if (rc != EOK) {
|
---|
| 310 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 311 | } else {
|
---|
[aadf01e] | 312 | checksum = compute_checksum(0, ip_header, length);
|
---|
[fb04cba8] | 313 | /*
|
---|
| 314 | * The udp header checksum will be added with the first
|
---|
| 315 | * fragment later.
|
---|
| 316 | */
|
---|
[aadf01e] | 317 | free(ip_header);
|
---|
[21580dd] | 318 | }
|
---|
[7282582] | 319 | } else {
|
---|
[21580dd] | 320 | header->checksum = 0;
|
---|
| 321 | checksum = 0;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[7282582] | 324 | do {
|
---|
[457a6f5] | 325 | fragments++;
|
---|
[aadf01e] | 326 | length = packet_get_data_length(next_packet);
|
---|
[7282582] | 327 | if (length <= 0)
|
---|
[aadf01e] | 328 | return udp_release_and_return(packet, NO_DATA);
|
---|
[7282582] | 329 |
|
---|
| 330 | if (total_length < length) {
|
---|
[46ae62c] | 331 | rc = packet_trim(next_packet, 0, length - total_length);
|
---|
| 332 | if (rc != EOK)
|
---|
| 333 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 334 |
|
---|
[fb04cba8] | 335 | /* Add partial checksum if set */
|
---|
[7282582] | 336 | if (header->checksum) {
|
---|
| 337 | checksum = compute_checksum(checksum,
|
---|
| 338 | packet_get_data(packet),
|
---|
| 339 | packet_get_data_length(packet));
|
---|
[21580dd] | 340 | }
|
---|
[7282582] | 341 |
|
---|
[fb04cba8] | 342 | /* Relese the rest of the packet fragments */
|
---|
[aadf01e] | 343 | tmp_packet = pq_next(next_packet);
|
---|
[7282582] | 344 | while (tmp_packet) {
|
---|
[aadf01e] | 345 | next_packet = pq_detach(tmp_packet);
|
---|
[7282582] | 346 | pq_release_remote(udp_globals.net_phone,
|
---|
| 347 | packet_get_id(tmp_packet));
|
---|
[21580dd] | 348 | tmp_packet = next_packet;
|
---|
| 349 | }
|
---|
[7282582] | 350 |
|
---|
[fb04cba8] | 351 | /* Exit the loop */
|
---|
[21580dd] | 352 | break;
|
---|
| 353 | }
|
---|
| 354 | total_length -= length;
|
---|
[7282582] | 355 |
|
---|
[fb04cba8] | 356 | /* Add partial checksum if set */
|
---|
[7282582] | 357 | if (header->checksum) {
|
---|
| 358 | checksum = compute_checksum(checksum,
|
---|
| 359 | packet_get_data(packet),
|
---|
| 360 | packet_get_data_length(packet));
|
---|
[21580dd] | 361 | }
|
---|
[7282582] | 362 |
|
---|
| 363 | } while ((next_packet = pq_next(next_packet)) && (total_length > 0));
|
---|
[21580dd] | 364 |
|
---|
[fb04cba8] | 365 | /* Verify checksum */
|
---|
[7282582] | 366 | if (header->checksum) {
|
---|
| 367 | if (flip_checksum(compact_checksum(checksum)) !=
|
---|
| 368 | IP_CHECKSUM_ZERO) {
|
---|
| 369 | if (tl_prepare_icmp_packet(udp_globals.net_phone,
|
---|
| 370 | udp_globals.icmp_phone, packet, error) == EOK) {
|
---|
[fb04cba8] | 371 | /* Checksum error ICMP */
|
---|
[7282582] | 372 | icmp_parameter_problem_msg(
|
---|
| 373 | udp_globals.icmp_phone, ICMP_PARAM_POINTER,
|
---|
| 374 | ((size_t) ((void *) &header->checksum)) -
|
---|
| 375 | ((size_t) ((void *) header)), packet);
|
---|
[21580dd] | 376 | }
|
---|
| 377 | return EINVAL;
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[fb04cba8] | 381 | /* Queue the received packet */
|
---|
[46ae62c] | 382 | rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
|
---|
| 383 | SOCKET_MAX_RECEIVED_SIZE);
|
---|
| 384 | if (rc != EOK)
|
---|
| 385 | return udp_release_and_return(packet, rc);
|
---|
| 386 |
|
---|
| 387 | rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
|
---|
| 388 | &udp_globals.dimensions, device_id, &packet_dimension);
|
---|
| 389 | if (rc != EOK)
|
---|
| 390 | return udp_release_and_return(packet, rc);
|
---|
[21580dd] | 391 |
|
---|
[fb04cba8] | 392 | /* Notify the destination socket */
|
---|
[aadf01e] | 393 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[7282582] | 394 | async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
|
---|
| 395 | (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0,
|
---|
| 396 | (ipcarg_t) fragments);
|
---|
| 397 |
|
---|
[2e99277] | 398 | return EOK;
|
---|
[21580dd] | 399 | }
|
---|
| 400 |
|
---|
[457a6f5] | 401 | /** Processes the received UDP packet queue.
|
---|
| 402 | *
|
---|
| 403 | * Is used as an entry point from the underlying IP module.
|
---|
| 404 | * Locks the global lock and calls udp_process_packet() function.
|
---|
| 405 | *
|
---|
| 406 | * @param[in] device_id The receiving device identifier.
|
---|
| 407 | * @param[in,out] packet The received packet queue.
|
---|
| 408 | * @param receiver The target service. Ignored parameter.
|
---|
| 409 | * @param[in] error The packet error reporting service. Prefixes the
|
---|
| 410 | * received packet.
|
---|
[1bfd3d3] | 411 | * @return EOK on success.
|
---|
| 412 | * @return Other error codes as defined for the
|
---|
[457a6f5] | 413 | * udp_process_packet() function.
|
---|
| 414 | */
|
---|
[fb04cba8] | 415 | static int udp_received_msg(device_id_t device_id, packet_t packet,
|
---|
| 416 | services_t receiver, services_t error)
|
---|
[7282582] | 417 | {
|
---|
[457a6f5] | 418 | int result;
|
---|
[a8a13d0] | 419 |
|
---|
[457a6f5] | 420 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 421 | result = udp_process_packet(device_id, packet, error);
|
---|
| 422 | if (result != EOK)
|
---|
| 423 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[21580dd] | 424 |
|
---|
[457a6f5] | 425 | return result;
|
---|
[21580dd] | 426 | }
|
---|
| 427 |
|
---|
[457a6f5] | 428 | /** Sends data from the socket to the remote address.
|
---|
| 429 | *
|
---|
| 430 | * Binds the socket to a free port if not already connected/bound.
|
---|
| 431 | * Handles the NET_SOCKET_SENDTO message.
|
---|
| 432 | * Supports AF_INET and AF_INET6 address families.
|
---|
| 433 | *
|
---|
| 434 | * @param[in,out] local_sockets The application local sockets.
|
---|
| 435 | * @param[in] socket_id Socket identifier.
|
---|
| 436 | * @param[in] addr The destination address.
|
---|
| 437 | * @param[in] addrlen The address length.
|
---|
| 438 | * @param[in] fragments The number of data fragments.
|
---|
| 439 | * @param[out] data_fragment_size The data fragment size in bytes.
|
---|
| 440 | * @param[in] flags Various send flags.
|
---|
[1bfd3d3] | 441 | * @return EOK on success.
|
---|
| 442 | * @return EAFNOTSUPPORT if the address family is not supported.
|
---|
| 443 | * @return ENOTSOCK if the socket is not found.
|
---|
| 444 | * @return EINVAL if the address is invalid.
|
---|
| 445 | * @return ENOTCONN if the sending socket is not and cannot be
|
---|
[457a6f5] | 446 | * bound.
|
---|
[1bfd3d3] | 447 | * @return ENOMEM if there is not enough memory left.
|
---|
| 448 | * @return Other error codes as defined for the
|
---|
[457a6f5] | 449 | * socket_read_packet_data() function.
|
---|
[1bfd3d3] | 450 | * @return Other error codes as defined for the
|
---|
[457a6f5] | 451 | * ip_client_prepare_packet() function.
|
---|
[1bfd3d3] | 452 | * @return Other error codes as defined for the ip_send_msg()
|
---|
[457a6f5] | 453 | * function.
|
---|
| 454 | */
|
---|
[aaa3f33a] | 455 | static int udp_sendto_message(socket_cores_t *local_sockets, int socket_id,
|
---|
[7282582] | 456 | const struct sockaddr *addr, socklen_t addrlen, int fragments,
|
---|
| 457 | size_t *data_fragment_size, int flags)
|
---|
| 458 | {
|
---|
[88a1bb9] | 459 | socket_core_t *socket;
|
---|
[aadf01e] | 460 | packet_t packet;
|
---|
| 461 | packet_t next_packet;
|
---|
[4e5c7ba] | 462 | udp_header_t *header;
|
---|
[aadf01e] | 463 | int index;
|
---|
| 464 | size_t total_length;
|
---|
| 465 | int result;
|
---|
| 466 | uint16_t dest_port;
|
---|
| 467 | uint32_t checksum;
|
---|
[14f1db0] | 468 | void *ip_header;
|
---|
[aadf01e] | 469 | size_t headerlen;
|
---|
| 470 | device_id_t device_id;
|
---|
[f772bc55] | 471 | packet_dimension_t *packet_dimension;
|
---|
[46ae62c] | 472 | int rc;
|
---|
| 473 |
|
---|
| 474 | rc = tl_get_address_port(addr, addrlen, &dest_port);
|
---|
| 475 | if (rc != EOK)
|
---|
| 476 | return rc;
|
---|
[aadf01e] | 477 |
|
---|
| 478 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
[7282582] | 479 | if (!socket)
|
---|
[aadf01e] | 480 | return ENOTSOCK;
|
---|
| 481 |
|
---|
[7282582] | 482 | if ((socket->port <= 0) && udp_globals.autobinding) {
|
---|
[fb04cba8] | 483 | /* Bind the socket to a random free port if not bound */
|
---|
[a873201] | 484 | rc = socket_bind_free_port(&udp_globals.sockets, socket,
|
---|
| 485 | UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
---|
| 486 | udp_globals.last_used_port);
|
---|
| 487 | if (rc != EOK)
|
---|
| 488 | return rc;
|
---|
[fb04cba8] | 489 | /* Set the next port as the search starting port number */
|
---|
[a873201] | 490 | udp_globals.last_used_port = socket->port;
|
---|
[21580dd] | 491 | }
|
---|
| 492 |
|
---|
[7282582] | 493 | if (udp_globals.checksum_computing) {
|
---|
[46ae62c] | 494 | rc = ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr,
|
---|
| 495 | addrlen, &device_id, &ip_header, &headerlen);
|
---|
| 496 | if (rc != EOK)
|
---|
[d94f309] | 497 | return rc;
|
---|
[fb04cba8] | 498 | /* Get the device packet dimension */
|
---|
[46ae62c] | 499 | // rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
|
---|
| 500 | // &udp_globals.dimensions, device_id, &packet_dimension);
|
---|
| 501 | // if (rc != EOK)
|
---|
| 502 | // return rc;
|
---|
[21580dd] | 503 | }
|
---|
[7282582] | 504 | // } else {
|
---|
[fb04cba8] | 505 | /* Do not ask all the time */
|
---|
[46ae62c] | 506 | rc = ip_packet_size_req(udp_globals.ip_phone, -1,
|
---|
| 507 | &udp_globals.packet_dimension);
|
---|
| 508 | if (rc != EOK)
|
---|
| 509 | return rc;
|
---|
[aadf01e] | 510 | packet_dimension = &udp_globals.packet_dimension;
|
---|
[21580dd] | 511 | // }
|
---|
| 512 |
|
---|
[fb04cba8] | 513 | /* Read the first packet fragment */
|
---|
[7282582] | 514 | result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
|
---|
| 515 | UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
|
---|
| 516 | if (result < 0)
|
---|
[aadf01e] | 517 | return result;
|
---|
[7282582] | 518 |
|
---|
[aadf01e] | 519 | total_length = (size_t) result;
|
---|
[7282582] | 520 | if (udp_globals.checksum_computing)
|
---|
| 521 | checksum = compute_checksum(0, packet_get_data(packet),
|
---|
| 522 | packet_get_data_length(packet));
|
---|
| 523 | else
|
---|
[21580dd] | 524 | checksum = 0;
|
---|
[7282582] | 525 |
|
---|
[fb04cba8] | 526 | /* Prefix the UDP header */
|
---|
[aadf01e] | 527 | header = PACKET_PREFIX(packet, udp_header_t);
|
---|
[457a6f5] | 528 | if (!header)
|
---|
[aadf01e] | 529 | return udp_release_and_return(packet, ENOMEM);
|
---|
[7282582] | 530 |
|
---|
[aadf01e] | 531 | bzero(header, sizeof(*header));
|
---|
[fb04cba8] | 532 |
|
---|
| 533 | /* Read the rest of the packet fragments */
|
---|
[457a6f5] | 534 | for (index = 1; index < fragments; index++) {
|
---|
[7282582] | 535 | result = tl_socket_read_packet_data(udp_globals.net_phone,
|
---|
| 536 | &next_packet, 0, packet_dimension, addr, addrlen);
|
---|
| 537 | if (result < 0)
|
---|
[aadf01e] | 538 | return udp_release_and_return(packet, result);
|
---|
[7282582] | 539 |
|
---|
[46ae62c] | 540 | rc = pq_add(&packet, next_packet, index, 0);
|
---|
| 541 | if (rc != EOK)
|
---|
| 542 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 543 |
|
---|
[aadf01e] | 544 | total_length += (size_t) result;
|
---|
[7282582] | 545 | if (udp_globals.checksum_computing) {
|
---|
| 546 | checksum = compute_checksum(checksum,
|
---|
| 547 | packet_get_data(next_packet),
|
---|
| 548 | packet_get_data_length(next_packet));
|
---|
[21580dd] | 549 | }
|
---|
| 550 | }
|
---|
[7282582] | 551 |
|
---|
[fb04cba8] | 552 | /* Set the UDP header */
|
---|
[aadf01e] | 553 | header->source_port = htons((socket->port > 0) ? socket->port : 0);
|
---|
| 554 | header->destination_port = htons(dest_port);
|
---|
| 555 | header->total_length = htons(total_length + sizeof(*header));
|
---|
[21580dd] | 556 | header->checksum = 0;
|
---|
[fb04cba8] | 557 |
|
---|
[7282582] | 558 | if (udp_globals.checksum_computing) {
|
---|
[fb04cba8] | 559 | /* Update the pseudo header */
|
---|
[46ae62c] | 560 | rc = ip_client_set_pseudo_header_data_length(ip_header,
|
---|
| 561 | headerlen, total_length + UDP_HEADER_SIZE);
|
---|
| 562 | if (rc != EOK) {
|
---|
[aadf01e] | 563 | free(ip_header);
|
---|
[46ae62c] | 564 | return udp_release_and_return(packet, rc);
|
---|
[21580dd] | 565 | }
|
---|
[7282582] | 566 |
|
---|
[fb04cba8] | 567 | /* Finish the checksum computation */
|
---|
[aadf01e] | 568 | checksum = compute_checksum(checksum, ip_header, headerlen);
|
---|
[7282582] | 569 | checksum = compute_checksum(checksum, (uint8_t *) header,
|
---|
| 570 | sizeof(*header));
|
---|
| 571 | header->checksum =
|
---|
| 572 | htons(flip_checksum(compact_checksum(checksum)));
|
---|
[aadf01e] | 573 | free(ip_header);
|
---|
[7282582] | 574 | } else {
|
---|
[ede63e4] | 575 | device_id = DEVICE_INVALID_ID;
|
---|
[21580dd] | 576 | }
|
---|
[7282582] | 577 |
|
---|
[fb04cba8] | 578 | /* Prepare the first packet fragment */
|
---|
[46ae62c] | 579 | rc = ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0);
|
---|
| 580 | if (rc != EOK)
|
---|
| 581 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 582 |
|
---|
[f3cb50e] | 583 | /* Release the UDP global lock on success. */
|
---|
[aadf01e] | 584 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[f3cb50e] | 585 |
|
---|
[fb04cba8] | 586 | /* Send the packet */
|
---|
[aadf01e] | 587 | ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
|
---|
[7282582] | 588 |
|
---|
[21580dd] | 589 | return EOK;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
[457a6f5] | 592 | /** Receives data to the socket.
|
---|
| 593 | *
|
---|
| 594 | * Handles the NET_SOCKET_RECVFROM message.
|
---|
| 595 | * Replies the source address as well.
|
---|
| 596 | *
|
---|
| 597 | * @param[in] local_sockets The application local sockets.
|
---|
| 598 | * @param[in] socket_id Socket identifier.
|
---|
| 599 | * @param[in] flags Various receive flags.
|
---|
| 600 | * @param[out] addrlen The source address length.
|
---|
[1bfd3d3] | 601 | * @return The number of bytes received.
|
---|
| 602 | * @return ENOTSOCK if the socket is not found.
|
---|
| 603 | * @return NO_DATA if there are no received packets or data.
|
---|
| 604 | * @return ENOMEM if there is not enough memory left.
|
---|
| 605 | * @return EINVAL if the received address is not an IP address.
|
---|
| 606 | * @return Other error codes as defined for the packet_translate()
|
---|
[457a6f5] | 607 | * function.
|
---|
[1bfd3d3] | 608 | * @return Other error codes as defined for the data_reply()
|
---|
[457a6f5] | 609 | * function.
|
---|
| 610 | */
|
---|
[aaa3f33a] | 611 | static int udp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
|
---|
[fb04cba8] | 612 | int flags, size_t *addrlen)
|
---|
[7282582] | 613 | {
|
---|
[88a1bb9] | 614 | socket_core_t *socket;
|
---|
[aadf01e] | 615 | int packet_id;
|
---|
| 616 | packet_t packet;
|
---|
[4e5c7ba] | 617 | udp_header_t *header;
|
---|
[7282582] | 618 | struct sockaddr *addr;
|
---|
[aadf01e] | 619 | size_t length;
|
---|
[7282582] | 620 | uint8_t *data;
|
---|
[aadf01e] | 621 | int result;
|
---|
[46ae62c] | 622 | int rc;
|
---|
[21580dd] | 623 |
|
---|
[fb04cba8] | 624 | /* Find the socket */
|
---|
[aadf01e] | 625 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
[7282582] | 626 | if (!socket)
|
---|
[aadf01e] | 627 | return ENOTSOCK;
|
---|
[7282582] | 628 |
|
---|
[fb04cba8] | 629 | /* Get the next received packet */
|
---|
[08042bd] | 630 | packet_id = dyn_fifo_value(&socket->received);
|
---|
[7282582] | 631 | if (packet_id < 0)
|
---|
[aadf01e] | 632 | return NO_DATA;
|
---|
[46ae62c] | 633 |
|
---|
| 634 | rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
|
---|
[08042bd] | 635 | if (rc != EOK) {
|
---|
| 636 | (void) dyn_fifo_pop(&socket->received);
|
---|
[46ae62c] | 637 | return rc;
|
---|
[08042bd] | 638 | }
|
---|
[7282582] | 639 |
|
---|
[fb04cba8] | 640 | /* Get UDP header */
|
---|
[aadf01e] | 641 | data = packet_get_data(packet);
|
---|
[08042bd] | 642 | if (!data) {
|
---|
| 643 | (void) dyn_fifo_pop(&socket->received);
|
---|
[de229f8e] | 644 | return udp_release_and_return(packet, NO_DATA);
|
---|
[08042bd] | 645 | }
|
---|
[4e5c7ba] | 646 | header = (udp_header_t *) data;
|
---|
[21580dd] | 647 |
|
---|
[fb04cba8] | 648 | /* Set the source address port */
|
---|
[aadf01e] | 649 | result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
|
---|
[46ae62c] | 650 | rc = tl_set_address_port(addr, result, ntohs(header->source_port));
|
---|
[08042bd] | 651 | if (rc != EOK) {
|
---|
| 652 | (void) dyn_fifo_pop(&socket->received);
|
---|
[de229f8e] | 653 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 654 | }
|
---|
[aadf01e] | 655 | *addrlen = (size_t) result;
|
---|
[7282582] | 656 |
|
---|
[fb04cba8] | 657 | /* Send the source address */
|
---|
[46ae62c] | 658 | rc = data_reply(addr, *addrlen);
|
---|
[08042bd] | 659 | switch (rc) {
|
---|
| 660 | case EOK:
|
---|
| 661 | break;
|
---|
| 662 | case EOVERFLOW:
|
---|
| 663 | return rc;
|
---|
| 664 | default:
|
---|
| 665 | (void) dyn_fifo_pop(&socket->received);
|
---|
[a63ff7d] | 666 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 667 | }
|
---|
[21580dd] | 668 |
|
---|
[fb04cba8] | 669 | /* Trim the header */
|
---|
[46ae62c] | 670 | rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
|
---|
[08042bd] | 671 | if (rc != EOK) {
|
---|
| 672 | (void) dyn_fifo_pop(&socket->received);
|
---|
[a63ff7d] | 673 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 674 | }
|
---|
[21580dd] | 675 |
|
---|
[fb04cba8] | 676 | /* Reply the packets */
|
---|
[46ae62c] | 677 | rc = socket_reply_packets(packet, &length);
|
---|
[08042bd] | 678 | switch (rc) {
|
---|
| 679 | case EOK:
|
---|
| 680 | break;
|
---|
| 681 | case EOVERFLOW:
|
---|
| 682 | return rc;
|
---|
| 683 | default:
|
---|
| 684 | (void) dyn_fifo_pop(&socket->received);
|
---|
[a63ff7d] | 685 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 686 | }
|
---|
| 687 |
|
---|
| 688 | (void) dyn_fifo_pop(&socket->received);
|
---|
[7282582] | 689 |
|
---|
[fb04cba8] | 690 | /* Release the packet and return the total length */
|
---|
[de229f8e] | 691 | return udp_release_and_return(packet, (int) length);
|
---|
[21580dd] | 692 | }
|
---|
| 693 |
|
---|
[457a6f5] | 694 | /** Processes the socket client messages.
|
---|
| 695 | *
|
---|
| 696 | * Runs until the client module disconnects.
|
---|
| 697 | *
|
---|
| 698 | * @param[in] callid The message identifier.
|
---|
| 699 | * @param[in] call The message parameters.
|
---|
[1bfd3d3] | 700 | * @return EOK on success.
|
---|
[457a6f5] | 701 | *
|
---|
| 702 | * @see socket.h
|
---|
| 703 | */
|
---|
| 704 | static int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
|
---|
[7282582] | 705 | {
|
---|
[457a6f5] | 706 | int res;
|
---|
| 707 | bool keep_on_going = true;
|
---|
| 708 | socket_cores_t local_sockets;
|
---|
| 709 | int app_phone = IPC_GET_PHONE(&call);
|
---|
| 710 | struct sockaddr *addr;
|
---|
| 711 | int socket_id;
|
---|
| 712 | size_t addrlen;
|
---|
| 713 | size_t size;
|
---|
| 714 | ipc_call_t answer;
|
---|
| 715 | int answer_count;
|
---|
[f772bc55] | 716 | packet_dimension_t *packet_dimension;
|
---|
[457a6f5] | 717 |
|
---|
| 718 | /*
|
---|
| 719 | * Accept the connection
|
---|
| 720 | * - Answer the first IPC_M_CONNECT_TO_ME call.
|
---|
| 721 | */
|
---|
| 722 | res = EOK;
|
---|
| 723 | answer_count = 0;
|
---|
| 724 |
|
---|
[fb04cba8] | 725 | /*
|
---|
| 726 | * The client connection is only in one fibril and therefore no
|
---|
| 727 | * additional locks are needed.
|
---|
| 728 | */
|
---|
[457a6f5] | 729 |
|
---|
| 730 | socket_cores_initialize(&local_sockets);
|
---|
| 731 |
|
---|
| 732 | while (keep_on_going) {
|
---|
| 733 |
|
---|
[fb04cba8] | 734 | /* Answer the call */
|
---|
[457a6f5] | 735 | answer_call(callid, res, &answer, answer_count);
|
---|
| 736 |
|
---|
[fb04cba8] | 737 | /* Refresh data */
|
---|
[457a6f5] | 738 | refresh_answer(&answer, &answer_count);
|
---|
| 739 |
|
---|
[fb04cba8] | 740 | /* Get the next call */
|
---|
[457a6f5] | 741 | callid = async_get_call(&call);
|
---|
| 742 |
|
---|
[fb04cba8] | 743 | /* Process the call */
|
---|
[457a6f5] | 744 | switch (IPC_GET_METHOD(call)) {
|
---|
| 745 | case IPC_M_PHONE_HUNGUP:
|
---|
| 746 | keep_on_going = false;
|
---|
| 747 | res = EHANGUP;
|
---|
| 748 | break;
|
---|
| 749 |
|
---|
| 750 | case NET_SOCKET:
|
---|
| 751 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 752 | res = socket_create(&local_sockets, app_phone, NULL,
|
---|
| 753 | &socket_id);
|
---|
| 754 | SOCKET_SET_SOCKET_ID(answer, socket_id);
|
---|
| 755 |
|
---|
| 756 | if (res != EOK)
|
---|
| 757 | break;
|
---|
| 758 |
|
---|
| 759 | if (tl_get_ip_packet_dimension(udp_globals.ip_phone,
|
---|
| 760 | &udp_globals.dimensions, DEVICE_INVALID_ID,
|
---|
| 761 | &packet_dimension) == EOK) {
|
---|
| 762 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
|
---|
| 763 | packet_dimension->content);
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | // SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
|
---|
| 767 | // MAX_UDP_FRAGMENT_SIZE);
|
---|
| 768 | SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
|
---|
| 769 | answer_count = 3;
|
---|
| 770 | break;
|
---|
| 771 |
|
---|
| 772 | case NET_SOCKET_BIND:
|
---|
| 773 | res = data_receive((void **) &addr, &addrlen);
|
---|
| 774 | if (res != EOK)
|
---|
| 775 | break;
|
---|
| 776 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 777 | res = socket_bind(&local_sockets, &udp_globals.sockets,
|
---|
| 778 | SOCKET_GET_SOCKET_ID(call), addr, addrlen,
|
---|
| 779 | UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
---|
| 780 | udp_globals.last_used_port);
|
---|
| 781 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 782 | free(addr);
|
---|
| 783 | break;
|
---|
| 784 |
|
---|
| 785 | case NET_SOCKET_SENDTO:
|
---|
| 786 | res = data_receive((void **) &addr, &addrlen);
|
---|
| 787 | if (res != EOK)
|
---|
| 788 | break;
|
---|
| 789 |
|
---|
| 790 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 791 | res = udp_sendto_message(&local_sockets,
|
---|
| 792 | SOCKET_GET_SOCKET_ID(call), addr, addrlen,
|
---|
| 793 | SOCKET_GET_DATA_FRAGMENTS(call), &size,
|
---|
| 794 | SOCKET_GET_FLAGS(call));
|
---|
| 795 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
|
---|
| 796 |
|
---|
| 797 | if (res != EOK)
|
---|
| 798 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 799 | else
|
---|
| 800 | answer_count = 2;
|
---|
| 801 |
|
---|
| 802 | free(addr);
|
---|
| 803 | break;
|
---|
| 804 |
|
---|
| 805 | case NET_SOCKET_RECVFROM:
|
---|
| 806 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 807 | res = udp_recvfrom_message(&local_sockets,
|
---|
| 808 | SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
|
---|
| 809 | &addrlen);
|
---|
| 810 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 811 |
|
---|
| 812 | if (res <= 0)
|
---|
| 813 | break;
|
---|
| 814 |
|
---|
| 815 | SOCKET_SET_READ_DATA_LENGTH(answer, res);
|
---|
| 816 | SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
|
---|
| 817 | answer_count = 3;
|
---|
| 818 | res = EOK;
|
---|
| 819 | break;
|
---|
| 820 |
|
---|
| 821 | case NET_SOCKET_CLOSE:
|
---|
| 822 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 823 | res = socket_destroy(udp_globals.net_phone,
|
---|
| 824 | SOCKET_GET_SOCKET_ID(call), &local_sockets,
|
---|
| 825 | &udp_globals.sockets, NULL);
|
---|
| 826 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 827 | break;
|
---|
| 828 |
|
---|
| 829 | case NET_SOCKET_GETSOCKOPT:
|
---|
| 830 | case NET_SOCKET_SETSOCKOPT:
|
---|
| 831 | default:
|
---|
| 832 | res = ENOTSUP;
|
---|
| 833 | break;
|
---|
| 834 | }
|
---|
| 835 | }
|
---|
| 836 |
|
---|
[fb04cba8] | 837 | /* Release the application phone */
|
---|
[457a6f5] | 838 | ipc_hangup(app_phone);
|
---|
| 839 |
|
---|
[fb04cba8] | 840 | /* Release all local sockets */
|
---|
[457a6f5] | 841 | socket_cores_release(udp_globals.net_phone, &local_sockets,
|
---|
| 842 | &udp_globals.sockets, NULL);
|
---|
| 843 |
|
---|
| 844 | return res;
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 | /** Processes the UDP message.
|
---|
| 848 | *
|
---|
| 849 | * @param[in] callid The message identifier.
|
---|
| 850 | * @param[in] call The message parameters.
|
---|
| 851 | * @param[out] answer The message answer parameters.
|
---|
| 852 | * @param[out] answer_count The last parameter for the actual answer in the
|
---|
| 853 | * answer parameter.
|
---|
[1bfd3d3] | 854 | * @return EOK on success.
|
---|
| 855 | * @return ENOTSUP if the message is not known.
|
---|
[457a6f5] | 856 | *
|
---|
| 857 | * @see udp_interface.h
|
---|
| 858 | * @see IS_NET_UDP_MESSAGE()
|
---|
| 859 | */
|
---|
[fb04cba8] | 860 | int udp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
|
---|
[457a6f5] | 861 | ipc_call_t *answer, int *answer_count)
|
---|
| 862 | {
|
---|
| 863 | packet_t packet;
|
---|
[46ae62c] | 864 | int rc;
|
---|
[457a6f5] | 865 |
|
---|
| 866 | *answer_count = 0;
|
---|
| 867 |
|
---|
| 868 | switch (IPC_GET_METHOD(*call)) {
|
---|
| 869 | case NET_TL_RECEIVED:
|
---|
[46ae62c] | 870 | rc = packet_translate_remote(udp_globals.net_phone, &packet,
|
---|
| 871 | IPC_GET_PACKET(call));
|
---|
| 872 | if (rc != EOK)
|
---|
| 873 | return rc;
|
---|
| 874 | return udp_received_msg(IPC_GET_DEVICE(call), packet,
|
---|
| 875 | SERVICE_UDP, IPC_GET_ERROR(call));
|
---|
[457a6f5] | 876 | case IPC_M_CONNECT_TO_ME:
|
---|
| 877 | return udp_process_client_messages(callid, * call);
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | return ENOTSUP;
|
---|
[21580dd] | 881 | }
|
---|
| 882 |
|
---|
[849ed54] | 883 | /** Default thread for new connections.
|
---|
| 884 | *
|
---|
[46ae62c] | 885 | * @param[in] iid The initial message identifier.
|
---|
| 886 | * @param[in] icall The initial message call structure.
|
---|
[849ed54] | 887 | */
|
---|
| 888 | static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)
|
---|
| 889 | {
|
---|
| 890 | /*
|
---|
| 891 | * Accept the connection
|
---|
| 892 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 893 | */
|
---|
| 894 | ipc_answer_0(iid, EOK);
|
---|
| 895 |
|
---|
[7282582] | 896 | while (true) {
|
---|
[849ed54] | 897 | ipc_call_t answer;
|
---|
| 898 | int answer_count;
|
---|
| 899 |
|
---|
| 900 | /* Clear the answer structure */
|
---|
| 901 | refresh_answer(&answer, &answer_count);
|
---|
| 902 |
|
---|
| 903 | /* Fetch the next message */
|
---|
| 904 | ipc_call_t call;
|
---|
| 905 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 906 |
|
---|
| 907 | /* Process the message */
|
---|
[14f1db0] | 908 | int res = tl_module_message_standalone(callid, &call, &answer,
|
---|
| 909 | &answer_count);
|
---|
[849ed54] | 910 |
|
---|
[7282582] | 911 | /*
|
---|
[457a6f5] | 912 | * End if told to either by the message or the processing
|
---|
| 913 | * result.
|
---|
[7282582] | 914 | */
|
---|
| 915 | if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
|
---|
| 916 | (res == EHANGUP))
|
---|
[849ed54] | 917 | return;
|
---|
| 918 |
|
---|
| 919 | /* Answer the message */
|
---|
| 920 | answer_call(callid, res, &answer, answer_count);
|
---|
| 921 | }
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | /** Starts the module.
|
---|
| 925 | *
|
---|
[1bfd3d3] | 926 | * @return EOK on success.
|
---|
| 927 | * @return Other error codes as defined for each specific module
|
---|
[7282582] | 928 | * start function.
|
---|
[849ed54] | 929 | */
|
---|
| 930 | int main(int argc, char *argv[])
|
---|
| 931 | {
|
---|
[46ae62c] | 932 | int rc;
|
---|
[849ed54] | 933 |
|
---|
| 934 | /* Start the module */
|
---|
[46ae62c] | 935 | rc = tl_module_start_standalone(tl_client_connection);
|
---|
| 936 | return rc;
|
---|
[849ed54] | 937 | }
|
---|
| 938 |
|
---|
[21580dd] | 939 | /** @}
|
---|
| 940 | */
|
---|