source: mainline/uspace/srv/net/udp/sock.c@ 257feec

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

cstyle (no change in functionality)

  • Property mode set to 100644
File size: 18.6 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 int rc;
162 struct sockaddr_in *addr;
163 size_t addr_size;
164 socket_core_t *sock_core;
165 udp_sockdata_t *socket;
166 udp_sock_t fsock;
167 udp_error_t urc;
168
169 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()");
170 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
171
172 addr = NULL;
173
174 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
175 if (rc != EOK) {
176 async_answer_0(callid, rc);
177 goto out;
178 }
179
180 if (addr_size != sizeof(struct sockaddr_in)) {
181 async_answer_0(callid, EINVAL);
182 goto out;
183 }
184
185 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
186 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
187 addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
188 last_used_port);
189 if (rc != EOK) {
190 async_answer_0(callid, rc);
191 goto out;
192 }
193
194 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
195 sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
196 if (sock_core == NULL) {
197 async_answer_0(callid, ENOENT);
198 goto out;
199 }
200
201 socket = (udp_sockdata_t *) sock_core->specific_data;
202
203 inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr),
204 &fsock.addr);
205 fsock.port = sock_core->port;
206 urc = udp_uc_set_local(socket->assoc, &fsock);
207
208 switch (urc) {
209 case UDP_EOK:
210 rc = EOK;
211 break;
212/* case TCP_ENOTEXIST:
213 rc = ENOTCONN;
214 break;
215 case TCP_ECLOSING:
216 rc = ENOTCONN;
217 break;
218 case TCP_ERESET:
219 rc = ECONNABORTED;
220 break;*/
221 default:
222 assert(false);
223 }
224
225 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
226 async_answer_0(callid, rc);
227out:
228 if (addr != NULL)
229 free(addr);
230}
231
232static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
233{
234 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_listen()");
235 async_answer_0(callid, ENOTSUP);
236}
237
238static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
239{
240 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connect()");
241 async_answer_0(callid, ENOTSUP);
242}
243
244static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
245{
246 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_accept()");
247 async_answer_0(callid, ENOTSUP);
248}
249
250static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
251{
252 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
253
254 struct sockaddr_in *addr = NULL;
255 udp_sock_t fsock;
256 udp_sock_t *fsock_ptr;
257
258 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
259 size_t addr_size;
260 int rc = async_data_write_accept((void **) &addr, false,
261 0, 0, 0, &addr_size);
262 if (rc != EOK) {
263 async_answer_0(callid, rc);
264 goto out;
265 }
266
267 if (addr_size != sizeof(struct sockaddr_in)) {
268 async_answer_0(callid, EINVAL);
269 goto out;
270 }
271
272 inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr),
273 &fsock.addr);
274 fsock.port = uint16_t_be2host(addr->sin_port);
275 fsock_ptr = &fsock;
276 } else
277 fsock_ptr = NULL;
278
279 int socket_id = SOCKET_GET_SOCKET_ID(call);
280
281 SOCKET_GET_FLAGS(call);
282
283 socket_core_t *sock_core =
284 socket_cores_find(&client->sockets, socket_id);
285 if (sock_core == NULL) {
286 async_answer_0(callid, ENOTSOCK);
287 goto out;
288 }
289
290 udp_sockdata_t *socket =
291 (udp_sockdata_t *) sock_core->specific_data;
292
293 if (sock_core->port <= 0) {
294 /* Implicitly bind socket to port */
295 int rc = socket_bind_free_port(&gsock, sock_core,
296 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port);
297 if (rc != EOK) {
298 async_answer_0(callid, rc);
299 goto out;
300 }
301
302 assert(sock_core->port > 0);
303
304 udp_error_t urc = udp_uc_set_local_port(socket->assoc,
305 sock_core->port);
306
307 if (urc != UDP_EOK) {
308 // TODO: better error handling
309 async_answer_0(callid, EINTR);
310 goto out;
311 }
312
313 last_used_port = sock_core->port;
314 }
315
316 fibril_mutex_lock(&socket->lock);
317
318 if (inet_addr_is_any(&socket->assoc->ident.local.addr)) {
319 /* Determine local IP address */
320 inet_addr_t loc_addr;
321 inet_addr_t rem_addr;
322
323 rem_addr = fsock_ptr ? fsock.addr :
324 socket->assoc->ident.foreign.addr;
325
326 int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
327 if (rc != EOK) {
328 fibril_mutex_unlock(&socket->lock);
329 async_answer_0(callid, rc);
330 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to "
331 "determine local address.");
332 return;
333 }
334
335 socket->assoc->ident.local.addr = loc_addr;
336 }
337
338 assert(socket->assoc != NULL);
339
340 int fragments = SOCKET_GET_DATA_FRAGMENTS(call);
341 for (int index = 0; index < fragments; index++) {
342 ipc_callid_t wcallid;
343 size_t length;
344
345 if (!async_data_write_receive(&wcallid, &length)) {
346 fibril_mutex_unlock(&socket->lock);
347 async_answer_0(callid, EINVAL);
348 goto out;
349 }
350
351 if (length > UDP_FRAGMENT_SIZE)
352 length = UDP_FRAGMENT_SIZE;
353
354 uint8_t buffer[UDP_FRAGMENT_SIZE];
355 int rc = async_data_write_finalize(wcallid, buffer, length);
356 if (rc != EOK) {
357 fibril_mutex_unlock(&socket->lock);
358 async_answer_0(callid, rc);
359 goto out;
360 }
361
362 udp_error_t urc =
363 udp_uc_send(socket->assoc, fsock_ptr, buffer, length, 0);
364
365 switch (urc) {
366 case UDP_EOK:
367 rc = EOK;
368 break;
369 case UDP_ENORES:
370 rc = ENOMEM;
371 break;
372 case UDP_EUNSPEC:
373 rc = EINVAL;
374 break;
375 case UDP_ENOROUTE:
376 rc = EIO;
377 break;
378 default:
379 assert(false);
380 }
381
382 if (rc != EOK) {
383 fibril_mutex_unlock(&socket->lock);
384 async_answer_0(callid, rc);
385 goto out;
386 }
387 }
388
389 ipc_call_t answer;
390
391 IPC_SET_ARG1(answer, 0);
392 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, UDP_FRAGMENT_SIZE);
393 async_answer_2(callid, EOK, IPC_GET_ARG1(answer),
394 IPC_GET_ARG2(answer));
395 fibril_mutex_unlock(&socket->lock);
396
397out:
398 if (addr != NULL)
399 free(addr);
400}
401
402static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
403{
404 int socket_id;
405 int flags;
406 size_t addr_length, length;
407 socket_core_t *sock_core;
408 udp_sockdata_t *socket;
409 ipc_call_t answer;
410 ipc_callid_t rcallid;
411 size_t data_len;
412 udp_error_t urc;
413 udp_sock_t *rsock;
414 struct sockaddr_in addr;
415 int rc;
416
417 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
418
419 socket_id = SOCKET_GET_SOCKET_ID(call);
420 flags = SOCKET_GET_FLAGS(call);
421
422 sock_core = socket_cores_find(&client->sockets, socket_id);
423 if (sock_core == NULL) {
424 async_answer_0(callid, ENOTSOCK);
425 return;
426 }
427
428 socket = (udp_sockdata_t *)sock_core->specific_data;
429 fibril_mutex_lock(&socket->lock);
430
431 if (socket->assoc == NULL) {
432 fibril_mutex_unlock(&socket->lock);
433 async_answer_0(callid, ENOTCONN);
434 return;
435 }
436
437 (void)flags;
438
439 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
440 fibril_mutex_lock(&socket->recv_buffer_lock);
441 while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) {
442 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
443 fibril_condvar_wait(&socket->recv_buffer_cv,
444 &socket->recv_buffer_lock);
445 }
446
447 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
448
449 rsock = &socket->recv_fsock;
450 data_len = socket->recv_buffer_used;
451 urc = socket->recv_error;
452
453 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len);
454
455 switch (urc) {
456 case UDP_EOK:
457 rc = EOK;
458 break;
459/* case TCP_ENOTEXIST:
460 case TCP_ECLOSING:
461 rc = ENOTCONN;
462 break;
463 case TCP_ERESET:
464 rc = ECONNABORTED;
465 break;*/
466 default:
467 assert(false);
468 }
469
470 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
471
472 if (rc != EOK) {
473 fibril_mutex_unlock(&socket->recv_buffer_lock);
474 fibril_mutex_unlock(&socket->lock);
475 async_answer_0(callid, rc);
476 return;
477 }
478
479 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
480 /* Fill address */
481 uint32_t rsock_addr;
482 int rc = inet_addr_pack(&rsock->addr, &rsock_addr);
483 if (rc != EOK) {
484 fibril_mutex_unlock(&socket->recv_buffer_lock);
485 fibril_mutex_unlock(&socket->lock);
486 async_answer_0(callid, rc);
487 return;
488 }
489
490 addr.sin_family = AF_INET;
491 addr.sin_addr.s_addr = host2uint32_t_be(rsock_addr);
492 addr.sin_port = host2uint16_t_be(rsock->port);
493
494 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
495 if (!async_data_read_receive(&rcallid, &addr_length)) {
496 fibril_mutex_unlock(&socket->recv_buffer_lock);
497 fibril_mutex_unlock(&socket->lock);
498 async_answer_0(callid, EINVAL);
499 return;
500 }
501
502 if (addr_length > sizeof(addr))
503 addr_length = sizeof(addr);
504
505 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
506 rc = async_data_read_finalize(rcallid, &addr, addr_length);
507 if (rc != EOK) {
508 fibril_mutex_unlock(&socket->recv_buffer_lock);
509 fibril_mutex_unlock(&socket->lock);
510 async_answer_0(callid, EINVAL);
511 return;
512 }
513 }
514
515 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
516 if (!async_data_read_receive(&rcallid, &length)) {
517 fibril_mutex_unlock(&socket->recv_buffer_lock);
518 fibril_mutex_unlock(&socket->lock);
519 async_answer_0(callid, EINVAL);
520 return;
521 }
522
523 if (length > data_len)
524 length = data_len;
525
526 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
527 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
528
529 if (length < data_len && rc == EOK)
530 rc = EOVERFLOW;
531
532 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length);
533 IPC_SET_ARG2(answer, 0);
534 SOCKET_SET_READ_DATA_LENGTH(answer, length);
535 SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));
536 async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
537 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
538
539 socket->recv_buffer_used = 0;
540
541 fibril_condvar_broadcast(&socket->recv_buffer_cv);
542 fibril_mutex_unlock(&socket->recv_buffer_lock);
543 fibril_mutex_unlock(&socket->lock);
544}
545
546static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
547{
548 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
549 int socket_id = SOCKET_GET_SOCKET_ID(call);
550
551 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - find core");
552 socket_core_t *sock_core =
553 socket_cores_find(&client->sockets, socket_id);
554 if (sock_core == NULL) {
555 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - core not found");
556 async_answer_0(callid, ENOTSOCK);
557 return;
558 }
559
560 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - spec data");
561 udp_sockdata_t *socket =
562 (udp_sockdata_t *) sock_core->specific_data;
563 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket");
564 fibril_mutex_lock(&socket->lock);
565
566 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket buffer");
567 fibril_mutex_lock(&socket->recv_buffer_lock);
568 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - set socket->sock_core = NULL");
569 socket->sock_core = NULL;
570 fibril_mutex_unlock(&socket->recv_buffer_lock);
571
572 udp_uc_reset(socket->assoc);
573
574 int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
575 udp_free_sock_data);
576 if (rc != EOK) {
577 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - socket_destroy failed");
578 fibril_mutex_unlock(&socket->lock);
579 async_answer_0(callid, rc);
580 return;
581 }
582
583 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - broadcast recv_buffer_cv");
584 fibril_condvar_broadcast(&socket->recv_buffer_cv);
585
586 fibril_mutex_unlock(&socket->lock);
587 async_answer_0(callid, EOK);
588}
589
590static void udp_sock_getsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
591{
592 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_getsockopt()");
593 async_answer_0(callid, ENOTSUP);
594}
595
596static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
597{
598 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt()");
599 async_answer_0(callid, ENOTSUP);
600}
601
602static int udp_sock_recv_fibril(void *arg)
603{
604 udp_sockdata_t *sock = (udp_sockdata_t *)arg;
605 udp_error_t urc;
606 xflags_t xflags;
607 size_t rcvd;
608
609 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()");
610
611 fibril_mutex_lock(&sock->recv_buffer_lock);
612
613 while (true) {
614 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()");
615 while ((sock->recv_buffer_used != 0) && (sock->sock_core != NULL)) {
616 fibril_condvar_wait(&sock->recv_buffer_cv,
617 &sock->recv_buffer_lock);
618 }
619
620 fibril_mutex_unlock(&sock->recv_buffer_lock);
621
622 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
623 urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
624 UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
625 fibril_mutex_lock(&sock->recv_buffer_lock);
626 sock->recv_error = urc;
627
628 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] udp_uc_receive -> %d", urc);
629
630 if (sock->sock_core != NULL)
631 udp_sock_notify_data(sock->sock_core);
632
633 if (urc != UDP_EOK) {
634 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break");
635 fibril_condvar_broadcast(&sock->recv_buffer_cv);
636 break;
637 }
638
639 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
640
641 sock->recv_buffer_used = rcvd;
642 fibril_condvar_broadcast(&sock->recv_buffer_cv);
643 }
644
645 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() exited loop");
646 fibril_mutex_unlock(&sock->recv_buffer_lock);
647 udp_uc_destroy(sock->assoc);
648
649 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() terminated");
650
651 return 0;
652}
653
654static void udp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
655{
656 ipc_callid_t callid;
657 ipc_call_t call;
658 udp_client_t client;
659
660 /* Accept the connection */
661 async_answer_0(iid, EOK);
662
663 client.sess = async_callback_receive(EXCHANGE_SERIALIZE);
664 socket_cores_initialize(&client.sockets);
665
666 while (true) {
667 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: wait");
668 callid = async_get_call(&call);
669 if (!IPC_GET_IMETHOD(call))
670 break;
671
672 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: METHOD=%d",
673 (int)IPC_GET_IMETHOD(call));
674
675 switch (IPC_GET_IMETHOD(call)) {
676 case NET_SOCKET:
677 udp_sock_socket(&client, callid, call);
678 break;
679 case NET_SOCKET_BIND:
680 udp_sock_bind(&client, callid, call);
681 break;
682 case NET_SOCKET_LISTEN:
683 udp_sock_listen(&client, callid, call);
684 break;
685 case NET_SOCKET_CONNECT:
686 udp_sock_connect(&client, callid, call);
687 break;
688 case NET_SOCKET_ACCEPT:
689 udp_sock_accept(&client, callid, call);
690 break;
691 case NET_SOCKET_SEND:
692 case NET_SOCKET_SENDTO:
693 udp_sock_sendto(&client, callid, call);
694 break;
695 case NET_SOCKET_RECV:
696 case NET_SOCKET_RECVFROM:
697 udp_sock_recvfrom(&client, callid, call);
698 break;
699 case NET_SOCKET_CLOSE:
700 udp_sock_close(&client, callid, call);
701 break;
702 case NET_SOCKET_GETSOCKOPT:
703 udp_sock_getsockopt(&client, callid, call);
704 break;
705 case NET_SOCKET_SETSOCKOPT:
706 udp_sock_setsockopt(&client, callid, call);
707 break;
708 default:
709 async_answer_0(callid, ENOTSUP);
710 break;
711 }
712 }
713
714 /* Clean up */
715 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: Clean up");
716 async_hangup(client.sess);
717 socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
718}
719
720/**
721 * @}
722 */
Note: See TracBrowser for help on using the repository browser.