[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.
|
---|
| 99 | * @returns EOK on success.
|
---|
| 100 | * @returns 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 | };
|
---|
[aadf01e] | 114 | measured_string_ref configuration;
|
---|
| 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 |
|
---|
[21580dd] | 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 |
|
---|
| 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.
|
---|
| 207 | * @returns EOK on success.
|
---|
| 208 | * @returns EINVAL if the packet is not valid.
|
---|
| 209 | * @returns EINVAL if the stored packet address is not the
|
---|
| 210 | * an_addr_t.
|
---|
| 211 | * @returns EINVAL if the packet does not contain any data.
|
---|
| 212 | * @returns NO_DATA if the packet content is shorter than the user
|
---|
| 213 | * datagram header.
|
---|
| 214 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 215 | * @returns EADDRNOTAVAIL if the destination socket does not exist.
|
---|
| 216 | * @returns Other error codes as defined for the
|
---|
| 217 | * ip_client_process_packet() function.
|
---|
| 218 | */
|
---|
| 219 | static int
|
---|
| 220 | udp_process_packet(device_id_t device_id, packet_t packet, services_t error)
|
---|
[7282582] | 221 | {
|
---|
[aadf01e] | 222 | size_t length;
|
---|
| 223 | size_t offset;
|
---|
| 224 | int result;
|
---|
| 225 | udp_header_ref header;
|
---|
| 226 | socket_core_ref socket;
|
---|
| 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;
|
---|
[aadf01e] | 237 | packet_dimension_ref packet_dimension;
|
---|
[46ae62c] | 238 | int rc;
|
---|
[aadf01e] | 239 |
|
---|
[457a6f5] | 240 | switch (error) {
|
---|
| 241 | case SERVICE_NONE:
|
---|
| 242 | break;
|
---|
| 243 | case SERVICE_ICMP:
|
---|
| 244 | // ignore error
|
---|
| 245 | // length = icmp_client_header_length(packet);
|
---|
| 246 | // process error
|
---|
| 247 | result = icmp_client_process_packet(packet, &type,
|
---|
| 248 | &code, NULL, NULL);
|
---|
| 249 | if (result < 0)
|
---|
| 250 | return udp_release_and_return(packet, result);
|
---|
| 251 | length = (size_t) result;
|
---|
[46ae62c] | 252 | rc = packet_trim(packet, length, 0);
|
---|
| 253 | if (rc != EOK)
|
---|
| 254 | return udp_release_and_return(packet, rc);
|
---|
[457a6f5] | 255 | break;
|
---|
| 256 | default:
|
---|
| 257 | return udp_release_and_return(packet, ENOTSUP);
|
---|
[21580dd] | 258 | }
|
---|
[7282582] | 259 |
|
---|
[21580dd] | 260 | // TODO process received ipopts?
|
---|
[aadf01e] | 261 | result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
|
---|
[7282582] | 262 | if (result < 0)
|
---|
[aadf01e] | 263 | return udp_release_and_return(packet, result);
|
---|
| 264 | offset = (size_t) result;
|
---|
[21580dd] | 265 |
|
---|
[aadf01e] | 266 | length = packet_get_data_length(packet);
|
---|
[7282582] | 267 | if (length <= 0)
|
---|
[aadf01e] | 268 | return udp_release_and_return(packet, EINVAL);
|
---|
[7282582] | 269 | if (length < UDP_HEADER_SIZE + offset)
|
---|
[aadf01e] | 270 | return udp_release_and_return(packet, NO_DATA);
|
---|
[21580dd] | 271 |
|
---|
| 272 | // trim all but UDP header
|
---|
[46ae62c] | 273 | rc = packet_trim(packet, offset, 0);
|
---|
| 274 | if (rc != EOK)
|
---|
| 275 | return udp_release_and_return(packet, rc);
|
---|
[21580dd] | 276 |
|
---|
| 277 | // get udp header
|
---|
[aadf01e] | 278 | header = (udp_header_ref) packet_get_data(packet);
|
---|
[7282582] | 279 | if (!header)
|
---|
[aadf01e] | 280 | return udp_release_and_return(packet, NO_DATA);
|
---|
[7282582] | 281 |
|
---|
[21580dd] | 282 | // find the destination socket
|
---|
[7282582] | 283 | socket = socket_port_find(&udp_globals.sockets,
|
---|
| 284 | ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
|
---|
| 285 | if (!socket) {
|
---|
| 286 | if (tl_prepare_icmp_packet(udp_globals.net_phone,
|
---|
| 287 | udp_globals.icmp_phone, packet, error) == EOK) {
|
---|
| 288 | icmp_destination_unreachable_msg(udp_globals.icmp_phone,
|
---|
| 289 | ICMP_PORT_UNREACH, 0, packet);
|
---|
[21580dd] | 290 | }
|
---|
| 291 | return EADDRNOTAVAIL;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | // count the received packet fragments
|
---|
| 295 | next_packet = packet;
|
---|
| 296 | fragments = 0;
|
---|
[aadf01e] | 297 | total_length = ntohs(header->total_length);
|
---|
[7282582] | 298 |
|
---|
[21580dd] | 299 | // compute header checksum if set
|
---|
[457a6f5] | 300 | if (header->checksum && !error) {
|
---|
[7282582] | 301 | result = packet_get_addr(packet, (uint8_t **) &src,
|
---|
| 302 | (uint8_t **) &dest);
|
---|
[457a6f5] | 303 | if (result <= 0)
|
---|
[aadf01e] | 304 | return udp_release_and_return(packet, result);
|
---|
[46ae62c] | 305 |
|
---|
| 306 | rc = ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest,
|
---|
| 307 | result, total_length, &ip_header, &length);
|
---|
| 308 | if (rc != EOK) {
|
---|
| 309 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 310 | } else {
|
---|
[aadf01e] | 311 | checksum = compute_checksum(0, ip_header, length);
|
---|
[7282582] | 312 | // the udp header checksum will be added with the first
|
---|
| 313 | // fragment later
|
---|
[aadf01e] | 314 | free(ip_header);
|
---|
[21580dd] | 315 | }
|
---|
[7282582] | 316 | } else {
|
---|
[21580dd] | 317 | header->checksum = 0;
|
---|
| 318 | checksum = 0;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[7282582] | 321 | do {
|
---|
[457a6f5] | 322 | fragments++;
|
---|
[aadf01e] | 323 | length = packet_get_data_length(next_packet);
|
---|
[7282582] | 324 | if (length <= 0)
|
---|
[aadf01e] | 325 | return udp_release_and_return(packet, NO_DATA);
|
---|
[7282582] | 326 |
|
---|
| 327 | if (total_length < length) {
|
---|
[46ae62c] | 328 | rc = packet_trim(next_packet, 0, length - total_length);
|
---|
| 329 | if (rc != EOK)
|
---|
| 330 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 331 |
|
---|
[21580dd] | 332 | // add partial checksum if set
|
---|
[7282582] | 333 | if (header->checksum) {
|
---|
| 334 | checksum = compute_checksum(checksum,
|
---|
| 335 | packet_get_data(packet),
|
---|
| 336 | packet_get_data_length(packet));
|
---|
[21580dd] | 337 | }
|
---|
[7282582] | 338 |
|
---|
[21580dd] | 339 | // relese the rest of the packet fragments
|
---|
[aadf01e] | 340 | tmp_packet = pq_next(next_packet);
|
---|
[7282582] | 341 | while (tmp_packet) {
|
---|
[aadf01e] | 342 | next_packet = pq_detach(tmp_packet);
|
---|
[7282582] | 343 | pq_release_remote(udp_globals.net_phone,
|
---|
| 344 | packet_get_id(tmp_packet));
|
---|
[21580dd] | 345 | tmp_packet = next_packet;
|
---|
| 346 | }
|
---|
[7282582] | 347 |
|
---|
[21580dd] | 348 | // exit the loop
|
---|
| 349 | break;
|
---|
| 350 | }
|
---|
| 351 | total_length -= length;
|
---|
[7282582] | 352 |
|
---|
[21580dd] | 353 | // add partial checksum if set
|
---|
[7282582] | 354 | if (header->checksum) {
|
---|
| 355 | checksum = compute_checksum(checksum,
|
---|
| 356 | packet_get_data(packet),
|
---|
| 357 | packet_get_data_length(packet));
|
---|
[21580dd] | 358 | }
|
---|
[7282582] | 359 |
|
---|
| 360 | } while ((next_packet = pq_next(next_packet)) && (total_length > 0));
|
---|
[21580dd] | 361 |
|
---|
| 362 | // check checksum
|
---|
[7282582] | 363 | if (header->checksum) {
|
---|
| 364 | if (flip_checksum(compact_checksum(checksum)) !=
|
---|
| 365 | IP_CHECKSUM_ZERO) {
|
---|
| 366 | if (tl_prepare_icmp_packet(udp_globals.net_phone,
|
---|
| 367 | udp_globals.icmp_phone, packet, error) == EOK) {
|
---|
[21580dd] | 368 | // checksum error ICMP
|
---|
[7282582] | 369 | icmp_parameter_problem_msg(
|
---|
| 370 | udp_globals.icmp_phone, ICMP_PARAM_POINTER,
|
---|
| 371 | ((size_t) ((void *) &header->checksum)) -
|
---|
| 372 | ((size_t) ((void *) header)), packet);
|
---|
[21580dd] | 373 | }
|
---|
| 374 | return EINVAL;
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | // queue the received packet
|
---|
[46ae62c] | 379 | rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
|
---|
| 380 | SOCKET_MAX_RECEIVED_SIZE);
|
---|
| 381 | if (rc != EOK)
|
---|
| 382 | return udp_release_and_return(packet, rc);
|
---|
| 383 |
|
---|
| 384 | rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
|
---|
| 385 | &udp_globals.dimensions, device_id, &packet_dimension);
|
---|
| 386 | if (rc != EOK)
|
---|
| 387 | return udp_release_and_return(packet, rc);
|
---|
[21580dd] | 388 |
|
---|
| 389 | // notify the destination socket
|
---|
[aadf01e] | 390 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[7282582] | 391 | async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
|
---|
| 392 | (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0,
|
---|
| 393 | (ipcarg_t) fragments);
|
---|
| 394 |
|
---|
[2e99277] | 395 | return EOK;
|
---|
[21580dd] | 396 | }
|
---|
| 397 |
|
---|
[457a6f5] | 398 | /** Processes the received UDP packet queue.
|
---|
| 399 | *
|
---|
| 400 | * Is used as an entry point from the underlying IP module.
|
---|
| 401 | * Locks the global lock and calls udp_process_packet() function.
|
---|
| 402 | *
|
---|
| 403 | * @param[in] device_id The receiving device identifier.
|
---|
| 404 | * @param[in,out] packet The received packet queue.
|
---|
| 405 | * @param receiver The target service. Ignored parameter.
|
---|
| 406 | * @param[in] error The packet error reporting service. Prefixes the
|
---|
| 407 | * received packet.
|
---|
| 408 | * @returns EOK on success.
|
---|
| 409 | * @returns Other error codes as defined for the
|
---|
| 410 | * udp_process_packet() function.
|
---|
| 411 | */
|
---|
| 412 | static int
|
---|
| 413 | udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
|
---|
| 414 | services_t error)
|
---|
[7282582] | 415 | {
|
---|
[457a6f5] | 416 | int result;
|
---|
[a8a13d0] | 417 |
|
---|
[457a6f5] | 418 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 419 | result = udp_process_packet(device_id, packet, error);
|
---|
| 420 | if (result != EOK)
|
---|
| 421 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[21580dd] | 422 |
|
---|
[457a6f5] | 423 | return result;
|
---|
[21580dd] | 424 | }
|
---|
| 425 |
|
---|
[457a6f5] | 426 | /** Sends data from the socket to the remote address.
|
---|
| 427 | *
|
---|
| 428 | * Binds the socket to a free port if not already connected/bound.
|
---|
| 429 | * Handles the NET_SOCKET_SENDTO message.
|
---|
| 430 | * Supports AF_INET and AF_INET6 address families.
|
---|
| 431 | *
|
---|
| 432 | * @param[in,out] local_sockets The application local sockets.
|
---|
| 433 | * @param[in] socket_id Socket identifier.
|
---|
| 434 | * @param[in] addr The destination address.
|
---|
| 435 | * @param[in] addrlen The address length.
|
---|
| 436 | * @param[in] fragments The number of data fragments.
|
---|
| 437 | * @param[out] data_fragment_size The data fragment size in bytes.
|
---|
| 438 | * @param[in] flags Various send flags.
|
---|
| 439 | * @returns EOK on success.
|
---|
| 440 | * @returns EAFNOTSUPPORT if the address family is not supported.
|
---|
| 441 | * @returns ENOTSOCK if the socket is not found.
|
---|
| 442 | * @returns EINVAL if the address is invalid.
|
---|
| 443 | * @returns ENOTCONN if the sending socket is not and cannot be
|
---|
| 444 | * bound.
|
---|
| 445 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 446 | * @returns Other error codes as defined for the
|
---|
| 447 | * socket_read_packet_data() function.
|
---|
| 448 | * @returns Other error codes as defined for the
|
---|
| 449 | * ip_client_prepare_packet() function.
|
---|
| 450 | * @returns Other error codes as defined for the ip_send_msg()
|
---|
| 451 | * function.
|
---|
| 452 | */
|
---|
| 453 | static int
|
---|
[7282582] | 454 | udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
|
---|
| 455 | const struct sockaddr *addr, socklen_t addrlen, int fragments,
|
---|
| 456 | size_t *data_fragment_size, int flags)
|
---|
| 457 | {
|
---|
[aadf01e] | 458 | socket_core_ref socket;
|
---|
| 459 | packet_t packet;
|
---|
| 460 | packet_t next_packet;
|
---|
| 461 | udp_header_ref header;
|
---|
| 462 | int index;
|
---|
| 463 | size_t total_length;
|
---|
| 464 | int result;
|
---|
| 465 | uint16_t dest_port;
|
---|
| 466 | uint32_t checksum;
|
---|
[14f1db0] | 467 | void *ip_header;
|
---|
[aadf01e] | 468 | size_t headerlen;
|
---|
| 469 | device_id_t device_id;
|
---|
| 470 | packet_dimension_ref packet_dimension;
|
---|
[46ae62c] | 471 | int rc;
|
---|
| 472 |
|
---|
| 473 | rc = tl_get_address_port(addr, addrlen, &dest_port);
|
---|
| 474 | if (rc != EOK)
|
---|
| 475 | return rc;
|
---|
[aadf01e] | 476 |
|
---|
| 477 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
[7282582] | 478 | if (!socket)
|
---|
[aadf01e] | 479 | return ENOTSOCK;
|
---|
| 480 |
|
---|
[7282582] | 481 | if ((socket->port <= 0) && udp_globals.autobinding) {
|
---|
[21580dd] | 482 | // bind the socket to a random free port if not bound
|
---|
[a873201] | 483 | rc = socket_bind_free_port(&udp_globals.sockets, socket,
|
---|
| 484 | UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
---|
| 485 | udp_globals.last_used_port);
|
---|
| 486 | if (rc != EOK)
|
---|
| 487 | return rc;
|
---|
| 488 | // set the next port as the search starting port number
|
---|
| 489 | udp_globals.last_used_port = socket->port;
|
---|
[21580dd] | 490 | }
|
---|
| 491 |
|
---|
[7282582] | 492 | if (udp_globals.checksum_computing) {
|
---|
[46ae62c] | 493 | rc = ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr,
|
---|
| 494 | addrlen, &device_id, &ip_header, &headerlen);
|
---|
| 495 | if (rc != EOK)
|
---|
[d94f309] | 496 | return rc;
|
---|
[21580dd] | 497 | // get the device packet dimension
|
---|
[46ae62c] | 498 | // rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
|
---|
| 499 | // &udp_globals.dimensions, device_id, &packet_dimension);
|
---|
| 500 | // if (rc != EOK)
|
---|
| 501 | // return rc;
|
---|
[21580dd] | 502 | }
|
---|
[7282582] | 503 | // } else {
|
---|
[21580dd] | 504 | // do not ask all the time
|
---|
[46ae62c] | 505 | rc = ip_packet_size_req(udp_globals.ip_phone, -1,
|
---|
| 506 | &udp_globals.packet_dimension);
|
---|
| 507 | if (rc != EOK)
|
---|
| 508 | return rc;
|
---|
[aadf01e] | 509 | packet_dimension = &udp_globals.packet_dimension;
|
---|
[21580dd] | 510 | // }
|
---|
| 511 |
|
---|
| 512 | // read the first packet fragment
|
---|
[7282582] | 513 | result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
|
---|
| 514 | UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
|
---|
| 515 | if (result < 0)
|
---|
[aadf01e] | 516 | return result;
|
---|
[7282582] | 517 |
|
---|
[aadf01e] | 518 | total_length = (size_t) result;
|
---|
[7282582] | 519 | if (udp_globals.checksum_computing)
|
---|
| 520 | checksum = compute_checksum(0, packet_get_data(packet),
|
---|
| 521 | packet_get_data_length(packet));
|
---|
| 522 | else
|
---|
[21580dd] | 523 | checksum = 0;
|
---|
[7282582] | 524 |
|
---|
[21580dd] | 525 | // prefix the udp header
|
---|
[aadf01e] | 526 | header = PACKET_PREFIX(packet, udp_header_t);
|
---|
[457a6f5] | 527 | if (!header)
|
---|
[aadf01e] | 528 | return udp_release_and_return(packet, ENOMEM);
|
---|
[7282582] | 529 |
|
---|
[aadf01e] | 530 | bzero(header, sizeof(*header));
|
---|
[21580dd] | 531 | // read the rest of the packet fragments
|
---|
[457a6f5] | 532 | for (index = 1; index < fragments; index++) {
|
---|
[7282582] | 533 | result = tl_socket_read_packet_data(udp_globals.net_phone,
|
---|
| 534 | &next_packet, 0, packet_dimension, addr, addrlen);
|
---|
| 535 | if (result < 0)
|
---|
[aadf01e] | 536 | return udp_release_and_return(packet, result);
|
---|
[7282582] | 537 |
|
---|
[46ae62c] | 538 | rc = pq_add(&packet, next_packet, index, 0);
|
---|
| 539 | if (rc != EOK)
|
---|
| 540 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 541 |
|
---|
[aadf01e] | 542 | total_length += (size_t) result;
|
---|
[7282582] | 543 | if (udp_globals.checksum_computing) {
|
---|
| 544 | checksum = compute_checksum(checksum,
|
---|
| 545 | packet_get_data(next_packet),
|
---|
| 546 | packet_get_data_length(next_packet));
|
---|
[21580dd] | 547 | }
|
---|
| 548 | }
|
---|
[7282582] | 549 |
|
---|
[21580dd] | 550 | // set the udp header
|
---|
[aadf01e] | 551 | header->source_port = htons((socket->port > 0) ? socket->port : 0);
|
---|
| 552 | header->destination_port = htons(dest_port);
|
---|
| 553 | header->total_length = htons(total_length + sizeof(*header));
|
---|
[21580dd] | 554 | header->checksum = 0;
|
---|
[7282582] | 555 | if (udp_globals.checksum_computing) {
|
---|
[2e99277] | 556 | // update the pseudo header
|
---|
[46ae62c] | 557 | rc = ip_client_set_pseudo_header_data_length(ip_header,
|
---|
| 558 | headerlen, total_length + UDP_HEADER_SIZE);
|
---|
| 559 | if (rc != EOK) {
|
---|
[aadf01e] | 560 | free(ip_header);
|
---|
[46ae62c] | 561 | return udp_release_and_return(packet, rc);
|
---|
[21580dd] | 562 | }
|
---|
[7282582] | 563 |
|
---|
[2e99277] | 564 | // finish the checksum computation
|
---|
[aadf01e] | 565 | checksum = compute_checksum(checksum, ip_header, headerlen);
|
---|
[7282582] | 566 | checksum = compute_checksum(checksum, (uint8_t *) header,
|
---|
| 567 | sizeof(*header));
|
---|
| 568 | header->checksum =
|
---|
| 569 | htons(flip_checksum(compact_checksum(checksum)));
|
---|
[aadf01e] | 570 | free(ip_header);
|
---|
[7282582] | 571 | } else {
|
---|
[ede63e4] | 572 | device_id = DEVICE_INVALID_ID;
|
---|
[21580dd] | 573 | }
|
---|
[7282582] | 574 |
|
---|
[21580dd] | 575 | // prepare the first packet fragment
|
---|
[46ae62c] | 576 | rc = ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0);
|
---|
| 577 | if (rc != EOK)
|
---|
| 578 | return udp_release_and_return(packet, rc);
|
---|
[7282582] | 579 |
|
---|
[f3cb50e] | 580 | /* Release the UDP global lock on success. */
|
---|
[aadf01e] | 581 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
[f3cb50e] | 582 |
|
---|
| 583 | // send the packet
|
---|
[aadf01e] | 584 | ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
|
---|
[7282582] | 585 |
|
---|
[21580dd] | 586 | return EOK;
|
---|
| 587 | }
|
---|
| 588 |
|
---|
[457a6f5] | 589 | /** Receives data to the socket.
|
---|
| 590 | *
|
---|
| 591 | * Handles the NET_SOCKET_RECVFROM message.
|
---|
| 592 | * Replies the source address as well.
|
---|
| 593 | *
|
---|
| 594 | * @param[in] local_sockets The application local sockets.
|
---|
| 595 | * @param[in] socket_id Socket identifier.
|
---|
| 596 | * @param[in] flags Various receive flags.
|
---|
| 597 | * @param[out] addrlen The source address length.
|
---|
| 598 | * @returns The number of bytes received.
|
---|
| 599 | * @returns ENOTSOCK if the socket is not found.
|
---|
| 600 | * @returns NO_DATA if there are no received packets or data.
|
---|
| 601 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 602 | * @returns EINVAL if the received address is not an IP address.
|
---|
| 603 | * @returns Other error codes as defined for the packet_translate()
|
---|
| 604 | * function.
|
---|
| 605 | * @returns Other error codes as defined for the data_reply()
|
---|
| 606 | * function.
|
---|
| 607 | */
|
---|
| 608 | static int
|
---|
[7282582] | 609 | udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
|
---|
| 610 | size_t *addrlen)
|
---|
| 611 | {
|
---|
[aadf01e] | 612 | socket_core_ref socket;
|
---|
| 613 | int packet_id;
|
---|
| 614 | packet_t packet;
|
---|
| 615 | udp_header_ref header;
|
---|
[7282582] | 616 | struct sockaddr *addr;
|
---|
[aadf01e] | 617 | size_t length;
|
---|
[7282582] | 618 | uint8_t *data;
|
---|
[aadf01e] | 619 | int result;
|
---|
[46ae62c] | 620 | int rc;
|
---|
[21580dd] | 621 |
|
---|
| 622 | // find the socket
|
---|
[aadf01e] | 623 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
[7282582] | 624 | if (!socket)
|
---|
[aadf01e] | 625 | return ENOTSOCK;
|
---|
[7282582] | 626 |
|
---|
[21580dd] | 627 | // get the next received packet
|
---|
[08042bd] | 628 | packet_id = dyn_fifo_value(&socket->received);
|
---|
[7282582] | 629 | if (packet_id < 0)
|
---|
[aadf01e] | 630 | return NO_DATA;
|
---|
[46ae62c] | 631 |
|
---|
| 632 | rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
|
---|
[08042bd] | 633 | if (rc != EOK) {
|
---|
| 634 | (void) dyn_fifo_pop(&socket->received);
|
---|
[46ae62c] | 635 | return rc;
|
---|
[08042bd] | 636 | }
|
---|
[7282582] | 637 |
|
---|
[21580dd] | 638 | // get udp header
|
---|
[aadf01e] | 639 | data = packet_get_data(packet);
|
---|
[08042bd] | 640 | if (!data) {
|
---|
| 641 | (void) dyn_fifo_pop(&socket->received);
|
---|
[de229f8e] | 642 | return udp_release_and_return(packet, NO_DATA);
|
---|
[08042bd] | 643 | }
|
---|
[aadf01e] | 644 | header = (udp_header_ref) data;
|
---|
[21580dd] | 645 |
|
---|
| 646 | // set the source address port
|
---|
[aadf01e] | 647 | result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
|
---|
[46ae62c] | 648 | rc = tl_set_address_port(addr, result, ntohs(header->source_port));
|
---|
[08042bd] | 649 | if (rc != EOK) {
|
---|
| 650 | (void) dyn_fifo_pop(&socket->received);
|
---|
[de229f8e] | 651 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 652 | }
|
---|
[aadf01e] | 653 | *addrlen = (size_t) result;
|
---|
[7282582] | 654 |
|
---|
[21580dd] | 655 | // send the source address
|
---|
[46ae62c] | 656 | rc = data_reply(addr, *addrlen);
|
---|
[08042bd] | 657 | switch (rc) {
|
---|
| 658 | case EOK:
|
---|
| 659 | break;
|
---|
| 660 | case EOVERFLOW:
|
---|
| 661 | return rc;
|
---|
| 662 | default:
|
---|
| 663 | (void) dyn_fifo_pop(&socket->received);
|
---|
[a63ff7d] | 664 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 665 | }
|
---|
[21580dd] | 666 |
|
---|
| 667 | // trim the header
|
---|
[46ae62c] | 668 | rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
|
---|
[08042bd] | 669 | if (rc != EOK) {
|
---|
| 670 | (void) dyn_fifo_pop(&socket->received);
|
---|
[a63ff7d] | 671 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 672 | }
|
---|
[21580dd] | 673 |
|
---|
| 674 | // reply the packets
|
---|
[46ae62c] | 675 | rc = socket_reply_packets(packet, &length);
|
---|
[08042bd] | 676 | switch (rc) {
|
---|
| 677 | case EOK:
|
---|
| 678 | break;
|
---|
| 679 | case EOVERFLOW:
|
---|
| 680 | return rc;
|
---|
| 681 | default:
|
---|
| 682 | (void) dyn_fifo_pop(&socket->received);
|
---|
[a63ff7d] | 683 | return udp_release_and_return(packet, rc);
|
---|
[08042bd] | 684 | }
|
---|
| 685 |
|
---|
| 686 | (void) dyn_fifo_pop(&socket->received);
|
---|
[7282582] | 687 |
|
---|
[de229f8e] | 688 | // release the packet and return the total length
|
---|
| 689 | return udp_release_and_return(packet, (int) length);
|
---|
[21580dd] | 690 | }
|
---|
| 691 |
|
---|
[457a6f5] | 692 | /** Processes the socket client messages.
|
---|
| 693 | *
|
---|
| 694 | * Runs until the client module disconnects.
|
---|
| 695 | *
|
---|
| 696 | * @param[in] callid The message identifier.
|
---|
| 697 | * @param[in] call The message parameters.
|
---|
| 698 | * @returns EOK on success.
|
---|
| 699 | *
|
---|
| 700 | * @see socket.h
|
---|
| 701 | */
|
---|
| 702 | static int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
|
---|
[7282582] | 703 | {
|
---|
[457a6f5] | 704 | int res;
|
---|
| 705 | bool keep_on_going = true;
|
---|
| 706 | socket_cores_t local_sockets;
|
---|
| 707 | int app_phone = IPC_GET_PHONE(&call);
|
---|
| 708 | struct sockaddr *addr;
|
---|
| 709 | int socket_id;
|
---|
| 710 | size_t addrlen;
|
---|
| 711 | size_t size;
|
---|
| 712 | ipc_call_t answer;
|
---|
| 713 | int answer_count;
|
---|
| 714 | packet_dimension_ref packet_dimension;
|
---|
| 715 |
|
---|
| 716 | /*
|
---|
| 717 | * Accept the connection
|
---|
| 718 | * - Answer the first IPC_M_CONNECT_TO_ME call.
|
---|
| 719 | */
|
---|
| 720 | res = EOK;
|
---|
| 721 | answer_count = 0;
|
---|
| 722 |
|
---|
| 723 | // The client connection is only in one fibril and therefore no
|
---|
| 724 | // additional locks are needed.
|
---|
| 725 |
|
---|
| 726 | socket_cores_initialize(&local_sockets);
|
---|
| 727 |
|
---|
| 728 | while (keep_on_going) {
|
---|
| 729 |
|
---|
| 730 | // answer the call
|
---|
| 731 | answer_call(callid, res, &answer, answer_count);
|
---|
| 732 |
|
---|
| 733 | // refresh data
|
---|
| 734 | refresh_answer(&answer, &answer_count);
|
---|
| 735 |
|
---|
| 736 | // get the next call
|
---|
| 737 | callid = async_get_call(&call);
|
---|
| 738 |
|
---|
| 739 | // process the call
|
---|
| 740 | switch (IPC_GET_METHOD(call)) {
|
---|
| 741 | case IPC_M_PHONE_HUNGUP:
|
---|
| 742 | keep_on_going = false;
|
---|
| 743 | res = EHANGUP;
|
---|
| 744 | break;
|
---|
| 745 |
|
---|
| 746 | case NET_SOCKET:
|
---|
| 747 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 748 | res = socket_create(&local_sockets, app_phone, NULL,
|
---|
| 749 | &socket_id);
|
---|
| 750 | SOCKET_SET_SOCKET_ID(answer, socket_id);
|
---|
| 751 |
|
---|
| 752 | if (res != EOK)
|
---|
| 753 | break;
|
---|
| 754 |
|
---|
| 755 | if (tl_get_ip_packet_dimension(udp_globals.ip_phone,
|
---|
| 756 | &udp_globals.dimensions, DEVICE_INVALID_ID,
|
---|
| 757 | &packet_dimension) == EOK) {
|
---|
| 758 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
|
---|
| 759 | packet_dimension->content);
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 | // SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
|
---|
| 763 | // MAX_UDP_FRAGMENT_SIZE);
|
---|
| 764 | SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
|
---|
| 765 | answer_count = 3;
|
---|
| 766 | break;
|
---|
| 767 |
|
---|
| 768 | case NET_SOCKET_BIND:
|
---|
| 769 | res = data_receive((void **) &addr, &addrlen);
|
---|
| 770 | if (res != EOK)
|
---|
| 771 | break;
|
---|
| 772 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 773 | res = socket_bind(&local_sockets, &udp_globals.sockets,
|
---|
| 774 | SOCKET_GET_SOCKET_ID(call), addr, addrlen,
|
---|
| 775 | UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
---|
| 776 | udp_globals.last_used_port);
|
---|
| 777 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 778 | free(addr);
|
---|
| 779 | break;
|
---|
| 780 |
|
---|
| 781 | case NET_SOCKET_SENDTO:
|
---|
| 782 | res = data_receive((void **) &addr, &addrlen);
|
---|
| 783 | if (res != EOK)
|
---|
| 784 | break;
|
---|
| 785 |
|
---|
| 786 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 787 | res = udp_sendto_message(&local_sockets,
|
---|
| 788 | SOCKET_GET_SOCKET_ID(call), addr, addrlen,
|
---|
| 789 | SOCKET_GET_DATA_FRAGMENTS(call), &size,
|
---|
| 790 | SOCKET_GET_FLAGS(call));
|
---|
| 791 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
|
---|
| 792 |
|
---|
| 793 | if (res != EOK)
|
---|
| 794 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 795 | else
|
---|
| 796 | answer_count = 2;
|
---|
| 797 |
|
---|
| 798 | free(addr);
|
---|
| 799 | break;
|
---|
| 800 |
|
---|
| 801 | case NET_SOCKET_RECVFROM:
|
---|
| 802 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 803 | res = udp_recvfrom_message(&local_sockets,
|
---|
| 804 | SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
|
---|
| 805 | &addrlen);
|
---|
| 806 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 807 |
|
---|
| 808 | if (res <= 0)
|
---|
| 809 | break;
|
---|
| 810 |
|
---|
| 811 | SOCKET_SET_READ_DATA_LENGTH(answer, res);
|
---|
| 812 | SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
|
---|
| 813 | answer_count = 3;
|
---|
| 814 | res = EOK;
|
---|
| 815 | break;
|
---|
| 816 |
|
---|
| 817 | case NET_SOCKET_CLOSE:
|
---|
| 818 | fibril_rwlock_write_lock(&udp_globals.lock);
|
---|
| 819 | res = socket_destroy(udp_globals.net_phone,
|
---|
| 820 | SOCKET_GET_SOCKET_ID(call), &local_sockets,
|
---|
| 821 | &udp_globals.sockets, NULL);
|
---|
| 822 | fibril_rwlock_write_unlock(&udp_globals.lock);
|
---|
| 823 | break;
|
---|
| 824 |
|
---|
| 825 | case NET_SOCKET_GETSOCKOPT:
|
---|
| 826 | case NET_SOCKET_SETSOCKOPT:
|
---|
| 827 | default:
|
---|
| 828 | res = ENOTSUP;
|
---|
| 829 | break;
|
---|
| 830 | }
|
---|
| 831 | }
|
---|
| 832 |
|
---|
| 833 | // release the application phone
|
---|
| 834 | ipc_hangup(app_phone);
|
---|
| 835 |
|
---|
| 836 | // release all local sockets
|
---|
| 837 | socket_cores_release(udp_globals.net_phone, &local_sockets,
|
---|
| 838 | &udp_globals.sockets, NULL);
|
---|
| 839 |
|
---|
| 840 | return res;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | /** Processes the UDP message.
|
---|
| 844 | *
|
---|
| 845 | * @param[in] callid The message identifier.
|
---|
| 846 | * @param[in] call The message parameters.
|
---|
| 847 | * @param[out] answer The message answer parameters.
|
---|
| 848 | * @param[out] answer_count The last parameter for the actual answer in the
|
---|
| 849 | * answer parameter.
|
---|
| 850 | * @returns EOK on success.
|
---|
| 851 | * @returns ENOTSUP if the message is not known.
|
---|
| 852 | *
|
---|
| 853 | * @see udp_interface.h
|
---|
| 854 | * @see IS_NET_UDP_MESSAGE()
|
---|
| 855 | */
|
---|
| 856 | int
|
---|
| 857 | udp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
|
---|
| 858 | ipc_call_t *answer, int *answer_count)
|
---|
| 859 | {
|
---|
| 860 | packet_t packet;
|
---|
[46ae62c] | 861 | int rc;
|
---|
[457a6f5] | 862 |
|
---|
| 863 | *answer_count = 0;
|
---|
| 864 |
|
---|
| 865 | switch (IPC_GET_METHOD(*call)) {
|
---|
| 866 | case NET_TL_RECEIVED:
|
---|
[46ae62c] | 867 | rc = packet_translate_remote(udp_globals.net_phone, &packet,
|
---|
| 868 | IPC_GET_PACKET(call));
|
---|
| 869 | if (rc != EOK)
|
---|
| 870 | return rc;
|
---|
| 871 | return udp_received_msg(IPC_GET_DEVICE(call), packet,
|
---|
| 872 | SERVICE_UDP, IPC_GET_ERROR(call));
|
---|
[457a6f5] | 873 | case IPC_M_CONNECT_TO_ME:
|
---|
| 874 | return udp_process_client_messages(callid, * call);
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 | return ENOTSUP;
|
---|
[21580dd] | 878 | }
|
---|
| 879 |
|
---|
[849ed54] | 880 | /** Default thread for new connections.
|
---|
| 881 | *
|
---|
[46ae62c] | 882 | * @param[in] iid The initial message identifier.
|
---|
| 883 | * @param[in] icall The initial message call structure.
|
---|
[849ed54] | 884 | */
|
---|
| 885 | static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)
|
---|
| 886 | {
|
---|
| 887 | /*
|
---|
| 888 | * Accept the connection
|
---|
| 889 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 890 | */
|
---|
| 891 | ipc_answer_0(iid, EOK);
|
---|
| 892 |
|
---|
[7282582] | 893 | while (true) {
|
---|
[849ed54] | 894 | ipc_call_t answer;
|
---|
| 895 | int answer_count;
|
---|
| 896 |
|
---|
| 897 | /* Clear the answer structure */
|
---|
| 898 | refresh_answer(&answer, &answer_count);
|
---|
| 899 |
|
---|
| 900 | /* Fetch the next message */
|
---|
| 901 | ipc_call_t call;
|
---|
| 902 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 903 |
|
---|
| 904 | /* Process the message */
|
---|
[14f1db0] | 905 | int res = tl_module_message_standalone(callid, &call, &answer,
|
---|
| 906 | &answer_count);
|
---|
[849ed54] | 907 |
|
---|
[7282582] | 908 | /*
|
---|
[457a6f5] | 909 | * End if told to either by the message or the processing
|
---|
| 910 | * result.
|
---|
[7282582] | 911 | */
|
---|
| 912 | if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
|
---|
| 913 | (res == EHANGUP))
|
---|
[849ed54] | 914 | return;
|
---|
| 915 |
|
---|
| 916 | /* Answer the message */
|
---|
| 917 | answer_call(callid, res, &answer, answer_count);
|
---|
| 918 | }
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | /** Starts the module.
|
---|
| 922 | *
|
---|
[457a6f5] | 923 | * @returns EOK on success.
|
---|
| 924 | * @returns Other error codes as defined for each specific module
|
---|
[7282582] | 925 | * start function.
|
---|
[849ed54] | 926 | */
|
---|
| 927 | int main(int argc, char *argv[])
|
---|
| 928 | {
|
---|
[46ae62c] | 929 | int rc;
|
---|
[849ed54] | 930 |
|
---|
| 931 | /* Start the module */
|
---|
[46ae62c] | 932 | rc = tl_module_start_standalone(tl_client_connection);
|
---|
| 933 | return rc;
|
---|
[849ed54] | 934 | }
|
---|
| 935 |
|
---|
[21580dd] | 936 | /** @}
|
---|
| 937 | */
|
---|