PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/net/neighbor/neighbor_add.c

https://bitbucket.org/hg42/nuttx
C | 134 lines | 48 code | 21 blank | 65 comment | 9 complexity | adf5d556e56a031116aa64d8013288a8 MD5 | raw file
Possible License(s): 0BSD
  1. /****************************************************************************
  2. * net/neighbor/neighbor_add.c
  3. *
  4. * Copyright (C) 2007-2009, 2015, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * A leverage of logic from uIP which also has a BSD style license
  8. *
  9. * Copyright (c) 2006, Swedish Institute of Computer Science. All rights
  10. * reserved.
  11. * Author: Adam Dunkels <adam@sics.se>
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of the Institute nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <stdint.h>
  44. #include <string.h>
  45. #include <assert.h>
  46. #include <debug.h>
  47. #include <net/if.h>
  48. #include <nuttx/net/net.h>
  49. #include <nuttx/net/ip.h>
  50. #include "netdev/netdev.h"
  51. #include "neighbor/neighbor.h"
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: neighbor_add
  57. *
  58. * Description:
  59. * Add the new address association to the Neighbor Table (if it is not
  60. * already there).
  61. *
  62. * Input Parameters:
  63. * dev - Driver instance associated with the MAC
  64. * ipaddr - The IPv6 address of the mapping.
  65. * addr - The link layer address of the mapping
  66. *
  67. * Returned Value:
  68. * None
  69. *
  70. ****************************************************************************/
  71. void neighbor_add(FAR struct net_driver_s *dev, FAR net_ipv6addr_t ipaddr,
  72. FAR uint8_t *addr)
  73. {
  74. uint8_t lltype;
  75. uint8_t oldest_time;
  76. int oldest_ndx;
  77. int i;
  78. DEBUGASSERT(dev != NULL && addr != NULL);
  79. /* Find the first unused entry or the oldest used entry. */
  80. oldest_time = 0;
  81. oldest_ndx = 0;
  82. lltype = dev->d_lltype;
  83. for (i = 0; i < CONFIG_NET_IPv6_NCONF_ENTRIES; ++i)
  84. {
  85. if (g_neighbors[i].ne_time == NEIGHBOR_MAXTIME)
  86. {
  87. oldest_ndx = i;
  88. break;
  89. }
  90. if (g_neighbors[i].ne_addr.na_lltype == lltype &&
  91. net_ipv6addr_cmp(g_neighbors[i].ne_ipaddr, ipaddr))
  92. {
  93. oldest_ndx = i;
  94. break;
  95. }
  96. if (g_neighbors[i].ne_time > oldest_time)
  97. {
  98. oldest_ndx = i;
  99. oldest_time = g_neighbors[i].ne_time;
  100. }
  101. }
  102. /* Use the oldest or first free entry (either pointed to by the
  103. * "oldest_ndx" variable).
  104. */
  105. g_neighbors[oldest_ndx].ne_time = 0;
  106. net_ipv6addr_copy(g_neighbors[oldest_ndx].ne_ipaddr, ipaddr);
  107. g_neighbors[oldest_ndx].ne_addr.na_lltype = lltype;
  108. g_neighbors[oldest_ndx].ne_addr.na_llsize = netdev_dev_lladdrsize(dev);
  109. memcpy(&g_neighbors[oldest_ndx].ne_addr.u, addr,
  110. g_neighbors[oldest_ndx].ne_addr.na_llsize);
  111. /* Dump the contents of the new entry */
  112. neighbor_dumpentry("Added entry", &g_neighbors[oldest_ndx]);
  113. }