source: mainline/uspace/srv/net/ethip/atrans.c@ f303f2cf

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

Make sure we wait the specified time before giving up on ARP translation.

  • Property mode set to 100644
File size: 4.1 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 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/iplink_srv.h>
41#include <stdlib.h>
42
43#include "atrans.h"
44#include "ethip.h"
45
46/** Address translation list (of ethip_atrans_t) */
47static FIBRIL_MUTEX_INITIALIZE(atrans_list_lock);
48static LIST_INITIALIZE(atrans_list);
49static FIBRIL_CONDVAR_INITIALIZE(atrans_cv);
50
51static ethip_atrans_t *atrans_find(addr32_t ip_addr)
52{
53 list_foreach(atrans_list, atrans_list, ethip_atrans_t, atrans) {
54 if (atrans->ip_addr == ip_addr)
55 return atrans;
56 }
57
58 return NULL;
59}
60
61int atrans_add(addr32_t ip_addr, addr48_t mac_addr)
62{
63 ethip_atrans_t *atrans;
64 ethip_atrans_t *prev;
65
66 atrans = calloc(1, sizeof(ethip_atrans_t));
67 if (atrans == NULL)
68 return ENOMEM;
69
70 atrans->ip_addr = ip_addr;
71 addr48(mac_addr, atrans->mac_addr);
72
73 fibril_mutex_lock(&atrans_list_lock);
74 prev = atrans_find(ip_addr);
75 if (prev != NULL) {
76 list_remove(&prev->atrans_list);
77 free(prev);
78 }
79
80 list_append(&atrans->atrans_list, &atrans_list);
81 fibril_mutex_unlock(&atrans_list_lock);
82 fibril_condvar_broadcast(&atrans_cv);
83
84 return EOK;
85}
86
87int atrans_remove(addr32_t ip_addr)
88{
89 ethip_atrans_t *atrans;
90
91 fibril_mutex_lock(&atrans_list_lock);
92 atrans = atrans_find(ip_addr);
93 if (atrans == NULL) {
94 fibril_mutex_unlock(&atrans_list_lock);
95 return ENOENT;
96 }
97
98 list_remove(&atrans->atrans_list);
99 fibril_mutex_unlock(&atrans_list_lock);
100 free(atrans);
101
102 return EOK;
103}
104
105static int atrans_lookup_locked(addr32_t ip_addr, addr48_t mac_addr)
106{
107 ethip_atrans_t *atrans = atrans_find(ip_addr);
108 if (atrans == NULL)
109 return ENOENT;
110
111 addr48(atrans->mac_addr, mac_addr);
112 return EOK;
113}
114
115int atrans_lookup(addr32_t ip_addr, addr48_t mac_addr)
116{
117 int rc;
118
119 fibril_mutex_lock(&atrans_list_lock);
120 rc = atrans_lookup_locked(ip_addr, mac_addr);
121 fibril_mutex_unlock(&atrans_list_lock);
122
123 return rc;
124}
125
126static void atrans_lookup_timeout_handler(void *arg)
127{
128 bool *timedout = (bool *)arg;
129
130 fibril_mutex_lock(&atrans_list_lock);
131 *timedout = true;
132 fibril_mutex_unlock(&atrans_list_lock);
133 fibril_condvar_broadcast(&atrans_cv);
134}
135
136int atrans_lookup_timeout(addr32_t ip_addr, suseconds_t timeout,
137 addr48_t mac_addr)
138{
139 fibril_timer_t *t;
140 bool timedout;
141 int rc;
142
143 t = fibril_timer_create(NULL);
144 if (t == NULL)
145 return ENOMEM;
146
147 timedout = false;
148 fibril_timer_set(t, timeout, atrans_lookup_timeout_handler, &timedout);
149
150 fibril_mutex_lock(&atrans_list_lock);
151
152 while ((rc = atrans_lookup_locked(ip_addr, mac_addr)) == ENOENT &&
153 !timedout) {
154 fibril_condvar_wait(&atrans_cv, &atrans_list_lock);
155 }
156
157 fibril_mutex_unlock(&atrans_list_lock);
158 (void) fibril_timer_clear(t);
159
160 return rc;
161}
162
163/** @}
164 */
Note: See TracBrowser for help on using the repository browser.