| 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/modules.h>
|
|---|
| 46 | #include <net/socket.h>
|
|---|
| 47 | #include <ns.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "sock.h"
|
|---|
| 50 | #include "std.h"
|
|---|
| 51 | #include "udp_type.h"
|
|---|
| 52 | #include "ucall.h"
|
|---|
| 53 |
|
|---|
| 54 | #define FRAGMENT_SIZE 1024
|
|---|
| 55 |
|
|---|
| 56 | /** Free ports pool start. */
|
|---|
| 57 | #define UDP_FREE_PORTS_START 1025
|
|---|
| 58 |
|
|---|
| 59 | /** Free ports pool end. */
|
|---|
| 60 | #define UDP_FREE_PORTS_END 65535
|
|---|
| 61 |
|
|---|
| 62 | static int last_used_port = UDP_FREE_PORTS_START - 1;
|
|---|
| 63 | static socket_ports_t gsock;
|
|---|
| 64 |
|
|---|
| 65 | static void udp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
|---|
| 66 |
|
|---|
| 67 | int udp_sock_init(void)
|
|---|
| 68 | {
|
|---|
| 69 | int rc;
|
|---|
| 70 |
|
|---|
| 71 | socket_ports_initialize(&gsock);
|
|---|
| 72 |
|
|---|
| 73 | async_set_client_connection(udp_sock_connection);
|
|---|
| 74 |
|
|---|
| 75 | rc = service_register(SERVICE_UDP);
|
|---|
| 76 | if (rc != EOK)
|
|---|
| 77 | return EEXIST;
|
|---|
| 78 |
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static void udp_free_sock_data(socket_core_t *sock_core)
|
|---|
| 83 | {
|
|---|
| 84 | udp_sockdata_t *socket;
|
|---|
| 85 |
|
|---|
| 86 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
|---|
| 87 | assert(socket->assoc != NULL);
|
|---|
| 88 | udp_uc_destroy(socket->assoc);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static void udp_sock_notify_data(socket_core_t *sock_core)
|
|---|
| 92 | {
|
|---|
| 93 | log_msg(LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
|
|---|
| 94 | async_exch_t *exch = async_exchange_begin(sock_core->sess);
|
|---|
| 95 | async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t)sock_core->socket_id,
|
|---|
| 96 | FRAGMENT_SIZE, 0, 0, 1);
|
|---|
| 97 | async_exchange_end(exch);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static void udp_sock_socket(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 101 | {
|
|---|
| 102 | udp_sockdata_t *sock;
|
|---|
| 103 | socket_core_t *sock_core;
|
|---|
| 104 | int sock_id;
|
|---|
| 105 | int rc;
|
|---|
| 106 | ipc_call_t answer;
|
|---|
| 107 |
|
|---|
| 108 | log_msg(LVL_DEBUG, "udp_sock_socket()");
|
|---|
| 109 | sock = calloc(sizeof(udp_sockdata_t), 1);
|
|---|
| 110 | if (sock == NULL) {
|
|---|
| 111 | async_answer_0(callid, ENOMEM);
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | fibril_mutex_initialize(&sock->lock);
|
|---|
| 116 | sock->client = client;
|
|---|
| 117 |
|
|---|
| 118 | rc = udp_uc_create(&sock->assoc);
|
|---|
| 119 | if (rc != EOK) {
|
|---|
| 120 | udp_uc_destroy(sock->assoc);
|
|---|
| 121 | free(sock);
|
|---|
| 122 | async_answer_0(callid, rc);
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | sock_id = SOCKET_GET_SOCKET_ID(call);
|
|---|
| 127 | rc = socket_create(&client->sockets, client->sess, sock, &sock_id);
|
|---|
| 128 | if (rc != EOK) {
|
|---|
| 129 | async_answer_0(callid, rc);
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | sock_core = socket_cores_find(&client->sockets, sock_id);
|
|---|
| 134 | assert(sock_core != NULL);
|
|---|
| 135 | sock->sock_core = sock_core;
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | refresh_answer(&answer, NULL);
|
|---|
| 139 | SOCKET_SET_SOCKET_ID(answer, sock_id);
|
|---|
| 140 |
|
|---|
| 141 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, FRAGMENT_SIZE);
|
|---|
| 142 | SOCKET_SET_HEADER_SIZE(answer, sizeof(udp_header_t));
|
|---|
| 143 | answer_call(callid, EOK, &answer, 3);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 147 | {
|
|---|
| 148 | int rc;
|
|---|
| 149 | struct sockaddr_in *addr;
|
|---|
| 150 | size_t addr_size;
|
|---|
| 151 | socket_core_t *sock_core;
|
|---|
| 152 | udp_sockdata_t *socket;
|
|---|
| 153 | udp_sock_t fsock;
|
|---|
| 154 | udp_error_t urc;
|
|---|
| 155 |
|
|---|
| 156 | log_msg(LVL_DEBUG, "udp_sock_bind()");
|
|---|
| 157 | log_msg(LVL_DEBUG, " - async_data_write_accept");
|
|---|
| 158 |
|
|---|
| 159 | addr = NULL;
|
|---|
| 160 |
|
|---|
| 161 | rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
|
|---|
| 162 | if (rc != EOK) {
|
|---|
| 163 | async_answer_0(callid, rc);
|
|---|
| 164 | goto out;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | log_msg(LVL_DEBUG, " - call socket_bind");
|
|---|
| 168 | rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
|
|---|
| 169 | addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
|---|
| 170 | last_used_port);
|
|---|
| 171 | if (rc != EOK) {
|
|---|
| 172 | async_answer_0(callid, rc);
|
|---|
| 173 | goto out;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (addr_size != sizeof(struct sockaddr_in)) {
|
|---|
| 177 | async_answer_0(callid, EINVAL);
|
|---|
| 178 | goto out;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | log_msg(LVL_DEBUG, " - call socket_cores_find");
|
|---|
| 182 | sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
|
|---|
| 183 | if (sock_core == NULL) {
|
|---|
| 184 | async_answer_0(callid, ENOENT);
|
|---|
| 185 | goto out;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
|---|
| 189 |
|
|---|
| 190 | fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
|
|---|
| 191 | fsock.port = sock_core->port;
|
|---|
| 192 | urc = udp_uc_set_local(socket->assoc, &fsock);
|
|---|
| 193 |
|
|---|
| 194 | switch (urc) {
|
|---|
| 195 | case UDP_EOK:
|
|---|
| 196 | rc = EOK;
|
|---|
| 197 | break;
|
|---|
| 198 | /* case TCP_ENOTEXIST:
|
|---|
| 199 | rc = ENOTCONN;
|
|---|
| 200 | break;
|
|---|
| 201 | case TCP_ECLOSING:
|
|---|
| 202 | rc = ENOTCONN;
|
|---|
| 203 | break;
|
|---|
| 204 | case TCP_ERESET:
|
|---|
| 205 | rc = ECONNABORTED;
|
|---|
| 206 | break;*/
|
|---|
| 207 | default:
|
|---|
| 208 | assert(false);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | udp_sock_notify_data(sock_core);
|
|---|
| 212 |
|
|---|
| 213 | log_msg(LVL_DEBUG, " - success");
|
|---|
| 214 | async_answer_0(callid, rc);
|
|---|
| 215 | out:
|
|---|
| 216 | if (addr != NULL)
|
|---|
| 217 | free(addr);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 221 | {
|
|---|
| 222 | log_msg(LVL_DEBUG, "udp_sock_listen()");
|
|---|
| 223 | async_answer_0(callid, ENOTSUP);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 227 | {
|
|---|
| 228 | log_msg(LVL_DEBUG, "udp_sock_connect()");
|
|---|
| 229 | async_answer_0(callid, ENOTSUP);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 233 | {
|
|---|
| 234 | log_msg(LVL_DEBUG, "udp_sock_accept()");
|
|---|
| 235 | async_answer_0(callid, ENOTSUP);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 239 | {
|
|---|
| 240 | int socket_id;
|
|---|
| 241 | int fragments;
|
|---|
| 242 | int index;
|
|---|
| 243 | struct sockaddr_in *addr;
|
|---|
| 244 | size_t addr_size;
|
|---|
| 245 | socket_core_t *sock_core;
|
|---|
| 246 | udp_sockdata_t *socket;
|
|---|
| 247 | udp_sock_t fsock, *fsockp;
|
|---|
| 248 | ipc_call_t answer;
|
|---|
| 249 | ipc_callid_t wcallid;
|
|---|
| 250 | size_t length;
|
|---|
| 251 | uint8_t buffer[FRAGMENT_SIZE];
|
|---|
| 252 | udp_error_t urc;
|
|---|
| 253 | int rc;
|
|---|
| 254 |
|
|---|
| 255 | log_msg(LVL_DEBUG, "udp_sock_send()");
|
|---|
| 256 |
|
|---|
| 257 | addr = NULL;
|
|---|
| 258 |
|
|---|
| 259 | if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
|
|---|
| 260 | 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 | fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
|
|---|
| 273 | fsock.port = uint16_t_be2host(addr->sin_port);
|
|---|
| 274 | fsockp = &fsock;
|
|---|
| 275 | } else {
|
|---|
| 276 | fsockp = NULL;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
|---|
| 280 | fragments = SOCKET_GET_DATA_FRAGMENTS(call);
|
|---|
| 281 | SOCKET_GET_FLAGS(call);
|
|---|
| 282 |
|
|---|
| 283 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
|---|
| 284 | if (sock_core == NULL) {
|
|---|
| 285 | async_answer_0(callid, ENOTSOCK);
|
|---|
| 286 | goto out;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | if (sock_core->port == 0) {
|
|---|
| 290 | /* Implicitly bind socket to port */
|
|---|
| 291 | rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
|
|---|
| 292 | addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
|
|---|
| 293 | last_used_port);
|
|---|
| 294 | if (rc != EOK) {
|
|---|
| 295 | async_answer_0(callid, rc);
|
|---|
| 296 | goto out;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | udp_sock_notify_data(sock_core);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
|---|
| 303 | fibril_mutex_lock(&socket->lock);
|
|---|
| 304 |
|
|---|
| 305 | if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {
|
|---|
| 306 | /* Determine local IP address */
|
|---|
| 307 | inet_addr_t loc_addr, rem_addr;
|
|---|
| 308 |
|
|---|
| 309 | rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 :
|
|---|
| 310 | socket->assoc->ident.foreign.addr.ipv4;
|
|---|
| 311 |
|
|---|
| 312 | rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
|
|---|
| 313 | if (rc != EOK) {
|
|---|
| 314 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 315 | async_answer_0(callid, rc);
|
|---|
| 316 | log_msg(LVL_DEBUG, "udp_sock_sendto: Failed to "
|
|---|
| 317 | "determine local address.");
|
|---|
| 318 | return;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4;
|
|---|
| 322 | log_msg(LVL_DEBUG, "Local IP address is %x",
|
|---|
| 323 | socket->assoc->ident.local.addr.ipv4);
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 | assert(socket->assoc != NULL);
|
|---|
| 328 |
|
|---|
| 329 | for (index = 0; index < fragments; index++) {
|
|---|
| 330 | if (!async_data_write_receive(&wcallid, &length)) {
|
|---|
| 331 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 332 | async_answer_0(callid, EINVAL);
|
|---|
| 333 | goto out;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | if (length > FRAGMENT_SIZE)
|
|---|
| 337 | length = FRAGMENT_SIZE;
|
|---|
| 338 |
|
|---|
| 339 | rc = async_data_write_finalize(wcallid, buffer, length);
|
|---|
| 340 | if (rc != EOK) {
|
|---|
| 341 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 342 | async_answer_0(callid, rc);
|
|---|
| 343 | goto out;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0);
|
|---|
| 347 |
|
|---|
| 348 | switch (urc) {
|
|---|
| 349 | case UDP_EOK:
|
|---|
| 350 | rc = EOK;
|
|---|
| 351 | break;
|
|---|
| 352 | /* case TCP_ENOTEXIST:
|
|---|
| 353 | rc = ENOTCONN;
|
|---|
| 354 | break;
|
|---|
| 355 | case TCP_ECLOSING:
|
|---|
| 356 | rc = ENOTCONN;
|
|---|
| 357 | break;
|
|---|
| 358 | case TCP_ERESET:
|
|---|
| 359 | rc = ECONNABORTED;
|
|---|
| 360 | break;*/
|
|---|
| 361 | default:
|
|---|
| 362 | assert(false);
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | if (rc != EOK) {
|
|---|
| 366 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 367 | async_answer_0(callid, rc);
|
|---|
| 368 | goto out;
|
|---|
| 369 | }
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | refresh_answer(&answer, NULL);
|
|---|
| 373 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, FRAGMENT_SIZE);
|
|---|
| 374 | answer_call(callid, EOK, &answer, 2);
|
|---|
| 375 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 376 | out:
|
|---|
| 377 | if (addr != NULL)
|
|---|
| 378 | free(addr);
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
|---|
| 382 | {
|
|---|
| 383 | int socket_id;
|
|---|
| 384 | int flags;
|
|---|
| 385 | size_t addr_length, length;
|
|---|
| 386 | socket_core_t *sock_core;
|
|---|
| 387 | udp_sockdata_t *socket;
|
|---|
| 388 | ipc_call_t answer;
|
|---|
| 389 | ipc_callid_t rcallid;
|
|---|
| 390 | uint8_t buffer[FRAGMENT_SIZE];
|
|---|
| 391 | size_t data_len;
|
|---|
| 392 | xflags_t xflags;
|
|---|
| 393 | udp_error_t urc;
|
|---|
| 394 | struct sockaddr_in addr;
|
|---|
| 395 | udp_sock_t rsock;
|
|---|
| 396 | int rc;
|
|---|
| 397 |
|
|---|
| 398 | log_msg(LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
|
|---|
| 399 |
|
|---|
| 400 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
|---|
| 401 | flags = SOCKET_GET_FLAGS(call);
|
|---|
| 402 |
|
|---|
| 403 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
|---|
| 404 | if (sock_core == NULL) {
|
|---|
| 405 | async_answer_0(callid, ENOTSOCK);
|
|---|
| 406 | return;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | socket = (udp_sockdata_t *)sock_core->specific_data;
|
|---|
| 410 | fibril_mutex_lock(&socket->lock);
|
|---|
| 411 |
|
|---|
| 412 | if (socket->assoc == NULL) {
|
|---|
| 413 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 414 | async_answer_0(callid, ENOTCONN);
|
|---|
| 415 | return;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | (void)flags;
|
|---|
| 419 |
|
|---|
| 420 | urc = udp_uc_receive(socket->assoc, buffer, FRAGMENT_SIZE, &data_len,
|
|---|
| 421 | &xflags, &rsock);
|
|---|
| 422 | log_msg(LVL_DEBUG, "**** udp_uc_receive done, data_len=%zu", data_len);
|
|---|
| 423 |
|
|---|
| 424 | switch (urc) {
|
|---|
| 425 | case UDP_EOK:
|
|---|
| 426 | rc = EOK;
|
|---|
| 427 | break;
|
|---|
| 428 | /* case TCP_ENOTEXIST:
|
|---|
| 429 | case TCP_ECLOSING:
|
|---|
| 430 | rc = ENOTCONN;
|
|---|
| 431 | break;
|
|---|
| 432 | case TCP_ERESET:
|
|---|
| 433 | rc = ECONNABORTED;
|
|---|
| 434 | break;*/
|
|---|
| 435 | default:
|
|---|
| 436 | assert(false);
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | log_msg(LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
|
|---|
| 440 | if (rc != EOK) {
|
|---|
| 441 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 442 | async_answer_0(callid, rc);
|
|---|
| 443 | return;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
|
|---|
| 447 | /* Fill addr */
|
|---|
| 448 | addr.sin_family = AF_INET;
|
|---|
| 449 | addr.sin_addr.s_addr = host2uint32_t_be(rsock.addr.ipv4);
|
|---|
| 450 | addr.sin_port = host2uint16_t_be(rsock.port);
|
|---|
| 451 |
|
|---|
| 452 | log_msg(LVL_DEBUG, "addr read receive");
|
|---|
| 453 | if (!async_data_read_receive(&rcallid, &addr_length)) {
|
|---|
| 454 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 455 | async_answer_0(callid, EINVAL);
|
|---|
| 456 | return;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | if (addr_length > sizeof(addr))
|
|---|
| 460 | addr_length = sizeof(addr);
|
|---|
| 461 |
|
|---|
| 462 | log_msg(LVL_DEBUG, "addr read finalize");
|
|---|
| 463 | rc = async_data_read_finalize(rcallid, &addr, addr_length);
|
|---|
| 464 | if (rc != EOK) {
|
|---|
| 465 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 466 | async_answer_0(callid, EINVAL);
|
|---|
| 467 | return;
|
|---|
| 468 | }
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | log_msg(LVL_DEBUG, "data read receive");
|
|---|
| 472 | if (!async_data_read_receive(&rcallid, &length)) {
|
|---|
| 473 | fibril_mutex_unlock(&socket->lock);
|
|---|
| 474 | async_answer_0(callid, EINVAL);
|
|---|
| 475 | return;
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | if (length > data_len)
|
|---|
| 479 | length = data_len;
|
|---|
| 480 |
|
|---|
| 481 | log_msg(LVL_DEBUG, "data read finalize");
|
|---|
| 482 | rc = async_data_read_finalize(rcallid, buffer, length);
|
|---|
| 483 |
|
|---|
| 484 | if (length < data_len && rc == EOK)
|
|---|
| 485 | rc = EOVERFLOW;
|
|---|
| 486 |
|
|---|
| 487 | log_msg(LVL_DEBUG, "read_data_length <- %zu", length);
|
|---|
| 488 | SOCKET_SET_READ_DATA_LENGTH(answer, length);
|
|---|
| 489 | SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));
|
|---|
| 490 | answer_call(callid, EOK, &answer, 3);
|
|---|
| 491 |
|
|---|
| 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) {
|
|---|
| 553 | log_msg(LVL_DEBUG, "udp_sock_connection: wait");
|
|---|
| 554 | callid = async_get_call(&call);
|
|---|
| 555 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 556 | break;
|
|---|
| 557 |
|
|---|
| 558 | log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d",
|
|---|
| 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 | }
|
|---|
| 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);
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | /**
|
|---|
| 607 | * @}
|
|---|
| 608 | */
|
|---|