[f9d3dd4] | 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) */
|
---|
| 47 | static FIBRIL_MUTEX_INITIALIZE(atrans_list_lock);
|
---|
| 48 | static LIST_INITIALIZE(atrans_list);
|
---|
[3d016ac] | 49 | static FIBRIL_CONDVAR_INITIALIZE(atrans_cv);
|
---|
[f9d3dd4] | 50 |
|
---|
[d8b47eca] | 51 | static ethip_atrans_t *atrans_find(addr32_t ip_addr)
|
---|
[f9d3dd4] | 52 | {
|
---|
[feeac0d] | 53 | list_foreach(atrans_list, atrans_list, ethip_atrans_t, atrans) {
|
---|
[a2e3ee6] | 54 | if (atrans->ip_addr == ip_addr)
|
---|
[f9d3dd4] | 55 | return atrans;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return NULL;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[d8b47eca] | 61 | int atrans_add(addr32_t ip_addr, addr48_t mac_addr)
|
---|
[f9d3dd4] | 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 |
|
---|
[a2e3ee6] | 70 | atrans->ip_addr = ip_addr;
|
---|
[02a09ed] | 71 | addr48(mac_addr, atrans->mac_addr);
|
---|
[f9d3dd4] | 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);
|
---|
[3d016ac] | 82 | fibril_condvar_broadcast(&atrans_cv);
|
---|
[f9d3dd4] | 83 |
|
---|
| 84 | return EOK;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[d8b47eca] | 87 | int atrans_remove(addr32_t ip_addr)
|
---|
[f9d3dd4] | 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 |
|
---|
[f303f2cf] | 105 | static int atrans_lookup_locked(addr32_t ip_addr, addr48_t mac_addr)
|
---|
[f9d3dd4] | 106 | {
|
---|
[02a09ed] | 107 | ethip_atrans_t *atrans = atrans_find(ip_addr);
|
---|
[f303f2cf] | 108 | if (atrans == NULL)
|
---|
[f9d3dd4] | 109 | return ENOENT;
|
---|
[f303f2cf] | 110 |
|
---|
[02a09ed] | 111 | addr48(atrans->mac_addr, mac_addr);
|
---|
[f9d3dd4] | 112 | return EOK;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[f303f2cf] | 115 | int 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 |
|
---|
| 126 | static 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 |
|
---|
| 136 | int atrans_lookup_timeout(addr32_t ip_addr, suseconds_t timeout,
|
---|
| 137 | addr48_t mac_addr)
|
---|
[3d016ac] | 138 | {
|
---|
[f303f2cf] | 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 |
|
---|
[3d016ac] | 150 | fibril_mutex_lock(&atrans_list_lock);
|
---|
[f303f2cf] | 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 |
|
---|
[3d016ac] | 157 | fibril_mutex_unlock(&atrans_list_lock);
|
---|
[f303f2cf] | 158 | (void) fibril_timer_clear(t);
|
---|
| 159 |
|
---|
[3d016ac] | 160 | return rc;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[f9d3dd4] | 163 | /** @}
|
---|
| 164 | */
|
---|