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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d08abdb7 was 8426912a, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Merge mainline changes

  • 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/ipc.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_interface.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/** UDP module name. */
74#define NAME "udp"
75
76/** Default UDP checksum computing. */
77#define NET_DEFAULT_UDP_CHECKSUM_COMPUTING true
78
79/** Default UDP autobind when sending via unbound sockets. */
80#define NET_DEFAULT_UDP_AUTOBINDING true
81
82/** Maximum UDP fragment size. */
83#define MAX_UDP_FRAGMENT_SIZE 65535
84
85/** Free ports pool start. */
86#define UDP_FREE_PORTS_START 1025
87
88/** Free ports pool end. */
89#define UDP_FREE_PORTS_END 65535
90
91/** UDP global data. */
92udp_globals_t udp_globals;
93
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 */
100static int udp_release_and_return(packet_t *packet, int result)
101{
102 pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
103 return result;
104}
105
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.
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
118 * an_addr_t.
119 * @return EINVAL if the packet does not contain any data.
120 * @return NO_DATA if the packet content is shorter than the user
121 * datagram header.
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
125 * ip_client_process_packet() function.
126 */
127static int udp_process_packet(device_id_t device_id, packet_t *packet,
128 services_t error)
129{
130 size_t length;
131 size_t offset;
132 int result;
133 udp_header_t *header;
134 socket_core_t *socket;
135 packet_t *next_packet;
136 size_t total_length;
137 uint32_t checksum;
138 int fragments;
139 packet_t *tmp_packet;
140 icmp_type_t type;
141 icmp_code_t code;
142 void *ip_header;
143 struct sockaddr *src;
144 struct sockaddr *dest;
145 packet_dimension_t *packet_dimension;
146 int rc;
147
148 switch (error) {
149 case SERVICE_NONE:
150 break;
151 case SERVICE_ICMP:
152 /* Ignore error */
153 // length = icmp_client_header_length(packet);
154
155 /* Process error */
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;
161 rc = packet_trim(packet, length, 0);
162 if (rc != EOK)
163 return udp_release_and_return(packet, rc);
164 break;
165 default:
166 return udp_release_and_return(packet, ENOTSUP);
167 }
168
169 /* TODO process received ipopts? */
170 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
171 if (result < 0)
172 return udp_release_and_return(packet, result);
173 offset = (size_t) result;
174
175 length = packet_get_data_length(packet);
176 if (length <= 0)
177 return udp_release_and_return(packet, EINVAL);
178 if (length < UDP_HEADER_SIZE + offset)
179 return udp_release_and_return(packet, NO_DATA);
180
181 /* Trim all but UDP header */
182 rc = packet_trim(packet, offset, 0);
183 if (rc != EOK)
184 return udp_release_and_return(packet, rc);
185
186 /* Get UDP header */
187 header = (udp_header_t *) packet_get_data(packet);
188 if (!header)
189 return udp_release_and_return(packet, NO_DATA);
190
191 /* Find the destination socket */
192 socket = socket_port_find(&udp_globals.sockets,
193 ntohs(header->destination_port), (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
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);
199 }
200 return EADDRNOTAVAIL;
201 }
202
203 /* Count the received packet fragments */
204 next_packet = packet;
205 fragments = 0;
206 total_length = ntohs(header->total_length);
207
208 /* Compute header checksum if set */
209 if (header->checksum && !error) {
210 result = packet_get_addr(packet, (uint8_t **) &src,
211 (uint8_t **) &dest);
212 if (result <= 0)
213 return udp_release_and_return(packet, result);
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);
219 } else {
220 checksum = compute_checksum(0, ip_header, length);
221 /*
222 * The udp header checksum will be added with the first
223 * fragment later.
224 */
225 free(ip_header);
226 }
227 } else {
228 header->checksum = 0;
229 checksum = 0;
230 }
231
232 do {
233 fragments++;
234 length = packet_get_data_length(next_packet);
235 if (length <= 0)
236 return udp_release_and_return(packet, NO_DATA);
237
238 if (total_length < length) {
239 rc = packet_trim(next_packet, 0, length - total_length);
240 if (rc != EOK)
241 return udp_release_and_return(packet, rc);
242
243 /* Add partial checksum if set */
244 if (header->checksum) {
245 checksum = compute_checksum(checksum,
246 packet_get_data(packet),
247 packet_get_data_length(packet));
248 }
249
250 /* Relese the rest of the packet fragments */
251 tmp_packet = pq_next(next_packet);
252 while (tmp_packet) {
253 next_packet = pq_detach(tmp_packet);
254 pq_release_remote(udp_globals.net_phone,
255 packet_get_id(tmp_packet));
256 tmp_packet = next_packet;
257 }
258
259 /* Exit the loop */
260 break;
261 }
262 total_length -= length;
263
264 /* Add partial checksum if set */
265 if (header->checksum) {
266 checksum = compute_checksum(checksum,
267 packet_get_data(packet),
268 packet_get_data_length(packet));
269 }
270
271 } while ((next_packet = pq_next(next_packet)) && (total_length > 0));
272
273 /* Verify checksum */
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) {
279 /* Checksum error ICMP */
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);
284 }
285 return EINVAL;
286 }
287 }
288
289 /* Queue the received packet */
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);
299
300 /* Notify the destination socket */
301 fibril_rwlock_write_unlock(&udp_globals.lock);
302 async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
303 (sysarg_t) socket->socket_id, packet_dimension->content, 0, 0,
304 (sysarg_t) fragments);
305
306 return EOK;
307}
308
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.
319 * @return EOK on success.
320 * @return Other error codes as defined for the
321 * udp_process_packet() function.
322 */
323static int udp_received_msg(device_id_t device_id, packet_t *packet,
324 services_t receiver, services_t error)
325{
326 int result;
327
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);
332
333 return result;
334}
335
336/** Process IPC messages from the IP module
337 *
338 * @param[in] iid Message identifier.
339 * @param[in,out] icall Message parameters.
340 *
341 */
342static 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 */
374int 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
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.
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
477 * bound.
478 * @return ENOMEM if there is not enough memory left.
479 * @return Other error codes as defined for the
480 * socket_read_packet_data() function.
481 * @return Other error codes as defined for the
482 * ip_client_prepare_packet() function.
483 * @return Other error codes as defined for the ip_send_msg()
484 * function.
485 */
486static int udp_sendto_message(socket_cores_t *local_sockets, int socket_id,
487 const struct sockaddr *addr, socklen_t addrlen, int fragments,
488 size_t *data_fragment_size, int flags)
489{
490 socket_core_t *socket;
491 packet_t *packet;
492 packet_t *next_packet;
493 udp_header_t *header;
494 int index;
495 size_t total_length;
496 int result;
497 uint16_t dest_port;
498 uint32_t checksum;
499 void *ip_header;
500 size_t headerlen;
501 device_id_t device_id;
502 packet_dimension_t *packet_dimension;
503 int rc;
504
505 rc = tl_get_address_port(addr, addrlen, &dest_port);
506 if (rc != EOK)
507 return rc;
508
509 socket = socket_cores_find(local_sockets, socket_id);
510 if (!socket)
511 return ENOTSOCK;
512
513 if ((socket->port <= 0) && udp_globals.autobinding) {
514 /* Bind the socket to a random free port if not bound */
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;
520 /* Set the next port as the search starting port number */
521 udp_globals.last_used_port = socket->port;
522 }
523
524 if (udp_globals.checksum_computing) {
525 rc = ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr,
526 addrlen, &device_id, &ip_header, &headerlen);
527 if (rc != EOK)
528 return rc;
529 /* Get the device packet dimension */
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;
534 }
535// } else {
536 /* Do not ask all the time */
537 rc = ip_packet_size_req(udp_globals.ip_phone, -1,
538 &udp_globals.packet_dimension);
539 if (rc != EOK)
540 return rc;
541 packet_dimension = &udp_globals.packet_dimension;
542// }
543
544 /* Read the first packet fragment */
545 result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
546 UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
547 if (result < 0)
548 return result;
549
550 total_length = (size_t) result;
551 if (udp_globals.checksum_computing)
552 checksum = compute_checksum(0, packet_get_data(packet),
553 packet_get_data_length(packet));
554 else
555 checksum = 0;
556
557 /* Prefix the UDP header */
558 header = PACKET_PREFIX(packet, udp_header_t);
559 if (!header)
560 return udp_release_and_return(packet, ENOMEM);
561
562 bzero(header, sizeof(*header));
563
564 /* Read the rest of the packet fragments */
565 for (index = 1; index < fragments; index++) {
566 result = tl_socket_read_packet_data(udp_globals.net_phone,
567 &next_packet, 0, packet_dimension, addr, addrlen);
568 if (result < 0)
569 return udp_release_and_return(packet, result);
570
571 rc = pq_add(&packet, next_packet, index, 0);
572 if (rc != EOK)
573 return udp_release_and_return(packet, rc);
574
575 total_length += (size_t) result;
576 if (udp_globals.checksum_computing) {
577 checksum = compute_checksum(checksum,
578 packet_get_data(next_packet),
579 packet_get_data_length(next_packet));
580 }
581 }
582
583 /* Set the UDP header */
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));
587 header->checksum = 0;
588
589 if (udp_globals.checksum_computing) {
590 /* Update the pseudo header */
591 rc = ip_client_set_pseudo_header_data_length(ip_header,
592 headerlen, total_length + UDP_HEADER_SIZE);
593 if (rc != EOK) {
594 free(ip_header);
595 return udp_release_and_return(packet, rc);
596 }
597
598 /* Finish the checksum computation */
599 checksum = compute_checksum(checksum, ip_header, headerlen);
600 checksum = compute_checksum(checksum, (uint8_t *) header,
601 sizeof(*header));
602 header->checksum =
603 htons(flip_checksum(compact_checksum(checksum)));
604 free(ip_header);
605 } else {
606 device_id = DEVICE_INVALID_ID;
607 }
608
609 /* Prepare the first packet fragment */
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);
613
614 /* Release the UDP global lock on success. */
615 fibril_rwlock_write_unlock(&udp_globals.lock);
616
617 /* Send the packet */
618 ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
619
620 return EOK;
621}
622
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.
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()
638 * function.
639 * @return Other error codes as defined for the data_reply()
640 * function.
641 */
642static int udp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
643 int flags, size_t *addrlen)
644{
645 socket_core_t *socket;
646 int packet_id;
647 packet_t *packet;
648 udp_header_t *header;
649 struct sockaddr *addr;
650 size_t length;
651 uint8_t *data;
652 int result;
653 int rc;
654
655 /* Find the socket */
656 socket = socket_cores_find(local_sockets, socket_id);
657 if (!socket)
658 return ENOTSOCK;
659
660 /* Get the next received packet */
661 packet_id = dyn_fifo_value(&socket->received);
662 if (packet_id < 0)
663 return NO_DATA;
664
665 rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
666 if (rc != EOK) {
667 (void) dyn_fifo_pop(&socket->received);
668 return rc;
669 }
670
671 /* Get UDP header */
672 data = packet_get_data(packet);
673 if (!data) {
674 (void) dyn_fifo_pop(&socket->received);
675 return udp_release_and_return(packet, NO_DATA);
676 }
677 header = (udp_header_t *) data;
678
679 /* Set the source address port */
680 result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
681 rc = tl_set_address_port(addr, result, ntohs(header->source_port));
682 if (rc != EOK) {
683 (void) dyn_fifo_pop(&socket->received);
684 return udp_release_and_return(packet, rc);
685 }
686 *addrlen = (size_t) result;
687
688 /* Send the source address */
689 rc = data_reply(addr, *addrlen);
690 switch (rc) {
691 case EOK:
692 break;
693 case EOVERFLOW:
694 return rc;
695 default:
696 (void) dyn_fifo_pop(&socket->received);
697 return udp_release_and_return(packet, rc);
698 }
699
700 /* Trim the header */
701 rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
702 if (rc != EOK) {
703 (void) dyn_fifo_pop(&socket->received);
704 return udp_release_and_return(packet, rc);
705 }
706
707 /* Reply the packets */
708 rc = socket_reply_packets(packet, &length);
709 switch (rc) {
710 case EOK:
711 break;
712 case EOVERFLOW:
713 return rc;
714 default:
715 (void) dyn_fifo_pop(&socket->received);
716 return udp_release_and_return(packet, rc);
717 }
718
719 (void) dyn_fifo_pop(&socket->received);
720
721 /* Release the packet and return the total length */
722 return udp_release_and_return(packet, (int) length);
723}
724
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.
731 * @return EOK on success.
732 *
733 * @see socket.h
734 */
735static int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
736{
737 int res;
738 bool keep_on_going = true;
739 socket_cores_t local_sockets;
740 int app_phone = IPC_GET_PHONE(call);
741 struct sockaddr *addr;
742 int socket_id;
743 size_t addrlen;
744 size_t size = 0;
745 ipc_call_t answer;
746 size_t answer_count;
747 packet_dimension_t *packet_dimension;
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
756 /*
757 * The client connection is only in one fibril and therefore no
758 * additional locks are needed.
759 */
760
761 socket_cores_initialize(&local_sockets);
762
763 while (keep_on_going) {
764
765 /* Answer the call */
766 answer_call(callid, res, &answer, answer_count);
767
768 /* Refresh data */
769 refresh_answer(&answer, &answer_count);
770
771 /* Get the next call */
772 callid = async_get_call(&call);
773
774 /* Process the call */
775 switch (IPC_GET_IMETHOD(call)) {
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:
804 res = async_data_write_accept((void **) &addr, false,
805 0, 0, 0, &addrlen);
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:
818 res = async_data_write_accept((void **) &addr, false,
819 0, 0, 0, &addrlen);
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
870 /* Release the application phone */
871 ipc_hangup(app_phone);
872
873 /* Release all local sockets */
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.
887 * @return EOK on success.
888 * @return ENOTSUP if the message is not known.
889 *
890 * @see udp_interface.h
891 * @see IS_NET_UDP_MESSAGE()
892 */
893int tl_module_message(ipc_callid_t callid, ipc_call_t *call,
894 ipc_call_t *answer, size_t *answer_count)
895{
896 *answer_count = 0;
897
898 switch (IPC_GET_IMETHOD(*call)) {
899 case IPC_M_CONNECT_TO_ME:
900 return udp_process_client_messages(callid, *call);
901 }
902
903 return ENOTSUP;
904}
905
906int main(int argc, char *argv[])
907{
908 /* Start the module */
909 return tl_module_start(SERVICE_UDP);
910}
911
912/** @}
913 */
Note: See TracBrowser for help on using the repository browser.