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 |
|
---|
43 | static void udp_cb_conn(ipc_callid_t, ipc_call_t *, void *);
|
---|
44 |
|
---|
45 | /** Create callback connection from UDP service.
|
---|
46 | *
|
---|
47 | * @param udp UDP service
|
---|
48 | * @return EOK on success or negative error code
|
---|
49 | */
|
---|
50 | static int udp_callback_create(udp_t *udp)
|
---|
51 | {
|
---|
52 | async_exch_t *exch = async_exchange_begin(udp->sess);
|
---|
53 |
|
---|
54 | aid_t req = async_send_0(exch, UDP_CALLBACK_CREATE, NULL);
|
---|
55 |
|
---|
56 | port_id_t port;
|
---|
57 | int rc = async_create_callback_port(exch, INTERFACE_UDP_CB, 0, 0,
|
---|
58 | udp_cb_conn, udp, &port);
|
---|
59 |
|
---|
60 | async_exchange_end(exch);
|
---|
61 |
|
---|
62 | if (rc != EOK)
|
---|
63 | return rc;
|
---|
64 |
|
---|
65 | sysarg_t retval;
|
---|
66 | async_wait_for(req, &retval);
|
---|
67 |
|
---|
68 | return retval;
|
---|
69 | }
|
---|
70 |
|
---|
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 | */
|
---|
77 | int udp_create(udp_t **rudp)
|
---|
78 | {
|
---|
79 | udp_t *udp;
|
---|
80 | service_id_t udp_svcid;
|
---|
81 | int rc;
|
---|
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);
|
---|
90 | fibril_mutex_initialize(&udp->lock);
|
---|
91 | fibril_condvar_initialize(&udp->cv);
|
---|
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 |
|
---|
100 | udp->sess = loc_service_connect(udp_svcid, INTERFACE_UDP,
|
---|
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;
|
---|
115 | error:
|
---|
116 | free(udp);
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Destroy UDP client instance.
|
---|
121 | *
|
---|
122 | * @param udp UDP client
|
---|
123 | */
|
---|
124 | void udp_destroy(udp_t *udp)
|
---|
125 | {
|
---|
126 | if (udp == NULL)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | async_hangup(udp->sess);
|
---|
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 |
|
---|
136 | free(udp);
|
---|
137 | }
|
---|
138 |
|
---|
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 | *
|
---|
160 | * @return EOK on success or negative error code.
|
---|
161 | */
|
---|
162 | int udp_assoc_create(udp_t *udp, inet_ep2_t *epp, udp_cb_t *cb, void *arg,
|
---|
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);
|
---|
175 | sysarg_t rc = async_data_write_start(exch, (void *)epp,
|
---|
176 | sizeof(inet_ep2_t));
|
---|
177 | async_exchange_end(exch);
|
---|
178 |
|
---|
179 | if (rc != EOK) {
|
---|
180 | sysarg_t rc_orig;
|
---|
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;
|
---|
192 | assoc->id = IPC_GET_ARG1(answer);
|
---|
193 | assoc->cb = cb;
|
---|
194 | assoc->cb_arg = arg;
|
---|
195 |
|
---|
196 | list_append(&assoc->ludp, &udp->assoc);
|
---|
197 | *rassoc = assoc;
|
---|
198 |
|
---|
199 | return EOK;
|
---|
200 | error:
|
---|
201 | free(assoc);
|
---|
202 | return (int) rc;
|
---|
203 | }
|
---|
204 |
|
---|
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 | */
|
---|
212 | void 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);
|
---|
222 | sysarg_t rc = async_req_1_0(exch, UDP_ASSOC_DESTROY, assoc->id);
|
---|
223 | async_exchange_end(exch);
|
---|
224 |
|
---|
225 | free(assoc);
|
---|
226 | (void) rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Send message via UDP association.
|
---|
230 | *
|
---|
231 | * @param assoc Association
|
---|
232 | * @param dest Destination endpoint or @c NULL to use association's remote ep.
|
---|
233 | * @param data Message data
|
---|
234 | * @param bytes Message size in bytes
|
---|
235 | *
|
---|
236 | * @return EOK on success or negative error code
|
---|
237 | */
|
---|
238 | int udp_assoc_send_msg(udp_assoc_t *assoc, inet_ep_t *dest, void *data,
|
---|
239 | size_t bytes)
|
---|
240 | {
|
---|
241 | async_exch_t *exch;
|
---|
242 |
|
---|
243 | exch = async_exchange_begin(assoc->udp->sess);
|
---|
244 | aid_t req = async_send_1(exch, UDP_ASSOC_SEND_MSG, assoc->id, NULL);
|
---|
245 |
|
---|
246 | sysarg_t rc = async_data_write_start(exch, (void *)dest,
|
---|
247 | sizeof(inet_ep_t));
|
---|
248 | if (rc != EOK) {
|
---|
249 | async_exchange_end(exch);
|
---|
250 | async_forget(req);
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|
254 | rc = async_data_write_start(exch, data, bytes);
|
---|
255 | if (rc != EOK) {
|
---|
256 | async_forget(req);
|
---|
257 | return rc;
|
---|
258 | }
|
---|
259 |
|
---|
260 | async_exchange_end(exch);
|
---|
261 |
|
---|
262 | if (rc != EOK) {
|
---|
263 | async_forget(req);
|
---|
264 | return rc;
|
---|
265 | }
|
---|
266 |
|
---|
267 | async_wait_for(req, &rc);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Get the user/callback argument for an association.
|
---|
272 | *
|
---|
273 | * @param assoc UDP association
|
---|
274 | * @return User argument associated with association
|
---|
275 | */
|
---|
276 | void *udp_assoc_userptr(udp_assoc_t *assoc)
|
---|
277 | {
|
---|
278 | return assoc->cb_arg;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** Get size of received message in bytes.
|
---|
282 | *
|
---|
283 | * Assuming jumbo messages can be received, the caller first needs to determine
|
---|
284 | * the size of the received message by calling this function, then they can
|
---|
285 | * read the message piece-wise using udp_rmsg_read().
|
---|
286 | *
|
---|
287 | * @param rmsg Received message
|
---|
288 | * @return Size of received message in bytes
|
---|
289 | */
|
---|
290 | size_t udp_rmsg_size(udp_rmsg_t *rmsg)
|
---|
291 | {
|
---|
292 | return rmsg->size;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Read part of received message.
|
---|
296 | *
|
---|
297 | * @param rmsg Received message
|
---|
298 | * @param off Start offset
|
---|
299 | * @param buf Buffer for storing data
|
---|
300 | * @param bsize Buffer size
|
---|
301 | *
|
---|
302 | * @return EOK on success or negative error code.
|
---|
303 | */
|
---|
304 | int udp_rmsg_read(udp_rmsg_t *rmsg, size_t off, void *buf, size_t bsize)
|
---|
305 | {
|
---|
306 | async_exch_t *exch;
|
---|
307 | ipc_call_t answer;
|
---|
308 |
|
---|
309 | exch = async_exchange_begin(rmsg->udp->sess);
|
---|
310 | aid_t req = async_send_1(exch, UDP_RMSG_READ, off, &answer);
|
---|
311 | int rc = async_data_read_start(exch, buf, bsize);
|
---|
312 | async_exchange_end(exch);
|
---|
313 |
|
---|
314 | if (rc != EOK) {
|
---|
315 | async_forget(req);
|
---|
316 | return rc;
|
---|
317 | }
|
---|
318 |
|
---|
319 | sysarg_t retval;
|
---|
320 | async_wait_for(req, &retval);
|
---|
321 | if (retval != EOK) {
|
---|
322 | return retval;
|
---|
323 | }
|
---|
324 |
|
---|
325 | return EOK;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /** Get remote endpoint of received message.
|
---|
329 | *
|
---|
330 | * Place the remote endpoint (the one from which the message was supposedly
|
---|
331 | * sent) to @a ep.
|
---|
332 | *
|
---|
333 | * @param rmsg Received message
|
---|
334 | * @param ep Place to store remote endpoint
|
---|
335 | */
|
---|
336 | void udp_rmsg_remote_ep(udp_rmsg_t *rmsg, inet_ep_t *ep)
|
---|
337 | {
|
---|
338 | *ep = rmsg->remote_ep;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /** Get type of received ICMP error message.
|
---|
342 | *
|
---|
343 | * @param rerr Received error message
|
---|
344 | * @return Error message type
|
---|
345 | */
|
---|
346 | uint8_t udp_rerr_type(udp_rerr_t *rerr)
|
---|
347 | {
|
---|
348 | return 0;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /** Get code of received ICMP error message.
|
---|
352 | *
|
---|
353 | * @param rerr Received error message
|
---|
354 | * @return Error message code
|
---|
355 | */
|
---|
356 | uint8_t udp_rerr_code(udp_rerr_t *rerr)
|
---|
357 | {
|
---|
358 | return 0;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** Get information about the next received message from UDP service.
|
---|
362 | *
|
---|
363 | * @param udp UDP client
|
---|
364 | * @param rmsg Place to store message information
|
---|
365 | *
|
---|
366 | * @return EOK on success or negative error code
|
---|
367 | */
|
---|
368 | static int udp_rmsg_info(udp_t *udp, udp_rmsg_t *rmsg)
|
---|
369 | {
|
---|
370 | async_exch_t *exch;
|
---|
371 | inet_ep_t ep;
|
---|
372 | ipc_call_t answer;
|
---|
373 |
|
---|
374 | exch = async_exchange_begin(udp->sess);
|
---|
375 | aid_t req = async_send_0(exch, UDP_RMSG_INFO, &answer);
|
---|
376 | int rc = async_data_read_start(exch, &ep, sizeof(inet_ep_t));
|
---|
377 | async_exchange_end(exch);
|
---|
378 |
|
---|
379 | if (rc != EOK) {
|
---|
380 | async_forget(req);
|
---|
381 | return rc;
|
---|
382 | }
|
---|
383 |
|
---|
384 | sysarg_t retval;
|
---|
385 | async_wait_for(req, &retval);
|
---|
386 | if (retval != EOK)
|
---|
387 | return retval;
|
---|
388 |
|
---|
389 | rmsg->udp = udp;
|
---|
390 | rmsg->assoc_id = IPC_GET_ARG1(answer);
|
---|
391 | rmsg->size = IPC_GET_ARG2(answer);
|
---|
392 | rmsg->remote_ep = ep;
|
---|
393 | return EOK;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /** Discard next received message in UDP service.
|
---|
397 | *
|
---|
398 | * @param udp UDP client
|
---|
399 | * @return EOK on success or negative error code
|
---|
400 | */
|
---|
401 | static int udp_rmsg_discard(udp_t *udp)
|
---|
402 | {
|
---|
403 | async_exch_t *exch;
|
---|
404 |
|
---|
405 | exch = async_exchange_begin(udp->sess);
|
---|
406 | sysarg_t rc = async_req_0_0(exch, UDP_RMSG_DISCARD);
|
---|
407 | async_exchange_end(exch);
|
---|
408 |
|
---|
409 | return rc;
|
---|
410 | }
|
---|
411 |
|
---|
412 | /** Get association based on its ID.
|
---|
413 | *
|
---|
414 | * @param udp UDP client
|
---|
415 | * @param id Association ID
|
---|
416 | * @param rassoc Place to store pointer to association
|
---|
417 | *
|
---|
418 | * @return EOK on success, EINVAL if no association with the given ID exists
|
---|
419 | */
|
---|
420 | static int udp_assoc_get(udp_t *udp, sysarg_t id, udp_assoc_t **rassoc)
|
---|
421 | {
|
---|
422 | list_foreach(udp->assoc, ludp, udp_assoc_t, assoc) {
|
---|
423 | if (assoc->id == id) {
|
---|
424 | *rassoc = assoc;
|
---|
425 | return EOK;
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | return EINVAL;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /** Handle 'data' event, i.e. some message(s) arrived.
|
---|
433 | *
|
---|
434 | * For each received message, get information about it, call @c recv_msg
|
---|
435 | * callback and discard it.
|
---|
436 | *
|
---|
437 | * @param udp UDP client
|
---|
438 | * @param iid IPC message ID
|
---|
439 | * @param icall IPC message
|
---|
440 | */
|
---|
441 | static void udp_ev_data(udp_t *udp, ipc_callid_t iid, ipc_call_t *icall)
|
---|
442 | {
|
---|
443 | udp_rmsg_t rmsg;
|
---|
444 | udp_assoc_t *assoc;
|
---|
445 | int rc;
|
---|
446 |
|
---|
447 | while (true) {
|
---|
448 | rc = udp_rmsg_info(udp, &rmsg);
|
---|
449 | if (rc != EOK) {
|
---|
450 | break;
|
---|
451 | }
|
---|
452 |
|
---|
453 | rc = udp_assoc_get(udp, rmsg.assoc_id, &assoc);
|
---|
454 | if (rc != EOK) {
|
---|
455 | continue;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if (assoc->cb != NULL && assoc->cb->recv_msg != NULL)
|
---|
459 | assoc->cb->recv_msg(assoc, &rmsg);
|
---|
460 |
|
---|
461 | rc = udp_rmsg_discard(udp);
|
---|
462 | if (rc != EOK) {
|
---|
463 | break;
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | async_answer_0(iid, EOK);
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** UDP service callback connection.
|
---|
471 | *
|
---|
472 | * @param iid Connect message ID
|
---|
473 | * @param icall Connect message
|
---|
474 | * @param arg Argument, UDP client
|
---|
475 | */
|
---|
476 | static void udp_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
477 | {
|
---|
478 | udp_t *udp = (udp_t *)arg;
|
---|
479 |
|
---|
480 | async_answer_0(iid, EOK);
|
---|
481 |
|
---|
482 | while (true) {
|
---|
483 | ipc_call_t call;
|
---|
484 | ipc_callid_t callid = async_get_call(&call);
|
---|
485 |
|
---|
486 | if (!IPC_GET_IMETHOD(call)) {
|
---|
487 | /* Hangup */
|
---|
488 | goto out;
|
---|
489 | }
|
---|
490 |
|
---|
491 | switch (IPC_GET_IMETHOD(call)) {
|
---|
492 | case UDP_EV_DATA:
|
---|
493 | udp_ev_data(udp, callid, &call);
|
---|
494 | break;
|
---|
495 | default:
|
---|
496 | async_answer_0(callid, ENOTSUP);
|
---|
497 | break;
|
---|
498 | }
|
---|
499 | }
|
---|
500 | out:
|
---|
501 | fibril_mutex_lock(&udp->lock);
|
---|
502 | udp->cb_done = true;
|
---|
503 | fibril_mutex_unlock(&udp->lock);
|
---|
504 | fibril_condvar_broadcast(&udp->cv);
|
---|
505 | }
|
---|
506 |
|
---|
507 | /** @}
|
---|
508 | */
|
---|