[04cd242] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Lukas Mejdrech
|
---|
| 3 | * Copyright (c) 2011 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 tcp
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file Socket provider
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <errno.h>
|
---|
[c76e926] | 40 | #include <inet/inet.h>
|
---|
[04cd242] | 41 | #include <io/log.h>
|
---|
[c76e926] | 42 | #include <ipc/services.h>
|
---|
[04cd242] | 43 | #include <ipc/socket.h>
|
---|
[a4ee3ab2] | 44 | #include <net/socket.h>
|
---|
[c76e926] | 45 | #include <ns.h>
|
---|
[04cd242] | 46 |
|
---|
| 47 | #include "sock.h"
|
---|
| 48 | #include "std.h"
|
---|
| 49 | #include "tcp.h"
|
---|
| 50 | #include "tcp_type.h"
|
---|
| 51 | #include "ucall.h"
|
---|
| 52 |
|
---|
[ae481e0] | 53 | #define MAX_BACKLOG 128
|
---|
| 54 |
|
---|
[04cd242] | 55 | /** Free ports pool start. */
|
---|
| 56 | #define TCP_FREE_PORTS_START 1025
|
---|
| 57 |
|
---|
| 58 | /** Free ports pool end. */
|
---|
| 59 | #define TCP_FREE_PORTS_END 65535
|
---|
| 60 |
|
---|
[a4ee3ab2] | 61 | static int last_used_port = TCP_FREE_PORTS_START - 1;
|
---|
[04cd242] | 62 | static socket_ports_t gsock;
|
---|
| 63 |
|
---|
[c76e926] | 64 | static void tcp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
[ae481e0] | 65 | static void tcp_sock_cstate_cb(tcp_conn_t *conn, void *arg);
|
---|
[d786dea9] | 66 | static int tcp_sock_recv_fibril(void *arg);
|
---|
[ae481e0] | 67 |
|
---|
[c76e926] | 68 | int tcp_sock_init(void)
|
---|
[a4ee3ab2] | 69 | {
|
---|
| 70 | socket_ports_initialize(&gsock);
|
---|
[1038a9c] | 71 |
|
---|
[c76e926] | 72 | async_set_client_connection(tcp_sock_connection);
|
---|
[1038a9c] | 73 |
|
---|
| 74 | int rc = service_register(SERVICE_TCP);
|
---|
[c76e926] | 75 | if (rc != EOK)
|
---|
| 76 | return EEXIST;
|
---|
[1038a9c] | 77 |
|
---|
[c76e926] | 78 | return EOK;
|
---|
[a4ee3ab2] | 79 | }
|
---|
| 80 |
|
---|
[04cd242] | 81 | static void tcp_free_sock_data(socket_core_t *sock_core)
|
---|
| 82 | {
|
---|
| 83 | tcp_sockdata_t *socket;
|
---|
| 84 |
|
---|
| 85 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
| 86 | (void)socket;
|
---|
[0d520a2] | 87 |
|
---|
| 88 | /* XXX We need to initiate connection cleanup here */
|
---|
[04cd242] | 89 | }
|
---|
| 90 |
|
---|
| 91 | static void tcp_sock_notify_data(socket_core_t *sock_core)
|
---|
| 92 | {
|
---|
[a1a101d] | 93 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_notify_data(%d)", sock_core->socket_id);
|
---|
[04cd242] | 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,
|
---|
[d786dea9] | 96 | TCP_SOCK_FRAGMENT_SIZE, 0, 0, 1);
|
---|
[04cd242] | 97 | async_exchange_end(exch);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[a4ee3ab2] | 100 | static void tcp_sock_notify_aconn(socket_core_t *lsock_core)
|
---|
| 101 | {
|
---|
[a1a101d] | 102 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_notify_aconn(%d)", lsock_core->socket_id);
|
---|
[a4ee3ab2] | 103 | async_exch_t *exch = async_exchange_begin(lsock_core->sess);
|
---|
| 104 | async_msg_5(exch, NET_SOCKET_ACCEPTED, (sysarg_t)lsock_core->socket_id,
|
---|
[d786dea9] | 105 | TCP_SOCK_FRAGMENT_SIZE, 0, 0, 0);
|
---|
[a4ee3ab2] | 106 | async_exchange_end(exch);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[d786dea9] | 109 | static int tcp_sock_create(tcp_client_t *client, tcp_sockdata_t **rsock)
|
---|
[04cd242] | 110 | {
|
---|
| 111 | tcp_sockdata_t *sock;
|
---|
[d786dea9] | 112 |
|
---|
[a1a101d] | 113 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_create()");
|
---|
[d786dea9] | 114 | *rsock = NULL;
|
---|
| 115 |
|
---|
[db81577] | 116 | sock = calloc(1, sizeof(tcp_sockdata_t));
|
---|
[d786dea9] | 117 | if (sock == NULL)
|
---|
| 118 | return ENOMEM;
|
---|
| 119 |
|
---|
| 120 | fibril_mutex_initialize(&sock->lock);
|
---|
| 121 | sock->client = client;
|
---|
| 122 |
|
---|
| 123 | sock->recv_buffer_used = 0;
|
---|
| 124 | sock->recv_error = TCP_EOK;
|
---|
| 125 | fibril_mutex_initialize(&sock->recv_buffer_lock);
|
---|
| 126 | fibril_condvar_initialize(&sock->recv_buffer_cv);
|
---|
| 127 | list_initialize(&sock->ready);
|
---|
| 128 |
|
---|
| 129 | *rsock = sock;
|
---|
| 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | static void tcp_sock_uncreate(tcp_sockdata_t *sock)
|
---|
| 134 | {
|
---|
[a1a101d] | 135 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_uncreate()");
|
---|
[d786dea9] | 136 | free(sock);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | static int tcp_sock_finish_setup(tcp_sockdata_t *sock, int *sock_id)
|
---|
| 140 | {
|
---|
[ae481e0] | 141 | socket_core_t *sock_core;
|
---|
[d786dea9] | 142 | int rc;
|
---|
| 143 |
|
---|
[a1a101d] | 144 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_finish_setup()");
|
---|
[d786dea9] | 145 |
|
---|
| 146 | sock->recv_fibril = fibril_create(tcp_sock_recv_fibril, sock);
|
---|
| 147 | if (sock->recv_fibril == 0)
|
---|
| 148 | return ENOMEM;
|
---|
| 149 |
|
---|
| 150 | rc = socket_create(&sock->client->sockets, sock->client->sess,
|
---|
| 151 | sock, sock_id);
|
---|
| 152 |
|
---|
[32d19f7] | 153 | if (rc != EOK) {
|
---|
| 154 | fibril_destroy(sock->recv_fibril);
|
---|
| 155 | sock->recv_fibril = 0;
|
---|
[d786dea9] | 156 | return rc;
|
---|
[32d19f7] | 157 | }
|
---|
[d786dea9] | 158 |
|
---|
| 159 | sock_core = socket_cores_find(&sock->client->sockets, *sock_id);
|
---|
| 160 | assert(sock_core != NULL);
|
---|
| 161 | sock->sock_core = sock_core;
|
---|
| 162 |
|
---|
| 163 | return EOK;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static void tcp_sock_socket(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 167 | {
|
---|
| 168 | tcp_sockdata_t *sock;
|
---|
[04cd242] | 169 | int sock_id;
|
---|
| 170 | int rc;
|
---|
| 171 | ipc_call_t answer;
|
---|
| 172 |
|
---|
[a1a101d] | 173 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_socket()");
|
---|
[d786dea9] | 174 |
|
---|
| 175 | rc = tcp_sock_create(client, &sock);
|
---|
| 176 | if (rc != EOK) {
|
---|
| 177 | async_answer_0(callid, rc);
|
---|
[04cd242] | 178 | return;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[a2e3ee6] | 181 | inet_addr_any(&sock->laddr);
|
---|
[ae481e0] | 182 | sock->lconn = NULL;
|
---|
| 183 | sock->backlog = 0;
|
---|
[04cd242] | 184 |
|
---|
| 185 | sock_id = SOCKET_GET_SOCKET_ID(call);
|
---|
[d786dea9] | 186 | rc = tcp_sock_finish_setup(sock, &sock_id);
|
---|
[04cd242] | 187 | if (rc != EOK) {
|
---|
[d786dea9] | 188 | tcp_sock_uncreate(sock);
|
---|
[04cd242] | 189 | async_answer_0(callid, rc);
|
---|
| 190 | return;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | SOCKET_SET_SOCKET_ID(answer, sock_id);
|
---|
| 194 |
|
---|
[d786dea9] | 195 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, TCP_SOCK_FRAGMENT_SIZE);
|
---|
[04cd242] | 196 | SOCKET_SET_HEADER_SIZE(answer, sizeof(tcp_header_t));
|
---|
[b1bd89ea] | 197 |
|
---|
| 198 | async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
|
---|
| 199 | IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
|
---|
[04cd242] | 200 | }
|
---|
| 201 |
|
---|
| 202 | static void tcp_sock_bind(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 203 | {
|
---|
| 204 | int rc;
|
---|
[05bfce7] | 205 | struct sockaddr_in *addr;
|
---|
| 206 | size_t addr_size;
|
---|
[04cd242] | 207 | socket_core_t *sock_core;
|
---|
| 208 | tcp_sockdata_t *socket;
|
---|
[05bfce7] | 209 |
|
---|
[a1a101d] | 210 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_bind()");
|
---|
| 211 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
|
---|
[05bfce7] | 212 |
|
---|
| 213 | addr = NULL;
|
---|
| 214 |
|
---|
| 215 | rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
|
---|
[04cd242] | 216 | if (rc != EOK) {
|
---|
| 217 | async_answer_0(callid, rc);
|
---|
[05bfce7] | 218 | goto out;
|
---|
[04cd242] | 219 | }
|
---|
[05bfce7] | 220 |
|
---|
| 221 | if (addr_size != sizeof(struct sockaddr_in)) {
|
---|
| 222 | async_answer_0(callid, EINVAL);
|
---|
| 223 | goto out;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[a1a101d] | 226 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
|
---|
[04cd242] | 227 | rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
|
---|
[05bfce7] | 228 | addr, addr_size, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
[04cd242] | 229 | last_used_port);
|
---|
| 230 | if (rc != EOK) {
|
---|
| 231 | async_answer_0(callid, rc);
|
---|
[05bfce7] | 232 | goto out;
|
---|
[04cd242] | 233 | }
|
---|
[05bfce7] | 234 |
|
---|
[a1a101d] | 235 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
|
---|
[04cd242] | 236 | sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
|
---|
[05bfce7] | 237 | if (sock_core == NULL) {
|
---|
| 238 | async_answer_0(callid, ENOENT);
|
---|
| 239 | goto out;
|
---|
[04cd242] | 240 | }
|
---|
[05bfce7] | 241 |
|
---|
| 242 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
| 243 | /* XXX Anything to do? */
|
---|
| 244 | (void) socket;
|
---|
| 245 |
|
---|
[a1a101d] | 246 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
|
---|
[a4ee3ab2] | 247 | async_answer_0(callid, EOK);
|
---|
[05bfce7] | 248 |
|
---|
| 249 | out:
|
---|
| 250 | if (addr != NULL)
|
---|
| 251 | free(addr);
|
---|
[04cd242] | 252 | }
|
---|
| 253 |
|
---|
| 254 | static void tcp_sock_listen(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 255 | {
|
---|
| 256 | int socket_id;
|
---|
| 257 | int backlog;
|
---|
| 258 | socket_core_t *sock_core;
|
---|
| 259 | tcp_sockdata_t *socket;
|
---|
[ae481e0] | 260 | tcp_error_t trc;
|
---|
| 261 | tcp_sock_t lsocket;
|
---|
| 262 | tcp_sock_t fsocket;
|
---|
| 263 | tcp_conn_t *conn;
|
---|
| 264 | tcp_sock_lconn_t *lconn;
|
---|
| 265 | int i;
|
---|
[05bfce7] | 266 | int rc;
|
---|
[04cd242] | 267 |
|
---|
[a1a101d] | 268 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_listen()");
|
---|
[04cd242] | 269 |
|
---|
| 270 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 271 | backlog = SOCKET_GET_BACKLOG(call);
|
---|
| 272 |
|
---|
| 273 | if (backlog < 0) {
|
---|
| 274 | async_answer_0(callid, EINVAL);
|
---|
| 275 | return;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[ae481e0] | 278 | if (backlog > MAX_BACKLOG)
|
---|
| 279 | backlog = MAX_BACKLOG;
|
---|
| 280 |
|
---|
[04cd242] | 281 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 282 | if (sock_core == NULL) {
|
---|
| 283 | async_answer_0(callid, ENOTSOCK);
|
---|
| 284 | return;
|
---|
| 285 | }
|
---|
[05bfce7] | 286 |
|
---|
| 287 | if (sock_core->port <= 0) {
|
---|
| 288 | rc = socket_bind_free_port(&gsock, sock_core,
|
---|
| 289 | TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
| 290 | last_used_port);
|
---|
| 291 | if (rc != EOK) {
|
---|
| 292 | async_answer_0(callid, rc);
|
---|
| 293 | return;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | last_used_port = sock_core->port;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | socket = (tcp_sockdata_t *) sock_core->specific_data;
|
---|
| 300 |
|
---|
[a4ee3ab2] | 301 | /*
|
---|
[ae481e0] | 302 | * Prepare @c backlog listening connections.
|
---|
[a4ee3ab2] | 303 | */
|
---|
[ae481e0] | 304 | fibril_mutex_lock(&socket->lock);
|
---|
[05bfce7] | 305 |
|
---|
[ae481e0] | 306 | socket->backlog = backlog;
|
---|
[db81577] | 307 | socket->lconn = calloc(backlog, sizeof(tcp_conn_t *));
|
---|
[ae481e0] | 308 | if (socket->lconn == NULL) {
|
---|
| 309 | fibril_mutex_unlock(&socket->lock);
|
---|
| 310 | async_answer_0(callid, ENOMEM);
|
---|
| 311 | return;
|
---|
| 312 | }
|
---|
[05bfce7] | 313 |
|
---|
[a1a101d] | 314 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - open connections");
|
---|
[05bfce7] | 315 |
|
---|
[a2e3ee6] | 316 | inet_addr_any(&lsocket.addr);
|
---|
[ae481e0] | 317 | lsocket.port = sock_core->port;
|
---|
[a2e3ee6] | 318 |
|
---|
| 319 | inet_addr_any(&fsocket.addr);
|
---|
[ae481e0] | 320 | fsocket.port = TCP_PORT_ANY;
|
---|
[05bfce7] | 321 |
|
---|
[ae481e0] | 322 | for (i = 0; i < backlog; i++) {
|
---|
| 323 |
|
---|
[db81577] | 324 | lconn = calloc(1, sizeof(tcp_sock_lconn_t));
|
---|
[ae481e0] | 325 | if (lconn == NULL) {
|
---|
| 326 | /* XXX Clean up */
|
---|
| 327 | fibril_mutex_unlock(&socket->lock);
|
---|
| 328 | async_answer_0(callid, ENOMEM);
|
---|
| 329 | return;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | trc = tcp_uc_open(&lsocket, &fsocket, ap_passive,
|
---|
| 333 | tcp_open_nonblock, &conn);
|
---|
| 334 | if (conn == NULL) {
|
---|
| 335 | /* XXX Clean up */
|
---|
| 336 | fibril_mutex_unlock(&socket->lock);
|
---|
| 337 | async_answer_0(callid, ENOMEM);
|
---|
| 338 | return;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | tcp_uc_set_cstate_cb(conn, tcp_sock_cstate_cb, lconn);
|
---|
| 342 |
|
---|
| 343 | assert(trc == TCP_EOK);
|
---|
| 344 | conn->name = (char *)"S";
|
---|
| 345 |
|
---|
| 346 | lconn->conn = conn;
|
---|
| 347 | lconn->socket = socket;
|
---|
| 348 | link_initialize(&lconn->ready_list);
|
---|
| 349 | socket->lconn[i] = lconn;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | fibril_mutex_unlock(&socket->lock);
|
---|
[a4ee3ab2] | 353 | async_answer_0(callid, EOK);
|
---|
[04cd242] | 354 | }
|
---|
| 355 |
|
---|
| 356 | static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 357 | {
|
---|
| 358 | int rc;
|
---|
| 359 | struct sockaddr_in *addr;
|
---|
| 360 | int socket_id;
|
---|
| 361 | size_t addr_len;
|
---|
| 362 | socket_core_t *sock_core;
|
---|
| 363 | tcp_sockdata_t *socket;
|
---|
| 364 | tcp_error_t trc;
|
---|
[0ac2158] | 365 | tcp_sock_t lsocket;
|
---|
[04cd242] | 366 | tcp_sock_t fsocket;
|
---|
| 367 |
|
---|
[a1a101d] | 368 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()");
|
---|
[04cd242] | 369 |
|
---|
| 370 | rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len);
|
---|
| 371 | if (rc != EOK || addr_len != sizeof(struct sockaddr_in)) {
|
---|
| 372 | async_answer_0(callid, rc);
|
---|
| 373 | return;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 377 |
|
---|
| 378 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 379 | if (sock_core == NULL) {
|
---|
| 380 | async_answer_0(callid, ENOTSOCK);
|
---|
| 381 | return;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
[a4ee3ab2] | 385 | if (sock_core->port <= 0) {
|
---|
| 386 | rc = socket_bind_free_port(&gsock, sock_core,
|
---|
| 387 | TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
| 388 | last_used_port);
|
---|
| 389 | if (rc != EOK) {
|
---|
| 390 | async_answer_0(callid, rc);
|
---|
| 391 | return;
|
---|
| 392 | }
|
---|
[05bfce7] | 393 |
|
---|
[a4ee3ab2] | 394 | last_used_port = sock_core->port;
|
---|
| 395 | }
|
---|
[04cd242] | 396 |
|
---|
[ae481e0] | 397 | fibril_mutex_lock(&socket->lock);
|
---|
| 398 |
|
---|
[a2e3ee6] | 399 | if (inet_addr_is_any(&socket->laddr)) {
|
---|
[c76e926] | 400 | /* Determine local IP address */
|
---|
[a2e3ee6] | 401 | inet_addr_t loc_addr;
|
---|
| 402 | inet_addr_t rem_addr;
|
---|
| 403 |
|
---|
| 404 | inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr),
|
---|
| 405 | &rem_addr);
|
---|
[c76e926] | 406 | rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
|
---|
[0ac2158] | 407 | if (rc != EOK) {
|
---|
[ae481e0] | 408 | fibril_mutex_unlock(&socket->lock);
|
---|
[0ac2158] | 409 | async_answer_0(callid, rc);
|
---|
[a1a101d] | 410 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect: Failed to "
|
---|
[c76e926] | 411 | "determine local address.");
|
---|
[0ac2158] | 412 | return;
|
---|
| 413 | }
|
---|
[a2e3ee6] | 414 |
|
---|
| 415 | socket->laddr = loc_addr;
|
---|
[0ac2158] | 416 | }
|
---|
[a2e3ee6] | 417 |
|
---|
| 418 | lsocket.addr = socket->laddr;
|
---|
[0ac2158] | 419 | lsocket.port = sock_core->port;
|
---|
[a2e3ee6] | 420 |
|
---|
| 421 | inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr),
|
---|
| 422 | &fsocket.addr);
|
---|
[04cd242] | 423 | fsocket.port = uint16_t_be2host(addr->sin_port);
|
---|
| 424 |
|
---|
[ae481e0] | 425 | trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, &socket->conn);
|
---|
[704586fb] | 426 |
|
---|
| 427 | if (socket->conn != NULL)
|
---|
| 428 | socket->conn->name = (char *)"C";
|
---|
[04cd242] | 429 |
|
---|
[ae481e0] | 430 | fibril_mutex_unlock(&socket->lock);
|
---|
| 431 |
|
---|
[04cd242] | 432 | switch (trc) {
|
---|
| 433 | case TCP_EOK:
|
---|
| 434 | rc = EOK;
|
---|
| 435 | break;
|
---|
| 436 | case TCP_ERESET:
|
---|
| 437 | rc = ECONNREFUSED;
|
---|
| 438 | break;
|
---|
| 439 | default:
|
---|
| 440 | assert(false);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[d786dea9] | 443 | if (rc == EOK)
|
---|
| 444 | fibril_add_ready(socket->recv_fibril);
|
---|
[8fcf74f] | 445 |
|
---|
[d786dea9] | 446 | async_answer_0(callid, rc);
|
---|
[04cd242] | 447 | }
|
---|
| 448 |
|
---|
| 449 | static void tcp_sock_accept(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 450 | {
|
---|
| 451 | ipc_call_t answer;
|
---|
| 452 | int socket_id;
|
---|
[a4ee3ab2] | 453 | int asock_id;
|
---|
[04cd242] | 454 | socket_core_t *sock_core;
|
---|
| 455 | tcp_sockdata_t *socket;
|
---|
[a4ee3ab2] | 456 | tcp_sockdata_t *asocket;
|
---|
| 457 | tcp_error_t trc;
|
---|
[0ac2158] | 458 | tcp_sock_t lsocket;
|
---|
[a4ee3ab2] | 459 | tcp_sock_t fsocket;
|
---|
| 460 | tcp_conn_t *conn;
|
---|
[ae481e0] | 461 | tcp_conn_t *rconn;
|
---|
| 462 | tcp_sock_lconn_t *lconn;
|
---|
[a4ee3ab2] | 463 | int rc;
|
---|
[04cd242] | 464 |
|
---|
[a1a101d] | 465 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_accept()");
|
---|
[04cd242] | 466 |
|
---|
| 467 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
[a4ee3ab2] | 468 | asock_id = SOCKET_GET_NEW_SOCKET_ID(call);
|
---|
[04cd242] | 469 |
|
---|
| 470 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 471 | if (sock_core == NULL) {
|
---|
| 472 | async_answer_0(callid, ENOTSOCK);
|
---|
| 473 | return;
|
---|
| 474 | }
|
---|
[05bfce7] | 475 |
|
---|
| 476 | if (sock_core->port <= 0) {
|
---|
| 477 | rc = socket_bind_free_port(&gsock, sock_core,
|
---|
| 478 | TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
| 479 | last_used_port);
|
---|
| 480 | if (rc != EOK) {
|
---|
| 481 | async_answer_0(callid, rc);
|
---|
| 482 | return;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | last_used_port = sock_core->port;
|
---|
| 486 | }
|
---|
[04cd242] | 487 |
|
---|
| 488 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
[ae481e0] | 489 | fibril_mutex_lock(&socket->lock);
|
---|
[a4ee3ab2] | 490 |
|
---|
[a1a101d] | 491 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - verify socket->conn");
|
---|
[a4ee3ab2] | 492 | if (socket->conn != NULL) {
|
---|
[ae481e0] | 493 | fibril_mutex_unlock(&socket->lock);
|
---|
[a4ee3ab2] | 494 | async_answer_0(callid, EINVAL);
|
---|
| 495 | return;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[ae481e0] | 498 | if (list_empty(&socket->ready)) {
|
---|
| 499 | fibril_mutex_unlock(&socket->lock);
|
---|
| 500 | async_answer_0(callid, ENOENT);
|
---|
| 501 | return;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | lconn = list_get_instance(list_first(&socket->ready),
|
---|
| 505 | tcp_sock_lconn_t, ready_list);
|
---|
| 506 | list_remove(&lconn->ready_list);
|
---|
| 507 |
|
---|
| 508 | conn = lconn->conn;
|
---|
| 509 | tcp_uc_set_cstate_cb(conn, NULL, NULL);
|
---|
| 510 |
|
---|
| 511 | /* Replenish listening connection */
|
---|
[a4ee3ab2] | 512 |
|
---|
[a2e3ee6] | 513 | inet_addr_any(&lsocket.addr);
|
---|
[0ac2158] | 514 | lsocket.port = sock_core->port;
|
---|
[a2e3ee6] | 515 |
|
---|
| 516 | inet_addr_any(&fsocket.addr);
|
---|
[3aa2642a] | 517 | fsocket.port = TCP_PORT_ANY;
|
---|
[a4ee3ab2] | 518 |
|
---|
[ae481e0] | 519 | trc = tcp_uc_open(&lsocket, &fsocket, ap_passive, tcp_open_nonblock,
|
---|
| 520 | &rconn);
|
---|
| 521 | if (rconn == NULL) {
|
---|
| 522 | /* XXX Clean up */
|
---|
| 523 | fibril_mutex_unlock(&socket->lock);
|
---|
| 524 | async_answer_0(callid, ENOMEM);
|
---|
| 525 | return;
|
---|
| 526 | }
|
---|
[a4ee3ab2] | 527 |
|
---|
[ae481e0] | 528 | tcp_uc_set_cstate_cb(rconn, tcp_sock_cstate_cb, lconn);
|
---|
[a4ee3ab2] | 529 |
|
---|
[ae481e0] | 530 | assert(trc == TCP_EOK);
|
---|
| 531 | rconn->name = (char *)"S";
|
---|
[a4ee3ab2] | 532 |
|
---|
[ae481e0] | 533 | lconn->conn = rconn;
|
---|
| 534 |
|
---|
| 535 | /* Allocate socket for accepted connection */
|
---|
[a4ee3ab2] | 536 |
|
---|
[d786dea9] | 537 | rc = tcp_sock_create(client, &asocket);
|
---|
| 538 | if (rc != EOK) {
|
---|
[ae481e0] | 539 | fibril_mutex_unlock(&socket->lock);
|
---|
[d786dea9] | 540 | async_answer_0(callid, rc);
|
---|
[a4ee3ab2] | 541 | return;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | asocket->conn = conn;
|
---|
[a1a101d] | 545 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_accept():create asocket\n");
|
---|
[a4ee3ab2] | 546 |
|
---|
[d786dea9] | 547 | rc = tcp_sock_finish_setup(asocket, &asock_id);
|
---|
[a4ee3ab2] | 548 | if (rc != EOK) {
|
---|
[d786dea9] | 549 | tcp_sock_uncreate(asocket);
|
---|
[ae481e0] | 550 | fibril_mutex_unlock(&socket->lock);
|
---|
[a4ee3ab2] | 551 | async_answer_0(callid, rc);
|
---|
| 552 | return;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
[d786dea9] | 555 | fibril_add_ready(asocket->recv_fibril);
|
---|
[a4ee3ab2] | 556 |
|
---|
[a1a101d] | 557 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_accept(): find acore\n");
|
---|
[d786dea9] | 558 |
|
---|
| 559 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, TCP_SOCK_FRAGMENT_SIZE);
|
---|
[a4ee3ab2] | 560 | SOCKET_SET_SOCKET_ID(answer, asock_id);
|
---|
| 561 | SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(struct sockaddr_in));
|
---|
[b1bd89ea] | 562 |
|
---|
[d786dea9] | 563 | async_answer_3(callid, asocket->sock_core->socket_id,
|
---|
[b1bd89ea] | 564 | IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),
|
---|
| 565 | IPC_GET_ARG3(answer));
|
---|
| 566 |
|
---|
[a4ee3ab2] | 567 | /* Push one fragment notification to client's queue */
|
---|
[a1a101d] | 568 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_accept(): notify data\n");
|
---|
[ae481e0] | 569 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 570 | }
|
---|
| 571 |
|
---|
| 572 | static void tcp_sock_send(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 573 | {
|
---|
| 574 | int socket_id;
|
---|
| 575 | int fragments;
|
---|
| 576 | int index;
|
---|
| 577 | socket_core_t *sock_core;
|
---|
| 578 | tcp_sockdata_t *socket;
|
---|
| 579 | ipc_call_t answer;
|
---|
| 580 | ipc_callid_t wcallid;
|
---|
| 581 | size_t length;
|
---|
[d786dea9] | 582 | uint8_t buffer[TCP_SOCK_FRAGMENT_SIZE];
|
---|
[04cd242] | 583 | tcp_error_t trc;
|
---|
| 584 | int rc;
|
---|
| 585 |
|
---|
[a1a101d] | 586 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_send()");
|
---|
[04cd242] | 587 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 588 | fragments = SOCKET_GET_DATA_FRAGMENTS(call);
|
---|
| 589 | SOCKET_GET_FLAGS(call);
|
---|
| 590 |
|
---|
| 591 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 592 | if (sock_core == NULL) {
|
---|
| 593 | async_answer_0(callid, ENOTSOCK);
|
---|
| 594 | return;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
[ae481e0] | 598 | fibril_mutex_lock(&socket->lock);
|
---|
| 599 |
|
---|
[04cd242] | 600 | if (socket->conn == NULL) {
|
---|
[ae481e0] | 601 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 602 | async_answer_0(callid, ENOTCONN);
|
---|
| 603 | return;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | for (index = 0; index < fragments; index++) {
|
---|
| 607 | if (!async_data_write_receive(&wcallid, &length)) {
|
---|
[ae481e0] | 608 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 609 | async_answer_0(callid, EINVAL);
|
---|
| 610 | return;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
[d786dea9] | 613 | if (length > TCP_SOCK_FRAGMENT_SIZE)
|
---|
| 614 | length = TCP_SOCK_FRAGMENT_SIZE;
|
---|
[04cd242] | 615 |
|
---|
| 616 | rc = async_data_write_finalize(wcallid, buffer, length);
|
---|
| 617 | if (rc != EOK) {
|
---|
[ae481e0] | 618 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 619 | async_answer_0(callid, rc);
|
---|
| 620 | return;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | trc = tcp_uc_send(socket->conn, buffer, length, 0);
|
---|
| 624 |
|
---|
| 625 | switch (trc) {
|
---|
| 626 | case TCP_EOK:
|
---|
| 627 | rc = EOK;
|
---|
| 628 | break;
|
---|
| 629 | case TCP_ENOTEXIST:
|
---|
| 630 | rc = ENOTCONN;
|
---|
| 631 | break;
|
---|
| 632 | case TCP_ECLOSING:
|
---|
| 633 | rc = ENOTCONN;
|
---|
| 634 | break;
|
---|
[d9e14fa4] | 635 | case TCP_ERESET:
|
---|
| 636 | rc = ECONNABORTED;
|
---|
| 637 | break;
|
---|
[04cd242] | 638 | default:
|
---|
| 639 | assert(false);
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | if (rc != EOK) {
|
---|
[ae481e0] | 643 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 644 | async_answer_0(callid, rc);
|
---|
| 645 | return;
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[b1bd89ea] | 649 | IPC_SET_ARG1(answer, 0);
|
---|
[d786dea9] | 650 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, TCP_SOCK_FRAGMENT_SIZE);
|
---|
[b1bd89ea] | 651 | async_answer_2(callid, EOK, IPC_GET_ARG1(answer),
|
---|
| 652 | IPC_GET_ARG2(answer));
|
---|
[ae481e0] | 653 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 654 | }
|
---|
| 655 |
|
---|
| 656 | static void tcp_sock_sendto(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 657 | {
|
---|
[a1a101d] | 658 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_sendto()");
|
---|
[04cd242] | 659 | async_answer_0(callid, ENOTSUP);
|
---|
| 660 | }
|
---|
| 661 |
|
---|
[a4ee3ab2] | 662 | static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
[04cd242] | 663 | {
|
---|
| 664 | int socket_id;
|
---|
| 665 | int flags;
|
---|
[a4ee3ab2] | 666 | size_t addr_length, length;
|
---|
[04cd242] | 667 | socket_core_t *sock_core;
|
---|
| 668 | tcp_sockdata_t *socket;
|
---|
| 669 | ipc_call_t answer;
|
---|
| 670 | ipc_callid_t rcallid;
|
---|
| 671 | size_t data_len;
|
---|
[a4ee3ab2] | 672 | struct sockaddr_in addr;
|
---|
| 673 | tcp_sock_t *rsock;
|
---|
[04cd242] | 674 | int rc;
|
---|
| 675 |
|
---|
[a1a101d] | 676 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: tcp_sock_recv[from]()", client);
|
---|
[04cd242] | 677 |
|
---|
| 678 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 679 | flags = SOCKET_GET_FLAGS(call);
|
---|
| 680 |
|
---|
| 681 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 682 | if (sock_core == NULL) {
|
---|
| 683 | async_answer_0(callid, ENOTSOCK);
|
---|
| 684 | return;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
[ae481e0] | 688 | fibril_mutex_lock(&socket->lock);
|
---|
| 689 |
|
---|
[a4ee3ab2] | 690 | if (socket->conn == NULL) {
|
---|
[ae481e0] | 691 | fibril_mutex_unlock(&socket->lock);
|
---|
[a4ee3ab2] | 692 | async_answer_0(callid, ENOTCONN);
|
---|
| 693 | return;
|
---|
| 694 | }
|
---|
[04cd242] | 695 |
|
---|
| 696 | (void)flags;
|
---|
| 697 |
|
---|
[a1a101d] | 698 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom(): lock recv_buffer_lock");
|
---|
[d786dea9] | 699 | fibril_mutex_lock(&socket->recv_buffer_lock);
|
---|
[7c912b6] | 700 | while (socket->recv_buffer_used == 0 && socket->recv_error == TCP_EOK) {
|
---|
[a1a101d] | 701 | log_msg(LOG_DEFAULT, LVL_DEBUG, "wait for recv_buffer_cv + recv_buffer_used != 0");
|
---|
[d786dea9] | 702 | fibril_condvar_wait(&socket->recv_buffer_cv,
|
---|
| 703 | &socket->recv_buffer_lock);
|
---|
| 704 | }
|
---|
[04cd242] | 705 |
|
---|
[a1a101d] | 706 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
|
---|
[d786dea9] | 707 |
|
---|
| 708 | data_len = socket->recv_buffer_used;
|
---|
| 709 | rc = socket->recv_error;
|
---|
| 710 |
|
---|
| 711 | switch (socket->recv_error) {
|
---|
[04cd242] | 712 | case TCP_EOK:
|
---|
| 713 | rc = EOK;
|
---|
| 714 | break;
|
---|
| 715 | case TCP_ENOTEXIST:
|
---|
| 716 | case TCP_ECLOSING:
|
---|
| 717 | rc = ENOTCONN;
|
---|
| 718 | break;
|
---|
[d9e14fa4] | 719 | case TCP_ERESET:
|
---|
| 720 | rc = ECONNABORTED;
|
---|
| 721 | break;
|
---|
[04cd242] | 722 | default:
|
---|
| 723 | assert(false);
|
---|
| 724 | }
|
---|
| 725 |
|
---|
[a1a101d] | 726 | log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv result -> %d", rc);
|
---|
[04cd242] | 727 | if (rc != EOK) {
|
---|
[d786dea9] | 728 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
[ae481e0] | 729 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 730 | async_answer_0(callid, rc);
|
---|
| 731 | return;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[a4ee3ab2] | 734 | if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
|
---|
[a2e3ee6] | 735 | /* Fill address */
|
---|
[a4ee3ab2] | 736 | rsock = &socket->conn->ident.foreign;
|
---|
[a2e3ee6] | 737 |
|
---|
| 738 | uint32_t rsock_addr;
|
---|
| 739 | int rc = inet_addr_pack(&rsock->addr, &rsock_addr);
|
---|
| 740 | if (rc != EOK) {
|
---|
| 741 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
| 742 | fibril_mutex_unlock(&socket->lock);
|
---|
| 743 | async_answer_0(callid, rc);
|
---|
| 744 | return;
|
---|
| 745 | }
|
---|
| 746 |
|
---|
[a4ee3ab2] | 747 | addr.sin_family = AF_INET;
|
---|
[a2e3ee6] | 748 | addr.sin_addr.s_addr = host2uint32_t_be(rsock_addr);
|
---|
[a4ee3ab2] | 749 | addr.sin_port = host2uint16_t_be(rsock->port);
|
---|
| 750 |
|
---|
[a1a101d] | 751 | log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
|
---|
[a4ee3ab2] | 752 | if (!async_data_read_receive(&rcallid, &addr_length)) {
|
---|
[d786dea9] | 753 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
[ae481e0] | 754 | fibril_mutex_unlock(&socket->lock);
|
---|
[a4ee3ab2] | 755 | async_answer_0(callid, EINVAL);
|
---|
| 756 | return;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | if (addr_length > sizeof(addr))
|
---|
| 760 | addr_length = sizeof(addr);
|
---|
| 761 |
|
---|
[a1a101d] | 762 | log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
|
---|
[a4ee3ab2] | 763 | rc = async_data_read_finalize(rcallid, &addr, addr_length);
|
---|
| 764 | if (rc != EOK) {
|
---|
[d786dea9] | 765 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
[ae481e0] | 766 | fibril_mutex_unlock(&socket->lock);
|
---|
[a4ee3ab2] | 767 | async_answer_0(callid, EINVAL);
|
---|
| 768 | return;
|
---|
| 769 | }
|
---|
| 770 | }
|
---|
| 771 |
|
---|
[a1a101d] | 772 | log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
|
---|
[04cd242] | 773 | if (!async_data_read_receive(&rcallid, &length)) {
|
---|
[d786dea9] | 774 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
[ae481e0] | 775 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 776 | async_answer_0(callid, EINVAL);
|
---|
| 777 | return;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[a4ee3ab2] | 780 | if (length > data_len)
|
---|
[04cd242] | 781 | length = data_len;
|
---|
| 782 |
|
---|
[a1a101d] | 783 | log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
|
---|
[d786dea9] | 784 | rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
|
---|
| 785 |
|
---|
[9094c0f] | 786 | socket->recv_buffer_used -= length;
|
---|
[a1a101d] | 787 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom: %zu left in buffer",
|
---|
[9094c0f] | 788 | socket->recv_buffer_used);
|
---|
| 789 | if (socket->recv_buffer_used > 0) {
|
---|
| 790 | memmove(socket->recv_buffer, socket->recv_buffer + length,
|
---|
| 791 | socket->recv_buffer_used);
|
---|
| 792 | tcp_sock_notify_data(socket->sock_core);
|
---|
| 793 | }
|
---|
| 794 |
|
---|
[d786dea9] | 795 | fibril_condvar_broadcast(&socket->recv_buffer_cv);
|
---|
[a4ee3ab2] | 796 |
|
---|
| 797 | if (length < data_len && rc == EOK)
|
---|
| 798 | rc = EOVERFLOW;
|
---|
[04cd242] | 799 |
|
---|
| 800 | SOCKET_SET_READ_DATA_LENGTH(answer, length);
|
---|
[b1bd89ea] | 801 | async_answer_1(callid, EOK, IPC_GET_ARG1(answer));
|
---|
[d786dea9] | 802 |
|
---|
| 803 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
[ae481e0] | 804 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 805 | }
|
---|
| 806 |
|
---|
| 807 | static void tcp_sock_close(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 808 | {
|
---|
| 809 | int socket_id;
|
---|
| 810 | socket_core_t *sock_core;
|
---|
| 811 | tcp_sockdata_t *socket;
|
---|
[b0d82d1] | 812 | tcp_error_t trc;
|
---|
[04cd242] | 813 | int rc;
|
---|
| 814 |
|
---|
[a1a101d] | 815 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
|
---|
[04cd242] | 816 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
| 817 |
|
---|
| 818 | sock_core = socket_cores_find(&client->sockets, socket_id);
|
---|
| 819 | if (sock_core == NULL) {
|
---|
| 820 | async_answer_0(callid, ENOTSOCK);
|
---|
| 821 | return;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | socket = (tcp_sockdata_t *)sock_core->specific_data;
|
---|
[ae481e0] | 825 | fibril_mutex_lock(&socket->lock);
|
---|
[b0d82d1] | 826 |
|
---|
[415578ef] | 827 | if (socket->conn != NULL) {
|
---|
[522a4f9] | 828 | trc = tcp_uc_close(socket->conn);
|
---|
| 829 | if (trc != TCP_EOK && trc != TCP_ENOTEXIST) {
|
---|
[ae481e0] | 830 | fibril_mutex_unlock(&socket->lock);
|
---|
[522a4f9] | 831 | async_answer_0(callid, EBADF);
|
---|
[415578ef] | 832 | return;
|
---|
| 833 | }
|
---|
| 834 | }
|
---|
[7a8c1c4e] | 835 |
|
---|
[798105ca] | 836 | /* Grab recv_buffer_lock because of CV wait in tcp_sock_recv_fibril() */
|
---|
| 837 | fibril_mutex_lock(&socket->recv_buffer_lock);
|
---|
| 838 | socket->sock_core = NULL;
|
---|
| 839 | fibril_mutex_unlock(&socket->recv_buffer_lock);
|
---|
| 840 |
|
---|
[c76e926] | 841 | rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
|
---|
[04cd242] | 842 | tcp_free_sock_data);
|
---|
| 843 | if (rc != EOK) {
|
---|
[ae481e0] | 844 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 845 | async_answer_0(callid, rc);
|
---|
| 846 | return;
|
---|
| 847 | }
|
---|
| 848 |
|
---|
[ae481e0] | 849 | fibril_mutex_unlock(&socket->lock);
|
---|
[04cd242] | 850 | async_answer_0(callid, EOK);
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | static void tcp_sock_getsockopt(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 854 | {
|
---|
[a1a101d] | 855 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_getsockopt()");
|
---|
[04cd242] | 856 | async_answer_0(callid, ENOTSUP);
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | static void tcp_sock_setsockopt(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
|
---|
| 860 | {
|
---|
[a1a101d] | 861 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_setsockopt()");
|
---|
[04cd242] | 862 | async_answer_0(callid, ENOTSUP);
|
---|
| 863 | }
|
---|
| 864 |
|
---|
[ae481e0] | 865 | /** Called when connection state changes. */
|
---|
| 866 | static void tcp_sock_cstate_cb(tcp_conn_t *conn, void *arg)
|
---|
| 867 | {
|
---|
| 868 | tcp_conn_status_t cstatus;
|
---|
| 869 | tcp_sock_lconn_t *lconn = (tcp_sock_lconn_t *)arg;
|
---|
| 870 | tcp_sockdata_t *socket = lconn->socket;
|
---|
| 871 |
|
---|
[a1a101d] | 872 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_cstate_cb()");
|
---|
[ae481e0] | 873 | fibril_mutex_lock(&socket->lock);
|
---|
| 874 | assert(conn == lconn->conn);
|
---|
| 875 |
|
---|
| 876 | tcp_uc_status(conn, &cstatus);
|
---|
| 877 | if (cstatus.cstate != st_established) {
|
---|
| 878 | fibril_mutex_unlock(&socket->lock);
|
---|
| 879 | return;
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 | assert_link_not_used(&lconn->ready_list);
|
---|
| 883 | list_append(&lconn->ready_list, &socket->ready);
|
---|
| 884 |
|
---|
[a1a101d] | 885 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_cstate_cb(): notify accept");
|
---|
[ae481e0] | 886 |
|
---|
| 887 | /* Push one accept notification to client's queue */
|
---|
| 888 | tcp_sock_notify_aconn(socket->sock_core);
|
---|
| 889 | fibril_mutex_unlock(&socket->lock);
|
---|
| 890 | }
|
---|
| 891 |
|
---|
[d786dea9] | 892 | static int tcp_sock_recv_fibril(void *arg)
|
---|
| 893 | {
|
---|
| 894 | tcp_sockdata_t *sock = (tcp_sockdata_t *)arg;
|
---|
| 895 | size_t data_len;
|
---|
| 896 | xflags_t xflags;
|
---|
| 897 | tcp_error_t trc;
|
---|
| 898 |
|
---|
[a1a101d] | 899 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recv_fibril()");
|
---|
[d786dea9] | 900 |
|
---|
[798105ca] | 901 | fibril_mutex_lock(&sock->recv_buffer_lock);
|
---|
| 902 |
|
---|
[d786dea9] | 903 | while (true) {
|
---|
[a1a101d] | 904 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call tcp_uc_receive()");
|
---|
[798105ca] | 905 | while (sock->recv_buffer_used != 0 && sock->sock_core != NULL)
|
---|
[d786dea9] | 906 | fibril_condvar_wait(&sock->recv_buffer_cv,
|
---|
| 907 | &sock->recv_buffer_lock);
|
---|
| 908 |
|
---|
| 909 | trc = tcp_uc_receive(sock->conn, sock->recv_buffer,
|
---|
| 910 | TCP_SOCK_FRAGMENT_SIZE, &data_len, &xflags);
|
---|
| 911 |
|
---|
| 912 | if (trc != TCP_EOK) {
|
---|
| 913 | sock->recv_error = trc;
|
---|
[7c912b6] | 914 | fibril_condvar_broadcast(&sock->recv_buffer_cv);
|
---|
[798105ca] | 915 | if (sock->sock_core != NULL)
|
---|
| 916 | tcp_sock_notify_data(sock->sock_core);
|
---|
[d786dea9] | 917 | break;
|
---|
| 918 | }
|
---|
| 919 |
|
---|
[a1a101d] | 920 | log_msg(LOG_DEFAULT, LVL_DEBUG, "got data - broadcast recv_buffer_cv");
|
---|
[d786dea9] | 921 |
|
---|
| 922 | sock->recv_buffer_used = data_len;
|
---|
| 923 | fibril_condvar_broadcast(&sock->recv_buffer_cv);
|
---|
[798105ca] | 924 | if (sock->sock_core != NULL)
|
---|
| 925 | tcp_sock_notify_data(sock->sock_core);
|
---|
[d786dea9] | 926 | }
|
---|
| 927 |
|
---|
[798105ca] | 928 | fibril_mutex_unlock(&sock->recv_buffer_lock);
|
---|
| 929 |
|
---|
[d786dea9] | 930 | tcp_uc_delete(sock->conn);
|
---|
| 931 |
|
---|
| 932 | return 0;
|
---|
| 933 | }
|
---|
| 934 |
|
---|
[c76e926] | 935 | static void tcp_sock_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[04cd242] | 936 | {
|
---|
| 937 | ipc_callid_t callid;
|
---|
| 938 | ipc_call_t call;
|
---|
| 939 | tcp_client_t client;
|
---|
| 940 |
|
---|
| 941 | /* Accept the connection */
|
---|
| 942 | async_answer_0(iid, EOK);
|
---|
| 943 |
|
---|
[c76e926] | 944 | client.sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
[04cd242] | 945 | socket_cores_initialize(&client.sockets);
|
---|
| 946 |
|
---|
| 947 | while (true) {
|
---|
| 948 | callid = async_get_call(&call);
|
---|
| 949 | if (!IPC_GET_IMETHOD(call))
|
---|
| 950 | break;
|
---|
| 951 |
|
---|
[a1a101d] | 952 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connection: METHOD=%d\n",
|
---|
[04cd242] | 953 | (int)IPC_GET_IMETHOD(call));
|
---|
| 954 |
|
---|
| 955 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 956 | case NET_SOCKET:
|
---|
| 957 | tcp_sock_socket(&client, callid, call);
|
---|
| 958 | break;
|
---|
| 959 | case NET_SOCKET_BIND:
|
---|
| 960 | tcp_sock_bind(&client, callid, call);
|
---|
| 961 | break;
|
---|
| 962 | case NET_SOCKET_LISTEN:
|
---|
| 963 | tcp_sock_listen(&client, callid, call);
|
---|
| 964 | break;
|
---|
| 965 | case NET_SOCKET_CONNECT:
|
---|
| 966 | tcp_sock_connect(&client, callid, call);
|
---|
| 967 | break;
|
---|
| 968 | case NET_SOCKET_ACCEPT:
|
---|
| 969 | tcp_sock_accept(&client, callid, call);
|
---|
| 970 | break;
|
---|
| 971 | case NET_SOCKET_SEND:
|
---|
| 972 | tcp_sock_send(&client, callid, call);
|
---|
| 973 | break;
|
---|
| 974 | case NET_SOCKET_SENDTO:
|
---|
| 975 | tcp_sock_sendto(&client, callid, call);
|
---|
| 976 | break;
|
---|
| 977 | case NET_SOCKET_RECV:
|
---|
| 978 | case NET_SOCKET_RECVFROM:
|
---|
| 979 | tcp_sock_recvfrom(&client, callid, call);
|
---|
| 980 | break;
|
---|
| 981 | case NET_SOCKET_CLOSE:
|
---|
| 982 | tcp_sock_close(&client, callid, call);
|
---|
| 983 | break;
|
---|
| 984 | case NET_SOCKET_GETSOCKOPT:
|
---|
| 985 | tcp_sock_getsockopt(&client, callid, call);
|
---|
| 986 | break;
|
---|
| 987 | case NET_SOCKET_SETSOCKOPT:
|
---|
| 988 | tcp_sock_setsockopt(&client, callid, call);
|
---|
| 989 | break;
|
---|
| 990 | default:
|
---|
| 991 | async_answer_0(callid, ENOTSUP);
|
---|
| 992 | break;
|
---|
| 993 | }
|
---|
| 994 | }
|
---|
[0d520a2] | 995 |
|
---|
| 996 | /* Clean up */
|
---|
[a1a101d] | 997 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connection: Clean up");
|
---|
[0d520a2] | 998 | async_hangup(client.sess);
|
---|
| 999 | socket_cores_release(NULL, &client.sockets, &gsock, tcp_free_sock_data);
|
---|
[04cd242] | 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | /**
|
---|
| 1003 | * @}
|
---|
| 1004 | */
|
---|