[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 | #define FRAGMENT_SIZE 1024
|
---|
| 54 |
|
---|
| 55 | /** Free ports pool start. */
|
---|
| 56 | #define UDP_FREE_PORTS_START 1025
|
---|
| 57 |
|
---|
| 58 | /** Free ports pool end. */
|
---|
| 59 | #define UDP_FREE_PORTS_END 65535
|
---|
| 60 |
|
---|
| 61 | static int last_used_port = UDP_FREE_PORTS_START - 1;
|
---|
| 62 | static socket_ports_t gsock;
|
---|
| 63 |
|
---|
| 64 | static void udp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
[66a272f8] | 65 |
|
---|
| 66 | int udp_sock_init(void)
|
---|
| 67 | {
|
---|
[e33bceb] | 68 | socket_ports_initialize(&gsock);
|
---|
[1038a9c] | 69 |
|
---|
[e33bceb] | 70 | async_set_client_connection(udp_sock_connection);
|
---|
[1038a9c] | 71 |
|
---|
| 72 | int rc = service_register(SERVICE_UDP);
|
---|
[e33bceb] | 73 | if (rc != EOK)
|
---|
| 74 | return EEXIST;
|
---|
[1038a9c] | 75 |
|
---|
[66a272f8] | 76 | return EOK;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[e33bceb] | 79 | static void udp_free_sock_data(socket_core_t *sock_core)
|
---|
| 80 | {
|
---|
| 81 | udp_sockdata_t *socket;
|
---|
| 82 |
|
---|
| 83 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
---|
[0d520a2] | 84 | assert(socket->assoc != NULL);
|
---|
| 85 | udp_uc_destroy(socket->assoc);
|
---|
[e33bceb] | 86 | }
|
---|
| 87 |
|
---|
| 88 | static void udp_sock_notify_data(socket_core_t *sock_core)
|
---|
| 89 | {
|
---|
| 90 | log_msg(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 | FRAGMENT_SIZE, 0, 0, 1);
|
---|
| 94 | async_exchange_end(exch);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | static 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(LVL_DEBUG, "udp_sock_socket()");
|
---|
| 106 | sock = calloc(sizeof(udp_sockdata_t), 1);
|
---|
| 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 | rc = udp_uc_create(&sock->assoc);
|
---|
| 116 | if (rc != EOK) {
|
---|
| 117 | udp_uc_destroy(sock->assoc);
|
---|
| 118 | free(sock);
|
---|
| 119 | async_answer_0(callid, rc);
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | sock_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 124 | rc = socket_create(&client->sockets, client->sess, sock, &sock_id);
|
---|
| 125 | if (rc != EOK) {
|
---|
| 126 | async_answer_0(callid, rc);
|
---|
| 127 | return;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | sock_core = socket_cores_find(&client->sockets, sock_id);
|
---|
| 131 | assert(sock_core != NULL);
|
---|
| 132 | sock->sock_core = sock_core;
|
---|
[b1bd89ea] | 133 |
|
---|
[e33bceb] | 134 | SOCKET_SET_SOCKET_ID(answer, sock_id);
|
---|
| 135 |
|
---|
| 136 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, FRAGMENT_SIZE);
|
---|
| 137 | SOCKET_SET_HEADER_SIZE(answer, sizeof(udp_header_t));
|
---|
[b1bd89ea] | 138 | async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
|
---|
| 139 | IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
|
---|
[e33bceb] | 140 | }
|
---|
| 141 |
|
---|
| 142 | static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 143 | {
|
---|
| 144 | int rc;
|
---|
| 145 | struct sockaddr_in *addr;
|
---|
| 146 | size_t addr_size;
|
---|
| 147 | socket_core_t *sock_core;
|
---|
| 148 | udp_sockdata_t *socket;
|
---|
| 149 | udp_sock_t fsock;
|
---|
| 150 | udp_error_t urc;
|
---|
| 151 |
|
---|
| 152 | log_msg(LVL_DEBUG, "udp_sock_bind()");
|
---|
| 153 | log_msg(LVL_DEBUG, " - async_data_write_accept");
|
---|
| 154 |
|
---|
| 155 | addr = NULL;
|
---|
| 156 |
|
---|
| 157 | rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
|
---|
| 158 | if (rc != EOK) {
|
---|
| 159 | async_answer_0(callid, rc);
|
---|
| 160 | goto out;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | log_msg(LVL_DEBUG, " - call socket_bind");
|
---|
| 164 | rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
|
---|
| 165 | addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
---|
| 166 | last_used_port);
|
---|
| 167 | if (rc != EOK) {
|
---|
| 168 | async_answer_0(callid, rc);
|
---|
| 169 | goto out;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (addr_size != sizeof(struct sockaddr_in)) {
|
---|
| 173 | async_answer_0(callid, EINVAL);
|
---|
| 174 | goto out;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | log_msg(LVL_DEBUG, " - call socket_cores_find");
|
---|
| 178 | sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
|
---|
| 179 | if (sock_core == NULL) {
|
---|
| 180 | async_answer_0(callid, ENOENT);
|
---|
| 181 | goto out;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
---|
| 185 |
|
---|
| 186 | fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
|
---|
[92b42442] | 187 | fsock.port = sock_core->port;
|
---|
[e33bceb] | 188 | urc = udp_uc_set_local(socket->assoc, &fsock);
|
---|
| 189 |
|
---|
| 190 | switch (urc) {
|
---|
| 191 | case UDP_EOK:
|
---|
| 192 | rc = EOK;
|
---|
| 193 | break;
|
---|
| 194 | /* case TCP_ENOTEXIST:
|
---|
| 195 | rc = ENOTCONN;
|
---|
| 196 | break;
|
---|
| 197 | case TCP_ECLOSING:
|
---|
| 198 | rc = ENOTCONN;
|
---|
| 199 | break;
|
---|
| 200 | case TCP_ERESET:
|
---|
| 201 | rc = ECONNABORTED;
|
---|
| 202 | break;*/
|
---|
| 203 | default:
|
---|
| 204 | assert(false);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[92b42442] | 207 | udp_sock_notify_data(sock_core);
|
---|
| 208 |
|
---|
[e33bceb] | 209 | log_msg(LVL_DEBUG, " - success");
|
---|
| 210 | async_answer_0(callid, rc);
|
---|
| 211 | out:
|
---|
| 212 | if (addr != NULL)
|
---|
| 213 | free(addr);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 217 | {
|
---|
| 218 | log_msg(LVL_DEBUG, "udp_sock_listen()");
|
---|
| 219 | async_answer_0(callid, ENOTSUP);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 223 | {
|
---|
| 224 | log_msg(LVL_DEBUG, "udp_sock_connect()");
|
---|
| 225 | async_answer_0(callid, ENOTSUP);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 229 | {
|
---|
| 230 | log_msg(LVL_DEBUG, "udp_sock_accept()");
|
---|
| 231 | async_answer_0(callid, ENOTSUP);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 235 | {
|
---|
| 236 | int socket_id;
|
---|
| 237 | int fragments;
|
---|
| 238 | int index;
|
---|
| 239 | struct sockaddr_in *addr;
|
---|
| 240 | size_t addr_size;
|
---|
| 241 | socket_core_t *sock_core;
|
---|
| 242 | udp_sockdata_t *socket;
|
---|
| 243 | udp_sock_t fsock, *fsockp;
|
---|
| 244 | ipc_call_t answer;
|
---|
| 245 | ipc_callid_t wcallid;
|
---|
| 246 | size_t length;
|
---|
| 247 | uint8_t buffer[FRAGMENT_SIZE];
|
---|
| 248 | udp_error_t urc;
|
---|
| 249 | int rc;
|
---|
| 250 |
|
---|
| 251 | log_msg(LVL_DEBUG, "udp_sock_send()");
|
---|
| 252 |
|
---|
| 253 | addr = NULL;
|
---|
| 254 |
|
---|
| 255 | if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
|
---|
| 256 | rc = async_data_write_accept((void **) &addr, false,
|
---|
| 257 | 0, 0, 0, &addr_size);
|
---|
| 258 | if (rc != EOK) {
|
---|
| 259 | async_answer_0(callid, rc);
|
---|
| 260 | goto out;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | if (addr_size != sizeof(struct sockaddr_in)) {
|
---|
| 264 | async_answer_0(callid, EINVAL);
|
---|
| 265 | goto out;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
|
---|
| 269 | fsock.port = uint16_t_be2host(addr->sin_port);
|
---|
| 270 | fsockp = &fsock;
|
---|
| 271 | } else {
|
---|
| 272 | fsockp = NULL;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 276 | fragments = SOCKET_GET_DATA_FRAGMENTS(call);
|
---|
| 277 | SOCKET_GET_FLAGS(call);
|
---|
| 278 |
|
---|
| 279 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 280 | if (sock_core == NULL) {
|
---|
| 281 | async_answer_0(callid, ENOTSOCK);
|
---|
| 282 | goto out;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if (sock_core->port == 0) {
|
---|
[92b42442] | 286 | /* Implicitly bind socket to port */
|
---|
[e33bceb] | 287 | rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
|
---|
| 288 | addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
---|
| 289 | last_used_port);
|
---|
| 290 | if (rc != EOK) {
|
---|
| 291 | async_answer_0(callid, rc);
|
---|
| 292 | goto out;
|
---|
| 293 | }
|
---|
[92b42442] | 294 |
|
---|
| 295 | udp_sock_notify_data(sock_core);
|
---|
[e33bceb] | 296 | }
|
---|
| 297 |
|
---|
| 298 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
---|
| 299 | fibril_mutex_lock(&socket->lock);
|
---|
| 300 |
|
---|
[92b42442] | 301 | if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {
|
---|
| 302 | /* Determine local IP address */
|
---|
| 303 | inet_addr_t loc_addr, rem_addr;
|
---|
| 304 |
|
---|
| 305 | rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 :
|
---|
| 306 | socket->assoc->ident.foreign.addr.ipv4;
|
---|
| 307 |
|
---|
| 308 | rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
|
---|
| 309 | if (rc != EOK) {
|
---|
| 310 | fibril_mutex_unlock(&socket->lock);
|
---|
| 311 | async_answer_0(callid, rc);
|
---|
| 312 | log_msg(LVL_DEBUG, "udp_sock_sendto: Failed to "
|
---|
| 313 | "determine local address.");
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4;
|
---|
| 318 | log_msg(LVL_DEBUG, "Local IP address is %x",
|
---|
| 319 | socket->assoc->ident.local.addr.ipv4);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 |
|
---|
[e33bceb] | 323 | assert(socket->assoc != NULL);
|
---|
| 324 |
|
---|
| 325 | for (index = 0; index < fragments; index++) {
|
---|
| 326 | if (!async_data_write_receive(&wcallid, &length)) {
|
---|
| 327 | fibril_mutex_unlock(&socket->lock);
|
---|
| 328 | async_answer_0(callid, EINVAL);
|
---|
| 329 | goto out;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | if (length > FRAGMENT_SIZE)
|
---|
| 333 | length = FRAGMENT_SIZE;
|
---|
| 334 |
|
---|
| 335 | rc = async_data_write_finalize(wcallid, buffer, length);
|
---|
| 336 | if (rc != EOK) {
|
---|
| 337 | fibril_mutex_unlock(&socket->lock);
|
---|
| 338 | async_answer_0(callid, rc);
|
---|
| 339 | goto out;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0);
|
---|
| 343 |
|
---|
| 344 | switch (urc) {
|
---|
| 345 | case UDP_EOK:
|
---|
| 346 | rc = EOK;
|
---|
| 347 | break;
|
---|
| 348 | /* case TCP_ENOTEXIST:
|
---|
| 349 | rc = ENOTCONN;
|
---|
| 350 | break;
|
---|
| 351 | case TCP_ECLOSING:
|
---|
| 352 | rc = ENOTCONN;
|
---|
| 353 | break;
|
---|
| 354 | case TCP_ERESET:
|
---|
| 355 | rc = ECONNABORTED;
|
---|
| 356 | break;*/
|
---|
| 357 | default:
|
---|
| 358 | assert(false);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | if (rc != EOK) {
|
---|
| 362 | fibril_mutex_unlock(&socket->lock);
|
---|
| 363 | async_answer_0(callid, rc);
|
---|
| 364 | goto out;
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
[b1bd89ea] | 367 |
|
---|
| 368 | IPC_SET_ARG1(answer, 0);
|
---|
[e33bceb] | 369 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, FRAGMENT_SIZE);
|
---|
[b1bd89ea] | 370 | async_answer_2(callid, EOK, IPC_GET_ARG1(answer),
|
---|
| 371 | IPC_GET_ARG2(answer));
|
---|
[e33bceb] | 372 | fibril_mutex_unlock(&socket->lock);
|
---|
[b1bd89ea] | 373 |
|
---|
[e33bceb] | 374 | out:
|
---|
| 375 | if (addr != NULL)
|
---|
| 376 | free(addr);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 380 | {
|
---|
| 381 | int socket_id;
|
---|
| 382 | int flags;
|
---|
| 383 | size_t addr_length, length;
|
---|
| 384 | socket_core_t *sock_core;
|
---|
| 385 | udp_sockdata_t *socket;
|
---|
| 386 | ipc_call_t answer;
|
---|
| 387 | ipc_callid_t rcallid;
|
---|
| 388 | uint8_t buffer[FRAGMENT_SIZE];
|
---|
| 389 | size_t data_len;
|
---|
| 390 | xflags_t xflags;
|
---|
| 391 | udp_error_t urc;
|
---|
| 392 | struct sockaddr_in addr;
|
---|
| 393 | udp_sock_t rsock;
|
---|
| 394 | int rc;
|
---|
| 395 |
|
---|
| 396 | log_msg(LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
|
---|
| 397 |
|
---|
| 398 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 399 | flags = SOCKET_GET_FLAGS(call);
|
---|
| 400 |
|
---|
| 401 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 402 | if (sock_core == NULL) {
|
---|
| 403 | async_answer_0(callid, ENOTSOCK);
|
---|
| 404 | return;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
---|
| 408 | fibril_mutex_lock(&socket->lock);
|
---|
| 409 |
|
---|
| 410 | if (socket->assoc == NULL) {
|
---|
| 411 | fibril_mutex_unlock(&socket->lock);
|
---|
| 412 | async_answer_0(callid, ENOTCONN);
|
---|
| 413 | return;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | (void)flags;
|
---|
| 417 |
|
---|
| 418 | urc = udp_uc_receive(socket->assoc, buffer, FRAGMENT_SIZE, &data_len,
|
---|
| 419 | &xflags, &rsock);
|
---|
[92b42442] | 420 | log_msg(LVL_DEBUG, "**** udp_uc_receive done, data_len=%zu", data_len);
|
---|
[e33bceb] | 421 |
|
---|
| 422 | switch (urc) {
|
---|
| 423 | case UDP_EOK:
|
---|
| 424 | rc = EOK;
|
---|
| 425 | break;
|
---|
| 426 | /* case TCP_ENOTEXIST:
|
---|
| 427 | case TCP_ECLOSING:
|
---|
| 428 | rc = ENOTCONN;
|
---|
| 429 | break;
|
---|
| 430 | case TCP_ERESET:
|
---|
| 431 | rc = ECONNABORTED;
|
---|
| 432 | break;*/
|
---|
| 433 | default:
|
---|
| 434 | assert(false);
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | log_msg(LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
|
---|
| 438 | if (rc != EOK) {
|
---|
| 439 | fibril_mutex_unlock(&socket->lock);
|
---|
| 440 | async_answer_0(callid, rc);
|
---|
| 441 | return;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
|
---|
| 445 | /* Fill addr */
|
---|
| 446 | addr.sin_family = AF_INET;
|
---|
| 447 | addr.sin_addr.s_addr = host2uint32_t_be(rsock.addr.ipv4);
|
---|
| 448 | addr.sin_port = host2uint16_t_be(rsock.port);
|
---|
| 449 |
|
---|
| 450 | log_msg(LVL_DEBUG, "addr read receive");
|
---|
| 451 | if (!async_data_read_receive(&rcallid, &addr_length)) {
|
---|
| 452 | fibril_mutex_unlock(&socket->lock);
|
---|
| 453 | async_answer_0(callid, EINVAL);
|
---|
| 454 | return;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | if (addr_length > sizeof(addr))
|
---|
| 458 | addr_length = sizeof(addr);
|
---|
| 459 |
|
---|
| 460 | log_msg(LVL_DEBUG, "addr read finalize");
|
---|
| 461 | rc = async_data_read_finalize(rcallid, &addr, addr_length);
|
---|
| 462 | if (rc != EOK) {
|
---|
| 463 | fibril_mutex_unlock(&socket->lock);
|
---|
| 464 | async_answer_0(callid, EINVAL);
|
---|
| 465 | return;
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | log_msg(LVL_DEBUG, "data read receive");
|
---|
| 470 | if (!async_data_read_receive(&rcallid, &length)) {
|
---|
| 471 | fibril_mutex_unlock(&socket->lock);
|
---|
| 472 | async_answer_0(callid, EINVAL);
|
---|
| 473 | return;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | if (length > data_len)
|
---|
| 477 | length = data_len;
|
---|
| 478 |
|
---|
| 479 | log_msg(LVL_DEBUG, "data read finalize");
|
---|
| 480 | rc = async_data_read_finalize(rcallid, buffer, length);
|
---|
| 481 |
|
---|
| 482 | if (length < data_len && rc == EOK)
|
---|
| 483 | rc = EOVERFLOW;
|
---|
| 484 |
|
---|
[92b42442] | 485 | log_msg(LVL_DEBUG, "read_data_length <- %zu", length);
|
---|
[b1bd89ea] | 486 | IPC_SET_ARG2(answer, 0);
|
---|
[e33bceb] | 487 | SOCKET_SET_READ_DATA_LENGTH(answer, length);
|
---|
[92b42442] | 488 | SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));
|
---|
[b1bd89ea] | 489 | async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
|
---|
| 490 | IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
|
---|
| 491 |
|
---|
[e33bceb] | 492 | /* Push one fragment notification to client's queue */
|
---|
| 493 | udp_sock_notify_data(sock_core);
|
---|
| 494 | fibril_mutex_unlock(&socket->lock);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 498 | {
|
---|
| 499 | int socket_id;
|
---|
| 500 | socket_core_t *sock_core;
|
---|
| 501 | udp_sockdata_t *socket;
|
---|
| 502 | int rc;
|
---|
| 503 |
|
---|
| 504 | log_msg(LVL_DEBUG, "tcp_sock_close()");
|
---|
| 505 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 506 |
|
---|
| 507 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 508 | if (sock_core == NULL) {
|
---|
| 509 | async_answer_0(callid, ENOTSOCK);
|
---|
| 510 | return;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
---|
| 514 | fibril_mutex_lock(&socket->lock);
|
---|
| 515 |
|
---|
| 516 | rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
|
---|
| 517 | udp_free_sock_data);
|
---|
| 518 | if (rc != EOK) {
|
---|
| 519 | fibril_mutex_unlock(&socket->lock);
|
---|
| 520 | async_answer_0(callid, rc);
|
---|
| 521 | return;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | fibril_mutex_unlock(&socket->lock);
|
---|
| 525 | async_answer_0(callid, EOK);
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | static void udp_sock_getsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 529 | {
|
---|
| 530 | log_msg(LVL_DEBUG, "udp_sock_getsockopt()");
|
---|
| 531 | async_answer_0(callid, ENOTSUP);
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 535 | {
|
---|
| 536 | log_msg(LVL_DEBUG, "udp_sock_setsockopt()");
|
---|
| 537 | async_answer_0(callid, ENOTSUP);
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | static void udp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 541 | {
|
---|
| 542 | ipc_callid_t callid;
|
---|
| 543 | ipc_call_t call;
|
---|
| 544 | udp_client_t client;
|
---|
| 545 |
|
---|
| 546 | /* Accept the connection */
|
---|
| 547 | async_answer_0(iid, EOK);
|
---|
| 548 |
|
---|
| 549 | client.sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 550 | socket_cores_initialize(&client.sockets);
|
---|
| 551 |
|
---|
| 552 | while (true) {
|
---|
[92b42442] | 553 | log_msg(LVL_DEBUG, "udp_sock_connection: wait");
|
---|
[e33bceb] | 554 | callid = async_get_call(&call);
|
---|
| 555 | if (!IPC_GET_IMETHOD(call))
|
---|
| 556 | break;
|
---|
| 557 |
|
---|
[92b42442] | 558 | log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d",
|
---|
[e33bceb] | 559 | (int)IPC_GET_IMETHOD(call));
|
---|
| 560 |
|
---|
| 561 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 562 | case NET_SOCKET:
|
---|
| 563 | udp_sock_socket(&client, callid, call);
|
---|
| 564 | break;
|
---|
| 565 | case NET_SOCKET_BIND:
|
---|
| 566 | udp_sock_bind(&client, callid, call);
|
---|
| 567 | break;
|
---|
| 568 | case NET_SOCKET_LISTEN:
|
---|
| 569 | udp_sock_listen(&client, callid, call);
|
---|
| 570 | break;
|
---|
| 571 | case NET_SOCKET_CONNECT:
|
---|
| 572 | udp_sock_connect(&client, callid, call);
|
---|
| 573 | break;
|
---|
| 574 | case NET_SOCKET_ACCEPT:
|
---|
| 575 | udp_sock_accept(&client, callid, call);
|
---|
| 576 | break;
|
---|
| 577 | case NET_SOCKET_SEND:
|
---|
| 578 | case NET_SOCKET_SENDTO:
|
---|
| 579 | udp_sock_sendto(&client, callid, call);
|
---|
| 580 | break;
|
---|
| 581 | case NET_SOCKET_RECV:
|
---|
| 582 | case NET_SOCKET_RECVFROM:
|
---|
| 583 | udp_sock_recvfrom(&client, callid, call);
|
---|
| 584 | break;
|
---|
| 585 | case NET_SOCKET_CLOSE:
|
---|
| 586 | udp_sock_close(&client, callid, call);
|
---|
| 587 | break;
|
---|
| 588 | case NET_SOCKET_GETSOCKOPT:
|
---|
| 589 | udp_sock_getsockopt(&client, callid, call);
|
---|
| 590 | break;
|
---|
| 591 | case NET_SOCKET_SETSOCKOPT:
|
---|
| 592 | udp_sock_setsockopt(&client, callid, call);
|
---|
| 593 | break;
|
---|
| 594 | default:
|
---|
| 595 | async_answer_0(callid, ENOTSUP);
|
---|
| 596 | break;
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
[0d520a2] | 599 |
|
---|
| 600 | /* Clean up */
|
---|
| 601 | log_msg(LVL_DEBUG, "udp_sock_connection: Clean up");
|
---|
| 602 | async_hangup(client.sess);
|
---|
| 603 | socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
|
---|
[e33bceb] | 604 | }
|
---|
| 605 |
|
---|
[66a272f8] | 606 | /**
|
---|
| 607 | * @}
|
---|
| 608 | */
|
---|