source: mainline/uspace/srv/net/udp/assoc.c@ 451481c8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 451481c8 was 451481c8, checked in by Martin Decky <martin@…>, 13 years ago

properly implement the implicit binding of a socket to a port

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Copyright (c) 2012 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 UDP associations
35 */
36
37#include <adt/list.h>
38#include <stdbool.h>
39#include <fibril_synch.h>
40#include <io/log.h>
41#include <stdlib.h>
42
43#include "assoc.h"
44#include "msg.h"
45#include "pdu.h"
46#include "ucall.h"
47#include "udp_inet.h"
48#include "udp_type.h"
49
50LIST_INITIALIZE(assoc_list);
51FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
52
53static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *);
54static int udp_assoc_queue_msg(udp_assoc_t *, udp_sockpair_t *, udp_msg_t *);
55static bool udp_socket_match(udp_sock_t *, udp_sock_t *);
56static bool udp_sockpair_match(udp_sockpair_t *, udp_sockpair_t *);
57
58/** Create new association structure.
59 *
60 * @param lsock Local socket (will be deeply copied)
61 * @param fsock Foreign socket (will be deeply copied)
62 * @return New association or NULL
63 */
64udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock)
65{
66 udp_assoc_t *assoc = NULL;
67
68 /* Allocate association structure */
69 assoc = calloc(1, sizeof(udp_assoc_t));
70 if (assoc == NULL)
71 goto error;
72
73 fibril_mutex_initialize(&assoc->lock);
74
75 /* One for the user */
76 atomic_set(&assoc->refcnt, 1);
77
78 /* Initialize receive queue */
79 list_initialize(&assoc->rcv_queue);
80 fibril_condvar_initialize(&assoc->rcv_queue_cv);
81
82 if (lsock != NULL)
83 assoc->ident.local = *lsock;
84 if (fsock != NULL)
85 assoc->ident.foreign = *fsock;
86
87 return assoc;
88error:
89 return NULL;
90}
91
92/** Destroy association structure.
93 *
94 * Association structure should be destroyed when the folowing conditions
95 * are met:
96 * (1) user has deleted the association
97 * (2) nobody is holding references to the association
98 *
99 * This happens when @a assoc->refcnt is zero as we count (1)
100 * as an extra reference.
101 *
102 * @param assoc Association
103 */
104static void udp_assoc_free(udp_assoc_t *assoc)
105{
106 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_free(%p)", assoc->name, assoc);
107
108 while (!list_empty(&assoc->rcv_queue)) {
109 link_t *link = list_first(&assoc->rcv_queue);
110 udp_rcv_queue_entry_t *rqe = list_get_instance(link,
111 udp_rcv_queue_entry_t, link);
112 list_remove(link);
113
114 udp_msg_delete(rqe->msg);
115 free(rqe);
116 }
117
118 free(assoc);
119}
120
121/** Add reference to association.
122 *
123 * Increase association reference count by one.
124 *
125 * @param assoc Association
126 */
127void udp_assoc_addref(udp_assoc_t *assoc)
128{
129 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: upd_assoc_addref(%p)", assoc->name, assoc);
130 atomic_inc(&assoc->refcnt);
131}
132
133/** Remove reference from association.
134 *
135 * Decrease association reference count by one.
136 *
137 * @param assoc Association
138 */
139void udp_assoc_delref(udp_assoc_t *assoc)
140{
141 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delref(%p)", assoc->name, assoc);
142
143 if (atomic_predec(&assoc->refcnt) == 0)
144 udp_assoc_free(assoc);
145}
146
147/** Delete association.
148 *
149 * The caller promises not make no further references to @a assoc.
150 * UDP will free @a assoc eventually.
151 *
152 * @param assoc Association
153 */
154void udp_assoc_delete(udp_assoc_t *assoc)
155{
156 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delete(%p)", assoc->name, assoc);
157
158 assert(assoc->deleted == false);
159 udp_assoc_delref(assoc);
160 assoc->deleted = true;
161}
162
163/** Enlist association.
164 *
165 * Add association to the association map.
166 */
167void udp_assoc_add(udp_assoc_t *assoc)
168{
169 udp_assoc_addref(assoc);
170 fibril_mutex_lock(&assoc_list_lock);
171 list_append(&assoc->link, &assoc_list);
172 fibril_mutex_unlock(&assoc_list_lock);
173}
174
175/** Delist association.
176 *
177 * Remove association from the association map.
178 */
179void udp_assoc_remove(udp_assoc_t *assoc)
180{
181 fibril_mutex_lock(&assoc_list_lock);
182 list_remove(&assoc->link);
183 fibril_mutex_unlock(&assoc_list_lock);
184 udp_assoc_delref(assoc);
185}
186
187/** Set foreign socket in association.
188 *
189 * @param assoc Association
190 * @param fsock Foreign socket (deeply copied)
191 */
192void udp_assoc_set_foreign(udp_assoc_t *assoc, udp_sock_t *fsock)
193{
194 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_foreign(%p, %p)", assoc, fsock);
195 fibril_mutex_lock(&assoc->lock);
196 assoc->ident.foreign = *fsock;
197 fibril_mutex_unlock(&assoc->lock);
198}
199
200/** Set local socket in association.
201 *
202 * @param assoc Association
203 * @param lsock Local socket (deeply copied)
204 *
205 */
206void udp_assoc_set_local(udp_assoc_t *assoc, udp_sock_t *lsock)
207{
208 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %p)", assoc, lsock);
209 fibril_mutex_lock(&assoc->lock);
210 assoc->ident.local = *lsock;
211 fibril_mutex_unlock(&assoc->lock);
212}
213
214/** Set local port in association.
215 *
216 * @param assoc Association
217 * @param lport Local port
218 *
219 */
220void udp_assoc_set_local_port(udp_assoc_t *assoc, uint16_t lport)
221{
222 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %" PRIu16 ")", assoc, lport);
223 fibril_mutex_lock(&assoc->lock);
224 assoc->ident.local.port = lport;
225 fibril_mutex_unlock(&assoc->lock);
226}
227
228/** Send message to association.
229 *
230 * @param assoc Association
231 * @param fsock Foreign socket or NULL not to override @a assoc
232 * @param msg Message
233 *
234 * @return EOK on success
235 * EINVAL if foreign socket is not set
236 * ENOMEM if out of resources
237 * EIO if no route to destination exists
238 */
239int udp_assoc_send(udp_assoc_t *assoc, udp_sock_t *fsock, udp_msg_t *msg)
240{
241 udp_pdu_t *pdu;
242 udp_sockpair_t sp;
243 int rc;
244
245 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
246 assoc, fsock, msg);
247
248 /* @a fsock can be used to override the foreign socket */
249 sp = assoc->ident;
250 if (fsock != NULL)
251 sp.foreign = *fsock;
252
253 if (sp.foreign.addr.ipv4 == 0 || sp.foreign.port == 0)
254 return EINVAL;
255
256 rc = udp_pdu_encode(&sp, msg, &pdu);
257 if (rc != EOK)
258 return ENOMEM;
259
260 rc = udp_transmit_pdu(pdu);
261 if (rc != EOK)
262 return EIO;
263
264 udp_pdu_delete(pdu);
265
266 return EOK;
267}
268
269/** Get a received message.
270 *
271 * Pull one message from the association's receive queue.
272 */
273int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, udp_sock_t *fsock)
274{
275 link_t *link;
276 udp_rcv_queue_entry_t *rqe;
277
278 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv()");
279
280 fibril_mutex_lock(&assoc->lock);
281 while (list_empty(&assoc->rcv_queue)) {
282 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - waiting");
283 fibril_condvar_wait(&assoc->rcv_queue_cv, &assoc->lock);
284 }
285
286 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - got a message");
287 link = list_first(&assoc->rcv_queue);
288 rqe = list_get_instance(link, udp_rcv_queue_entry_t, link);
289 list_remove(link);
290 fibril_mutex_unlock(&assoc->lock);
291
292 *msg = rqe->msg;
293 *fsock = rqe->sp.foreign;
294 free(rqe);
295
296 return EOK;
297}
298
299/** Message received.
300 *
301 * Find the association to which the message belongs and queue it.
302 */
303void udp_assoc_received(udp_sockpair_t *rsp, udp_msg_t *msg)
304{
305 udp_assoc_t *assoc;
306 int rc;
307
308 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", rsp, msg);
309
310 assoc = udp_assoc_find_ref(rsp);
311 if (assoc == NULL) {
312 log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
313 /* XXX Generate ICMP error. */
314 /* XXX Might propagate error directly by error return. */
315 return;
316 }
317
318 rc = udp_assoc_queue_msg(assoc, rsp, msg);
319 if (rc != EOK) {
320 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
321 /* XXX Generate ICMP error? */
322 }
323}
324
325static int udp_assoc_queue_msg(udp_assoc_t *assoc, udp_sockpair_t *sp,
326 udp_msg_t *msg)
327{
328 udp_rcv_queue_entry_t *rqe;
329
330 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
331 assoc, sp, msg);
332
333 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
334 if (rqe == NULL)
335 return ENOMEM;
336
337 link_initialize(&rqe->link);
338 rqe->sp = *sp;
339 rqe->msg = msg;
340
341 fibril_mutex_lock(&assoc->lock);
342 list_append(&rqe->link, &assoc->rcv_queue);
343 fibril_mutex_unlock(&assoc->lock);
344
345 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
346
347 return EOK;
348}
349
350/** Match socket with pattern. */
351static bool udp_socket_match(udp_sock_t *sock, udp_sock_t *patt)
352{
353 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_socket_match(sock=(%x,%u), pat=(%x,%u))",
354 sock->addr.ipv4, sock->port, patt->addr.ipv4, patt->port);
355
356 if (patt->addr.ipv4 != UDP_IPV4_ANY &&
357 patt->addr.ipv4 != sock->addr.ipv4)
358 return false;
359
360 if (patt->port != UDP_PORT_ANY &&
361 patt->port != sock->port)
362 return false;
363
364 log_msg(LOG_DEFAULT, LVL_DEBUG, " -> match");
365
366 return true;
367}
368
369/** Match socket pair with pattern. */
370static bool udp_sockpair_match(udp_sockpair_t *sp, udp_sockpair_t *pattern)
371{
372 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sockpair_match(%p, %p)", sp, pattern);
373
374 if (!udp_socket_match(&sp->local, &pattern->local))
375 return false;
376
377 if (!udp_socket_match(&sp->foreign, &pattern->foreign))
378 return false;
379
380 log_msg(LOG_DEFAULT, LVL_DEBUG, "Socket pair matched.");
381 return true;
382}
383
384
385/** Find association structure for specified socket pair.
386 *
387 * An association is uniquely identified by a socket pair. Look up our
388 * association map and return association structure based on socket pair.
389 * The association reference count is bumped by one.
390 *
391 * @param sp Socket pair
392 * @return Association structure or NULL if not found.
393 */
394static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *sp)
395{
396 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", sp);
397
398 fibril_mutex_lock(&assoc_list_lock);
399
400 list_foreach(assoc_list, link) {
401 udp_assoc_t *assoc = list_get_instance(link, udp_assoc_t, link);
402 udp_sockpair_t *asp = &assoc->ident;
403 log_msg(LOG_DEFAULT, LVL_DEBUG, "compare with assoc (f:(%x,%u), l:(%x,%u))",
404 asp->foreign.addr.ipv4, asp->foreign.port,
405 asp->local.addr.ipv4, asp->local.port);
406
407 /* Skip unbound associations */
408 if (asp->local.port == UDP_PORT_ANY)
409 continue;
410
411 if (udp_sockpair_match(sp, asp)) {
412 log_msg(LOG_DEFAULT, LVL_DEBUG, "Returning assoc %p", assoc);
413 udp_assoc_addref(assoc);
414 fibril_mutex_unlock(&assoc_list_lock);
415 return assoc;
416 }
417 }
418
419 fibril_mutex_unlock(&assoc_list_lock);
420 return NULL;
421}
422
423/**
424 * @}
425 */
Note: See TracBrowser for help on using the repository browser.