Blame view

RIOT/sys/net/routing/nhdp/lib_table.c 5.83 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  /*
   * Copyright (C) 2014 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     nhdp
   * @{
   *
   * @file
   * @brief       Local Information Base implementation for NHDP
   *
   * @author      Fabian Nack <nack@inf.fu-berlin.de>
   *
   * @}
   */
  
  #include "mutex.h"
  #include "utlist.h"
  #include "kernel_types.h"
  
  #include "rfc5444/rfc5444_iana.h"
  #include "rfc5444/rfc5444_writer.h"
  
  #include "lib_table.h"
  #include "nhdp_address.h"
  #include "nhdp_writer.h"
  
  /* Internal variables */
  static mutex_t mtx_lib_access = MUTEX_INIT;
  static lib_entry_t *lib_entry_head = NULL;
  
  /* Internal function prototypes */
  static int create_if_entry(kernel_pid_t if_pid, nhdp_addr_t *addr);
  static int add_address_to_if(lib_entry_t *if_entry, nhdp_addr_t *addr);
  
  
  /*---------------------------------------------------------------------------*
   *                       Local Information Base API                          *
   *---------------------------------------------------------------------------*/
  
  int lib_add_if_addr(kernel_pid_t if_pid, nhdp_addr_t *addr)
  {
      lib_entry_t *lib_elt;
      nhdp_addr_entry_t *addr_elt;
      int result = -1;
  
      mutex_lock(&mtx_lib_access);
  
      /* Check whether the given interface is already registered */
      LL_FOREACH(lib_entry_head, lib_elt) {
          if (lib_elt->if_pid == if_pid) {
              LL_FOREACH(lib_entry_head->if_addr_list_head, addr_elt) {
                  if (addr_elt->address == addr) {
                      /* Address already known for the interface */
                      result = 0;
                      break;
                  }
              }
  
              if (result) {
                  /* Existing interface entry, but new address */
                  result = add_address_to_if(lib_elt, addr);
                  break;
              }
          }
      }
  
      if (result) {
          /* New interface, create a lib entry */
          result = create_if_entry(if_pid, addr);
      }
  
      mutex_unlock(&mtx_lib_access);
  
      return result;
  }
  
  void lib_rem_if(kernel_pid_t if_pid)
  {
      lib_entry_t *lib_elt, *lib_tmp;
  
      mutex_lock(&mtx_lib_access);
  
      LL_FOREACH_SAFE(lib_entry_head, lib_elt, lib_tmp) {
          if (lib_elt->if_pid == if_pid) {
              nhdp_free_addr_list(lib_elt->if_addr_list_head);
              LL_DELETE(lib_entry_head, lib_elt);
              free(lib_elt);
              break;
          }
      }
  
      mutex_unlock(&mtx_lib_access);
  }
  
  void lib_fill_wr_addresses(kernel_pid_t if_pid, struct rfc5444_writer *wr)
  {
      lib_entry_t *lib_elt;
      nhdp_addr_entry_t *add_tmp;
  
      mutex_lock(&mtx_lib_access);
  
      /* First fill the list for LOCAL_IF = THIS_IF */
      LL_FOREACH(lib_entry_head, lib_elt) {
          if (lib_elt->if_pid == if_pid) {
              LL_FOREACH(lib_elt->if_addr_list_head, add_tmp) {
                  nhdp_writer_add_addr(wr, add_tmp->address,
                                       RFC5444_ADDRTLV_LOCAL_IF, RFC5444_LOCALIF_THIS_IF,
                                       NHDP_METRIC_UNKNOWN, NHDP_METRIC_UNKNOWN);
                  add_tmp->address->in_tmp_table = NHDP_ADDR_TMP_ANY;
              }
              break;
          }
      }
  
      /* Second fill the list for LOCAL_IF = OTHER_IF */
      LL_FOREACH(lib_entry_head, lib_elt) {
          if (lib_elt->if_pid != if_pid) {
              LL_FOREACH(lib_elt->if_addr_list_head, add_tmp) {
                  /* Check if this address is not already included in a list */
                  if (!NHDP_ADDR_TMP_IN_ANY(add_tmp->address)) {
                      /* Address can be added */
                      nhdp_writer_add_addr(wr, add_tmp->address,
                                           RFC5444_ADDRTLV_LOCAL_IF, RFC5444_LOCALIF_OTHER_IF,
                                           NHDP_METRIC_UNKNOWN, NHDP_METRIC_UNKNOWN);
                      add_tmp->address->in_tmp_table = NHDP_ADDR_TMP_ANY;
                  }
              }
          }
      }
  
      mutex_unlock(&mtx_lib_access);
  }
  
  uint8_t lib_is_reg_addr(kernel_pid_t if_pid, nhdp_addr_t *addr)
  {
      lib_entry_t *lib_elt;
      nhdp_addr_entry_t *addr_elt;
  
      LL_FOREACH(lib_entry_head, lib_elt) {
          LL_FOREACH(lib_elt->if_addr_list_head, addr_elt) {
              if (addr_elt->address == addr) {
                  if (lib_elt->if_pid == if_pid) {
                      /* Given address is assigned to the given IF */
                      return 1;
                  }
  
                  /* Given address is assigned to any other IF */
                  return 2;
              }
          }
      }
      return 0;
  }
  
  
  /*------------------------------------------------------------------------------------*/
  /*                                Internal functions                                  */
  /*------------------------------------------------------------------------------------*/
  
  /**
   * Create an entry for a newly registered interface
   */
  static int create_if_entry(kernel_pid_t if_pid, nhdp_addr_t *addr)
  {
      lib_entry_t *new_entry;
  
      new_entry = (lib_entry_t *) malloc(sizeof(lib_entry_t));
  
      if (!new_entry) {
          /* Insufficient memory */
          return -1;
      }
  
      new_entry->if_addr_list_head = NULL;
      new_entry->if_pid = if_pid;
  
      if (add_address_to_if(new_entry, addr)) {
          /* Insufficient memory */
          free(new_entry);
          return -1;
      }
  
      LL_PREPEND(lib_entry_head, new_entry);
  
      return 0;
  }
  
  /**
   * Add another address to an interface entry
   */
  static int add_address_to_if(lib_entry_t *if_entry, nhdp_addr_t *addr)
  {
      nhdp_addr_entry_t *new_entry = (nhdp_addr_entry_t *) malloc(sizeof(nhdp_addr_entry_t));
  
      if (!new_entry) {
          /* Insufficient memory */
          return -1;
      }
  
      /* Increment usage counter of address in central NHDP address storage */
      addr->usg_count++;
      new_entry->address = addr;
      LL_PREPEND(if_entry->if_addr_list_head, new_entry);
  
      return 0;
  }