| 1 | /*
|
|---|
| 2 | * Copyright (c) 2013 Antonin Steinhauser
|
|---|
| 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 | #include "ntrans.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Address translation list (of inet_ntrans_t) */
|
|---|
| 45 | static FIBRIL_MUTEX_INITIALIZE(ntrans_list_lock);
|
|---|
| 46 | static LIST_INITIALIZE(ntrans_list);
|
|---|
| 47 | static FIBRIL_CONDVAR_INITIALIZE(ntrans_cv);
|
|---|
| 48 |
|
|---|
| 49 | static inet_ntrans_t *ntrans_find(addr128_t ip_addr)
|
|---|
| 50 | {
|
|---|
| 51 | list_foreach(ntrans_list, link) {
|
|---|
| 52 | inet_ntrans_t *ntrans = list_get_instance(link,
|
|---|
| 53 | inet_ntrans_t, ntrans_list);
|
|---|
| 54 |
|
|---|
| 55 | if (addr128_compare(ntrans->ip_addr, ip_addr))
|
|---|
| 56 | return ntrans;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | return NULL;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | int ntrans_add(addr128_t ip_addr, addr48_t mac_addr)
|
|---|
| 63 | {
|
|---|
| 64 | inet_ntrans_t *ntrans;
|
|---|
| 65 | inet_ntrans_t *prev;
|
|---|
| 66 |
|
|---|
| 67 | ntrans = calloc(1, sizeof(inet_ntrans_t));
|
|---|
| 68 | if (ntrans == NULL)
|
|---|
| 69 | return ENOMEM;
|
|---|
| 70 |
|
|---|
| 71 | addr128(ip_addr, ntrans->ip_addr);
|
|---|
| 72 | addr48(mac_addr, ntrans->mac_addr);
|
|---|
| 73 |
|
|---|
| 74 | fibril_mutex_lock(&ntrans_list_lock);
|
|---|
| 75 | prev = ntrans_find(ip_addr);
|
|---|
| 76 | if (prev != NULL) {
|
|---|
| 77 | list_remove(&prev->ntrans_list);
|
|---|
| 78 | free(prev);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | list_append(&ntrans->ntrans_list, &ntrans_list);
|
|---|
| 82 | fibril_mutex_unlock(&ntrans_list_lock);
|
|---|
| 83 | fibril_condvar_broadcast(&ntrans_cv);
|
|---|
| 84 |
|
|---|
| 85 | return EOK;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | int ntrans_remove(addr128_t ip_addr)
|
|---|
| 89 | {
|
|---|
| 90 | inet_ntrans_t *ntrans;
|
|---|
| 91 |
|
|---|
| 92 | fibril_mutex_lock(&ntrans_list_lock);
|
|---|
| 93 | ntrans = ntrans_find(ip_addr);
|
|---|
| 94 | if (ntrans == NULL) {
|
|---|
| 95 | fibril_mutex_unlock(&ntrans_list_lock);
|
|---|
| 96 | return ENOENT;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | list_remove(&ntrans->ntrans_list);
|
|---|
| 100 | fibril_mutex_unlock(&ntrans_list_lock);
|
|---|
| 101 | free(ntrans);
|
|---|
| 102 |
|
|---|
| 103 | return EOK;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | int ntrans_lookup(addr128_t ip_addr, addr48_t mac_addr)
|
|---|
| 107 | {
|
|---|
| 108 | fibril_mutex_lock(&ntrans_list_lock);
|
|---|
| 109 | inet_ntrans_t *ntrans = ntrans_find(ip_addr);
|
|---|
| 110 | if (ntrans == NULL) {
|
|---|
| 111 | fibril_mutex_unlock(&ntrans_list_lock);
|
|---|
| 112 | return ENOENT;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | fibril_mutex_unlock(&ntrans_list_lock);
|
|---|
| 116 | addr48(ntrans->mac_addr, mac_addr);
|
|---|
| 117 | return EOK;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | int ntrans_wait_timeout(suseconds_t timeout)
|
|---|
| 121 | {
|
|---|
| 122 | fibril_mutex_lock(&ntrans_list_lock);
|
|---|
| 123 | int rc = fibril_condvar_wait_timeout(&ntrans_cv, &ntrans_list_lock,
|
|---|
| 124 | timeout);
|
|---|
| 125 | fibril_mutex_unlock(&ntrans_list_lock);
|
|---|
| 126 |
|
|---|
| 127 | return rc;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** @}
|
|---|
| 131 | */
|
|---|