-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.c
231 lines (200 loc) · 6.31 KB
/
config.c
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
224
225
226
227
228
229
230
231
#include "main.h"
extern struct app_config global_config;
uint8_t startWith( char *str, const char *pre )
{
return strncmp( pre, str, strlen( pre ) ) == 0;
}
uint32_t parseIP( char *str )
{
int ipv4_bytes[4];
int ret = sscanf( str, "%d.%d.%d.%d", &ipv4_bytes[0], &ipv4_bytes[1], &ipv4_bytes[2], &ipv4_bytes[3] );
if( ret == 4 )
{
uint32_t ipv4 = ( ipv4_bytes[0] << 24 ) + ( ipv4_bytes[1] << 16 ) + ( ipv4_bytes[2] << 8 ) + ipv4_bytes[3];
return ipv4;
}
return 0;
}
uint16_t parse_uint16_t( char *str )
{
uint16_t result;
int ret = sscanf( str, "%hu", &result );
if( ret == 1 )
return result;
return 0xFFFF;
}
uint8_t parse_uint8_t( char *str )
{
uint8_t result;
int ret = sscanf( str, "%hhu", &result );
if( ret == 1 )
return result;
return 0xFF;
}
char * parse_string( char *str )
{
char * temp = malloc( strlen( str ) + 1 );
memset( temp, 0, strlen( str ) + 1 );
strncpy( temp, str, strlen( str ) - 1);
return temp;
}
void parse_eal_args( char *str )
{
char * temp = malloc( strlen( str ) + 1 );
memset( temp, 0, strlen( str ) + 1 );
strncpy( temp, str, strlen( str ) - 1);
char name[12] = "http_block";
global_config.eal_args_count = 0;
global_config.eal_args[ global_config.eal_args_count ] = malloc( strlen( name ) + 1 );
memset( global_config.eal_args[ global_config.eal_args_count ], 0, strlen( name ) + 1 );
strncpy( global_config.eal_args[ global_config.eal_args_count ], name, strlen( name ) );
global_config.eal_args_count++;
char * tok = strtok( temp, " " );
while( tok != NULL )
{
global_config.eal_args[ global_config.eal_args_count ] = malloc( strlen( tok ) + 1 );
memset( global_config.eal_args[ global_config.eal_args_count ], 0, strlen( tok ) + 1 );
strncpy( global_config.eal_args[ global_config.eal_args_count ], tok, strlen( tok ) );
//printf( "Parsing string %s\n", global_config.eal_args[ global_config.eal_args_count ] );
global_config.eal_args_count++;
tok = strtok( NULL, " " );
}
}
void prepare_config( void )
{
// global_config.netflow.template_timer = NETFLOW_TEMPLATE_TIMER;
global_config.pool_conf.timeout_tcp_syn = CGNAT_XLATE_TIMEOUT_TCP_SYN;
global_config.pool_conf.timeout_tcp_est = CGNAT_XLATE_TIMEOUT_TCP_EST;
global_config.pool_conf.timeout_tcp_fin_res = CGNAT_XLATE_TIMEOUT_TCP_FIN_RES;
global_config.pool_conf.timeout_tcp_udp = CGNAT_XLATE_TIMEOUT_TCP_UDP;
global_config.pool_conf.timeout_tcp_icmp = CGNAT_XLATE_TIMEOUT_TCP_ICMP;
}
void print_config( void )
{
printf( "Transmit and receive mode: %x\n", global_config.mode );
printf( "EAL arguments count: %d\n", global_config.eal_args_count );
for( int i = 0; i < global_config.eal_args_count; i++ )
{
printf( "Argument[%d]: %s ", i, global_config.eal_args[ i ] );
}
printf( "\n" );
printf( "Input vlan: %d\n", global_config.vlan_in );
printf( "Output vlan: %d\n", global_config.vlan_out );
printf( "Pool range: " IPv4_BYTES_FMT " - " IPv4_BYTES_FMT "\n", IPv4_BYTES( global_config.pool_conf.ip_from ), IPv4_BYTES( global_config.pool_conf.ip_to ) );
printf( "Allowed ports: %d - %d\n", global_config.pool_conf.port_from, global_config.pool_conf.port_to );
printf( "PBA size: %d\n", 1 << global_config.pool_conf.pba_size );
printf( "Maximum subscribers: %d\n", 1 << global_config.pool_conf.maximum_subscribers );
printf( "Maximum xlations: %d\n", 1 << global_config.pool_conf.maximum_xlations );
printf( "EIM: %d, EIF: %d\n", global_config.pool_conf.endpoint_independent_mapping, global_config.pool_conf.endpoint_independent_filtering );
}
int load_config( char * file )
{
FILE * f = fopen( file, "r" );
if( f == NULL )
return CONFIG_FILE_NOT_EXIST;
char line[ 2048 ];
uint8_t section = 0;
memset( &global_config, 0, sizeof( global_config ) );
prepare_config();
while( 1 )
{
memset( line, 0, sizeof( line ) );
if( fgets( line, sizeof( line ), f ) == NULL )
break;
if( startWith( line, "[dpdk]" ) )
{
section = CONFIG_SECTION_DPDK;
continue;
}
if( startWith( line, "[pool]" ) )
{
section = CONFIG_SECTION_POOL;
continue;
}
if( startWith( line, "[classifier]" ) )
{
section = CONFIG_SECTION_POOL;
continue;
}
char *key = strtok( line, " =" );
if( key == NULL )
continue;
switch( section )
{
case CONFIG_SECTION_DPDK:
if( startWith( key, "eal_args" ) )
{
char *value = strtok( NULL, "=" );
parse_eal_args( value );
}
if( startWith( key, "mode" ) )
{
char *value = strtok( NULL, " =" );
global_config.mode = parse_uint8_t( value );
}
if( startWith( key, "vlan_in" ) )
{
char *value = strtok( NULL, " =" );
global_config.vlan_in = parse_uint16_t( value );
}
if( startWith( key, "vlan_out" ) )
{
char *value = strtok( NULL, " =" );
global_config.vlan_out = parse_uint16_t( value );
}
break;
case CONFIG_SECTION_POOL:
if( startWith( key, "ip_from" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.ip_from = parseIP( value );
}
if( startWith( key, "ip_to" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.ip_to = parseIP( value );
}
if( startWith( key, "port_from" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.port_from = parse_uint16_t( value );
}
if( startWith( key, "port_to" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.port_to = parse_uint16_t( value );
}
if( startWith( key, "pba_size" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.pba_size = parse_uint8_t( value );
}
if( startWith( key, "maximum_xlations" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.maximum_xlations = parse_uint8_t( value );
}
if( startWith( key, "maximum_subscribers" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.maximum_subscribers = parse_uint8_t( value );
}
if( startWith( key, "endpoint_independent_mapping" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.endpoint_independent_mapping = parse_uint8_t( value );
}
if( startWith( key, "endpoint_independent_filtering" ) )
{
char *value = strtok( NULL, " =" );
global_config.pool_conf.endpoint_independent_filtering = parse_uint8_t( value );
}
break;
default:
break;
}
}
print_config();
fclose( f );
return 0;
}