source: mainline/uspace/srv/net/tl/udp/udp.c@ 457a6f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 457a6f5 was 457a6f5, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cleanup udp.

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