summaryrefslogtreecommitdiffstats
path: root/stacks/lwip_stack/lwip_src/include/ip_module/ip_module_api.h
blob: 2c9b662104a909d11fb16f7c9701e32b6e99f958 (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
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
212
213
214
215
216
217
218
219
220
221
222
223
/*
*
* 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 _IP_MODULE_API_H_
#define _IP_MODULE_API_H_
#include "types.h"

typedef int bool_t;
#define IP_MODULE_TRUE 1
#define IP_MODULE_FALSE 0
#define IP_MODULE_LENGTH_32 32
#define IP_MODULE_LENGTH_64 64
#define IP_MODULE_LENGTH_256 256
#define IP_MODULE_LENGTH_1024 1024
#define IP_MODULE_SUBNET_MASK_LEN IP_MODULE_LENGTH_32
#define IP_MODULE_MAX_NAME_LEN IP_MODULE_LENGTH_256
#define IP_MODULE_PORT_JSON_LEN (2048 * 2)
#define IP_MODULE_NETWORK_JSON_LEN (2048 * 2)
#define MAX_NETWORK_COUNT IP_MODULE_LENGTH_64
#define MAX_NETWORK_IP_COUNT IP_MODULE_LENGTH_1024
#define MAX_NETWORK_NUM (MAX_NETWORK_COUNT * 2)
COMPAT_PROTECT_RETURN(MAX_NETWORK_NUM, 128);
#define MAX_PORT_NUM (MAX_NETWORK_IP_COUNT * 2)
COMPAT_PROTECT_RETURN(MAX_PORT_NUM, 2048);
#define NSCRTL_ERRBUF_LEN IP_MODULE_LENGTH_256

#define INVALID_EXPIRE_TIME 0x7FFFFFFF
#define NSTACLCTRL_MAX_TRY_TIMES 60

/*sockaddr_un.sun_path is an array of 108 bytes*/
#define IP_MODULE_MAX_PATH_LEN 108

typedef enum enumADERRCODE
{
    NSCRTL_OK = 0,
    NSCRTL_ERR,
    NSCRTL_RD_NOT_EXIST,
    NSCRTL_RD_EXIST = 3,
    NSCRTL_INPUT_ERR,
    NSCRTL_STATUS_ERR,
    NSCRTL_NETWORK_COUNT_EXCEED,
    NSCRTL_IP_COUNT_EXCEED,
    NSCRTL_FREE_ALL_PORT,       //when all ports in container were deleted, need to free container.

    NSCRTL_MAX_ERR = 127
} NSCRTL_ERR_CODE;

#define NSOPR_SET_ERRINFO(_errno, fmt, ...) \
    {\
        struct config_data* cf_data = get_config_data(); \
        if (cf_data)\
        {\
            size_t len_error_desc = strlen(cf_data->param.error_desc); \
            cf_data->param.error = _errno; \
            if ((_errno != NSCRTL_OK) && (len_error_desc < NSCRTL_ERRBUF_LEN))\
            {\
                if (-1 == (snprintf_s(cf_data->param.error_desc + len_error_desc, \
                                      NSCRTL_ERRBUF_LEN - len_error_desc, NSCRTL_ERRBUF_LEN - len_error_desc - 1, fmt, ## __VA_ARGS__)))\
                {\
                    NSOPR_LOGERR("SNPRINTF_S failed]"); \
                } \
            } \
        } \
    } \

struct ref_nic
{
    struct ref_nic *next;
    char nic_name[IP_MODULE_MAX_NAME_LEN];
};

struct phy_net
{
    struct ref_nic *header;
    char bond_name[IP_MODULE_MAX_NAME_LEN];
    int bond_mode;
};

struct container_port_ip_cidr
{
    struct container_port_ip_cidr *next;
    unsigned int ip;
    unsigned int mask_len;
};

struct container_multicast_id
{
    struct container_multicast_id *next;
    unsigned int ip;
};

typedef struct
{
    char port_json[IP_MODULE_PORT_JSON_LEN];
} port_buffer;

struct container_port
{
    struct container_port *next;
    struct container_port_ip_cidr *ip_cidr_list;
    struct container_multicast_id *multicast_list;
    char port_name[IP_MODULE_MAX_NAME_LEN];
    port_buffer *buffer;
};

#define get_port_json(obj) obj->buffer->port_json

struct container_ip
{
    struct container_ip *next;
    struct container_port *ports_list;
    char container_id[IP_MODULE_MAX_NAME_LEN];
};

struct container_list
{
    struct container_ip *header;
};

struct ip_subnet
{
    struct ip_subnet *next;
    unsigned int subnet;
    unsigned int mask_len;
};

typedef struct
{
    char network_json[IP_MODULE_NETWORK_JSON_LEN];
} network_buffer;

struct network_configuration
{
    struct network_configuration *next;
    struct phy_net *phy_net;
    struct ip_subnet *ip_subnet;
    char network_name[IP_MODULE_MAX_NAME_LEN];
    char type_name[IP_MODULE_MAX_NAME_LEN];
    char nic_type_name[IP_MODULE_MAX_NAME_LEN];
    network_buffer *buffer;
};

#define get_network_json(obj) obj->buffer->network_json

struct network_list
{
    struct network_configuration *header;
};

void ip_subnet_print(struct ip_subnet *subnet);
bool_t is_in_same_network(unsigned int src_ip, unsigned int dst_ip);
bool_t is_ip_match_netif(unsigned int ip, char *netif_name);
bool_t is_ip_exist(unsigned int ip);
struct network_configuration *get_network_list();
inline struct network_configuration *get_network_by_ip_with_tree(unsigned int
                                                                 ip);

/* "type" option */
typedef enum
{
    IP_MODULE_NETWORK,
    IP_MODULE_IP,
    IP_MODULE_NETWORK_ALL,
    IP_MODULE_IP_ALL,
    IP_MODULE_ALL,
} ip_module_type;

/* "action" option */
typedef enum
{
    IP_MODULE_OPERATE_NULL,
    IP_MODULE_OPERATE_ADD,
    IP_MODULE_OPERATE_DEL,
    IP_MODULE_OPERATE_QUERY,
    IP_MODULE_OPERATE_SET,
    IP_MODULE_GET_VERSION,
    IP_MODULE_QUERY_NET,
    IP_MODULE_MAX,              //new type should be added before IP_MODULE_MAX
    IP_MODULE_BOTTOM = 0xFFFFFFFF
} ip_module_operate_type;

typedef int (*post_to_fn) (void *arg, ip_module_type type,
                           ip_module_operate_type operate_type);
typedef int (*add_netif_ip_fn) (char *netif_name, unsigned int ip,
                                unsigned int mask);
typedef int (*del_netif_ip_fn) (char *netif_name, unsigned int ip);

typedef struct
{
    post_to_fn post_to;
    add_netif_ip_fn add_netif_ip;
    del_netif_ip_fn del_netif_ip;
} output_api;

void regist_output_api(output_api * api);
output_api *get_output_api();
int init_configuration_reader();
int process_post(void *arg, ip_module_type type,
                 ip_module_operate_type operate_type);
int process_configuration(void *arg, ip_module_type type,
                          ip_module_operate_type operate_type);

port_buffer *malloc_port_buffer();
void free_port_buffer(port_buffer * buffer);
network_buffer *malloc_network_buffer();
void free_network_buffer(network_buffer * buffer);
int get_network_json_data();
int get_ip_json_data();

#endif