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 libnettl
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file Association map
|
---|
35 | *
|
---|
36 | * Manages allocations of endpoints / endpoint pairs (corresponding to
|
---|
37 | * UDP associations, TCP listeners and TCP connections)
|
---|
38 | *
|
---|
39 | * An association map contains different types of entries, based on which
|
---|
40 | * set of attributes (key) they specify. In order from most specific to the
|
---|
41 | * least specific one:
|
---|
42 | *
|
---|
43 | * - repla (remote endpoint, local address)
|
---|
44 | * - laddr (local address)
|
---|
45 | * - llink (local link)
|
---|
46 | * - unspec (unspecified)
|
---|
47 | *
|
---|
48 | * In the unspecified case only the local port is known and the entry matches
|
---|
49 | * all remote and local addresses.
|
---|
50 | */
|
---|
51 |
|
---|
52 | #include <adt/list.h>
|
---|
53 | #include <errno.h>
|
---|
54 | #include <inet/addr.h>
|
---|
55 | #include <inet/inet.h>
|
---|
56 | #include <io/log.h>
|
---|
57 | #include <nettl/amap.h>
|
---|
58 | #include <stdint.h>
|
---|
59 | #include <stdlib.h>
|
---|
60 |
|
---|
61 | /** Create association map.
|
---|
62 | *
|
---|
63 | * @param rmap Place to store pointer to new association map
|
---|
64 | * @return EOk on success, ENOMEM if out of memory
|
---|
65 | */
|
---|
66 | int amap_create(amap_t **rmap)
|
---|
67 | {
|
---|
68 | amap_t *map;
|
---|
69 | int rc;
|
---|
70 |
|
---|
71 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_create()");
|
---|
72 |
|
---|
73 | map = calloc(1, sizeof(amap_t));
|
---|
74 | if (map == NULL)
|
---|
75 | return ENOMEM;
|
---|
76 |
|
---|
77 | rc = portrng_create(&map->unspec);
|
---|
78 | if (rc != EOK) {
|
---|
79 | assert(rc == ENOMEM);
|
---|
80 | free(map);
|
---|
81 | return ENOMEM;
|
---|
82 | }
|
---|
83 |
|
---|
84 | list_initialize(&map->repla);
|
---|
85 | list_initialize(&map->laddr);
|
---|
86 | list_initialize(&map->llink);
|
---|
87 |
|
---|
88 | *rmap = map;
|
---|
89 | return EOK;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Destroy association map.
|
---|
93 | *
|
---|
94 | * @param map Association map
|
---|
95 | */
|
---|
96 | void amap_destroy(amap_t *map)
|
---|
97 | {
|
---|
98 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_destroy()");
|
---|
99 | free(map);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Find exact repla.
|
---|
103 | *
|
---|
104 | * Find repla (remote endpoint, local address) entry by exact match.
|
---|
105 | *
|
---|
106 | * @param map Association map
|
---|
107 | * @param rep Remote endpoint
|
---|
108 | * @param la Local address
|
---|
109 | * @param rrepla Place to store pointer to repla
|
---|
110 | *
|
---|
111 | * @return EOK on success, ENOENT if not found
|
---|
112 | */
|
---|
113 | static int amap_repla_find(amap_t *map, inet_ep_t *rep, inet_addr_t *la,
|
---|
114 | amap_repla_t **rrepla)
|
---|
115 | {
|
---|
116 | char *sraddr, *sladdr;
|
---|
117 |
|
---|
118 | (void) inet_addr_format(&rep->addr, &sraddr);
|
---|
119 | (void) inet_addr_format(la, &sladdr);
|
---|
120 |
|
---|
121 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_repla_find(): rep=(%s,%" PRIu16
|
---|
122 | ") la=%s", sraddr, rep->port, sladdr);
|
---|
123 | free(sraddr);
|
---|
124 | free(sladdr);
|
---|
125 |
|
---|
126 | list_foreach(map->repla, lamap, amap_repla_t, repla) {
|
---|
127 | (void) inet_addr_format(&repla->rep.addr, &sraddr);
|
---|
128 | (void) inet_addr_format(&repla->laddr, &sladdr);
|
---|
129 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_repla_find(): "
|
---|
130 | "compare to rep=(%s, %" PRIu16 ") la=%s",
|
---|
131 | sraddr, repla->rep.port, sladdr);
|
---|
132 | free(sraddr);
|
---|
133 | free(sladdr);
|
---|
134 | if (inet_addr_compare(&repla->rep.addr, &rep->addr) &&
|
---|
135 | repla->rep.port == rep->port &&
|
---|
136 | inet_addr_compare(&repla->laddr, la)) {
|
---|
137 | *rrepla = repla;
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | *rrepla = NULL;
|
---|
143 | return ENOENT;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Insert repla.
|
---|
147 | *
|
---|
148 | * Insert new repla (remote endpoint, local address) entry to association map.
|
---|
149 | *
|
---|
150 | * @param amap Association map
|
---|
151 | * @param rep Remote endpoint
|
---|
152 | * @param la Local address
|
---|
153 | * @param rrepla Place to store pointer to new repla
|
---|
154 | *
|
---|
155 | * @return EOK on success, ENOMEM if out of memory
|
---|
156 | */
|
---|
157 | static int amap_repla_insert(amap_t *map, inet_ep_t *rep, inet_addr_t *la,
|
---|
158 | amap_repla_t **rrepla)
|
---|
159 | {
|
---|
160 | amap_repla_t *repla;
|
---|
161 | int rc;
|
---|
162 |
|
---|
163 | repla = calloc(1, sizeof(amap_repla_t));
|
---|
164 | if (repla == NULL) {
|
---|
165 | *rrepla = NULL;
|
---|
166 | return ENOMEM;
|
---|
167 | }
|
---|
168 |
|
---|
169 | rc = portrng_create(&repla->portrng);
|
---|
170 | if (rc != EOK) {
|
---|
171 | free(repla);
|
---|
172 | return ENOMEM;
|
---|
173 | }
|
---|
174 |
|
---|
175 | repla->rep = *rep;
|
---|
176 | repla->laddr = *la;
|
---|
177 | list_append(&repla->lamap, &map->repla);
|
---|
178 |
|
---|
179 | *rrepla = repla;
|
---|
180 | return EOK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Remove repla from association map.
|
---|
184 | *
|
---|
185 | * Remove repla (remote endpoint, local address) from association map.
|
---|
186 | *
|
---|
187 | * @param map Association map
|
---|
188 | * @param repla Repla
|
---|
189 | */
|
---|
190 | static void amap_repla_remove(amap_t *map, amap_repla_t *repla)
|
---|
191 | {
|
---|
192 | list_remove(&repla->lamap);
|
---|
193 | portrng_destroy(repla->portrng);
|
---|
194 | free(repla);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** Find exact laddr.
|
---|
198 | *
|
---|
199 | * Find laddr (local address) entry by exact match.
|
---|
200 | *
|
---|
201 | * @param map Association map
|
---|
202 | * @param addr Address
|
---|
203 | * @param rladdr Place to store pointer to laddr entry
|
---|
204 | *
|
---|
205 | * @return EOK on success, ENOENT if not found.
|
---|
206 | */
|
---|
207 | static int amap_laddr_find(amap_t *map, inet_addr_t *addr,
|
---|
208 | amap_laddr_t **rladdr)
|
---|
209 | {
|
---|
210 | list_foreach(map->laddr, lamap, amap_laddr_t, laddr) {
|
---|
211 | if (inet_addr_compare(&laddr->laddr, addr)) {
|
---|
212 | *rladdr = laddr;
|
---|
213 | return EOK;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | *rladdr = NULL;
|
---|
218 | return ENOENT;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Insert laddr.
|
---|
222 | *
|
---|
223 | * Insert new laddr (local address) entry to association map.
|
---|
224 | *
|
---|
225 | * @param addr Local address
|
---|
226 | * @param rladdr Place to store pointer to new laddr
|
---|
227 | *
|
---|
228 | * @return EOK on success, ENOMEM if out of memory
|
---|
229 | */
|
---|
230 | static int amap_laddr_insert(amap_t *map, inet_addr_t *addr,
|
---|
231 | amap_laddr_t **rladdr)
|
---|
232 | {
|
---|
233 | amap_laddr_t *laddr;
|
---|
234 | int rc;
|
---|
235 |
|
---|
236 | laddr = calloc(1, sizeof(amap_laddr_t));
|
---|
237 | if (laddr == NULL) {
|
---|
238 | *rladdr = NULL;
|
---|
239 | return ENOMEM;
|
---|
240 | }
|
---|
241 |
|
---|
242 | rc = portrng_create(&laddr->portrng);
|
---|
243 | if (rc != EOK) {
|
---|
244 | free(laddr);
|
---|
245 | return ENOMEM;
|
---|
246 | }
|
---|
247 |
|
---|
248 | laddr->laddr = *addr;
|
---|
249 | list_append(&laddr->lamap, &map->laddr);
|
---|
250 |
|
---|
251 | *rladdr = laddr;
|
---|
252 | return EOK;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** Remove laddr from association map.
|
---|
256 | *
|
---|
257 | * Remove laddr (local address) entry from association map.
|
---|
258 | *
|
---|
259 | * @param map Association map
|
---|
260 | * @param laddr Laddr entry
|
---|
261 | */
|
---|
262 | static void amap_laddr_remove(amap_t *map, amap_laddr_t *laddr)
|
---|
263 | {
|
---|
264 | list_remove(&laddr->lamap);
|
---|
265 | portrng_destroy(laddr->portrng);
|
---|
266 | free(laddr);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Find exact llink.
|
---|
270 | *
|
---|
271 | * Find llink (local link) entry by exact match.
|
---|
272 | *
|
---|
273 | * @param map Association map
|
---|
274 | * @param link_id Local link ID
|
---|
275 | * @param rllink Place to store pointer to llink entry
|
---|
276 | *
|
---|
277 | * @return EOK on success, ENOENT if not found.
|
---|
278 | */
|
---|
279 | static int amap_llink_find(amap_t *map, sysarg_t link_id,
|
---|
280 | amap_llink_t **rllink)
|
---|
281 | {
|
---|
282 | list_foreach(map->llink, lamap, amap_llink_t, llink) {
|
---|
283 | if (llink->llink == link_id) {
|
---|
284 | *rllink = llink;
|
---|
285 | return EOK;
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | *rllink = NULL;
|
---|
290 | return ENOENT;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Insert llink.
|
---|
294 | *
|
---|
295 | * Insert new llink (local link) entry to association map.
|
---|
296 | *
|
---|
297 | * @param link_id Local link
|
---|
298 | * @param rllink Place to store pointer to new llink
|
---|
299 | *
|
---|
300 | * @return EOK on success, ENOMEM if out of memory
|
---|
301 | */
|
---|
302 | static int amap_llink_insert(amap_t *map, sysarg_t link_id,
|
---|
303 | amap_llink_t **rllink)
|
---|
304 | {
|
---|
305 | amap_llink_t *llink;
|
---|
306 | int rc;
|
---|
307 |
|
---|
308 | llink = calloc(1, sizeof(amap_llink_t));
|
---|
309 | if (llink == NULL) {
|
---|
310 | *rllink = NULL;
|
---|
311 | return ENOMEM;
|
---|
312 | }
|
---|
313 |
|
---|
314 | rc = portrng_create(&llink->portrng);
|
---|
315 | if (rc != EOK) {
|
---|
316 | free(llink);
|
---|
317 | return ENOMEM;
|
---|
318 | }
|
---|
319 |
|
---|
320 | llink->llink = link_id;
|
---|
321 | list_append(&llink->lamap, &map->llink);
|
---|
322 |
|
---|
323 | *rllink = llink;
|
---|
324 | return EOK;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /** Remove llink from association map.
|
---|
328 | *
|
---|
329 | * Remove llink (local link) entry from association map.
|
---|
330 | *
|
---|
331 | * @param map Association map
|
---|
332 | * @param llink Llink entry
|
---|
333 | */
|
---|
334 | static void amap_llink_remove(amap_t *map, amap_llink_t *llink)
|
---|
335 | {
|
---|
336 | list_remove(&llink->lamap);
|
---|
337 | portrng_destroy(llink->portrng);
|
---|
338 | free(llink);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /** Insert endpoint pair into map with repla as key.
|
---|
342 | *
|
---|
343 | * If local port number is not specified, it is allocated.
|
---|
344 | *
|
---|
345 | * @param map Association map
|
---|
346 | * @param epp Endpoint pair, possibly with local port inet_port_any
|
---|
347 | * @param arg arg User value
|
---|
348 | * @param flags Flags
|
---|
349 | * @param aepp Place to store actual endpoint pair, possibly with allocated port
|
---|
350 | *
|
---|
351 | * @return EOK on success, EEXIST if conflicting epp exists,
|
---|
352 | * ENOMEM if out of memory
|
---|
353 | */
|
---|
354 | static int amap_insert_repla(amap_t *map, inet_ep2_t *epp, void *arg,
|
---|
355 | amap_flags_t flags, inet_ep2_t *aepp)
|
---|
356 | {
|
---|
357 | amap_repla_t *repla;
|
---|
358 | inet_ep2_t mepp;
|
---|
359 | int rc;
|
---|
360 |
|
---|
361 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_repla()");
|
---|
362 |
|
---|
363 | rc = amap_repla_find(map, &epp->remote, &epp->local.addr, &repla);
|
---|
364 | if (rc != EOK) {
|
---|
365 | /* New repla */
|
---|
366 | rc = amap_repla_insert(map, &epp->remote, &epp->local.addr,
|
---|
367 | &repla);
|
---|
368 | if (rc != EOK) {
|
---|
369 | assert(rc == ENOMEM);
|
---|
370 | return rc;
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | mepp = *epp;
|
---|
375 |
|
---|
376 | rc = portrng_alloc(repla->portrng, epp->local.port, arg, flags,
|
---|
377 | &mepp.local.port);
|
---|
378 | if (rc != EOK) {
|
---|
379 | return rc;
|
---|
380 | }
|
---|
381 |
|
---|
382 | *aepp = mepp;
|
---|
383 | return EOK;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /** Insert endpoint pair into map with laddr as key.
|
---|
387 | *
|
---|
388 | * If local port number is not specified, it is allocated.
|
---|
389 | *
|
---|
390 | * @param map Association map
|
---|
391 | * @param epp Endpoint pair, possibly with local port inet_port_any
|
---|
392 | * @param arg arg User value
|
---|
393 | * @param flags Flags
|
---|
394 | * @param aepp Place to store actual endpoint pair, possibly with allocated port
|
---|
395 | *
|
---|
396 | * @return EOK on success, EEXIST if conflicting epp exists,
|
---|
397 | * ENOMEM if out of memory
|
---|
398 | */
|
---|
399 | static int amap_insert_laddr(amap_t *map, inet_ep2_t *epp, void *arg,
|
---|
400 | amap_flags_t flags, inet_ep2_t *aepp)
|
---|
401 | {
|
---|
402 | amap_laddr_t *laddr;
|
---|
403 | inet_ep2_t mepp;
|
---|
404 | int rc;
|
---|
405 |
|
---|
406 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_laddr()");
|
---|
407 |
|
---|
408 | rc = amap_laddr_find(map, &epp->local.addr, &laddr);
|
---|
409 | if (rc != EOK) {
|
---|
410 | /* New laddr */
|
---|
411 | rc = amap_laddr_insert(map, &epp->local.addr, &laddr);
|
---|
412 | if (rc != EOK) {
|
---|
413 | assert(rc == ENOMEM);
|
---|
414 | return rc;
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | mepp = *epp;
|
---|
419 |
|
---|
420 | rc = portrng_alloc(laddr->portrng, epp->local.port, arg, flags,
|
---|
421 | &mepp.local.port);
|
---|
422 | if (rc != EOK) {
|
---|
423 | return rc;
|
---|
424 | }
|
---|
425 |
|
---|
426 | *aepp = mepp;
|
---|
427 | return EOK;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /** Insert endpoint pair into map with llink as key.
|
---|
431 | *
|
---|
432 | * If local port number is not specified, it is allocated.
|
---|
433 | *
|
---|
434 | * @param map Association map
|
---|
435 | * @param epp Endpoint pair, possibly with local port inet_port_any
|
---|
436 | * @param arg arg User value
|
---|
437 | * @param flags Flags
|
---|
438 | * @param aepp Place to store actual endpoint pair, possibly with allocated port
|
---|
439 | *
|
---|
440 | * @return EOK on success, EEXIST if conflicting epp exists,
|
---|
441 | * ENOMEM if out of memory
|
---|
442 | */
|
---|
443 | static int amap_insert_llink(amap_t *map, inet_ep2_t *epp, void *arg,
|
---|
444 | amap_flags_t flags, inet_ep2_t *aepp)
|
---|
445 | {
|
---|
446 | amap_llink_t *llink;
|
---|
447 | inet_ep2_t mepp;
|
---|
448 | int rc;
|
---|
449 |
|
---|
450 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_llink()");
|
---|
451 |
|
---|
452 | rc = amap_llink_find(map, epp->local_link, &llink);
|
---|
453 | if (rc != EOK) {
|
---|
454 | /* New llink */
|
---|
455 | rc = amap_llink_insert(map, epp->local_link, &llink);
|
---|
456 | if (rc != EOK) {
|
---|
457 | assert(rc == ENOMEM);
|
---|
458 | return rc;
|
---|
459 | }
|
---|
460 | }
|
---|
461 |
|
---|
462 | mepp = *epp;
|
---|
463 |
|
---|
464 | rc = portrng_alloc(llink->portrng, epp->local.port, arg, flags,
|
---|
465 | &mepp.local.port);
|
---|
466 | if (rc != EOK) {
|
---|
467 | return rc;
|
---|
468 | }
|
---|
469 |
|
---|
470 | *aepp = mepp;
|
---|
471 | return EOK;
|
---|
472 | }
|
---|
473 |
|
---|
474 | /** Insert endpoint pair into map with unspec as key.
|
---|
475 | *
|
---|
476 | * If local port number is not specified, it is allocated.
|
---|
477 | *
|
---|
478 | * @param map Association map
|
---|
479 | * @param epp Endpoint pair, possibly with local port inet_port_any
|
---|
480 | * @param arg arg User value
|
---|
481 | * @param flags Flags
|
---|
482 | * @param aepp Place to store actual endpoint pair, possibly with allocated port
|
---|
483 | *
|
---|
484 | * @return EOK on success, EEXIST if conflicting epp exists,
|
---|
485 | * ENOMEM if out of memory
|
---|
486 | */
|
---|
487 | static int amap_insert_unspec(amap_t *map, inet_ep2_t *epp, void *arg,
|
---|
488 | amap_flags_t flags, inet_ep2_t *aepp)
|
---|
489 | {
|
---|
490 | inet_ep2_t mepp;
|
---|
491 | int rc;
|
---|
492 |
|
---|
493 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert_unspec()");
|
---|
494 | mepp = *epp;
|
---|
495 |
|
---|
496 | rc = portrng_alloc(map->unspec, epp->local.port, arg, flags,
|
---|
497 | &mepp.local.port);
|
---|
498 | if (rc != EOK) {
|
---|
499 | return rc;
|
---|
500 | }
|
---|
501 |
|
---|
502 | *aepp = mepp;
|
---|
503 | return EOK;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /** Insert endpoint pair into map.
|
---|
507 | *
|
---|
508 | * If local endpoint is not fully specified, it is filled in (determine
|
---|
509 | * source address, allocate port number). Checks for conflicting endpoint pair.
|
---|
510 | *
|
---|
511 | * @param map Association map
|
---|
512 | * @param epp Endpoint pair, possibly with local port inet_port_any
|
---|
513 | * @param arg arg User value
|
---|
514 | * @param flags Flags
|
---|
515 | * @param aepp Place to store actual endpoint pair, possibly with allocated port
|
---|
516 | *
|
---|
517 | * @return EOK on success, EEXIST if conflicting epp exists,
|
---|
518 | * ENOMEM if out of memory
|
---|
519 | */
|
---|
520 | int amap_insert(amap_t *map, inet_ep2_t *epp, void *arg, amap_flags_t flags,
|
---|
521 | inet_ep2_t *aepp)
|
---|
522 | {
|
---|
523 | bool raddr, rport, laddr, llink;
|
---|
524 | inet_ep2_t mepp;
|
---|
525 | int rc;
|
---|
526 |
|
---|
527 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert()");
|
---|
528 |
|
---|
529 | mepp = *epp;
|
---|
530 |
|
---|
531 | /* Fill in local address? */
|
---|
532 | if (!inet_addr_is_any(&epp->remote.addr) &&
|
---|
533 | inet_addr_is_any(&epp->local.addr)) {
|
---|
534 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert: "
|
---|
535 | "determine local address");
|
---|
536 | rc = inet_get_srcaddr(&epp->remote.addr, 0, &mepp.local.addr);
|
---|
537 | if (rc != EOK) {
|
---|
538 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert: "
|
---|
539 | "cannot determine local address");
|
---|
540 | return rc;
|
---|
541 | }
|
---|
542 | } else {
|
---|
543 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert: "
|
---|
544 | "local address specified or remote address not specified");
|
---|
545 | }
|
---|
546 |
|
---|
547 | raddr = !inet_addr_is_any(&mepp.remote.addr);
|
---|
548 | rport = mepp.remote.port != inet_port_any;
|
---|
549 | laddr = !inet_addr_is_any(&mepp.local.addr);
|
---|
550 | llink = mepp.local_link != 0;
|
---|
551 |
|
---|
552 | if (raddr && rport && laddr && !llink) {
|
---|
553 | return amap_insert_repla(map, &mepp, arg, flags, aepp);
|
---|
554 | } else if (!raddr && !rport && laddr && !llink) {
|
---|
555 | return amap_insert_laddr(map, &mepp, arg, flags, aepp);
|
---|
556 | } else if (!raddr && !rport && !laddr && llink) {
|
---|
557 | return amap_insert_llink(map, &mepp, arg, flags, aepp);
|
---|
558 | } else if (!raddr && !rport && !laddr && !llink) {
|
---|
559 | return amap_insert_unspec(map, &mepp, arg, flags, aepp);
|
---|
560 | } else {
|
---|
561 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_insert: invalid "
|
---|
562 | "combination of raddr=%d rport=%d laddr=%d llink=%d",
|
---|
563 | raddr, rport, laddr, llink);
|
---|
564 | return EINVAL;
|
---|
565 | }
|
---|
566 |
|
---|
567 | return EOK;
|
---|
568 | }
|
---|
569 |
|
---|
570 | /** Remove endpoint pair using repla as key from map.
|
---|
571 | *
|
---|
572 | * The endpoint pair must be present in the map, otherwise behavior
|
---|
573 | * is unspecified.
|
---|
574 | *
|
---|
575 | * @param map Association map
|
---|
576 | * @param epp Endpoint pair
|
---|
577 | */
|
---|
578 | static void amap_remove_repla(amap_t *map, inet_ep2_t *epp)
|
---|
579 | {
|
---|
580 | amap_repla_t *repla;
|
---|
581 | int rc;
|
---|
582 |
|
---|
583 | rc = amap_repla_find(map, &epp->remote, &epp->local.addr, &repla);
|
---|
584 | if (rc != EOK) {
|
---|
585 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_remove_repla: not found");
|
---|
586 | return;
|
---|
587 | }
|
---|
588 |
|
---|
589 | portrng_free_port(repla->portrng, epp->local.port);
|
---|
590 |
|
---|
591 | if (portrng_empty(repla->portrng))
|
---|
592 | amap_repla_remove(map, repla);
|
---|
593 | }
|
---|
594 |
|
---|
595 | /** Remove endpoint pair using laddr as key from map.
|
---|
596 | *
|
---|
597 | * The endpoint pair must be present in the map, otherwise behavior
|
---|
598 | * is unspecified.
|
---|
599 | *
|
---|
600 | * @param map Association map
|
---|
601 | * @param epp Endpoint pair
|
---|
602 | */
|
---|
603 | static void amap_remove_laddr(amap_t *map, inet_ep2_t *epp)
|
---|
604 | {
|
---|
605 | amap_laddr_t *laddr;
|
---|
606 | int rc;
|
---|
607 |
|
---|
608 | rc = amap_laddr_find(map, &epp->local.addr, &laddr);
|
---|
609 | if (rc != EOK) {
|
---|
610 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_remove_laddr: not found");
|
---|
611 | return;
|
---|
612 | }
|
---|
613 |
|
---|
614 | portrng_free_port(laddr->portrng, epp->local.port);
|
---|
615 |
|
---|
616 | if (portrng_empty(laddr->portrng))
|
---|
617 | amap_laddr_remove(map, laddr);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /** Remove endpoint pair using llink as key from map.
|
---|
621 | *
|
---|
622 | * The endpoint pair must be present in the map, otherwise behavior
|
---|
623 | * is unspecified.
|
---|
624 | *
|
---|
625 | * @param map Association map
|
---|
626 | * @param epp Endpoint pair
|
---|
627 | */
|
---|
628 | static void amap_remove_llink(amap_t *map, inet_ep2_t *epp)
|
---|
629 | {
|
---|
630 | amap_llink_t *llink;
|
---|
631 | int rc;
|
---|
632 |
|
---|
633 | rc = amap_llink_find(map, epp->local_link, &llink);
|
---|
634 | if (rc != EOK) {
|
---|
635 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_remove_llink: not found");
|
---|
636 | return;
|
---|
637 | }
|
---|
638 |
|
---|
639 | portrng_free_port(llink->portrng, epp->local.port);
|
---|
640 |
|
---|
641 | if (portrng_empty(llink->portrng))
|
---|
642 | amap_llink_remove(map, llink);
|
---|
643 | }
|
---|
644 |
|
---|
645 | /** Remove endpoint pair using unspec as key from map.
|
---|
646 | *
|
---|
647 | * The endpoint pair must be present in the map, otherwise behavior
|
---|
648 | * is unspecified.
|
---|
649 | *
|
---|
650 | * @param map Association map
|
---|
651 | * @param epp Endpoint pair
|
---|
652 | */
|
---|
653 | static void amap_remove_unspec(amap_t *map, inet_ep2_t *epp)
|
---|
654 | {
|
---|
655 | portrng_free_port(map->unspec, epp->local.port);
|
---|
656 | }
|
---|
657 |
|
---|
658 | /** Remove endpoint pair from map.
|
---|
659 | *
|
---|
660 | * The endpoint pair must be present in the map, otherwise behavior
|
---|
661 | * is unspecified.
|
---|
662 | *
|
---|
663 | * @param map Association map
|
---|
664 | * @param epp Endpoint pair
|
---|
665 | */
|
---|
666 | void amap_remove(amap_t *map, inet_ep2_t *epp)
|
---|
667 | {
|
---|
668 | bool raddr, rport, laddr, llink;
|
---|
669 |
|
---|
670 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_remove()");
|
---|
671 |
|
---|
672 | raddr = !inet_addr_is_any(&epp->remote.addr);
|
---|
673 | rport = epp->remote.port != inet_port_any;
|
---|
674 | laddr = !inet_addr_is_any(&epp->local.addr);
|
---|
675 | llink = epp->local_link != 0;
|
---|
676 |
|
---|
677 | if (raddr && rport && laddr && !llink) {
|
---|
678 | amap_remove_repla(map, epp);
|
---|
679 | } else if (!raddr && !rport && laddr && !llink) {
|
---|
680 | amap_remove_laddr(map, epp);
|
---|
681 | } else if (!raddr && !rport && !laddr && llink) {
|
---|
682 | amap_remove_llink(map, epp);
|
---|
683 | } else if (!raddr && !rport && !laddr && !llink) {
|
---|
684 | amap_remove_unspec(map, epp);
|
---|
685 | } else {
|
---|
686 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_remove: invalid "
|
---|
687 | "combination of raddr=%d rport=%d laddr=%d llink=%d",
|
---|
688 | raddr, rport, laddr, llink);
|
---|
689 | return;
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | /** Find association matching an endpoint pair.
|
---|
694 | *
|
---|
695 | * Used to find which association to deliver a datagram to.
|
---|
696 | *
|
---|
697 | * @param map Association map
|
---|
698 | * @param epp Endpoint pair
|
---|
699 | * @param rarg Place to store user argument for the matching association.
|
---|
700 | *
|
---|
701 | * @return EOK on success, ENOENT if not found.
|
---|
702 | */
|
---|
703 | int amap_find_match(amap_t *map, inet_ep2_t *epp, void **rarg)
|
---|
704 | {
|
---|
705 | int rc;
|
---|
706 | amap_repla_t *repla;
|
---|
707 | amap_laddr_t *laddr;
|
---|
708 | amap_llink_t *llink;
|
---|
709 |
|
---|
710 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "amap_find_match(llink=%zu)",
|
---|
711 | epp->local_link);
|
---|
712 |
|
---|
713 | /* Remode endpoint, local address */
|
---|
714 | rc = amap_repla_find(map, &epp->remote, &epp->local.addr, &repla);
|
---|
715 | if (rc == EOK) {
|
---|
716 | rc = portrng_find_port(repla->portrng, epp->local.port,
|
---|
717 | rarg);
|
---|
718 | if (rc == EOK) {
|
---|
719 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "Matched repla / "
|
---|
720 | "port %" PRIu16, epp->local.port);
|
---|
721 | return EOK;
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 | /* Local address */
|
---|
726 | rc = amap_laddr_find(map, &epp->local.addr, &laddr);
|
---|
727 | if (rc == EOK) {
|
---|
728 | rc = portrng_find_port(laddr->portrng, epp->local.port,
|
---|
729 | rarg);
|
---|
730 | if (rc == EOK) {
|
---|
731 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "Matched laddr / "
|
---|
732 | "port %" PRIu16, epp->local.port);
|
---|
733 | return EOK;
|
---|
734 | }
|
---|
735 | }
|
---|
736 |
|
---|
737 | /* Local link */
|
---|
738 | rc = amap_llink_find(map, epp->local_link, &llink);
|
---|
739 | if (epp->local_link != 0 && rc == EOK) {
|
---|
740 | rc = portrng_find_port(llink->portrng, epp->local.port,
|
---|
741 | rarg);
|
---|
742 | if (rc == EOK) {
|
---|
743 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "Matched llink / "
|
---|
744 | "port %" PRIu16, epp->local.port);
|
---|
745 | return EOK;
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | /* Unspecified */
|
---|
750 | rc = portrng_find_port(map->unspec, epp->local.port, rarg);
|
---|
751 | if (rc == EOK) {
|
---|
752 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "Matched unspec / port %" PRIu16,
|
---|
753 | epp->local.port);
|
---|
754 | return EOK;
|
---|
755 | }
|
---|
756 |
|
---|
757 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "No match.");
|
---|
758 | return ENOENT;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * @}
|
---|
763 | */
|
---|