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