forked from liudf0716/xkcptun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xkcp_config.c
231 lines (184 loc) · 6.64 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
/********************************************************************\
* 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-c/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);
}
static 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);
}
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;
int nret = 0;
struct json_object *json_config = json_object_from_file(filename);
if (!json_config || !json_object_is_type(json_config, json_type_object)) {
debug(LOG_ERR, "json_object_from_file [%s] failed", filename);
return 1;
}
struct json_object *j_obj = NULL;
if (json_object_object_get_ex(json_config, "localinterface", &j_obj)) {
param->local_interface = strdup(json_object_get_string(j_obj));
} else
param->local_interface = strdup("br-lan");
debug(LOG_DEBUG, "local_interface is %s", param->local_interface);
if (json_object_object_get_ex(json_config, "localport", &j_obj)) {
param->local_port = json_object_get_int(j_obj);
} else
param->local_port = 9088;
debug(LOG_DEBUG, "local_port is %d", param->local_port);
if (json_object_object_get_ex(json_config, "remoteaddr", &j_obj)) {
param->remote_addr = strdup(json_object_get_string(j_obj));
debug(LOG_DEBUG, "remote_addr is %s", param->remote_addr);
}
if (json_object_object_get_ex(json_config, "remoteport", &j_obj)) {
param->remote_port = json_object_get_int(j_obj);
} else
param->remote_port = 9089;
debug(LOG_DEBUG, "remote_port is %d", param->remote_port);
if (json_object_object_get_ex(json_config, "key", &j_obj)) {
param->key = strdup(json_object_get_string(j_obj));
}
if (json_object_object_get_ex(json_config, "crypt", &j_obj)) {
param->crypt = strdup(json_object_get_string(j_obj));
}
if (json_object_object_get_ex(json_config, "mode", &j_obj)) {
param->mode = strdup(json_object_get_string(j_obj));
}
if (json_object_object_get_ex(json_config, "conn", &j_obj)) {
param->conn = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "autoexpire", &j_obj)) {
param->auto_expire = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "scavengettl", &j_obj)) {
param->scavenge_ttl = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "mtu", &j_obj)) {
param->mtu = json_object_get_int(j_obj);
debug(LOG_DEBUG, "mtu is %d", param->mtu);
}
if (json_object_object_get_ex(json_config, "sndwnd", &j_obj)) {
param->sndwnd = json_object_get_int(j_obj);
debug(LOG_DEBUG, "sndwnd is %d", param->sndwnd);
}
if (json_object_object_get_ex(json_config, "rcvwnd", &j_obj)) {
param->rcvwnd = json_object_get_int(j_obj);
debug(LOG_DEBUG, "rcvwnd is %d", param->rcvwnd);
}
if (json_object_object_get_ex(json_config, "datashard", &j_obj)) {
param->data_shard = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "parity_shard", &j_obj)) {
param->parity_shard = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "dscp", &j_obj)) {
param->dscp = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "nocomp", &j_obj)) {
param->nocomp = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "acknodelay", &j_obj)) {
param->ack_nodelay = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "nodelay", &j_obj)) {
param->nodelay = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "interval", &j_obj)) {
param->interval = json_object_get_int(j_obj);
debug(LOG_DEBUG, "interval is %d", param->interval);
}
if (json_object_object_get_ex(json_config, "resend", &j_obj)) {
param->resend = json_object_get_int(j_obj);
debug(LOG_DEBUG, "resend is %d", param->resend);
}
if (json_object_object_get_ex(json_config, "nc", &j_obj)) {
param->nc = json_object_get_int(j_obj);
debug(LOG_DEBUG, "nc is %d", param->nc);
}
if (json_object_object_get_ex(json_config, "sockbuf", &j_obj)) {
param->sock_buf = json_object_get_int(j_obj);
}
if (json_object_object_get_ex(json_config, "keepalive", &j_obj)) {
param->keepalive = json_object_get_int(j_obj);
}
debug(LOG_DEBUG, "keepalive is %d", param->keepalive);
err:
json_object_put(json_config);
if (nret) xkcp_param_free(param);
return nret;
}