source: mainline/uspace/srv/net/udp/sock.c@ 98abd40

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 98abd40 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
Line 
1/*
2 * Copyright (c) 2008 Lukas Mejdrech
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
38#include <async.h>
39#include <byteorder.h>
40#include <errno.h>
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>
47
48#include "sock.h"
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);
63static int udp_sock_recv_fibril(void *arg);
64
65int udp_sock_init(void)
66{
67 socket_ports_initialize(&gsock);
68
69 async_set_client_connection(udp_sock_connection);
70
71 int rc = service_register(SERVICE_UDP);
72 if (rc != EOK)
73 return EEXIST;
74
75 return EOK;
76}
77
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;
83 (void)socket;
84
85 /* XXX We need to force the receive fibril to quit */
86}
87
88static void udp_sock_notify_data(socket_core_t *sock_core)
89{
90 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
91 async_exch_t *exch = async_exchange_begin(sock_core->sess);
92 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) sock_core->socket_id,
93 UDP_FRAGMENT_SIZE, 0, 0, 1);
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
105 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_socket()");
106 sock = calloc(1, sizeof(udp_sockdata_t));
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
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
120 rc = udp_uc_create(&sock->assoc);
121 if (rc != EOK) {
122 free(sock);
123 async_answer_0(callid, rc);
124 return;
125 }
126
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
135 sock_id = SOCKET_GET_SOCKET_ID(call);
136 rc = socket_create(&client->sockets, client->sess, sock, &sock_id);
137 if (rc != EOK) {
138 fibril_destroy(sock->recv_fibril);
139 udp_uc_destroy(sock->assoc);
140 free(sock);
141 async_answer_0(callid, rc);
142 return;
143 }
144
145 fibril_add_ready(sock->recv_fibril);
146
147 sock_core = socket_cores_find(&client->sockets, sock_id);
148 assert(sock_core != NULL);
149 sock->sock_core = sock_core;
150
151 SOCKET_SET_SOCKET_ID(answer, sock_id);
152
153 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, UDP_FRAGMENT_SIZE);
154 SOCKET_SET_HEADER_SIZE(answer, sizeof(udp_header_t));
155 async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
156 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
157}
158
159static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
160{
161 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()");
162 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
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);
167 if (rc != EOK) {
168 async_answer_0(callid, rc);
169 return;
170 }
171
172 if ((addr_len != sizeof(struct sockaddr_in)) &&
173 (addr_len != sizeof(struct sockaddr_in6))) {
174 async_answer_0(callid, EINVAL);
175 goto out;
176 }
177
178 struct sockaddr_in *addr = (struct sockaddr_in *) addr6;
179
180 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
181
182 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
183 addr6, addr_len, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
184 last_used_port);
185 if (rc != EOK) {
186 async_answer_0(callid, rc);
187 goto out;
188 }
189
190 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
191
192 socket_core_t *sock_core = socket_cores_find(&client->sockets,
193 SOCKET_GET_SOCKET_ID(call));
194 if (sock_core == NULL) {
195 async_answer_0(callid, ENOENT);
196 goto out;
197 }
198
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
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 }
236
237 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
238 async_answer_0(callid, rc);
239
240out:
241 if (addr6 != NULL)
242 free(addr6);
243}
244
245static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
246{
247 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_listen()");
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{
253 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connect()");
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{
259 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_accept()");
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{
265 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
266
267 struct sockaddr_in6 *addr6 = NULL;
268 struct sockaddr_in *addr;
269 udp_sock_t fsocket;
270 udp_sock_t *fsocket_ptr;
271
272 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
273 size_t addr_len;
274 int rc = async_data_write_accept((void **) &addr6, false,
275 0, 0, 0, &addr_len);
276 if (rc != EOK) {
277 async_answer_0(callid, rc);
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);
284 goto out;
285 }
286
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:
297 async_answer_0(callid, EINVAL);
298 goto out;
299 }
300
301 fsocket.port = uint16_t_be2host(addr->sin_port);
302 fsocket_ptr = &fsocket;
303 } else
304 fsocket_ptr = NULL;
305
306 int socket_id = SOCKET_GET_SOCKET_ID(call);
307
308 SOCKET_GET_FLAGS(call);
309
310 socket_core_t *sock_core =
311 socket_cores_find(&client->sockets, socket_id);
312 if (sock_core == NULL) {
313 async_answer_0(callid, ENOTSOCK);
314 goto out;
315 }
316
317 udp_sockdata_t *socket =
318 (udp_sockdata_t *) sock_core->specific_data;
319
320 if (sock_core->port <= 0) {
321 /* Implicitly bind socket to port */
322 int rc = socket_bind_free_port(&gsock, sock_core,
323 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port);
324 if (rc != EOK) {
325 async_answer_0(callid, rc);
326 goto out;
327 }
328
329 assert(sock_core->port > 0);
330
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 }
339
340 last_used_port = sock_core->port;
341 }
342
343 fibril_mutex_lock(&socket->lock);
344
345 if (inet_addr_is_any(&socket->assoc->ident.local.addr)) {
346 /* Determine local IP address */
347 inet_addr_t loc_addr;
348 inet_addr_t rem_addr;
349
350 rem_addr = fsocket_ptr ? fsocket.addr :
351 socket->assoc->ident.foreign.addr;
352
353 int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
354 if (rc != EOK) {
355 fibril_mutex_unlock(&socket->lock);
356 async_answer_0(callid, rc);
357 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to "
358 "determine local address.");
359 return;
360 }
361
362 socket->assoc->ident.local.addr = loc_addr;
363 }
364
365 assert(socket->assoc != NULL);
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
372 if (!async_data_write_receive(&wcallid, &length)) {
373 fibril_mutex_unlock(&socket->lock);
374 async_answer_0(callid, EINVAL);
375 goto out;
376 }
377
378 if (length > UDP_FRAGMENT_SIZE)
379 length = UDP_FRAGMENT_SIZE;
380
381 uint8_t buffer[UDP_FRAGMENT_SIZE];
382 int rc = async_data_write_finalize(wcallid, buffer, length);
383 if (rc != EOK) {
384 fibril_mutex_unlock(&socket->lock);
385 async_answer_0(callid, rc);
386 goto out;
387 }
388
389 udp_error_t urc =
390 udp_uc_send(socket->assoc, fsocket_ptr, buffer, length, 0);
391
392 switch (urc) {
393 case UDP_EOK:
394 rc = EOK;
395 break;
396 case UDP_ENORES:
397 rc = ENOMEM;
398 break;
399 case UDP_EUNSPEC:
400 rc = EINVAL;
401 break;
402 case UDP_ENOROUTE:
403 rc = EIO;
404 break;
405 default:
406 assert(false);
407 }
408
409 if (rc != EOK) {
410 fibril_mutex_unlock(&socket->lock);
411 async_answer_0(callid, rc);
412 goto out;
413 }
414 }
415
416 ipc_call_t answer;
417
418 IPC_SET_ARG1(answer, 0);
419 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, UDP_FRAGMENT_SIZE);
420 async_answer_2(callid, EOK, IPC_GET_ARG1(answer),
421 IPC_GET_ARG2(answer));
422 fibril_mutex_unlock(&socket->lock);
423
424out:
425 if (addr6 != NULL)
426 free(addr6);
427}
428
429static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
430{
431 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
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);
437 if (sock_core == NULL) {
438 async_answer_0(callid, ENOTSOCK);
439 return;
440 }
441
442 udp_sockdata_t *socket =
443 (udp_sockdata_t *) sock_core->specific_data;
444
445 fibril_mutex_lock(&socket->lock);
446
447 if (socket->assoc == NULL) {
448 fibril_mutex_unlock(&socket->lock);
449 async_answer_0(callid, ENOTCONN);
450 return;
451 }
452
453 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
454
455 fibril_mutex_lock(&socket->recv_buffer_lock);
456
457 while ((socket->recv_buffer_used == 0) &&
458 (socket->recv_error == UDP_EOK)) {
459 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
460 fibril_condvar_wait(&socket->recv_buffer_cv,
461 &socket->recv_buffer_lock);
462 }
463
464 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
465
466 size_t data_len = socket->recv_buffer_used;
467 udp_error_t urc = socket->recv_error;
468
469 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len);
470
471 int rc;
472
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 }
487
488 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
489
490 if (rc != EOK) {
491 fibril_mutex_unlock(&socket->recv_buffer_lock);
492 fibril_mutex_unlock(&socket->lock);
493 async_answer_0(callid, rc);
494 return;
495 }
496
497 ipc_callid_t rcallid;
498 size_t addr_size = 0;
499
500 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
501 /* Fill address */
502 udp_sock_t *rsock = &socket->recv_fsock;
503 struct sockaddr_in addr;
504 struct sockaddr_in6 addr6;
505 size_t addr_length;
506
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:
564 fibril_mutex_unlock(&socket->recv_buffer_lock);
565 fibril_mutex_unlock(&socket->lock);
566 async_answer_0(callid, EINVAL);
567 return;
568 }
569 }
570
571 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
572
573 size_t length;
574 if (!async_data_read_receive(&rcallid, &length)) {
575 fibril_mutex_unlock(&socket->recv_buffer_lock);
576 fibril_mutex_unlock(&socket->lock);
577 async_answer_0(callid, EINVAL);
578 return;
579 }
580
581 if (length > data_len)
582 length = data_len;
583
584 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
585
586 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
587
588 if ((length < data_len) && (rc == EOK))
589 rc = EOVERFLOW;
590
591 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length);
592
593 ipc_call_t answer;
594
595 IPC_SET_ARG2(answer, 0);
596 SOCKET_SET_READ_DATA_LENGTH(answer, length);
597 SOCKET_SET_ADDRESS_LENGTH(answer, addr_size);
598 async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
599 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
600
601 socket->recv_buffer_used = 0;
602
603 fibril_condvar_broadcast(&socket->recv_buffer_cv);
604 fibril_mutex_unlock(&socket->recv_buffer_lock);
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{
610 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
611 int socket_id = SOCKET_GET_SOCKET_ID(call);
612
613 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - find core");
614 socket_core_t *sock_core =
615 socket_cores_find(&client->sockets, socket_id);
616 if (sock_core == NULL) {
617 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - core not found");
618 async_answer_0(callid, ENOTSOCK);
619 return;
620 }
621
622 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - spec data");
623 udp_sockdata_t *socket =
624 (udp_sockdata_t *) sock_core->specific_data;
625 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket");
626 fibril_mutex_lock(&socket->lock);
627
628 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket buffer");
629 fibril_mutex_lock(&socket->recv_buffer_lock);
630 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - set socket->sock_core = NULL");
631 socket->sock_core = NULL;
632 fibril_mutex_unlock(&socket->recv_buffer_lock);
633
634 udp_uc_reset(socket->assoc);
635
636 int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
637 udp_free_sock_data);
638 if (rc != EOK) {
639 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - socket_destroy failed");
640 fibril_mutex_unlock(&socket->lock);
641 async_answer_0(callid, rc);
642 return;
643 }
644
645 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - broadcast recv_buffer_cv");
646 fibril_condvar_broadcast(&socket->recv_buffer_cv);
647
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{
654 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_getsockopt()");
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{
660 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt()");
661 async_answer_0(callid, ENOTSUP);
662}
663
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
671 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()");
672
673 fibril_mutex_lock(&sock->recv_buffer_lock);
674
675 while (true) {
676 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()");
677 while ((sock->recv_buffer_used != 0) && (sock->sock_core != NULL)) {
678 fibril_condvar_wait(&sock->recv_buffer_cv,
679 &sock->recv_buffer_lock);
680 }
681
682 fibril_mutex_unlock(&sock->recv_buffer_lock);
683
684 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
685 urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
686 UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
687 fibril_mutex_lock(&sock->recv_buffer_lock);
688 sock->recv_error = urc;
689
690 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] udp_uc_receive -> %d", urc);
691
692 if (sock->sock_core != NULL)
693 udp_sock_notify_data(sock->sock_core);
694
695 if (urc != UDP_EOK) {
696 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break");
697 fibril_condvar_broadcast(&sock->recv_buffer_cv);
698 fibril_mutex_unlock(&sock->recv_buffer_lock);
699 break;
700 }
701
702 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
703
704 sock->recv_buffer_used = rcvd;
705 fibril_condvar_broadcast(&sock->recv_buffer_cv);
706 }
707
708 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() exited loop");
709 fibril_mutex_unlock(&sock->recv_buffer_lock);
710 udp_uc_destroy(sock->assoc);
711
712 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() terminated");
713
714 return 0;
715}
716
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) {
730 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: wait");
731 callid = async_get_call(&call);
732 if (!IPC_GET_IMETHOD(call))
733 break;
734
735 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: METHOD=%d",
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 }
776
777 /* Clean up */
778 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: Clean up");
779 async_hangup(client.sess);
780 socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
781}
782
783/**
784 * @}
785 */
Note: See TracBrowser for help on using the repository browser.