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

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

Integrate net_err.h into the standard library's err.h.

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