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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5d00e40 was 5d00e40, 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_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/** 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(ICMP_CONNECT_TIMEOUT);
396
397 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
398 SERVICE_UDP, udp_receiver);
399 if (udp_globals.ip_phone < 0) {
400 fibril_rwlock_write_unlock(&udp_globals.lock);
401 return udp_globals.ip_phone;
402 }
403
404 /* Read default packet dimensions */
405 int rc = ip_packet_size_req(udp_globals.ip_phone, -1,
406 &udp_globals.packet_dimension);
407 if (rc != EOK) {
408 fibril_rwlock_write_unlock(&udp_globals.lock);
409 return rc;
410 }
411
412 rc = socket_ports_initialize(&udp_globals.sockets);
413 if (rc != EOK) {
414 fibril_rwlock_write_unlock(&udp_globals.lock);
415 return rc;
416 }
417
418 rc = packet_dimensions_initialize(&udp_globals.dimensions);
419 if (rc != EOK) {
420 socket_ports_destroy(&udp_globals.sockets);
421 fibril_rwlock_write_unlock(&udp_globals.lock);
422 return rc;
423 }
424
425 udp_globals.packet_dimension.prefix += sizeof(udp_header_t);
426 udp_globals.packet_dimension.content -= sizeof(udp_header_t);
427 udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
428
429 udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
430 udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
431
432 /* Get configuration */
433 configuration = &names[0];
434 rc = net_get_conf_req(udp_globals.net_phone, &configuration, count,
435 &data);
436 if (rc != EOK) {
437 socket_ports_destroy(&udp_globals.sockets);
438 fibril_rwlock_write_unlock(&udp_globals.lock);
439 return rc;
440 }
441
442 if (configuration) {
443 if (configuration[0].value)
444 udp_globals.checksum_computing =
445 (configuration[0].value[0] == 'y');
446
447 if (configuration[1].value)
448 udp_globals.autobinding =
449 (configuration[1].value[0] == 'y');
450
451 net_free_settings(configuration, data);
452 }
453
454 fibril_rwlock_write_unlock(&udp_globals.lock);
455 return EOK;
456}
457
458/** Sends data from the socket to the remote address.
459 *
460 * Binds the socket to a free port if not already connected/bound.
461 * Handles the NET_SOCKET_SENDTO message.
462 * Supports AF_INET and AF_INET6 address families.
463 *
464 * @param[in,out] local_sockets The application local sockets.
465 * @param[in] socket_id Socket identifier.
466 * @param[in] addr The destination address.
467 * @param[in] addrlen The address length.
468 * @param[in] fragments The number of data fragments.
469 * @param[out] data_fragment_size The data fragment size in bytes.
470 * @param[in] flags Various send flags.
471 * @return EOK on success.
472 * @return EAFNOTSUPPORT if the address family is not supported.
473 * @return ENOTSOCK if the socket is not found.
474 * @return EINVAL if the address is invalid.
475 * @return ENOTCONN if the sending socket is not and cannot be
476 * bound.
477 * @return ENOMEM if there is not enough memory left.
478 * @return Other error codes as defined for the
479 * socket_read_packet_data() function.
480 * @return Other error codes as defined for the
481 * ip_client_prepare_packet() function.
482 * @return Other error codes as defined for the ip_send_msg()
483 * function.
484 */
485static int udp_sendto_message(socket_cores_t *local_sockets, int socket_id,
486 const struct sockaddr *addr, socklen_t addrlen, int fragments,
487 size_t *data_fragment_size, int flags)
488{
489 socket_core_t *socket;
490 packet_t *packet;
491 packet_t *next_packet;
492 udp_header_t *header;
493 int index;
494 size_t total_length;
495 int result;
496 uint16_t dest_port;
497 uint32_t checksum;
498 void *ip_header;
499 size_t headerlen;
500 device_id_t device_id;
501 packet_dimension_t *packet_dimension;
502 int rc;
503
504 rc = tl_get_address_port(addr, addrlen, &dest_port);
505 if (rc != EOK)
506 return rc;
507
508 socket = socket_cores_find(local_sockets, socket_id);
509 if (!socket)
510 return ENOTSOCK;
511
512 if ((socket->port <= 0) && udp_globals.autobinding) {
513 /* Bind the socket to a random free port if not bound */
514 rc = socket_bind_free_port(&udp_globals.sockets, socket,
515 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
516 udp_globals.last_used_port);
517 if (rc != EOK)
518 return rc;
519 /* Set the next port as the search starting port number */
520 udp_globals.last_used_port = socket->port;
521 }
522
523 if (udp_globals.checksum_computing) {
524 rc = ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr,
525 addrlen, &device_id, &ip_header, &headerlen);
526 if (rc != EOK)
527 return rc;
528 /* Get the device packet dimension */
529// rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
530// &udp_globals.dimensions, device_id, &packet_dimension);
531// if (rc != EOK)
532// return rc;
533 }
534// } else {
535 /* Do not ask all the time */
536 rc = ip_packet_size_req(udp_globals.ip_phone, -1,
537 &udp_globals.packet_dimension);
538 if (rc != EOK)
539 return rc;
540 packet_dimension = &udp_globals.packet_dimension;
541// }
542
543 /* Read the first packet fragment */
544 result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
545 UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
546 if (result < 0)
547 return result;
548
549 total_length = (size_t) result;
550 if (udp_globals.checksum_computing)
551 checksum = compute_checksum(0, packet_get_data(packet),
552 packet_get_data_length(packet));
553 else
554 checksum = 0;
555
556 /* Prefix the UDP header */
557 header = PACKET_PREFIX(packet, udp_header_t);
558 if (!header)
559 return udp_release_and_return(packet, ENOMEM);
560
561 bzero(header, sizeof(*header));
562
563 /* Read the rest of the packet fragments */
564 for (index = 1; index < fragments; index++) {
565 result = tl_socket_read_packet_data(udp_globals.net_phone,
566 &next_packet, 0, packet_dimension, addr, addrlen);
567 if (result < 0)
568 return udp_release_and_return(packet, result);
569
570 rc = pq_add(&packet, next_packet, index, 0);
571 if (rc != EOK)
572 return udp_release_and_return(packet, rc);
573
574 total_length += (size_t) result;
575 if (udp_globals.checksum_computing) {
576 checksum = compute_checksum(checksum,
577 packet_get_data(next_packet),
578 packet_get_data_length(next_packet));
579 }
580 }
581
582 /* Set the UDP header */
583 header->source_port = htons((socket->port > 0) ? socket->port : 0);
584 header->destination_port = htons(dest_port);
585 header->total_length = htons(total_length + sizeof(*header));
586 header->checksum = 0;
587
588 if (udp_globals.checksum_computing) {
589 /* Update the pseudo header */
590 rc = ip_client_set_pseudo_header_data_length(ip_header,
591 headerlen, total_length + UDP_HEADER_SIZE);
592 if (rc != EOK) {
593 free(ip_header);
594 return udp_release_and_return(packet, rc);
595 }
596
597 /* Finish the checksum computation */
598 checksum = compute_checksum(checksum, ip_header, headerlen);
599 checksum = compute_checksum(checksum, (uint8_t *) header,
600 sizeof(*header));
601 header->checksum =
602 htons(flip_checksum(compact_checksum(checksum)));
603 free(ip_header);
604 } else {
605 device_id = DEVICE_INVALID_ID;
606 }
607
608 /* Prepare the first packet fragment */
609 rc = ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0);
610 if (rc != EOK)
611 return udp_release_and_return(packet, rc);
612
613 /* Release the UDP global lock on success. */
614 fibril_rwlock_write_unlock(&udp_globals.lock);
615
616 /* Send the packet */
617 ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
618
619 return EOK;
620}
621
622/** Receives data to the socket.
623 *
624 * Handles the NET_SOCKET_RECVFROM message.
625 * Replies the source address as well.
626 *
627 * @param[in] local_sockets The application local sockets.
628 * @param[in] socket_id Socket identifier.
629 * @param[in] flags Various receive flags.
630 * @param[out] addrlen The source address length.
631 * @return The number of bytes received.
632 * @return ENOTSOCK if the socket is not found.
633 * @return NO_DATA if there are no received packets or data.
634 * @return ENOMEM if there is not enough memory left.
635 * @return EINVAL if the received address is not an IP address.
636 * @return Other error codes as defined for the packet_translate()
637 * function.
638 * @return Other error codes as defined for the data_reply()
639 * function.
640 */
641static int udp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
642 int flags, size_t *addrlen)
643{
644 socket_core_t *socket;
645 int packet_id;
646 packet_t *packet;
647 udp_header_t *header;
648 struct sockaddr *addr;
649 size_t length;
650 uint8_t *data;
651 int result;
652 int rc;
653
654 /* Find the socket */
655 socket = socket_cores_find(local_sockets, socket_id);
656 if (!socket)
657 return ENOTSOCK;
658
659 /* Get the next received packet */
660 packet_id = dyn_fifo_value(&socket->received);
661 if (packet_id < 0)
662 return NO_DATA;
663
664 rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
665 if (rc != EOK) {
666 (void) dyn_fifo_pop(&socket->received);
667 return rc;
668 }
669
670 /* Get UDP header */
671 data = packet_get_data(packet);
672 if (!data) {
673 (void) dyn_fifo_pop(&socket->received);
674 return udp_release_and_return(packet, NO_DATA);
675 }
676 header = (udp_header_t *) data;
677
678 /* Set the source address port */
679 result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
680 rc = tl_set_address_port(addr, result, ntohs(header->source_port));
681 if (rc != EOK) {
682 (void) dyn_fifo_pop(&socket->received);
683 return udp_release_and_return(packet, rc);
684 }
685 *addrlen = (size_t) result;
686
687 /* Send the source address */
688 rc = data_reply(addr, *addrlen);
689 switch (rc) {
690 case EOK:
691 break;
692 case EOVERFLOW:
693 return rc;
694 default:
695 (void) dyn_fifo_pop(&socket->received);
696 return udp_release_and_return(packet, rc);
697 }
698
699 /* Trim the header */
700 rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
701 if (rc != EOK) {
702 (void) dyn_fifo_pop(&socket->received);
703 return udp_release_and_return(packet, rc);
704 }
705
706 /* Reply the packets */
707 rc = socket_reply_packets(packet, &length);
708 switch (rc) {
709 case EOK:
710 break;
711 case EOVERFLOW:
712 return rc;
713 default:
714 (void) dyn_fifo_pop(&socket->received);
715 return udp_release_and_return(packet, rc);
716 }
717
718 (void) dyn_fifo_pop(&socket->received);
719
720 /* Release the packet and return the total length */
721 return udp_release_and_return(packet, (int) length);
722}
723
724/** Processes the socket client messages.
725 *
726 * Runs until the client module disconnects.
727 *
728 * @param[in] callid The message identifier.
729 * @param[in] call The message parameters.
730 * @return EOK on success.
731 *
732 * @see socket.h
733 */
734static int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
735{
736 int res;
737 bool keep_on_going = true;
738 socket_cores_t local_sockets;
739 int app_phone = IPC_GET_PHONE(call);
740 struct sockaddr *addr;
741 int socket_id;
742 size_t addrlen;
743 size_t size = 0;
744 ipc_call_t answer;
745 size_t answer_count;
746 packet_dimension_t *packet_dimension;
747
748 /*
749 * Accept the connection
750 * - Answer the first IPC_M_CONNECT_TO_ME call.
751 */
752 res = EOK;
753 answer_count = 0;
754
755 /*
756 * The client connection is only in one fibril and therefore no
757 * additional locks are needed.
758 */
759
760 socket_cores_initialize(&local_sockets);
761
762 while (keep_on_going) {
763
764 /* Answer the call */
765 answer_call(callid, res, &answer, answer_count);
766
767 /* Refresh data */
768 refresh_answer(&answer, &answer_count);
769
770 /* Get the next call */
771 callid = async_get_call(&call);
772
773 /* Process the call */
774 switch (IPC_GET_IMETHOD(call)) {
775 case IPC_M_PHONE_HUNGUP:
776 keep_on_going = false;
777 res = EHANGUP;
778 break;
779
780 case NET_SOCKET:
781 socket_id = SOCKET_GET_SOCKET_ID(call);
782 res = socket_create(&local_sockets, app_phone, NULL,
783 &socket_id);
784 SOCKET_SET_SOCKET_ID(answer, socket_id);
785
786 if (res != EOK)
787 break;
788
789 if (tl_get_ip_packet_dimension(udp_globals.ip_phone,
790 &udp_globals.dimensions, DEVICE_INVALID_ID,
791 &packet_dimension) == EOK) {
792 SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
793 packet_dimension->content);
794 }
795
796// SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
797// MAX_UDP_FRAGMENT_SIZE);
798 SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
799 answer_count = 3;
800 break;
801
802 case NET_SOCKET_BIND:
803 res = async_data_write_accept((void **) &addr, false,
804 0, 0, 0, &addrlen);
805 if (res != EOK)
806 break;
807 fibril_rwlock_write_lock(&udp_globals.lock);
808 res = socket_bind(&local_sockets, &udp_globals.sockets,
809 SOCKET_GET_SOCKET_ID(call), addr, addrlen,
810 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
811 udp_globals.last_used_port);
812 fibril_rwlock_write_unlock(&udp_globals.lock);
813 free(addr);
814 break;
815
816 case NET_SOCKET_SENDTO:
817 res = async_data_write_accept((void **) &addr, false,
818 0, 0, 0, &addrlen);
819 if (res != EOK)
820 break;
821
822 fibril_rwlock_write_lock(&udp_globals.lock);
823 res = udp_sendto_message(&local_sockets,
824 SOCKET_GET_SOCKET_ID(call), addr, addrlen,
825 SOCKET_GET_DATA_FRAGMENTS(call), &size,
826 SOCKET_GET_FLAGS(call));
827 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
828
829 if (res != EOK)
830 fibril_rwlock_write_unlock(&udp_globals.lock);
831 else
832 answer_count = 2;
833
834 free(addr);
835 break;
836
837 case NET_SOCKET_RECVFROM:
838 fibril_rwlock_write_lock(&udp_globals.lock);
839 res = udp_recvfrom_message(&local_sockets,
840 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
841 &addrlen);
842 fibril_rwlock_write_unlock(&udp_globals.lock);
843
844 if (res <= 0)
845 break;
846
847 SOCKET_SET_READ_DATA_LENGTH(answer, res);
848 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
849 answer_count = 3;
850 res = EOK;
851 break;
852
853 case NET_SOCKET_CLOSE:
854 fibril_rwlock_write_lock(&udp_globals.lock);
855 res = socket_destroy(udp_globals.net_phone,
856 SOCKET_GET_SOCKET_ID(call), &local_sockets,
857 &udp_globals.sockets, NULL);
858 fibril_rwlock_write_unlock(&udp_globals.lock);
859 break;
860
861 case NET_SOCKET_GETSOCKOPT:
862 case NET_SOCKET_SETSOCKOPT:
863 default:
864 res = ENOTSUP;
865 break;
866 }
867 }
868
869 /* Release the application phone */
870 ipc_hangup(app_phone);
871
872 /* Release all local sockets */
873 socket_cores_release(udp_globals.net_phone, &local_sockets,
874 &udp_globals.sockets, NULL);
875
876 return res;
877}
878
879/** Per-connection initialization
880 *
881 */
882void tl_connection(void)
883{
884}
885
886/** Processes the UDP message.
887 *
888 * @param[in] callid The message identifier.
889 * @param[in] call The message parameters.
890 * @param[out] answer The message answer parameters.
891 * @param[out] answer_count The last parameter for the actual answer in the
892 * answer parameter.
893 * @return EOK on success.
894 * @return ENOTSUP if the message is not known.
895 *
896 * @see udp_interface.h
897 * @see IS_NET_UDP_MESSAGE()
898 */
899int tl_message(ipc_callid_t callid, ipc_call_t *call,
900 ipc_call_t *answer, size_t *answer_count)
901{
902 *answer_count = 0;
903
904 switch (IPC_GET_IMETHOD(*call)) {
905 case IPC_M_CONNECT_TO_ME:
906 return udp_process_client_messages(callid, *call);
907 }
908
909 return ENOTSUP;
910}
911
912int main(int argc, char *argv[])
913{
914 /* Start the module */
915 return tl_module_start(SERVICE_UDP);
916}
917
918/** @}
919 */
Note: See TracBrowser for help on using the repository browser.