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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since afe1d1e was afe1d1e, checked in by Martin Decky <martin@…>, 14 years ago

fix possible compiler warnings (thx Vojtech Horky)

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