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