1 | /*
|
---|
2 | * Copyright (c) 2021 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 inet
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <inet/eth_addr.h>
|
---|
41 | #include <inet/iplink_srv.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 |
|
---|
44 | #include "atrans.h"
|
---|
45 | #include "ethip.h"
|
---|
46 |
|
---|
47 | /** Address translation list (of ethip_atrans_t) */
|
---|
48 | static FIBRIL_MUTEX_INITIALIZE(atrans_list_lock);
|
---|
49 | static LIST_INITIALIZE(atrans_list);
|
---|
50 | static FIBRIL_CONDVAR_INITIALIZE(atrans_cv);
|
---|
51 |
|
---|
52 | static ethip_atrans_t *atrans_find(addr32_t ip_addr)
|
---|
53 | {
|
---|
54 | list_foreach(atrans_list, atrans_list, ethip_atrans_t, atrans) {
|
---|
55 | if (atrans->ip_addr == ip_addr)
|
---|
56 | return atrans;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return NULL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | errno_t atrans_add(addr32_t ip_addr, eth_addr_t *mac_addr)
|
---|
63 | {
|
---|
64 | ethip_atrans_t *atrans;
|
---|
65 | ethip_atrans_t *prev;
|
---|
66 |
|
---|
67 | atrans = calloc(1, sizeof(ethip_atrans_t));
|
---|
68 | if (atrans == NULL)
|
---|
69 | return ENOMEM;
|
---|
70 |
|
---|
71 | atrans->ip_addr = ip_addr;
|
---|
72 | atrans->mac_addr = *mac_addr;
|
---|
73 |
|
---|
74 | fibril_mutex_lock(&atrans_list_lock);
|
---|
75 | prev = atrans_find(ip_addr);
|
---|
76 | if (prev != NULL) {
|
---|
77 | list_remove(&prev->atrans_list);
|
---|
78 | free(prev);
|
---|
79 | }
|
---|
80 |
|
---|
81 | list_append(&atrans->atrans_list, &atrans_list);
|
---|
82 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
83 | fibril_condvar_broadcast(&atrans_cv);
|
---|
84 |
|
---|
85 | return EOK;
|
---|
86 | }
|
---|
87 |
|
---|
88 | errno_t atrans_remove(addr32_t ip_addr)
|
---|
89 | {
|
---|
90 | ethip_atrans_t *atrans;
|
---|
91 |
|
---|
92 | fibril_mutex_lock(&atrans_list_lock);
|
---|
93 | atrans = atrans_find(ip_addr);
|
---|
94 | if (atrans == NULL) {
|
---|
95 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
96 | return ENOENT;
|
---|
97 | }
|
---|
98 |
|
---|
99 | list_remove(&atrans->atrans_list);
|
---|
100 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
101 | free(atrans);
|
---|
102 |
|
---|
103 | return EOK;
|
---|
104 | }
|
---|
105 |
|
---|
106 | static errno_t atrans_lookup_locked(addr32_t ip_addr, eth_addr_t *mac_addr)
|
---|
107 | {
|
---|
108 | ethip_atrans_t *atrans = atrans_find(ip_addr);
|
---|
109 | if (atrans == NULL)
|
---|
110 | return ENOENT;
|
---|
111 |
|
---|
112 | *mac_addr = atrans->mac_addr;
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | errno_t atrans_lookup(addr32_t ip_addr, eth_addr_t *mac_addr)
|
---|
117 | {
|
---|
118 | errno_t rc;
|
---|
119 |
|
---|
120 | fibril_mutex_lock(&atrans_list_lock);
|
---|
121 | rc = atrans_lookup_locked(ip_addr, mac_addr);
|
---|
122 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
123 |
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 | static void atrans_lookup_timeout_handler(void *arg)
|
---|
128 | {
|
---|
129 | bool *timedout = (bool *)arg;
|
---|
130 |
|
---|
131 | fibril_mutex_lock(&atrans_list_lock);
|
---|
132 | *timedout = true;
|
---|
133 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
134 | fibril_condvar_broadcast(&atrans_cv);
|
---|
135 | }
|
---|
136 |
|
---|
137 | errno_t atrans_lookup_timeout(addr32_t ip_addr, usec_t timeout,
|
---|
138 | eth_addr_t *mac_addr)
|
---|
139 | {
|
---|
140 | fibril_timer_t *t;
|
---|
141 | bool timedout;
|
---|
142 | errno_t rc;
|
---|
143 |
|
---|
144 | t = fibril_timer_create(NULL);
|
---|
145 | if (t == NULL)
|
---|
146 | return ENOMEM;
|
---|
147 |
|
---|
148 | timedout = false;
|
---|
149 | fibril_timer_set(t, timeout, atrans_lookup_timeout_handler, &timedout);
|
---|
150 |
|
---|
151 | fibril_mutex_lock(&atrans_list_lock);
|
---|
152 |
|
---|
153 | while ((rc = atrans_lookup_locked(ip_addr, mac_addr)) == ENOENT &&
|
---|
154 | !timedout) {
|
---|
155 | fibril_condvar_wait(&atrans_cv, &atrans_list_lock);
|
---|
156 | }
|
---|
157 |
|
---|
158 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
159 | (void) fibril_timer_clear(t);
|
---|
160 | fibril_timer_destroy(t);
|
---|
161 |
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** @}
|
---|
166 | */
|
---|