-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
xkcp_config.c
236 lines (206 loc) · 6.77 KB
/
xkcp_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
232
233
234
235
236
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA gnu@gnu.org *
* *
\********************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include "json.h"
#include "debug.h"
#include "xkcp_config.h"
static struct xkcp_config config;
struct xkcp_config * xkcp_get_config(void)
{
return &config;
}
struct xkcp_param *xkcp_get_param(void)
{
return &config.param;
}
static void xkcp_param_init(struct xkcp_param *param)
{
memset(&config.param, 0, sizeof(struct xkcp_param));
param->local_interface = NULL;
param->remote_addr = NULL;
param->key = NULL;
param->crypt = NULL;
param->mode = NULL;
param->mtu = 1350;
param->sndwnd = 512;
param->rcvwnd = 512;
param->data_shard = 10;
param->parity_shard = 3;
param->dscp = 0;
param->nocomp = 1;
param->ack_nodelay = 0;
param->nodelay = 1;
param->interval = 20;
param->resend = 2;
param->nc = 0;
param->sock_buf = 4194304;
param->keepalive = 10;
}
void config_init()
{
config.daemon = 1;
config.is_server = 0;
xkcp_param_init(&config.param);
}
#if 0
void xkcp_param_free(struct xkcp_param *param)
{
if (param->local_interface)
free(param->local_interface);
if (param->remote_addr)
free(param->remote_addr);
if (param->key)
free(param->key);
if (param->crypt)
free(param->crypt);
if (param->mode)
free(param->mode);
}
#endif
static int parse_json_int(const json_value *value)
{
if (value->type == json_integer) {
return value->u.integer;
} else if (value->type == json_boolean) {
return value->u.boolean;
} else {
debug(LOG_ERR, "Need type :%d or %d, now get wrong type: %d", json_integer, json_boolean, value->type);
debug(LOG_ERR, "Invalid config format.");
}
return 0;
}
static char * parse_json_string(const json_value *value)
{
if (value->type == json_string) {
return strdup(value->u.string.ptr);
} else if (value->type == json_null) {
return NULL;
} else {
debug(LOG_ERR, "Need type :%d or %d, now get wrong type: %d", json_string, json_null, value->type);
debug(LOG_ERR, "Invalid config format.");
}
return 0;
}
int xkcp_parse_param(const char *filename)
{
return xkcp_parse_json_param(&config.param, filename);
}
// 1: error; 0, success
int xkcp_parse_json_param(struct xkcp_param *param, const char *filename)
{
if (!param)
return 1;
char * buf;
json_value *obj;
FILE *f = fopen(filename, "rb");
if (f == NULL) {
debug(LOG_ERR, "Invalid config path.");
return 1;
}
fseek(f, 0, SEEK_END);
long pos = ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(pos + 1);
if (buf == NULL) {
debug(LOG_ERR, "No enough memory.");
return 1;
}
int nread = fread(buf, pos, 1, f);
if (!nread) {
debug(LOG_ERR, "Failed to read the config file.");
return 1;
}
fclose(f);
buf[pos] = '\0'; // end of string
json_settings settings = { 0UL, 0, NULL, NULL, NULL };
char error_buf[512];
obj = json_parse_ex(&settings, buf, pos, error_buf);
if (obj == NULL) {
debug(LOG_ERR, "%s", error_buf);
return 1;
}
if (obj->type == json_object) {
unsigned int i;
for (i = 0; i < obj->u.object.length; i++) {
char *name = obj->u.object.values[i].name;
json_value *value = obj->u.object.values[i].value;
if (strcmp(name, "localinterface") == 0) {
param->local_interface = parse_json_string(value);
debug(LOG_DEBUG, "local_interface is %s", param->local_interface);
} else if (strcmp(name, "localport") == 0) {
param->local_port = parse_json_int(value);;
debug(LOG_DEBUG, "local_port is %d", param->local_port);
} else if (strcmp(name, "remoteaddr") == 0) {
param->remote_addr = parse_json_string(value);
debug(LOG_DEBUG, "remote_addr is %s", param->remote_addr);
} else if (strcmp(name, "remoteport") == 0) {
param->remote_port = parse_json_int(value);;
debug(LOG_DEBUG, "remote_port is %d", param->remote_port);
} else if (strcmp(name, "key") == 0) {
param->key = parse_json_string(value);
} else if (strcmp(name, "crypt") == 0) {
param->crypt = parse_json_string(value);
} else if (strcmp(name, "mode") == 0) {
param->mode = parse_json_string(value);
} else if (strcmp(name, "conn") == 0) {
param->conn = parse_json_int(value);;
} else if (strcmp(name, "autoexpire") == 0) {
param->auto_expire = parse_json_int(value);;
} else if (strcmp(name, "scavengettl") == 0) {
param->scavenge_ttl = parse_json_int(value);;
} else if (strcmp(name, "mtu") == 0) {
param->mtu = parse_json_int(value);;
} else if (strcmp(name, "sndwnd") == 0) {
param->sndwnd = parse_json_int(value);;
} else if (strcmp(name, "rcvwnd") == 0) {
param->rcvwnd = parse_json_int(value);;
} else if (strcmp(name, "datashard") == 0) {
param->data_shard = parse_json_int(value);;
} else if (strcmp(name, "parity_shard") == 0) {
param->parity_shard = parse_json_int(value);;
} else if (strcmp(name, "dscp") == 0) {
param->dscp = parse_json_int(value);;
} else if (strcmp(name, "nocomp") == 0) {
param->nocomp = parse_json_int(value);;
} else if (strcmp(name, "acknodelay") == 0) {
param->ack_nodelay = parse_json_int(value);;
} else if (strcmp(name, "nodelay") == 0) {
param->nodelay = parse_json_int(value);;
} else if (strcmp(name, "interval") == 0) {
param->interval = parse_json_int(value);;
} else if (strcmp(name, "resend") == 0) {
param->resend = parse_json_int(value);;
} else if (strcmp(name, "nc") == 0) {
param->nc = parse_json_int(value);;
} else if (strcmp(name, "keepalive") == 0) {
param->keepalive = parse_json_int(value);;
}
}
} else {
debug(LOG_DEBUG, "Invalid config file");
return 1;
}
free(buf);
json_value_free(obj);
return 0;
}