aboutsummaryrefslogtreecommitdiffstats
path: root/src/include/nstack_rd_api.h
blob: 894cae1f3cc3456f3f30e7f40a2b76863e28fb3d (plain)
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
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef __NSTACK_RD_API_H
#define __NSTACK_RD_API_H

#define NSTACK_IP_BIT_MAX    32

#define MASK_V(ipaddr, masklen)  ((ipaddr) & htonl(~0 << (NSTACK_IP_BIT_MAX - (masklen))))

typedef struct __rd_route_ip_data
{
    unsigned int addr;
    unsigned int masklen;
    unsigned int resev[2];
} rd_ip_data;

typedef struct __rd_type_data
{
    unsigned int value;
    unsigned int attr;
    unsigned char reserved[4];
} rd_type_data;

typedef struct __rd_proto_data
{
    unsigned int value;
    unsigned int attr;
} rd_proto_data;

/* rd table manipulation */
void *nstack_rd_malloc(const char *name);
void *nstack_local_rd_malloc(void);
int nstack_rd_free(const char *name);
void nstack_local_rd_free(void *p);
void nstack_rd_table_clear(void *table);

/* parse rd_config.json */
int nstack_rd_parse(const char *name, void *table);

/* manually insert/delete rd node */
int nstack_rd_ip_node_insert(const char *name, rd_ip_data * data,
                             void *table);
int nstack_rd_ip_node_delete(rd_ip_data * data, void *table);
int nstack_rd_type_node_insert(const char *name, rd_type_data * data,
                               void *table);
int nstack_rd_type_node_delete(rd_type_data * data, void *table);
int nstack_rd_proto_node_insert(const char *name, rd_proto_data * data,
                                void *table);
int nstack_rd_proto_node_delete(rd_proto_data * data, void *table);

#include <netinet/in.h>

#ifndef NSTACK_IP6_ADDR_DEF
#define NSTACK_IP6_ADDR_DEF
typedef struct ip6_addr
{
    union
    {
        uint32_t addr32[4];
        uint16_t addr16[8];
        uint8_t addr8[16];
    };
} ip6_addr_t;
#endif

typedef struct __rd_route_ip6_data
{
    ip6_addr_t addr;
    unsigned int masklen;
} rd_ip6_data;

char *ipv6_ntop(ip6_addr_t host_addr);
int nstack_rd_ip6_node_insert(const char *name, rd_ip6_data * data,
                              void *table);
int nstack_rd_ip6_node_delete(rd_ip6_data * data, void *table);

#define IPV6_ADDR32_IDX     4
#define IPV6_ADDR32_SIZE    32

static inline int ip6_addr_match(struct ip6_addr *a, struct ip6_addr *b,
                                 unsigned int masklen)
{
    int index;
    uint32_t mask;

    for (index = 0; index < IPV6_ADDR32_IDX && masklen >= IPV6_ADDR32_SIZE;
         ++index)
    {
        if (a->addr32[index] != b->addr32[index])
            return 0;

        masklen -= IPV6_ADDR32_SIZE;
    }

    /* masklen is 128 */
    if (index == IPV6_ADDR32_IDX)
    {
        return 1;
    }

    if (index > IPV6_ADDR32_IDX)
        return 0;

    /*compare in network order byte */
    mask = htonl(~0 << (IPV6_ADDR32_SIZE - masklen));

    return (a->addr32[index] & mask) == (b->addr32[index] & mask);
}

#endif // __NSTACK_RD_API_H