uip-neighbor.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006, Swedish Institute of Computer Science.
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the uIP TCP/IP stack
  30. *
  31. * $Id: uip-neighbor.c,v 1.2 2006/06/12 08:00:30 adam Exp $
  32. */
  33. /**
  34. * \file
  35. * Database of link-local neighbors, used by IPv6 code and
  36. * to be used by a future ARP code rewrite.
  37. * \author
  38. * Adam Dunkels <adam@sics.se>
  39. */
  40. #include "uip-neighbor.h"
  41. #include <string.h>
  42. #define MAX_TIME 128
  43. #ifdef UIP_NEIGHBOR_CONF_ENTRIES
  44. #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
  45. #else /* UIP_NEIGHBOR_CONF_ENTRIES */
  46. #define ENTRIES 8
  47. #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
  48. struct neighbor_entry {
  49. uip_ipaddr_t ipaddr;
  50. struct uip_neighbor_addr addr;
  51. u8_t time;
  52. };
  53. static struct neighbor_entry entries[ENTRIES];
  54. /*---------------------------------------------------------------------------*/
  55. void
  56. uip_neighbor_init(void)
  57. {
  58. int i;
  59. for(i = 0; i < ENTRIES; ++i) {
  60. entries[i].time = MAX_TIME;
  61. }
  62. }
  63. /*---------------------------------------------------------------------------*/
  64. void
  65. uip_neighbor_periodic(void)
  66. {
  67. int i;
  68. for(i = 0; i < ENTRIES; ++i) {
  69. if(entries[i].time < MAX_TIME) {
  70. entries[i].time++;
  71. }
  72. }
  73. }
  74. /*---------------------------------------------------------------------------*/
  75. void
  76. uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr)
  77. {
  78. int i, oldest;
  79. u8_t oldest_time;
  80. printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
  81. addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
  82. addr->addr.addr[4], addr->addr.addr[5]);
  83. /* Find the first unused entry or the oldest used entry. */
  84. oldest_time = 0;
  85. oldest = 0;
  86. for(i = 0; i < ENTRIES; ++i) {
  87. if(entries[i].time == MAX_TIME) {
  88. oldest = i;
  89. break;
  90. }
  91. if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) {
  92. oldest = i;
  93. break;
  94. }
  95. if(entries[i].time > oldest_time) {
  96. oldest = i;
  97. oldest_time = entries[i].time;
  98. }
  99. }
  100. /* Use the oldest or first free entry (either pointed to by the
  101. "oldest" variable). */
  102. entries[oldest].time = 0;
  103. uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr);
  104. memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
  105. }
  106. /*---------------------------------------------------------------------------*/
  107. static struct neighbor_entry *
  108. find_entry(uip_ipaddr_t ipaddr)
  109. {
  110. int i;
  111. for(i = 0; i < ENTRIES; ++i) {
  112. if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) {
  113. return &entries[i];
  114. }
  115. }
  116. return NULL;
  117. }
  118. /*---------------------------------------------------------------------------*/
  119. void
  120. uip_neighbor_update(uip_ipaddr_t ipaddr)
  121. {
  122. struct neighbor_entry *e;
  123. e = find_entry(ipaddr);
  124. if(e != NULL) {
  125. e->time = 0;
  126. }
  127. }
  128. /*---------------------------------------------------------------------------*/
  129. struct uip_neighbor_addr *
  130. uip_neighbor_lookup(uip_ipaddr_t ipaddr)
  131. {
  132. struct neighbor_entry *e;
  133. e = find_entry(ipaddr);
  134. if(e != NULL) {
  135. /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
  136. e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
  137. e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
  138. return &e->addr;
  139. }
  140. return NULL;
  141. }
  142. /*---------------------------------------------------------------------------*/