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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a07a454 was 9934f7d, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add extra argument to async connection handlers that can be used for passing
information from async_connect_to_me() to the handler.

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