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

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

Get rid of the ERROR_CODE madness in udp.

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