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

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

Cleanup udp.

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