source: mainline/uspace/srv/net/udp/sock.c@ 02a09ed

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02a09ed was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 20.0 KB
RevLine 
[66a272f8]1/*
[e33bceb]2 * Copyright (c) 2008 Lukas Mejdrech
[66a272f8]3 * Copyright (c) 2012 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup udp
31 * @{
32 */
33
34/**
35 * @file Socket provider
36 */
37
[e33bceb]38#include <async.h>
39#include <byteorder.h>
[66a272f8]40#include <errno.h>
[e33bceb]41#include <inet/inet.h>
42#include <io/log.h>
43#include <ipc/services.h>
44#include <ipc/socket.h>
45#include <net/socket.h>
46#include <ns.h>
[66a272f8]47
48#include "sock.h"
[e33bceb]49#include "std.h"
50#include "udp_type.h"
51#include "ucall.h"
52
53/** Free ports pool start. */
54#define UDP_FREE_PORTS_START 1025
55
56/** Free ports pool end. */
57#define UDP_FREE_PORTS_END 65535
58
59static int last_used_port = UDP_FREE_PORTS_START - 1;
60static socket_ports_t gsock;
61
62static void udp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
[32d19f7]63static int udp_sock_recv_fibril(void *arg);
[66a272f8]64
65int udp_sock_init(void)
66{
[e33bceb]67 socket_ports_initialize(&gsock);
[1038a9c]68
[e33bceb]69 async_set_client_connection(udp_sock_connection);
[1038a9c]70
71 int rc = service_register(SERVICE_UDP);
[e33bceb]72 if (rc != EOK)
73 return EEXIST;
[1038a9c]74
[66a272f8]75 return EOK;
76}
77
[e33bceb]78static void udp_free_sock_data(socket_core_t *sock_core)
79{
80 udp_sockdata_t *socket;
81
82 socket = (udp_sockdata_t *)sock_core->specific_data;
[32d19f7]83 (void)socket;
84
85 /* XXX We need to force the receive fibril to quit */
[e33bceb]86}
87
88static void udp_sock_notify_data(socket_core_t *sock_core)
89{
[a1a101d]90 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
[e33bceb]91 async_exch_t *exch = async_exchange_begin(sock_core->sess);
[42fd4d2]92 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) sock_core->socket_id,
[32d19f7]93 UDP_FRAGMENT_SIZE, 0, 0, 1);
[e33bceb]94 async_exchange_end(exch);
95}
96
97static void udp_sock_socket(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
98{
99 udp_sockdata_t *sock;
100 socket_core_t *sock_core;
101 int sock_id;
102 int rc;
103 ipc_call_t answer;
104
[a1a101d]105 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_socket()");
[db81577]106 sock = calloc(1, sizeof(udp_sockdata_t));
[e33bceb]107 if (sock == NULL) {
108 async_answer_0(callid, ENOMEM);
109 return;
110 }
111
112 fibril_mutex_initialize(&sock->lock);
113 sock->client = client;
114
[32d19f7]115 sock->recv_buffer_used = 0;
116 sock->recv_error = UDP_EOK;
117 fibril_mutex_initialize(&sock->recv_buffer_lock);
118 fibril_condvar_initialize(&sock->recv_buffer_cv);
119
[e33bceb]120 rc = udp_uc_create(&sock->assoc);
121 if (rc != EOK) {
122 free(sock);
123 async_answer_0(callid, rc);
124 return;
125 }
126
[32d19f7]127 sock->recv_fibril = fibril_create(udp_sock_recv_fibril, sock);
128 if (sock->recv_fibril == 0) {
129 udp_uc_destroy(sock->assoc);
130 free(sock);
131 async_answer_0(callid, ENOMEM);
132 return;
133 }
134
[e33bceb]135 sock_id = SOCKET_GET_SOCKET_ID(call);
136 rc = socket_create(&client->sockets, client->sess, sock, &sock_id);
137 if (rc != EOK) {
[32d19f7]138 fibril_destroy(sock->recv_fibril);
139 udp_uc_destroy(sock->assoc);
140 free(sock);
[e33bceb]141 async_answer_0(callid, rc);
142 return;
143 }
144
[32d19f7]145 fibril_add_ready(sock->recv_fibril);
146
[e33bceb]147 sock_core = socket_cores_find(&client->sockets, sock_id);
148 assert(sock_core != NULL);
149 sock->sock_core = sock_core;
[b1bd89ea]150
[e33bceb]151 SOCKET_SET_SOCKET_ID(answer, sock_id);
152
[32d19f7]153 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, UDP_FRAGMENT_SIZE);
[e33bceb]154 SOCKET_SET_HEADER_SIZE(answer, sizeof(udp_header_t));
[b1bd89ea]155 async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
156 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
[e33bceb]157}
158
159static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
160{
[a1a101d]161 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()");
162 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
[02a09ed]163
164 struct sockaddr_in6 *addr6 = NULL;
165 size_t addr_len;
166 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len);
[e33bceb]167 if (rc != EOK) {
168 async_answer_0(callid, rc);
[02a09ed]169 return;
[e33bceb]170 }
[8fc8969]171
[02a09ed]172 if ((addr_len != sizeof(struct sockaddr_in)) &&
173 (addr_len != sizeof(struct sockaddr_in6))) {
[8fc8969]174 async_answer_0(callid, EINVAL);
175 goto out;
176 }
177
[02a09ed]178 struct sockaddr_in *addr = (struct sockaddr_in *) addr6;
179
[a1a101d]180 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
[02a09ed]181
[e33bceb]182 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
[02a09ed]183 addr6, addr_len, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
[e33bceb]184 last_used_port);
185 if (rc != EOK) {
186 async_answer_0(callid, rc);
187 goto out;
188 }
[8fc8969]189
[a1a101d]190 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
[02a09ed]191
192 socket_core_t *sock_core = socket_cores_find(&client->sockets,
193 SOCKET_GET_SOCKET_ID(call));
[e33bceb]194 if (sock_core == NULL) {
195 async_answer_0(callid, ENOENT);
196 goto out;
197 }
[19a4f73]198
[02a09ed]199 udp_sockdata_t *socket =
200 (udp_sockdata_t *) sock_core->specific_data;
201
202 udp_sock_t fsocket;
203
204 fsocket.port = sock_core->port;
205
206 switch (addr->sin_family) {
207 case AF_INET:
208 inet_sockaddr_in_addr(addr, &fsocket.addr);
209 break;
210 case AF_INET6:
211 inet_sockaddr_in6_addr(addr6, &fsocket.addr);
212 break;
213 default:
214 async_answer_0(callid, EINVAL);
215 goto out;
216 }
217
218 udp_error_t urc = udp_uc_set_local(socket->assoc, &fsocket);
219
[e33bceb]220 switch (urc) {
221 case UDP_EOK:
222 rc = EOK;
223 break;
224/* case TCP_ENOTEXIST:
225 rc = ENOTCONN;
226 break;
227 case TCP_ECLOSING:
228 rc = ENOTCONN;
229 break;
230 case TCP_ERESET:
231 rc = ECONNABORTED;
232 break;*/
233 default:
234 assert(false);
235 }
[257feec]236
[a1a101d]237 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
[e33bceb]238 async_answer_0(callid, rc);
[02a09ed]239
[e33bceb]240out:
[02a09ed]241 if (addr6 != NULL)
242 free(addr6);
[e33bceb]243}
244
245static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
246{
[a1a101d]247 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_listen()");
[e33bceb]248 async_answer_0(callid, ENOTSUP);
249}
250
251static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
252{
[a1a101d]253 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connect()");
[e33bceb]254 async_answer_0(callid, ENOTSUP);
255}
256
257static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
258{
[a1a101d]259 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_accept()");
[e33bceb]260 async_answer_0(callid, ENOTSUP);
261}
262
263static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
264{
[a1a101d]265 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
[451481c8]266
[02a09ed]267 struct sockaddr_in6 *addr6 = NULL;
268 struct sockaddr_in *addr;
269 udp_sock_t fsocket;
270 udp_sock_t *fsocket_ptr;
[451481c8]271
[e33bceb]272 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
[02a09ed]273 size_t addr_len;
274 int rc = async_data_write_accept((void **) &addr6, false,
275 0, 0, 0, &addr_len);
[e33bceb]276 if (rc != EOK) {
277 async_answer_0(callid, rc);
[02a09ed]278 return;
279 }
280
281 if ((addr_len != sizeof(struct sockaddr_in)) &&
282 (addr_len != sizeof(struct sockaddr_in6))) {
283 async_answer_0(callid, EINVAL);
[e33bceb]284 goto out;
285 }
[451481c8]286
[02a09ed]287 addr = (struct sockaddr_in *) addr6;
288
289 switch (addr->sin_family) {
290 case AF_INET:
291 inet_sockaddr_in_addr(addr, &fsocket.addr);
292 break;
293 case AF_INET6:
294 inet_sockaddr_in6_addr(addr6, &fsocket.addr);
295 break;
296 default:
[e33bceb]297 async_answer_0(callid, EINVAL);
298 goto out;
299 }
[451481c8]300
[02a09ed]301 fsocket.port = uint16_t_be2host(addr->sin_port);
302 fsocket_ptr = &fsocket;
[451481c8]303 } else
[02a09ed]304 fsocket_ptr = NULL;
[451481c8]305
306 int socket_id = SOCKET_GET_SOCKET_ID(call);
307
[e33bceb]308 SOCKET_GET_FLAGS(call);
[451481c8]309
310 socket_core_t *sock_core =
311 socket_cores_find(&client->sockets, socket_id);
[e33bceb]312 if (sock_core == NULL) {
313 async_answer_0(callid, ENOTSOCK);
314 goto out;
315 }
[451481c8]316
317 udp_sockdata_t *socket =
318 (udp_sockdata_t *) sock_core->specific_data;
319
[6b0b508]320 if (sock_core->port <= 0) {
[92b42442]321 /* Implicitly bind socket to port */
[451481c8]322 int rc = socket_bind_free_port(&gsock, sock_core,
323 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port);
[e33bceb]324 if (rc != EOK) {
325 async_answer_0(callid, rc);
326 goto out;
327 }
[451481c8]328
329 assert(sock_core->port > 0);
[6b0b508]330
[451481c8]331 udp_error_t urc = udp_uc_set_local_port(socket->assoc,
332 sock_core->port);
333
334 if (urc != UDP_EOK) {
335 // TODO: better error handling
336 async_answer_0(callid, EINTR);
337 goto out;
338 }
[6b0b508]339
340 last_used_port = sock_core->port;
[e33bceb]341 }
[451481c8]342
[e33bceb]343 fibril_mutex_lock(&socket->lock);
[451481c8]344
[a2e3ee6]345 if (inet_addr_is_any(&socket->assoc->ident.local.addr)) {
[92b42442]346 /* Determine local IP address */
[a2e3ee6]347 inet_addr_t loc_addr;
348 inet_addr_t rem_addr;
[451481c8]349
[02a09ed]350 rem_addr = fsocket_ptr ? fsocket.addr :
[19a4f73]351 socket->assoc->ident.foreign.addr;
[451481c8]352
[a2e3ee6]353 int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
[92b42442]354 if (rc != EOK) {
355 fibril_mutex_unlock(&socket->lock);
356 async_answer_0(callid, rc);
[a1a101d]357 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to "
[92b42442]358 "determine local address.");
359 return;
360 }
[451481c8]361
[19a4f73]362 socket->assoc->ident.local.addr = loc_addr;
[92b42442]363 }
[451481c8]364
[e33bceb]365 assert(socket->assoc != NULL);
[451481c8]366
367 int fragments = SOCKET_GET_DATA_FRAGMENTS(call);
368 for (int index = 0; index < fragments; index++) {
369 ipc_callid_t wcallid;
370 size_t length;
371
[e33bceb]372 if (!async_data_write_receive(&wcallid, &length)) {
373 fibril_mutex_unlock(&socket->lock);
374 async_answer_0(callid, EINVAL);
375 goto out;
376 }
[451481c8]377
[32d19f7]378 if (length > UDP_FRAGMENT_SIZE)
379 length = UDP_FRAGMENT_SIZE;
[451481c8]380
381 uint8_t buffer[UDP_FRAGMENT_SIZE];
382 int rc = async_data_write_finalize(wcallid, buffer, length);
[e33bceb]383 if (rc != EOK) {
384 fibril_mutex_unlock(&socket->lock);
385 async_answer_0(callid, rc);
386 goto out;
387 }
[451481c8]388
389 udp_error_t urc =
[02a09ed]390 udp_uc_send(socket->assoc, fsocket_ptr, buffer, length, 0);
[451481c8]391
[e33bceb]392 switch (urc) {
393 case UDP_EOK:
394 rc = EOK;
395 break;
[08a6382]396 case UDP_ENORES:
[0ee053c1]397 rc = ENOMEM;
[e33bceb]398 break;
[08a6382]399 case UDP_EUNSPEC:
400 rc = EINVAL;
401 break;
402 case UDP_ENOROUTE:
403 rc = EIO;
[e33bceb]404 break;
405 default:
406 assert(false);
407 }
[451481c8]408
[e33bceb]409 if (rc != EOK) {
410 fibril_mutex_unlock(&socket->lock);
411 async_answer_0(callid, rc);
412 goto out;
413 }
414 }
[b1bd89ea]415
[451481c8]416 ipc_call_t answer;
417
[b1bd89ea]418 IPC_SET_ARG1(answer, 0);
[32d19f7]419 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, UDP_FRAGMENT_SIZE);
[b1bd89ea]420 async_answer_2(callid, EOK, IPC_GET_ARG1(answer),
421 IPC_GET_ARG2(answer));
[e33bceb]422 fibril_mutex_unlock(&socket->lock);
[b1bd89ea]423
[e33bceb]424out:
[02a09ed]425 if (addr6 != NULL)
426 free(addr6);
[e33bceb]427}
428
429static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
430{
[a1a101d]431 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
[02a09ed]432
433 int socket_id = SOCKET_GET_SOCKET_ID(call);
434
435 socket_core_t *sock_core =
436 socket_cores_find(&client->sockets, socket_id);
[e33bceb]437 if (sock_core == NULL) {
438 async_answer_0(callid, ENOTSOCK);
439 return;
440 }
[02a09ed]441
442 udp_sockdata_t *socket =
443 (udp_sockdata_t *) sock_core->specific_data;
444
[e33bceb]445 fibril_mutex_lock(&socket->lock);
[257feec]446
[e33bceb]447 if (socket->assoc == NULL) {
448 fibril_mutex_unlock(&socket->lock);
449 async_answer_0(callid, ENOTCONN);
450 return;
451 }
[02a09ed]452
[a1a101d]453 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
[02a09ed]454
[32d19f7]455 fibril_mutex_lock(&socket->recv_buffer_lock);
[02a09ed]456
457 while ((socket->recv_buffer_used == 0) &&
458 (socket->recv_error == UDP_EOK)) {
[a1a101d]459 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
[32d19f7]460 fibril_condvar_wait(&socket->recv_buffer_cv,
461 &socket->recv_buffer_lock);
462 }
[257feec]463
[a1a101d]464 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
[02a09ed]465
466 size_t data_len = socket->recv_buffer_used;
467 udp_error_t urc = socket->recv_error;
468
[a1a101d]469 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len);
[02a09ed]470
471 int rc;
472
[e33bceb]473 switch (urc) {
474 case UDP_EOK:
475 rc = EOK;
476 break;
477/* case TCP_ENOTEXIST:
478 case TCP_ECLOSING:
479 rc = ENOTCONN;
480 break;
481 case TCP_ERESET:
482 rc = ECONNABORTED;
483 break;*/
484 default:
485 assert(false);
486 }
[257feec]487
[a1a101d]488 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
[257feec]489
[e33bceb]490 if (rc != EOK) {
[32d19f7]491 fibril_mutex_unlock(&socket->recv_buffer_lock);
[e33bceb]492 fibril_mutex_unlock(&socket->lock);
493 async_answer_0(callid, rc);
494 return;
495 }
[02a09ed]496
497 ipc_callid_t rcallid;
498 size_t addr_size = 0;
499
[e33bceb]500 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
[19a4f73]501 /* Fill address */
[02a09ed]502 udp_sock_t *rsock = &socket->recv_fsock;
503 struct sockaddr_in addr;
504 struct sockaddr_in6 addr6;
505 size_t addr_length;
[19a4f73]506
[02a09ed]507 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr,
508 &addr6);
509
510 switch (addr_af) {
511 case AF_INET:
512 addr.sin_port = host2uint16_t_be(rsock->port);
513
514 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
515 if (!async_data_read_receive(&rcallid, &addr_length)) {
516 fibril_mutex_unlock(&socket->recv_buffer_lock);
517 fibril_mutex_unlock(&socket->lock);
518 async_answer_0(callid, EINVAL);
519 return;
520 }
521
522 if (addr_length > sizeof(addr))
523 addr_length = sizeof(addr);
524
525 addr_size = sizeof(addr);
526
527 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
528 rc = async_data_read_finalize(rcallid, &addr, addr_length);
529 if (rc != EOK) {
530 fibril_mutex_unlock(&socket->recv_buffer_lock);
531 fibril_mutex_unlock(&socket->lock);
532 async_answer_0(callid, EINVAL);
533 return;
534 }
535
536 break;
537 case AF_INET6:
538 addr6.sin6_port = host2uint16_t_be(rsock->port);
539
540 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive");
541 if (!async_data_read_receive(&rcallid, &addr_length)) {
542 fibril_mutex_unlock(&socket->recv_buffer_lock);
543 fibril_mutex_unlock(&socket->lock);
544 async_answer_0(callid, EINVAL);
545 return;
546 }
547
548 if (addr_length > sizeof(addr6))
549 addr_length = sizeof(addr6);
550
551 addr_size = sizeof(addr6);
552
553 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize");
554 rc = async_data_read_finalize(rcallid, &addr6, addr_length);
555 if (rc != EOK) {
556 fibril_mutex_unlock(&socket->recv_buffer_lock);
557 fibril_mutex_unlock(&socket->lock);
558 async_answer_0(callid, EINVAL);
559 return;
560 }
561
562 break;
563 default:
[32d19f7]564 fibril_mutex_unlock(&socket->recv_buffer_lock);
[e33bceb]565 fibril_mutex_unlock(&socket->lock);
566 async_answer_0(callid, EINVAL);
567 return;
568 }
569 }
[02a09ed]570
[a1a101d]571 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
[02a09ed]572
573 size_t length;
[e33bceb]574 if (!async_data_read_receive(&rcallid, &length)) {
[32d19f7]575 fibril_mutex_unlock(&socket->recv_buffer_lock);
[e33bceb]576 fibril_mutex_unlock(&socket->lock);
577 async_answer_0(callid, EINVAL);
578 return;
579 }
[257feec]580
[e33bceb]581 if (length > data_len)
582 length = data_len;
[257feec]583
[a1a101d]584 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
[02a09ed]585
[32d19f7]586 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
[02a09ed]587
588 if ((length < data_len) && (rc == EOK))
[e33bceb]589 rc = EOVERFLOW;
[257feec]590
[a1a101d]591 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length);
[02a09ed]592
593 ipc_call_t answer;
594
[b1bd89ea]595 IPC_SET_ARG2(answer, 0);
[e33bceb]596 SOCKET_SET_READ_DATA_LENGTH(answer, length);
[02a09ed]597 SOCKET_SET_ADDRESS_LENGTH(answer, addr_size);
[b1bd89ea]598 async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
599 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
[257feec]600
[32d19f7]601 socket->recv_buffer_used = 0;
[257feec]602
[32d19f7]603 fibril_condvar_broadcast(&socket->recv_buffer_cv);
604 fibril_mutex_unlock(&socket->recv_buffer_lock);
[e33bceb]605 fibril_mutex_unlock(&socket->lock);
606}
607
608static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
609{
[a1a101d]610 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
[42fd4d2]611 int socket_id = SOCKET_GET_SOCKET_ID(call);
[e33bceb]612
[ccb5165]613 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - find core");
[42fd4d2]614 socket_core_t *sock_core =
615 socket_cores_find(&client->sockets, socket_id);
[e33bceb]616 if (sock_core == NULL) {
[ccb5165]617 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - core not found");
[e33bceb]618 async_answer_0(callid, ENOTSOCK);
619 return;
620 }
621
[ccb5165]622 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - spec data");
[42fd4d2]623 udp_sockdata_t *socket =
624 (udp_sockdata_t *) sock_core->specific_data;
[ccb5165]625 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket");
[e33bceb]626 fibril_mutex_lock(&socket->lock);
627
[ccb5165]628 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket buffer");
[141a20d]629 fibril_mutex_lock(&socket->recv_buffer_lock);
[a1e2df13]630 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - set socket->sock_core = NULL");
[141a20d]631 socket->sock_core = NULL;
632 fibril_mutex_unlock(&socket->recv_buffer_lock);
633
634 udp_uc_reset(socket->assoc);
635
[42fd4d2]636 int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
[e33bceb]637 udp_free_sock_data);
638 if (rc != EOK) {
[a1e2df13]639 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - socket_destroy failed");
[e33bceb]640 fibril_mutex_unlock(&socket->lock);
641 async_answer_0(callid, rc);
642 return;
643 }
644
[a1e2df13]645 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - broadcast recv_buffer_cv");
[141a20d]646 fibril_condvar_broadcast(&socket->recv_buffer_cv);
647
[e33bceb]648 fibril_mutex_unlock(&socket->lock);
649 async_answer_0(callid, EOK);
650}
651
652static void udp_sock_getsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
653{
[a1a101d]654 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_getsockopt()");
[e33bceb]655 async_answer_0(callid, ENOTSUP);
656}
657
658static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
659{
[a1a101d]660 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt()");
[e33bceb]661 async_answer_0(callid, ENOTSUP);
662}
663
[32d19f7]664static int udp_sock_recv_fibril(void *arg)
665{
666 udp_sockdata_t *sock = (udp_sockdata_t *)arg;
667 udp_error_t urc;
668 xflags_t xflags;
669 size_t rcvd;
670
[a1a101d]671 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()");
[32d19f7]672
[141a20d]673 fibril_mutex_lock(&sock->recv_buffer_lock);
674
[32d19f7]675 while (true) {
[a1a101d]676 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()");
[257feec]677 while ((sock->recv_buffer_used != 0) && (sock->sock_core != NULL)) {
[32d19f7]678 fibril_condvar_wait(&sock->recv_buffer_cv,
679 &sock->recv_buffer_lock);
680 }
[ccb5165]681
682 fibril_mutex_unlock(&sock->recv_buffer_lock);
683
[a1a101d]684 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
[32d19f7]685 urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
686 UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
[ccb5165]687 fibril_mutex_lock(&sock->recv_buffer_lock);
[32d19f7]688 sock->recv_error = urc;
689
[a1e2df13]690 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] udp_uc_receive -> %d", urc);
[141a20d]691
692 if (sock->sock_core != NULL)
693 udp_sock_notify_data(sock->sock_core);
[ccb5165]694
[32d19f7]695 if (urc != UDP_EOK) {
[a1e2df13]696 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break");
[32d19f7]697 fibril_condvar_broadcast(&sock->recv_buffer_cv);
[02a09ed]698 fibril_mutex_unlock(&sock->recv_buffer_lock);
[32d19f7]699 break;
700 }
[ccb5165]701
[a1a101d]702 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
[ccb5165]703
[32d19f7]704 sock->recv_buffer_used = rcvd;
705 fibril_condvar_broadcast(&sock->recv_buffer_cv);
706 }
707
[a1e2df13]708 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() exited loop");
[141a20d]709 fibril_mutex_unlock(&sock->recv_buffer_lock);
[32d19f7]710 udp_uc_destroy(sock->assoc);
711
[a1e2df13]712 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() terminated");
[141a20d]713
[32d19f7]714 return 0;
715}
716
[e33bceb]717static void udp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
718{
719 ipc_callid_t callid;
720 ipc_call_t call;
721 udp_client_t client;
722
723 /* Accept the connection */
724 async_answer_0(iid, EOK);
725
726 client.sess = async_callback_receive(EXCHANGE_SERIALIZE);
727 socket_cores_initialize(&client.sockets);
728
729 while (true) {
[a1a101d]730 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: wait");
[e33bceb]731 callid = async_get_call(&call);
732 if (!IPC_GET_IMETHOD(call))
733 break;
734
[a1a101d]735 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: METHOD=%d",
[e33bceb]736 (int)IPC_GET_IMETHOD(call));
737
738 switch (IPC_GET_IMETHOD(call)) {
739 case NET_SOCKET:
740 udp_sock_socket(&client, callid, call);
741 break;
742 case NET_SOCKET_BIND:
743 udp_sock_bind(&client, callid, call);
744 break;
745 case NET_SOCKET_LISTEN:
746 udp_sock_listen(&client, callid, call);
747 break;
748 case NET_SOCKET_CONNECT:
749 udp_sock_connect(&client, callid, call);
750 break;
751 case NET_SOCKET_ACCEPT:
752 udp_sock_accept(&client, callid, call);
753 break;
754 case NET_SOCKET_SEND:
755 case NET_SOCKET_SENDTO:
756 udp_sock_sendto(&client, callid, call);
757 break;
758 case NET_SOCKET_RECV:
759 case NET_SOCKET_RECVFROM:
760 udp_sock_recvfrom(&client, callid, call);
761 break;
762 case NET_SOCKET_CLOSE:
763 udp_sock_close(&client, callid, call);
764 break;
765 case NET_SOCKET_GETSOCKOPT:
766 udp_sock_getsockopt(&client, callid, call);
767 break;
768 case NET_SOCKET_SETSOCKOPT:
769 udp_sock_setsockopt(&client, callid, call);
770 break;
771 default:
772 async_answer_0(callid, ENOTSUP);
773 break;
774 }
775 }
[0d520a2]776
777 /* Clean up */
[a1a101d]778 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: Clean up");
[0d520a2]779 async_hangup(client.sess);
780 socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
[e33bceb]781}
782
[66a272f8]783/**
784 * @}
785 */
Note: See TracBrowser for help on using the repository browser.