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