BA / Mbed OS BaBoRo1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers icmpv6_prefix.c Source File

icmpv6_prefix.c

00001 /*
00002  * Copyright (c) 2015-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 
00018 #include "nsconfig.h"
00019 #include "string.h"
00020 #include "ns_types.h"
00021 #include "ns_trace.h"
00022 #include "nsdynmemLIB.h"
00023 #include "ns_list.h"
00024 #include "Common_Protocols/icmpv6_prefix.h"
00025 #include "common_functions.h"
00026 
00027 /*
00028  * \file icmpv6_prefix.c
00029  * \brief Add short description about this file!!!
00030  *
00031  */
00032 prefix_entry_t *icmpv6_prefix_add(prefix_list_t *list, const uint8_t *prefixPtr, uint8_t prefix_len, uint32_t lifeTime, uint32_t prefTime, uint8_t flags)
00033 {
00034     prefix_entry_t *entry;
00035 
00036     entry = icmpv6_prefix_compare(list, prefixPtr, prefix_len);
00037     if (entry) {
00038         entry->options = flags;
00039         entry->lifetime = lifeTime;
00040         entry->preftime = prefTime;
00041         return entry;
00042     }
00043 
00044     entry = ns_dyn_mem_alloc(sizeof(prefix_entry_t));
00045     if (entry) {
00046         entry->prefix_len = prefix_len;
00047         entry->options = 0xff;
00048         entry->lifetime = lifeTime;
00049         entry->preftime = prefTime;
00050         memset(entry->prefix, 0, 16);
00051         bitcopy0(entry->prefix, prefixPtr, prefix_len);
00052         ns_list_add_to_end(list, entry);
00053     }
00054     return entry;
00055 }
00056 
00057 prefix_entry_t *icmpv6_prefix_compare(prefix_list_t *list, const uint8_t *addr, uint8_t prefix_len)
00058 {
00059     ns_list_foreach(prefix_entry_t, cur, list) {
00060         if (cur->prefix_len == prefix_len && bitsequal(cur->prefix, addr, prefix_len)) {
00061             return cur;
00062         }
00063     }
00064     return NULL;
00065 }
00066 
00067 
00068 void icmpv6_prefix_list_free(prefix_list_t *list)
00069 {
00070     ns_list_foreach_safe(prefix_entry_t, cur, list) {
00071         ns_list_remove(list, cur);
00072         ns_dyn_mem_free(cur);
00073     }
00074 }