Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pana_relay_table.c Source File

pana_relay_table.c

00001 /*
00002  * Copyright (c) 2013-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "nsconfig.h"
00018 #include "ns_types.h"
00019 #include "string.h"
00020 #include "Security/PANA/pana_relay_table.h"
00021 #include "nsdynmemLIB.h"
00022 
00023 #ifdef PANA
00024 
00025 static NS_LARGE relay_table_entry_t *relay_first_info = 0;
00026 
00027 
00028 void pana_relay_table_init(void)
00029 {
00030     /* Init Double linked Routing Table */
00031 
00032     if (relay_first_info) {
00033         relay_table_entry_t *cur = 0;
00034         while (relay_first_info) {
00035             cur = relay_first_info;
00036             relay_first_info = cur->next_info;
00037             ns_dyn_mem_free(cur);
00038         }
00039     }
00040 }
00041 
00042 
00043 relay_table_entry_t *pana_relay_table_update(uint8_t *address)
00044 {
00045     relay_table_entry_t *ret_val = 0;
00046 
00047     relay_table_entry_t *cur = 0;
00048     if (relay_first_info) {
00049         cur = relay_first_info;
00050         while (cur) {
00051 
00052             if (memcmp((uint8_t *)cur->relay_address, (uint8_t *) address, 16) == 0) {
00053                 /* General Update part */
00054                 cur->ttl = 20;
00055                 ret_val = cur;
00056                 break;
00057             }
00058             if (cur->next_info) {
00059                 cur = cur->next_info;
00060             } else {
00061                 break;
00062             }
00063         }
00064     }
00065 
00066     //allocate new
00067     if (ret_val == 0) {
00068         ret_val = (relay_table_entry_t *) ns_dyn_mem_alloc(sizeof(relay_table_entry_t));
00069         if (ret_val) {
00070             if (cur != 0) {
00071                 cur->next_info = ret_val;
00072                 ret_val->prev_info = cur;
00073             } else {
00074                 ret_val->prev_info = 0;
00075             }
00076             ret_val->next_info = 0;
00077             /* Copy Adrress IPv6 Address */
00078             memcpy((uint8_t *)ret_val->relay_address, address, 16);
00079             ret_val->ttl = 20;
00080             if (relay_first_info == 0) {
00081                 relay_first_info = ret_val;
00082             }
00083         }
00084     }
00085     return ret_val;
00086 }
00087 
00088 relay_table_entry_t *pana_relay_detect(uint8_t *address)
00089 {
00090     relay_table_entry_t *cur = 0;
00091     if (relay_first_info) {
00092         cur = relay_first_info;
00093         while (cur) {
00094             if (memcmp((uint8_t *)cur->relay_address, (uint8_t *) address, 16) == 0) {
00095                 /* General Update part */
00096                 return cur;
00097             }
00098             cur = cur->next_info;
00099         }
00100     }
00101     return 0;
00102 }
00103 
00104 
00105 void pana_relay_ttl_update(uint16_t ttl_time)
00106 {
00107     relay_table_entry_t *cur = 0;
00108     if (relay_first_info) {
00109         relay_table_entry_t *prev = 0;
00110         relay_table_entry_t *next = 0;
00111         cur = relay_first_info;
00112         while (cur) {
00113             if (cur->ttl < ttl_time) {
00114                 cur->ttl -= ttl_time;
00115                 cur = cur->next_info;
00116             } else {
00117                 prev = cur->prev_info;
00118                 next = cur->next_info;
00119                 if (prev) { //Not First
00120 
00121                     if (next) { //Not Last
00122                         prev->next_info = next;
00123                         next->prev_info = prev; //Link next to new prev
00124                     } else { //Last One
00125                         prev->next_info = 0;/* New last entry */
00126                     }
00127 
00128                 } else { //FIRST
00129                     if (next == 0) { /* Last entry */
00130                         relay_first_info = 0; //Reset Route table
00131                     } else { //New First
00132                         relay_first_info = next; // Setup new First
00133                         next->prev_info = 0;
00134                     }
00135                 }
00136                 ns_dyn_mem_free(cur);
00137                 cur = next;
00138             }
00139 
00140         }
00141     }
00142 }
00143 #endif