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