[fab2746] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup udp
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file HelenOS service implementation
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <inet/endpoint.h>
|
---|
| 40 | #include <io/log.h>
|
---|
| 41 | #include <ipc/services.h>
|
---|
| 42 | #include <ipc/udp.h>
|
---|
| 43 | #include <loc.h>
|
---|
| 44 | #include <macros.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 |
|
---|
| 47 | #include "assoc.h"
|
---|
| 48 | #include "msg.h"
|
---|
| 49 | #include "service.h"
|
---|
| 50 | #include "udp_type.h"
|
---|
| 51 |
|
---|
| 52 | #define NAME "udp"
|
---|
| 53 |
|
---|
[b10460a] | 54 | /** Maximum message size */
|
---|
[fab2746] | 55 | #define MAX_MSG_SIZE DATA_XFER_LIMIT
|
---|
| 56 |
|
---|
[2f19103] | 57 | static void udp_cassoc_recv_msg(void *, inet_ep2_t *, udp_msg_t *);
|
---|
[fab2746] | 58 |
|
---|
[b10460a] | 59 | /** Callbacks to tie us to association layer */
|
---|
[fab2746] | 60 | static udp_assoc_cb_t udp_cassoc_cb = {
|
---|
| 61 | .recv_msg = udp_cassoc_recv_msg
|
---|
| 62 | };
|
---|
| 63 |
|
---|
[b10460a] | 64 | /** Add message to client receive queue.
|
---|
| 65 | *
|
---|
| 66 | * @param cassoc Client association
|
---|
| 67 | * @param epp Endpoint pair on which message was received
|
---|
| 68 | * @param msg Message
|
---|
| 69 | *
|
---|
| 70 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 71 | */
|
---|
[b7fd2a0] | 72 | static errno_t udp_cassoc_queue_msg(udp_cassoc_t *cassoc, inet_ep2_t *epp,
|
---|
[fab2746] | 73 | udp_msg_t *msg)
|
---|
| 74 | {
|
---|
| 75 | udp_crcv_queue_entry_t *rqe;
|
---|
| 76 |
|
---|
[99ea91b2] | 77 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_cassoc_queue_msg(%p, %p, %p)",
|
---|
[2f19103] | 78 | cassoc, epp, msg);
|
---|
[fab2746] | 79 |
|
---|
| 80 | rqe = calloc(1, sizeof(udp_crcv_queue_entry_t));
|
---|
| 81 | if (rqe == NULL)
|
---|
| 82 | return ENOMEM;
|
---|
| 83 |
|
---|
| 84 | link_initialize(&rqe->link);
|
---|
[2f19103] | 85 | rqe->epp = *epp;
|
---|
[fab2746] | 86 | rqe->msg = msg;
|
---|
| 87 | rqe->cassoc = cassoc;
|
---|
| 88 |
|
---|
| 89 | // fibril_mutex_lock(&assoc->lock);
|
---|
| 90 | list_append(&rqe->link, &cassoc->client->crcv_queue);
|
---|
| 91 | // fibril_mutex_unlock(&assoc->lock);
|
---|
| 92 |
|
---|
| 93 | // fibril_condvar_broadcast(&assoc->rcv_queue_cv);
|
---|
| 94 |
|
---|
| 95 | return EOK;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[b10460a] | 98 | /** Send 'data' event to client.
|
---|
| 99 | *
|
---|
| 100 | * @param client Client
|
---|
| 101 | */
|
---|
[99ea91b2] | 102 | static void udp_ev_data(udp_client_t *client)
|
---|
[fab2746] | 103 | {
|
---|
| 104 | async_exch_t *exch;
|
---|
| 105 |
|
---|
[99ea91b2] | 106 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_ev_data()");
|
---|
[fab2746] | 107 |
|
---|
| 108 | exch = async_exchange_begin(client->sess);
|
---|
[99ea91b2] | 109 | aid_t req = async_send_0(exch, UDP_EV_DATA, NULL);
|
---|
[fab2746] | 110 | async_exchange_end(exch);
|
---|
| 111 |
|
---|
[99ea91b2] | 112 | async_forget(req);
|
---|
[fab2746] | 113 | }
|
---|
| 114 |
|
---|
[b10460a] | 115 | /** Create client association.
|
---|
| 116 | *
|
---|
| 117 | * This effectively adds an association into a client's namespace.
|
---|
| 118 | *
|
---|
| 119 | * @param client Client
|
---|
| 120 | * @param assoc Association
|
---|
| 121 | * @param rcassoc Place to store pointer to new client association
|
---|
| 122 | *
|
---|
| 123 | * @return EOK on soccess, ENOMEM if out of memory
|
---|
| 124 | */
|
---|
[b7fd2a0] | 125 | static errno_t udp_cassoc_create(udp_client_t *client, udp_assoc_t *assoc,
|
---|
[fab2746] | 126 | udp_cassoc_t **rcassoc)
|
---|
| 127 | {
|
---|
| 128 | udp_cassoc_t *cassoc;
|
---|
| 129 | sysarg_t id;
|
---|
| 130 |
|
---|
| 131 | cassoc = calloc(1, sizeof(udp_cassoc_t));
|
---|
| 132 | if (cassoc == NULL)
|
---|
| 133 | return ENOMEM;
|
---|
| 134 |
|
---|
| 135 | /* Allocate new ID */
|
---|
| 136 | id = 0;
|
---|
| 137 | list_foreach (client->cassoc, lclient, udp_cassoc_t, cassoc) {
|
---|
| 138 | if (cassoc->id >= id)
|
---|
| 139 | id = cassoc->id + 1;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | cassoc->id = id;
|
---|
| 143 | cassoc->client = client;
|
---|
| 144 | cassoc->assoc = assoc;
|
---|
| 145 |
|
---|
| 146 | list_append(&cassoc->lclient, &client->cassoc);
|
---|
| 147 | *rcassoc = cassoc;
|
---|
| 148 | return EOK;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[b10460a] | 151 | /** Destroy client association.
|
---|
| 152 | *
|
---|
| 153 | * @param cassoc Client association
|
---|
| 154 | */
|
---|
[fab2746] | 155 | static void udp_cassoc_destroy(udp_cassoc_t *cassoc)
|
---|
| 156 | {
|
---|
| 157 | list_remove(&cassoc->lclient);
|
---|
| 158 | free(cassoc);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[b10460a] | 161 | /** Get client association by ID.
|
---|
| 162 | *
|
---|
| 163 | * @param client Client
|
---|
| 164 | * @param id Client association ID
|
---|
| 165 | * @param rcassoc Place to store pointer to client association
|
---|
| 166 | *
|
---|
| 167 | * @return EOK on success, ENOENT if no client association with the given ID
|
---|
| 168 | * is found.
|
---|
| 169 | */
|
---|
[b7fd2a0] | 170 | static errno_t udp_cassoc_get(udp_client_t *client, sysarg_t id,
|
---|
[fab2746] | 171 | udp_cassoc_t **rcassoc)
|
---|
| 172 | {
|
---|
| 173 | list_foreach (client->cassoc, lclient, udp_cassoc_t, cassoc) {
|
---|
| 174 | if (cassoc->id == id) {
|
---|
| 175 | *rcassoc = cassoc;
|
---|
| 176 | return EOK;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return ENOENT;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[b10460a] | 183 | /** Message received on client association.
|
---|
| 184 | *
|
---|
| 185 | * Used as udp_assoc_cb.recv_msg callback.
|
---|
| 186 | *
|
---|
| 187 | * @param arg Callback argument, client association
|
---|
| 188 | * @param epp Endpoint pair where message was received
|
---|
| 189 | * @param msg Message
|
---|
| 190 | */
|
---|
[2f19103] | 191 | static void udp_cassoc_recv_msg(void *arg, inet_ep2_t *epp, udp_msg_t *msg)
|
---|
[fab2746] | 192 | {
|
---|
| 193 | udp_cassoc_t *cassoc = (udp_cassoc_t *) arg;
|
---|
| 194 |
|
---|
[2f19103] | 195 | udp_cassoc_queue_msg(cassoc, epp, msg);
|
---|
[fab2746] | 196 | udp_ev_data(cassoc->client);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[b10460a] | 199 | /** Create association.
|
---|
| 200 | *
|
---|
| 201 | * Handle client request to create association (with parameters unmarshalled).
|
---|
| 202 | *
|
---|
| 203 | * @param client UDP client
|
---|
| 204 | * @param epp Endpoint pair
|
---|
| 205 | * @param rassoc_id Place to store ID of new association
|
---|
| 206 | *
|
---|
[cde999a] | 207 | * @return EOK on success or an error code
|
---|
[b10460a] | 208 | */
|
---|
[b7fd2a0] | 209 | static errno_t udp_assoc_create_impl(udp_client_t *client, inet_ep2_t *epp,
|
---|
[fab2746] | 210 | sysarg_t *rassoc_id)
|
---|
| 211 | {
|
---|
| 212 | udp_assoc_t *assoc;
|
---|
| 213 | udp_cassoc_t *cassoc;
|
---|
[b7fd2a0] | 214 | errno_t rc;
|
---|
[779541b] | 215 |
|
---|
| 216 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_create_impl");
|
---|
[fab2746] | 217 |
|
---|
[2f19103] | 218 | assoc = udp_assoc_new(epp, NULL, NULL);
|
---|
[fab2746] | 219 | if (assoc == NULL)
|
---|
| 220 | return EIO;
|
---|
| 221 |
|
---|
| 222 | if (epp->local_link != 0)
|
---|
| 223 | udp_assoc_set_iplink(assoc, epp->local_link);
|
---|
| 224 |
|
---|
| 225 | rc = udp_cassoc_create(client, assoc, &cassoc);
|
---|
| 226 | if (rc != EOK) {
|
---|
| 227 | assert(rc == ENOMEM);
|
---|
| 228 | udp_assoc_delete(assoc);
|
---|
| 229 | return ENOMEM;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | assoc->cb = &udp_cassoc_cb;
|
---|
| 233 | assoc->cb_arg = cassoc;
|
---|
| 234 |
|
---|
[2989c7e] | 235 | rc = udp_assoc_add(assoc);
|
---|
| 236 | if (rc != EOK) {
|
---|
| 237 | udp_cassoc_destroy(cassoc);
|
---|
| 238 | udp_assoc_delete(assoc);
|
---|
| 239 | return rc;
|
---|
| 240 | }
|
---|
[fab2746] | 241 |
|
---|
| 242 | *rassoc_id = cassoc->id;
|
---|
| 243 | return EOK;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[b10460a] | 246 | /** Destroy association.
|
---|
| 247 | *
|
---|
| 248 | * Handle client request to destroy association (with parameters unmarshalled).
|
---|
| 249 | *
|
---|
| 250 | * @param client UDP client
|
---|
| 251 | * @param assoc_id Association ID
|
---|
| 252 | * @return EOK on success, ENOENT if no such association is found
|
---|
| 253 | */
|
---|
[b7fd2a0] | 254 | static errno_t udp_assoc_destroy_impl(udp_client_t *client, sysarg_t assoc_id)
|
---|
[fab2746] | 255 | {
|
---|
| 256 | udp_cassoc_t *cassoc;
|
---|
[b7fd2a0] | 257 | errno_t rc;
|
---|
[fab2746] | 258 |
|
---|
| 259 | rc = udp_cassoc_get(client, assoc_id, &cassoc);
|
---|
| 260 | if (rc != EOK) {
|
---|
| 261 | assert(rc == ENOENT);
|
---|
| 262 | return ENOENT;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | udp_assoc_remove(cassoc->assoc);
|
---|
| 266 | udp_assoc_reset(cassoc->assoc);
|
---|
| 267 | udp_assoc_delete(cassoc->assoc);
|
---|
| 268 | udp_cassoc_destroy(cassoc);
|
---|
| 269 | return EOK;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[58e8646] | 272 | /** Set association sending messages with no local address.
|
---|
| 273 | *
|
---|
| 274 | * Handle client request to set nolocal flag (with parameters unmarshalled).
|
---|
| 275 | *
|
---|
| 276 | * @param client UDP client
|
---|
| 277 | * @param assoc_id Association ID
|
---|
| 278 | * @return EOK on success, ENOENT if no such association is found
|
---|
| 279 | */
|
---|
[b7fd2a0] | 280 | static errno_t udp_assoc_set_nolocal_impl(udp_client_t *client, sysarg_t assoc_id)
|
---|
[58e8646] | 281 | {
|
---|
| 282 | udp_cassoc_t *cassoc;
|
---|
[b7fd2a0] | 283 | errno_t rc;
|
---|
[58e8646] | 284 |
|
---|
| 285 | rc = udp_cassoc_get(client, assoc_id, &cassoc);
|
---|
| 286 | if (rc != EOK) {
|
---|
| 287 | assert(rc == ENOENT);
|
---|
| 288 | return ENOENT;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | log_msg(LOG_DEFAULT, LVL_NOTE, "Setting nolocal to true");
|
---|
| 292 | cassoc->assoc->nolocal = true;
|
---|
| 293 | return EOK;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[b10460a] | 296 | /** Send message via association.
|
---|
| 297 | *
|
---|
| 298 | * Handle client request to send message (with parameters unmarshalled).
|
---|
| 299 | *
|
---|
| 300 | * @param client UDP client
|
---|
| 301 | * @param assoc_id Association ID
|
---|
| 302 | * @param dest Destination endpoint or @c NULL to use the default from
|
---|
| 303 | * association
|
---|
| 304 | * @param data Message data
|
---|
| 305 | * @param size Message size
|
---|
| 306 | *
|
---|
[cde999a] | 307 | * @return EOK on success or an error code
|
---|
[b10460a] | 308 | */
|
---|
[b7fd2a0] | 309 | static errno_t udp_assoc_send_msg_impl(udp_client_t *client, sysarg_t assoc_id,
|
---|
[fab2746] | 310 | inet_ep_t *dest, void *data, size_t size)
|
---|
| 311 | {
|
---|
| 312 | udp_msg_t msg;
|
---|
| 313 | udp_cassoc_t *cassoc;
|
---|
[b7fd2a0] | 314 | errno_t rc;
|
---|
[fab2746] | 315 |
|
---|
| 316 | rc = udp_cassoc_get(client, assoc_id, &cassoc);
|
---|
| 317 | if (rc != EOK)
|
---|
| 318 | return rc;
|
---|
| 319 |
|
---|
| 320 | msg.data = data;
|
---|
| 321 | msg.data_size = size;
|
---|
[2f19103] | 322 | rc = udp_assoc_send(cassoc->assoc, dest, &msg);
|
---|
[fab2746] | 323 | if (rc != EOK)
|
---|
| 324 | return rc;
|
---|
| 325 |
|
---|
| 326 | return EOK;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[b10460a] | 329 | /** Create callback session.
|
---|
| 330 | *
|
---|
| 331 | * Handle client request to create callback session.
|
---|
| 332 | *
|
---|
[cbb37b6] | 333 | * @param client UDP client
|
---|
[b752a31] | 334 | * @param icall_handle Async request call handle
|
---|
[cbb37b6] | 335 | * @param icall Async request data
|
---|
[b10460a] | 336 | */
|
---|
[cbb37b6] | 337 | static void
|
---|
| 338 | udp_callback_create_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 339 | ipc_call_t *icall)
|
---|
| 340 | {
|
---|
| 341 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_callback_create_srv()");
|
---|
| 342 |
|
---|
| 343 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 344 | if (sess == NULL) {
|
---|
[a46e56b] | 345 | async_answer_0(icall_handle, ENOMEM);
|
---|
[fab2746] | 346 | return;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | client->sess = sess;
|
---|
[a46e56b] | 350 | async_answer_0(icall_handle, EOK);
|
---|
[fab2746] | 351 | }
|
---|
| 352 |
|
---|
[b10460a] | 353 | /** Create association.
|
---|
| 354 | *
|
---|
| 355 | * Handle client request to create association.
|
---|
| 356 | *
|
---|
[cbb37b6] | 357 | * @param client UDP client
|
---|
| 358 | * @param icall_handle Async request call handle
|
---|
| 359 | * @param icall Async request data
|
---|
[b10460a] | 360 | */
|
---|
[18b6a88] | 361 | static void
|
---|
| 362 | udp_assoc_create_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 363 | ipc_call_t *icall)
|
---|
| 364 | {
|
---|
[a46e56b] | 365 | cap_call_handle_t chandle;
|
---|
[fab2746] | 366 | size_t size;
|
---|
| 367 | inet_ep2_t epp;
|
---|
| 368 | sysarg_t assoc_id;
|
---|
[b7fd2a0] | 369 | errno_t rc;
|
---|
[fab2746] | 370 |
|
---|
| 371 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_create_srv()");
|
---|
| 372 |
|
---|
[a46e56b] | 373 | if (!async_data_write_receive(&chandle, &size)) {
|
---|
| 374 | async_answer_0(chandle, EREFUSED);
|
---|
| 375 | async_answer_0(icall_handle, EREFUSED);
|
---|
[fab2746] | 376 | return;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | if (size != sizeof(inet_ep2_t)) {
|
---|
[a46e56b] | 380 | async_answer_0(chandle, EINVAL);
|
---|
| 381 | async_answer_0(icall_handle, EINVAL);
|
---|
[fab2746] | 382 | return;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
[a46e56b] | 385 | rc = async_data_write_finalize(chandle, &epp, size);
|
---|
[fab2746] | 386 | if (rc != EOK) {
|
---|
[a46e56b] | 387 | async_answer_0(chandle, rc);
|
---|
| 388 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 389 | return;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | rc = udp_assoc_create_impl(client, &epp, &assoc_id);
|
---|
| 393 | if (rc != EOK) {
|
---|
[a46e56b] | 394 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 395 | return;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[a46e56b] | 398 | async_answer_1(icall_handle, EOK, assoc_id);
|
---|
[fab2746] | 399 | }
|
---|
| 400 |
|
---|
[b10460a] | 401 | /** Destroy association.
|
---|
| 402 | *
|
---|
| 403 | * Handle client request to destroy association.
|
---|
| 404 | *
|
---|
[cbb37b6] | 405 | * @param client UDP client
|
---|
| 406 | * @param icall_handle Async request call handle
|
---|
| 407 | * @param icall Async request data
|
---|
[b10460a] | 408 | */
|
---|
[cbb37b6] | 409 | static void
|
---|
| 410 | udp_assoc_destroy_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 411 | ipc_call_t *icall)
|
---|
| 412 | {
|
---|
| 413 | sysarg_t assoc_id;
|
---|
[b7fd2a0] | 414 | errno_t rc;
|
---|
[fab2746] | 415 |
|
---|
| 416 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_destroy_srv()");
|
---|
| 417 |
|
---|
| 418 | assoc_id = IPC_GET_ARG1(*icall);
|
---|
| 419 | rc = udp_assoc_destroy_impl(client, assoc_id);
|
---|
[a46e56b] | 420 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 421 | }
|
---|
| 422 |
|
---|
[58e8646] | 423 | /** Set association with no local address.
|
---|
| 424 | *
|
---|
| 425 | * Handle client request to set no local address flag.
|
---|
| 426 | *
|
---|
[cbb37b6] | 427 | * @param client UDP client
|
---|
| 428 | * @param icall_handle Async request call handle
|
---|
| 429 | * @param icall Async request data
|
---|
[58e8646] | 430 | */
|
---|
[cbb37b6] | 431 | static void
|
---|
| 432 | udp_assoc_set_nolocal_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[58e8646] | 433 | ipc_call_t *icall)
|
---|
| 434 | {
|
---|
| 435 | sysarg_t assoc_id;
|
---|
[b7fd2a0] | 436 | errno_t rc;
|
---|
[58e8646] | 437 |
|
---|
| 438 | log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_set_nolocal_srv()");
|
---|
| 439 |
|
---|
| 440 | assoc_id = IPC_GET_ARG1(*icall);
|
---|
| 441 | rc = udp_assoc_set_nolocal_impl(client, assoc_id);
|
---|
[a46e56b] | 442 | async_answer_0(icall_handle, rc);
|
---|
[58e8646] | 443 | }
|
---|
| 444 |
|
---|
[b10460a] | 445 | /** Send message via association.
|
---|
| 446 | *
|
---|
| 447 | * Handle client request to send message.
|
---|
| 448 | *
|
---|
[cbb37b6] | 449 | * @param client UDP client
|
---|
[b752a31] | 450 | * @param icall_handle Async request call handle
|
---|
[cbb37b6] | 451 | * @param icall Async request data
|
---|
[b10460a] | 452 | */
|
---|
[cbb37b6] | 453 | static void
|
---|
| 454 | udp_assoc_send_msg_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 455 | ipc_call_t *icall)
|
---|
| 456 | {
|
---|
[a46e56b] | 457 | cap_call_handle_t chandle;
|
---|
[fab2746] | 458 | size_t size;
|
---|
| 459 | inet_ep_t dest;
|
---|
| 460 | sysarg_t assoc_id;
|
---|
| 461 | void *data;
|
---|
[b7fd2a0] | 462 | errno_t rc;
|
---|
[fab2746] | 463 |
|
---|
| 464 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send_msg_srv()");
|
---|
| 465 |
|
---|
| 466 | /* Receive dest */
|
---|
| 467 |
|
---|
[a46e56b] | 468 | if (!async_data_write_receive(&chandle, &size)) {
|
---|
| 469 | async_answer_0(chandle, EREFUSED);
|
---|
| 470 | async_answer_0(icall_handle, EREFUSED);
|
---|
[fab2746] | 471 | return;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | if (size != sizeof(inet_ep_t)) {
|
---|
[a46e56b] | 475 | async_answer_0(chandle, EINVAL);
|
---|
| 476 | async_answer_0(icall_handle, EINVAL);
|
---|
[fab2746] | 477 | return;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[a46e56b] | 480 | rc = async_data_write_finalize(chandle, &dest, size);
|
---|
[fab2746] | 481 | if (rc != EOK) {
|
---|
[a46e56b] | 482 | async_answer_0(chandle, rc);
|
---|
| 483 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 484 | return;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | /* Receive message data */
|
---|
| 488 |
|
---|
[a46e56b] | 489 | if (!async_data_write_receive(&chandle, &size)) {
|
---|
| 490 | async_answer_0(chandle, EREFUSED);
|
---|
| 491 | async_answer_0(icall_handle, EREFUSED);
|
---|
[fab2746] | 492 | return;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | if (size > MAX_MSG_SIZE) {
|
---|
[a46e56b] | 496 | async_answer_0(chandle, EINVAL);
|
---|
| 497 | async_answer_0(icall_handle, EINVAL);
|
---|
[fab2746] | 498 | return;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | data = malloc(size);
|
---|
| 502 | if (data == NULL) {
|
---|
[a46e56b] | 503 | async_answer_0(chandle, ENOMEM);
|
---|
| 504 | async_answer_0(icall_handle, ENOMEM);
|
---|
[fab2746] | 505 | }
|
---|
| 506 |
|
---|
[a46e56b] | 507 | rc = async_data_write_finalize(chandle, data, size);
|
---|
[fab2746] | 508 | if (rc != EOK) {
|
---|
[a46e56b] | 509 | async_answer_0(chandle, rc);
|
---|
| 510 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 511 | free(data);
|
---|
| 512 | return;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | assoc_id = IPC_GET_ARG1(*icall);
|
---|
| 516 |
|
---|
| 517 | rc = udp_assoc_send_msg_impl(client, assoc_id, &dest, data, size);
|
---|
| 518 | if (rc != EOK) {
|
---|
[a46e56b] | 519 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 520 | free(data);
|
---|
| 521 | return;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[a46e56b] | 524 | async_answer_0(icall_handle, EOK);
|
---|
[fab2746] | 525 | free(data);
|
---|
| 526 | }
|
---|
| 527 |
|
---|
[b10460a] | 528 | /** Get next received message.
|
---|
| 529 | *
|
---|
| 530 | * @param client UDP Client
|
---|
| 531 | * @return Pointer to queue entry for next received message
|
---|
| 532 | */
|
---|
[fab2746] | 533 | static udp_crcv_queue_entry_t *udp_rmsg_get_next(udp_client_t *client)
|
---|
| 534 | {
|
---|
| 535 | link_t *link;
|
---|
| 536 |
|
---|
| 537 | link = list_first(&client->crcv_queue);
|
---|
| 538 | if (link == NULL)
|
---|
| 539 | return NULL;
|
---|
| 540 |
|
---|
| 541 | return list_get_instance(link, udp_crcv_queue_entry_t, link);
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[b10460a] | 544 | /** Get info on first received message.
|
---|
| 545 | *
|
---|
| 546 | * Handle client request to get information on received message.
|
---|
| 547 | *
|
---|
[cbb37b6] | 548 | * @param client UDP client
|
---|
| 549 | * @param icall_handle Async request call handle
|
---|
| 550 | * @param icall Async request data
|
---|
[b10460a] | 551 | */
|
---|
[cbb37b6] | 552 | static void
|
---|
| 553 | udp_rmsg_info_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 554 | ipc_call_t *icall)
|
---|
| 555 | {
|
---|
[a46e56b] | 556 | cap_call_handle_t chandle;
|
---|
[fab2746] | 557 | size_t size;
|
---|
| 558 | udp_crcv_queue_entry_t *enext;
|
---|
| 559 | sysarg_t assoc_id;
|
---|
[b7fd2a0] | 560 | errno_t rc;
|
---|
[fab2746] | 561 |
|
---|
| 562 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_rmsg_info_srv()");
|
---|
| 563 | enext = udp_rmsg_get_next(client);
|
---|
| 564 |
|
---|
[a46e56b] | 565 | if (!async_data_read_receive(&chandle, &size)) {
|
---|
| 566 | async_answer_0(chandle, EREFUSED);
|
---|
| 567 | async_answer_0(icall_handle, EREFUSED);
|
---|
[fab2746] | 568 | return;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | if (enext == NULL) {
|
---|
[a46e56b] | 572 | async_answer_0(chandle, ENOENT);
|
---|
| 573 | async_answer_0(icall_handle, ENOENT);
|
---|
[fab2746] | 574 | return;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[a46e56b] | 577 | rc = async_data_read_finalize(chandle, &enext->epp.remote,
|
---|
[bde5c04] | 578 | max(size, (size_t)sizeof(inet_ep_t)));
|
---|
[fab2746] | 579 | if (rc != EOK) {
|
---|
[a46e56b] | 580 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 581 | return;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | assoc_id = enext->cassoc->id;
|
---|
| 585 | size = enext->msg->data_size;
|
---|
| 586 |
|
---|
| 587 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_rmsg_info_srv(): assoc_id=%zu, "
|
---|
| 588 | "size=%zu", assoc_id, size);
|
---|
[a46e56b] | 589 | async_answer_2(icall_handle, EOK, assoc_id, size);
|
---|
[fab2746] | 590 | }
|
---|
| 591 |
|
---|
[b10460a] | 592 | /** Read data from first received message.
|
---|
| 593 | *
|
---|
| 594 | * Handle client request to read data from first received message.
|
---|
| 595 | *
|
---|
[cbb37b6] | 596 | * @param client UDP client
|
---|
| 597 | * @param icall_handle Async request call handle
|
---|
| 598 | * @param icall Async request data
|
---|
[b10460a] | 599 | */
|
---|
[cbb37b6] | 600 | static void
|
---|
| 601 | udp_rmsg_read_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 602 | ipc_call_t *icall)
|
---|
| 603 | {
|
---|
[a46e56b] | 604 | cap_call_handle_t chandle;
|
---|
[bde5c04] | 605 | size_t msg_size;
|
---|
[fab2746] | 606 | udp_crcv_queue_entry_t *enext;
|
---|
| 607 | void *data;
|
---|
| 608 | size_t size;
|
---|
[bde5c04] | 609 | size_t off;
|
---|
[b7fd2a0] | 610 | errno_t rc;
|
---|
[fab2746] | 611 |
|
---|
| 612 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_rmsg_read_srv()");
|
---|
| 613 | off = IPC_GET_ARG1(*icall);
|
---|
| 614 |
|
---|
| 615 | enext = udp_rmsg_get_next(client);
|
---|
| 616 |
|
---|
[a46e56b] | 617 | if (!async_data_read_receive(&chandle, &size)) {
|
---|
| 618 | async_answer_0(chandle, EREFUSED);
|
---|
| 619 | async_answer_0(icall_handle, EREFUSED);
|
---|
[fab2746] | 620 | return;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | if (enext == NULL) {
|
---|
[a46e56b] | 624 | async_answer_0(chandle, ENOENT);
|
---|
| 625 | async_answer_0(icall_handle, ENOENT);
|
---|
[fab2746] | 626 | return;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | data = enext->msg->data + off;
|
---|
| 630 | msg_size = enext->msg->data_size;
|
---|
| 631 |
|
---|
[bde5c04] | 632 | if (off > msg_size) {
|
---|
[a46e56b] | 633 | async_answer_0(chandle, EINVAL);
|
---|
| 634 | async_answer_0(icall_handle, EINVAL);
|
---|
[bde5c04] | 635 | return;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
[a46e56b] | 638 | rc = async_data_read_finalize(chandle, data, min(msg_size - off, size));
|
---|
[fab2746] | 639 | if (rc != EOK) {
|
---|
[a46e56b] | 640 | async_answer_0(icall_handle, rc);
|
---|
[fab2746] | 641 | return;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[a46e56b] | 644 | async_answer_0(icall_handle, EOK);
|
---|
[fab2746] | 645 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_rmsg_read_srv(): OK");
|
---|
| 646 | }
|
---|
| 647 |
|
---|
[b10460a] | 648 | /** Discard first received message.
|
---|
| 649 | *
|
---|
| 650 | * Handle client request to discard first received message, advancing
|
---|
| 651 | * to the next one.
|
---|
| 652 | *
|
---|
[cbb37b6] | 653 | * @param client UDP client
|
---|
| 654 | * @param icall_handle Async request call handle
|
---|
| 655 | * @param icall Async request data
|
---|
[b10460a] | 656 | */
|
---|
[cbb37b6] | 657 | static void
|
---|
| 658 | udp_rmsg_discard_srv(udp_client_t *client, cap_call_handle_t icall_handle,
|
---|
[fab2746] | 659 | ipc_call_t *icall)
|
---|
| 660 | {
|
---|
| 661 | udp_crcv_queue_entry_t *enext;
|
---|
| 662 |
|
---|
| 663 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_rmsg_discard_srv()");
|
---|
| 664 |
|
---|
| 665 | enext = udp_rmsg_get_next(client);
|
---|
| 666 | if (enext == NULL) {
|
---|
[99ea91b2] | 667 | log_msg(LOG_DEFAULT, LVL_DEBUG, "usg_rmsg_discard_srv: enext==NULL");
|
---|
[a46e56b] | 668 | async_answer_0(icall_handle, ENOENT);
|
---|
[fab2746] | 669 | return;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | list_remove(&enext->link);
|
---|
| 673 | udp_msg_delete(enext->msg);
|
---|
[99ea91b2] | 674 | free(enext);
|
---|
[a46e56b] | 675 | async_answer_0(icall_handle, EOK);
|
---|
[fab2746] | 676 | }
|
---|
| 677 |
|
---|
[b10460a] | 678 | /** Handle UDP client connection.
|
---|
| 679 | *
|
---|
[b752a31] | 680 | * @param icall_handle Connect call handle
|
---|
[eed4139] | 681 | * @param icall Connect call data
|
---|
| 682 | * @param arg Connection argument
|
---|
[b10460a] | 683 | */
|
---|
[eed4139] | 684 | static void udp_client_conn(cap_call_handle_t icall_handle, ipc_call_t *icall,
|
---|
| 685 | void *arg)
|
---|
[fab2746] | 686 | {
|
---|
| 687 | udp_client_t client;
|
---|
[be12474] | 688 | unsigned long n;
|
---|
[fab2746] | 689 |
|
---|
| 690 | /* Accept the connection */
|
---|
[a46e56b] | 691 | async_answer_0(icall_handle, EOK);
|
---|
[fab2746] | 692 |
|
---|
| 693 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_client_conn()");
|
---|
| 694 |
|
---|
[1f2b07a] | 695 | client.sess = NULL;
|
---|
[fab2746] | 696 | list_initialize(&client.cassoc);
|
---|
| 697 | list_initialize(&client.crcv_queue);
|
---|
| 698 |
|
---|
| 699 | while (true) {
|
---|
[99ea91b2] | 700 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_client_conn: wait req");
|
---|
[fab2746] | 701 | ipc_call_t call;
|
---|
[a46e56b] | 702 | cap_call_handle_t chandle = async_get_call(&call);
|
---|
[fab2746] | 703 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 704 |
|
---|
[99ea91b2] | 705 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_client_conn: method=%d",
|
---|
[fab2746] | 706 | (int)method);
|
---|
| 707 | if (!method) {
|
---|
| 708 | /* The other side has hung up */
|
---|
[a46e56b] | 709 | async_answer_0(chandle, EOK);
|
---|
[309469de] | 710 | break;
|
---|
[fab2746] | 711 | }
|
---|
| 712 |
|
---|
| 713 | switch (method) {
|
---|
| 714 | case UDP_CALLBACK_CREATE:
|
---|
[a46e56b] | 715 | udp_callback_create_srv(&client, chandle, &call);
|
---|
[fab2746] | 716 | break;
|
---|
| 717 | case UDP_ASSOC_CREATE:
|
---|
[a46e56b] | 718 | udp_assoc_create_srv(&client, chandle, &call);
|
---|
[fab2746] | 719 | break;
|
---|
| 720 | case UDP_ASSOC_DESTROY:
|
---|
[a46e56b] | 721 | udp_assoc_destroy_srv(&client, chandle, &call);
|
---|
[fab2746] | 722 | break;
|
---|
[58e8646] | 723 | case UDP_ASSOC_SET_NOLOCAL:
|
---|
[a46e56b] | 724 | udp_assoc_set_nolocal_srv(&client, chandle, &call);
|
---|
[58e8646] | 725 | break;
|
---|
[fab2746] | 726 | case UDP_ASSOC_SEND_MSG:
|
---|
[a46e56b] | 727 | udp_assoc_send_msg_srv(&client, chandle, &call);
|
---|
[fab2746] | 728 | break;
|
---|
| 729 | case UDP_RMSG_INFO:
|
---|
[a46e56b] | 730 | udp_rmsg_info_srv(&client, chandle, &call);
|
---|
[fab2746] | 731 | break;
|
---|
| 732 | case UDP_RMSG_READ:
|
---|
[a46e56b] | 733 | udp_rmsg_read_srv(&client, chandle, &call);
|
---|
[fab2746] | 734 | break;
|
---|
| 735 | case UDP_RMSG_DISCARD:
|
---|
[a46e56b] | 736 | udp_rmsg_discard_srv(&client, chandle, &call);
|
---|
[fab2746] | 737 | break;
|
---|
| 738 | default:
|
---|
[a46e56b] | 739 | async_answer_0(chandle, ENOTSUP);
|
---|
[fab2746] | 740 | break;
|
---|
| 741 | }
|
---|
| 742 | }
|
---|
[309469de] | 743 |
|
---|
| 744 | log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_client_conn: terminated");
|
---|
| 745 |
|
---|
| 746 | n = list_count(&client.cassoc);
|
---|
| 747 | if (n != 0) {
|
---|
| 748 | log_msg(LOG_DEFAULT, LVL_WARN, "udp_client_conn: "
|
---|
[1d03e86] | 749 | "Client with %lu active associations closed session.", n);
|
---|
[309469de] | 750 | /* XXX Clean up */
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | /* XXX Clean up client receive queue */
|
---|
[1f2b07a] | 754 |
|
---|
| 755 | if (client.sess != NULL)
|
---|
| 756 | async_hangup(client.sess);
|
---|
[fab2746] | 757 | }
|
---|
| 758 |
|
---|
[b10460a] | 759 | /** Initialize UDP service.
|
---|
| 760 | *
|
---|
[cde999a] | 761 | * @return EOK on success or an error code.
|
---|
[b10460a] | 762 | */
|
---|
[b7fd2a0] | 763 | errno_t udp_service_init(void)
|
---|
[fab2746] | 764 | {
|
---|
[b7fd2a0] | 765 | errno_t rc;
|
---|
[fab2746] | 766 | service_id_t sid;
|
---|
| 767 |
|
---|
[b688fd8] | 768 | async_set_fallback_port_handler(udp_client_conn, NULL);
|
---|
[fab2746] | 769 |
|
---|
| 770 | rc = loc_server_register(NAME);
|
---|
| 771 | if (rc != EOK) {
|
---|
| 772 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
|
---|
| 773 | return EIO;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | rc = loc_service_register(SERVICE_NAME_UDP, &sid);
|
---|
| 777 | if (rc != EOK) {
|
---|
| 778 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service.");
|
---|
| 779 | return EIO;
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | return EOK;
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | /**
|
---|
| 786 | * @}
|
---|
| 787 | */
|
---|