Get rid of sys/time.h
[helenos.git] / uspace / srv / net / ethip / atrans.c
blob1e8f6a25f6b6549e8287e9c3201ee76c7c1b01fa
1 /*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup inet
30 * @{
32 /**
33 * @file
34 * @brief
37 #include <adt/list.h>
38 #include <errno.h>
39 #include <fibril_synch.h>
40 #include <inet/iplink_srv.h>
41 #include <stdlib.h>
43 #include "atrans.h"
44 #include "ethip.h"
46 /** Address translation list (of ethip_atrans_t) */
47 static FIBRIL_MUTEX_INITIALIZE(atrans_list_lock);
48 static LIST_INITIALIZE(atrans_list);
49 static FIBRIL_CONDVAR_INITIALIZE(atrans_cv);
51 static ethip_atrans_t *atrans_find(addr32_t ip_addr)
53 list_foreach(atrans_list, atrans_list, ethip_atrans_t, atrans) {
54 if (atrans->ip_addr == ip_addr)
55 return atrans;
58 return NULL;
61 errno_t atrans_add(addr32_t ip_addr, addr48_t mac_addr)
63 ethip_atrans_t *atrans;
64 ethip_atrans_t *prev;
66 atrans = calloc(1, sizeof(ethip_atrans_t));
67 if (atrans == NULL)
68 return ENOMEM;
70 atrans->ip_addr = ip_addr;
71 addr48(mac_addr, atrans->mac_addr);
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);
80 list_append(&atrans->atrans_list, &atrans_list);
81 fibril_mutex_unlock(&atrans_list_lock);
82 fibril_condvar_broadcast(&atrans_cv);
84 return EOK;
87 errno_t atrans_remove(addr32_t ip_addr)
89 ethip_atrans_t *atrans;
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;
98 list_remove(&atrans->atrans_list);
99 fibril_mutex_unlock(&atrans_list_lock);
100 free(atrans);
102 return EOK;
105 static errno_t atrans_lookup_locked(addr32_t ip_addr, addr48_t mac_addr)
107 ethip_atrans_t *atrans = atrans_find(ip_addr);
108 if (atrans == NULL)
109 return ENOENT;
111 addr48(atrans->mac_addr, mac_addr);
112 return EOK;
115 errno_t atrans_lookup(addr32_t ip_addr, addr48_t mac_addr)
117 errno_t rc;
119 fibril_mutex_lock(&atrans_list_lock);
120 rc = atrans_lookup_locked(ip_addr, mac_addr);
121 fibril_mutex_unlock(&atrans_list_lock);
123 return rc;
126 static void atrans_lookup_timeout_handler(void *arg)
128 bool *timedout = (bool *)arg;
130 fibril_mutex_lock(&atrans_list_lock);
131 *timedout = true;
132 fibril_mutex_unlock(&atrans_list_lock);
133 fibril_condvar_broadcast(&atrans_cv);
136 errno_t atrans_lookup_timeout(addr32_t ip_addr, usec_t timeout,
137 addr48_t mac_addr)
139 fibril_timer_t *t;
140 bool timedout;
141 errno_t rc;
143 t = fibril_timer_create(NULL);
144 if (t == NULL)
145 return ENOMEM;
147 timedout = false;
148 fibril_timer_set(t, timeout, atrans_lookup_timeout_handler, &timedout);
150 fibril_mutex_lock(&atrans_list_lock);
152 while ((rc = atrans_lookup_locked(ip_addr, mac_addr)) == ENOENT &&
153 !timedout) {
154 fibril_condvar_wait(&atrans_cv, &atrans_list_lock);
157 fibril_mutex_unlock(&atrans_list_lock);
158 (void) fibril_timer_clear(t);
159 fibril_timer_destroy(t);
161 return rc;
164 /** @}