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

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

Do not leak the 'names' measured string values.

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