source: mainline/uspace/lib/inet/src/udp.c@ 241ab7e

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 241ab7e was ec7902d, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Move TCP/IP library support out of libc to separate library

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[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 libc
30 * @{
31 */
32/** @file UDP API
33 */
34
35#include <errno.h>
36#include <inet/endpoint.h>
37#include <inet/udp.h>
38#include <ipc/services.h>
39#include <ipc/udp.h>
40#include <loc.h>
41#include <stdlib.h>
42
[984a9ba]43static void udp_cb_conn(ipc_call_t *, void *);
[fab2746]44
[b10460a]45/** Create callback connection from UDP service.
46 *
47 * @param udp UDP service
[cde999a]48 * @return EOK on success or an error code
[b10460a]49 */
[b7fd2a0]50static errno_t udp_callback_create(udp_t *udp)
[fab2746]51{
52 async_exch_t *exch = async_exchange_begin(udp->sess);
53
54 aid_t req = async_send_0(exch, UDP_CALLBACK_CREATE, NULL);
[a35b458]55
[78bb04b]56 port_id_t port;
[b7fd2a0]57 errno_t rc = async_create_callback_port(exch, INTERFACE_UDP_CB, 0, 0,
[78bb04b]58 udp_cb_conn, udp, &port);
[a35b458]59
[fab2746]60 async_exchange_end(exch);
61
62 if (rc != EOK)
63 return rc;
64
[b7fd2a0]65 errno_t retval;
[fab2746]66 async_wait_for(req, &retval);
67
68 return retval;
69}
70
[b10460a]71/** Create UDP client instance.
72 *
73 * @param rudp Place to store pointer to new UDP client
74 * @return EOK on success, ENOMEM if out of memory, EIO if service
75 * cannot be contacted
76 */
[b7fd2a0]77errno_t udp_create(udp_t **rudp)
[fab2746]78{
79 udp_t *udp;
80 service_id_t udp_svcid;
[b7fd2a0]81 errno_t rc;
[fab2746]82
83 udp = calloc(1, sizeof(udp_t));
84 if (udp == NULL) {
85 rc = ENOMEM;
86 goto error;
87 }
88
89 list_initialize(&udp->assoc);
[1f2b07a]90 fibril_mutex_initialize(&udp->lock);
91 fibril_condvar_initialize(&udp->cv);
[fab2746]92
93 rc = loc_service_get_id(SERVICE_NAME_UDP, &udp_svcid,
94 IPC_FLAG_BLOCKING);
95 if (rc != EOK) {
96 rc = EIO;
97 goto error;
98 }
99
[f9b2cb4c]100 udp->sess = loc_service_connect(udp_svcid, INTERFACE_UDP,
[fab2746]101 IPC_FLAG_BLOCKING);
102 if (udp->sess == NULL) {
103 rc = EIO;
104 goto error;
105 }
106
107 rc = udp_callback_create(udp);
108 if (rc != EOK) {
109 rc = EIO;
110 goto error;
111 }
112
113 *rudp = udp;
114 return EOK;
115error:
116 free(udp);
117 return rc;
118}
119
[b10460a]120/** Destroy UDP client instance.
121 *
122 * @param udp UDP client
123 */
[fab2746]124void udp_destroy(udp_t *udp)
125{
126 if (udp == NULL)
127 return;
128
129 async_hangup(udp->sess);
[1f2b07a]130
131 fibril_mutex_lock(&udp->lock);
132 while (!udp->cb_done)
133 fibril_condvar_wait(&udp->cv, &udp->lock);
134 fibril_mutex_unlock(&udp->lock);
135
[fab2746]136 free(udp);
137}
138
[b10460a]139/** Create new UDP association.
140 *
141 * Create a UDP association that allows sending and receiving messages.
142 *
143 * @a epp may specify remote address and port, in which case only messages
144 * from that remote endpoint will be received. Also, that remote endpoint
145 * is used as default when @c NULL is passed as destination to
146 * udp_assoc_send_msg.
147 *
148 * @a epp may specify a local link or address. If it does not, the association
149 * will listen on all local links/addresses. If @a epp does not specify
150 * a local port number, a free dynamic port number will be allocated.
151 *
152 * The caller is informed about incoming data by invoking @a cb->recv_msg
153 *
154 * @param udp UDP client
155 * @param epp Internet endpoint pair
156 * @param cb Callbacks
157 * @param arg Argument to callbacks
158 * @param rassoc Place to store pointer to new association
159 *
[cde999a]160 * @return EOK on success or an error code.
[b10460a]161 */
[b7fd2a0]162errno_t udp_assoc_create(udp_t *udp, inet_ep2_t *epp, udp_cb_t *cb, void *arg,
[fab2746]163 udp_assoc_t **rassoc)
164{
165 async_exch_t *exch;
166 udp_assoc_t *assoc;
167 ipc_call_t answer;
168
169 assoc = calloc(1, sizeof(udp_assoc_t));
170 if (assoc == NULL)
171 return ENOMEM;
172
173 exch = async_exchange_begin(udp->sess);
174 aid_t req = async_send_0(exch, UDP_ASSOC_CREATE, &answer);
[b7fd2a0]175 errno_t rc = async_data_write_start(exch, (void *)epp,
[fab2746]176 sizeof(inet_ep2_t));
177 async_exchange_end(exch);
178
179 if (rc != EOK) {
[b7fd2a0]180 errno_t rc_orig;
[fab2746]181 async_wait_for(req, &rc_orig);
182 if (rc_orig != EOK)
183 rc = rc_orig;
184 goto error;
185 }
186
187 async_wait_for(req, &rc);
188 if (rc != EOK)
189 goto error;
190
191 assoc->udp = udp;
[fafb8e5]192 assoc->id = ipc_get_arg1(&answer);
[fab2746]193 assoc->cb = cb;
194 assoc->cb_arg = arg;
195
196 list_append(&assoc->ludp, &udp->assoc);
197 *rassoc = assoc;
198
199 return EOK;
200error:
201 free(assoc);
[b7fd2a0]202 return (errno_t) rc;
[fab2746]203}
204
[b10460a]205/** Destroy UDP association.
206 *
207 * Destroy UDP association. The caller should destroy all associations
208 * he created before destroying the UDP client and before terminating.
209 *
210 * @param assoc UDP association
211 */
[fab2746]212void udp_assoc_destroy(udp_assoc_t *assoc)
213{
214 async_exch_t *exch;
215
216 if (assoc == NULL)
217 return;
218
219 list_remove(&assoc->ludp);
220
221 exch = async_exchange_begin(assoc->udp->sess);
[b7fd2a0]222 errno_t rc = async_req_1_0(exch, UDP_ASSOC_DESTROY, assoc->id);
[fab2746]223 async_exchange_end(exch);
224
225 free(assoc);
226 (void) rc;
227}
228
[58e8646]229/** Set UDP association sending messages with no local address
230 *
231 * @param assoc Association
232 * @param flags Flags
233 */
[b7fd2a0]234errno_t udp_assoc_set_nolocal(udp_assoc_t *assoc)
[58e8646]235{
236 async_exch_t *exch;
237
238 exch = async_exchange_begin(assoc->udp->sess);
[b7fd2a0]239 errno_t rc = async_req_1_0(exch, UDP_ASSOC_SET_NOLOCAL, assoc->id);
[58e8646]240 async_exchange_end(exch);
241
242 return rc;
243}
244
[b10460a]245/** Send message via UDP association.
246 *
247 * @param assoc Association
248 * @param dest Destination endpoint or @c NULL to use association's remote ep.
249 * @param data Message data
250 * @param bytes Message size in bytes
251 *
[cde999a]252 * @return EOK on success or an error code
[b10460a]253 */
[b7fd2a0]254errno_t udp_assoc_send_msg(udp_assoc_t *assoc, inet_ep_t *dest, void *data,
[fab2746]255 size_t bytes)
256{
257 async_exch_t *exch;
258
259 exch = async_exchange_begin(assoc->udp->sess);
260 aid_t req = async_send_1(exch, UDP_ASSOC_SEND_MSG, assoc->id, NULL);
261
[b7fd2a0]262 errno_t rc = async_data_write_start(exch, (void *)dest,
[fab2746]263 sizeof(inet_ep_t));
264 if (rc != EOK) {
265 async_exchange_end(exch);
266 async_forget(req);
267 return rc;
268 }
269
270 rc = async_data_write_start(exch, data, bytes);
271 if (rc != EOK) {
272 async_forget(req);
273 return rc;
274 }
275
276 async_exchange_end(exch);
277
278 if (rc != EOK) {
279 async_forget(req);
280 return rc;
281 }
282
283 async_wait_for(req, &rc);
284 return rc;
285}
286
[b10460a]287/** Get the user/callback argument for an association.
288 *
289 * @param assoc UDP association
290 * @return User argument associated with association
291 */
[fab2746]292void *udp_assoc_userptr(udp_assoc_t *assoc)
293{
294 return assoc->cb_arg;
295}
296
[b10460a]297/** Get size of received message in bytes.
298 *
299 * Assuming jumbo messages can be received, the caller first needs to determine
300 * the size of the received message by calling this function, then they can
301 * read the message piece-wise using udp_rmsg_read().
302 *
303 * @param rmsg Received message
304 * @return Size of received message in bytes
305 */
[fab2746]306size_t udp_rmsg_size(udp_rmsg_t *rmsg)
307{
308 return rmsg->size;
309}
310
[b10460a]311/** Read part of received message.
312 *
313 * @param rmsg Received message
314 * @param off Start offset
315 * @param buf Buffer for storing data
316 * @param bsize Buffer size
317 *
[cde999a]318 * @return EOK on success or an error code.
[b10460a]319 */
[b7fd2a0]320errno_t udp_rmsg_read(udp_rmsg_t *rmsg, size_t off, void *buf, size_t bsize)
[fab2746]321{
322 async_exch_t *exch;
323 ipc_call_t answer;
324
325 exch = async_exchange_begin(rmsg->udp->sess);
326 aid_t req = async_send_1(exch, UDP_RMSG_READ, off, &answer);
[b7fd2a0]327 errno_t rc = async_data_read_start(exch, buf, bsize);
[fab2746]328 async_exchange_end(exch);
329
330 if (rc != EOK) {
331 async_forget(req);
332 return rc;
333 }
334
[b7fd2a0]335 errno_t retval;
[fab2746]336 async_wait_for(req, &retval);
337 if (retval != EOK) {
338 return retval;
339 }
340
341 return EOK;
342}
343
[b10460a]344/** Get remote endpoint of received message.
345 *
346 * Place the remote endpoint (the one from which the message was supposedly
347 * sent) to @a ep.
348 *
349 * @param rmsg Received message
350 * @param ep Place to store remote endpoint
351 */
[fab2746]352void udp_rmsg_remote_ep(udp_rmsg_t *rmsg, inet_ep_t *ep)
353{
354 *ep = rmsg->remote_ep;
355}
356
[b10460a]357/** Get type of received ICMP error message.
358 *
359 * @param rerr Received error message
360 * @return Error message type
361 */
[fab2746]362uint8_t udp_rerr_type(udp_rerr_t *rerr)
363{
364 return 0;
365}
366
[b10460a]367/** Get code of received ICMP error message.
368 *
369 * @param rerr Received error message
370 * @return Error message code
371 */
[fab2746]372uint8_t udp_rerr_code(udp_rerr_t *rerr)
373{
374 return 0;
375}
376
[b10460a]377/** Get information about the next received message from UDP service.
378 *
379 * @param udp UDP client
380 * @param rmsg Place to store message information
381 *
[cde999a]382 * @return EOK on success or an error code
[b10460a]383 */
[b7fd2a0]384static errno_t udp_rmsg_info(udp_t *udp, udp_rmsg_t *rmsg)
[fab2746]385{
386 async_exch_t *exch;
387 inet_ep_t ep;
388 ipc_call_t answer;
389
390 exch = async_exchange_begin(udp->sess);
391 aid_t req = async_send_0(exch, UDP_RMSG_INFO, &answer);
[b7fd2a0]392 errno_t rc = async_data_read_start(exch, &ep, sizeof(inet_ep_t));
[fab2746]393 async_exchange_end(exch);
394
395 if (rc != EOK) {
396 async_forget(req);
397 return rc;
398 }
399
[b7fd2a0]400 errno_t retval;
[fab2746]401 async_wait_for(req, &retval);
402 if (retval != EOK)
403 return retval;
404
405 rmsg->udp = udp;
[fafb8e5]406 rmsg->assoc_id = ipc_get_arg1(&answer);
407 rmsg->size = ipc_get_arg2(&answer);
[fab2746]408 rmsg->remote_ep = ep;
409 return EOK;
410}
411
[b10460a]412/** Discard next received message in UDP service.
413 *
414 * @param udp UDP client
[cde999a]415 * @return EOK on success or an error code
[b10460a]416 */
[b7fd2a0]417static errno_t udp_rmsg_discard(udp_t *udp)
[fab2746]418{
419 async_exch_t *exch;
420
421 exch = async_exchange_begin(udp->sess);
[b7fd2a0]422 errno_t rc = async_req_0_0(exch, UDP_RMSG_DISCARD);
[fab2746]423 async_exchange_end(exch);
424
425 return rc;
426}
427
[b10460a]428/** Get association based on its ID.
429 *
430 * @param udp UDP client
431 * @param id Association ID
432 * @param rassoc Place to store pointer to association
433 *
434 * @return EOK on success, EINVAL if no association with the given ID exists
435 */
[b7fd2a0]436static errno_t udp_assoc_get(udp_t *udp, sysarg_t id, udp_assoc_t **rassoc)
[fab2746]437{
438 list_foreach(udp->assoc, ludp, udp_assoc_t, assoc) {
439 if (assoc->id == id) {
440 *rassoc = assoc;
441 return EOK;
442 }
443 }
444
445 return EINVAL;
446}
447
[b10460a]448/** Handle 'data' event, i.e. some message(s) arrived.
449 *
450 * For each received message, get information about it, call @c recv_msg
451 * callback and discard it.
452 *
[984a9ba]453 * @param udp UDP client
[b10460a]454 * @param icall IPC message
[984a9ba]455 *
[b10460a]456 */
[984a9ba]457static void udp_ev_data(udp_t *udp, ipc_call_t *icall)
[fab2746]458{
459 udp_rmsg_t rmsg;
460 udp_assoc_t *assoc;
[b7fd2a0]461 errno_t rc;
[fab2746]462
463 while (true) {
464 rc = udp_rmsg_info(udp, &rmsg);
465 if (rc != EOK) {
466 break;
467 }
468
469 rc = udp_assoc_get(udp, rmsg.assoc_id, &assoc);
470 if (rc != EOK) {
471 continue;
472 }
473
474 if (assoc->cb != NULL && assoc->cb->recv_msg != NULL)
475 assoc->cb->recv_msg(assoc, &rmsg);
476
477 rc = udp_rmsg_discard(udp);
478 if (rc != EOK) {
479 break;
480 }
481 }
482
[984a9ba]483 async_answer_0(icall, EOK);
[fab2746]484}
485
[b10460a]486/** UDP service callback connection.
487 *
488 * @param icall Connect message
[984a9ba]489 * @param arg Argument, UDP client
490 *
[b10460a]491 */
[984a9ba]492static void udp_cb_conn(ipc_call_t *icall, void *arg)
[fab2746]493{
494 udp_t *udp = (udp_t *)arg;
495
496 while (true) {
497 ipc_call_t call;
[984a9ba]498 async_get_call(&call);
[fab2746]499
[fafb8e5]500 if (!ipc_get_imethod(&call)) {
[1f2b07a]501 /* Hangup */
[889cdb1]502 async_answer_0(&call, EOK);
[1f2b07a]503 goto out;
[fab2746]504 }
505
[fafb8e5]506 switch (ipc_get_imethod(&call)) {
[fab2746]507 case UDP_EV_DATA:
[984a9ba]508 udp_ev_data(udp, &call);
[fab2746]509 break;
510 default:
[984a9ba]511 async_answer_0(&call, ENOTSUP);
[fab2746]512 break;
513 }
514 }
[984a9ba]515
[1f2b07a]516out:
517 fibril_mutex_lock(&udp->lock);
518 udp->cb_done = true;
519 fibril_mutex_unlock(&udp->lock);
520 fibril_condvar_broadcast(&udp->cv);
[fab2746]521}
522
523/** @}
524 */
Note: See TracBrowser for help on using the repository browser.