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

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

Move the rest of net_messages.h to standard library.

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