-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
384 lines (360 loc) · 8.51 KB
/
parse.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright 2012+ Michal Soltys <[email protected]>
*
* This file is part of Yancat.
*
* Yancat 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 3 of the License, or (at your option) any later
* version.
*
* Yancat 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
* Yancat. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#ifndef h_mingw
# include <sys/socket.h>
# include <netinet/in_systm.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <netdb.h>
#else
# include <winsock2.h>
#endif
#include "common.h"
#include "parse.h"
#define SOPT_LEN 40
#define DEF_PORT 10841u
#define badstr (-2)
#define badopt (-3)
#define nomem (-4)
#define badcnv (-5)
#define baddom (-6)
#ifndef IP_TOS
# define IP_TOS 0
#endif
#ifndef TCP_MAXSEG
# define TCP_MAXSEG 0
#endif
#if 0
#ifndef TCP_QUICKACK
# define TCP_QUICKACK 0
#endif
#ifndef TCP_CORK
# define TCP_CORK 0
#endif
#ifndef UDP_CORK
# define UDP_CORK 0
#endif
#endif
/* apparently for win hosts everything is problematic ... */
#ifndef IPTOS_LOWDELAY
# define IPTOS_LOWDELAY 0
#endif
#ifndef IPTOS_THROUGHPUT
# define IPTOS_THROUGHPUT 0
#endif
#ifndef IPTOS_RELIABILITY
# define IPTOS_RELIABILITY 0
#endif
#ifndef IPTOS_LOWCOST
# define IPTOS_LOWCOST 0
#endif
static struct stropts_s {
const char *name;
int lvl, opt;
} opts_by_str[] = {
{ "IP_TOS", IPPROTO_IP, IP_TOS },
{ "SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR },
{ "SO_SNDBUF", SOL_SOCKET, SO_SNDBUF },
{ "SO_RCVBUF", SOL_SOCKET, SO_RCVBUF },
{ "TCP_MAXSEG", IPPROTO_TCP, TCP_MAXSEG },
{ "TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY },
#if 0
/* not really useful for us */
{ "TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK },
{ "TCP_CORK", IPPROTO_TCP, TCP_CORK },
{ "UDP_CORK", IPPROTO_UDP, UDP_CORK },
#endif
};
static struct strvals_s {
const char *name;
int val;
} vals_by_str[] = {
{ "IPTOS_LOWDELAY", IPTOS_LOWDELAY },
{ "IPTOS_THROUGHPUT", IPTOS_THROUGHPUT },
{ "IPTOS_RELIABILITY", IPTOS_RELIABILITY },
{ "IPTOS_LOWCOST", IPTOS_LOWCOST },
};
void sp_list_known(void)
{
unsigned int i;
fputs("Known socket options:\n", stderr);
for (i = 0; i < sizeof(opts_by_str) / sizeof(opts_by_str[0]); i++) {
if (!opts_by_str[i].opt)
continue;
fputs(opts_by_str[i].name, stderr);
fputc('\n', stderr);
}
fputs("\nKnown socket options' values:\n", stderr);
for (i = 0; i < sizeof(vals_by_str) / sizeof(vals_by_str[0]); i++) {
if (!vals_by_str[i].val)
continue;
fputs(vals_by_str[i].name, stderr);
fputc('\n', stderr);
}
}
const char *sp_getsobyint(int opt, int lvl)
{
unsigned int i;
for (i = 0; i < sizeof(opts_by_str) / sizeof(opts_by_str[0]); i++) {
if (!opts_by_str[i].opt)
continue;
if (opt == opts_by_str[i].opt && lvl == opts_by_str[i].lvl) {
return opts_by_str[i].name;
}
}
return NULL;
}
int sp_getsobystr(const char *name, int *lvl)
{
unsigned int i;
if (!name || !*name)
return -1;
for (i = 0; i < sizeof(opts_by_str) / sizeof(opts_by_str[0]); i++) {
if (opts_by_str[i].opt && !strcasecmp(name, opts_by_str[i].name)) {
*lvl = opts_by_str[i].lvl;
return opts_by_str[i].opt;
}
}
return -1;
}
/*
* null / empty are assumed 1
* unknown to host / unconvertable are assumed 0
*
* generally value can be named, empty or just a value;
* empty name is treated as 1 - e.g. socet options that are binary on/off
* flips, such as reuseaddr; if it's unknown to the table, it's attempted to be
* converted; successful conversion returns value, unsuccessful returns 0 and set
* errno; furthermore - unsupported options on some host (mostly windows) are
* set in table as 0 (which lets them be easily ignored)
*/
int sp_getvalbystr(const char *name)
{
unsigned int i;
int val;
if (!name || !*name)
return 1;
for (i = 0; i < sizeof(vals_by_str) / sizeof(vals_by_str[0]); i++) {
if (vals_by_str[i].val && !strcasecmp(name, vals_by_str[i].name)) {
return vals_by_str[i].val;
}
}
val = (int)get_ul(name);
return val;
}
void sp_addsobyint(struct nopts_s *opts, int *copts, int opt, int lvl, int val, int ovr)
{
int i;
if (!opt)
return;
for (i = 0; i < *copts; i++) {
if (opts[i].opt == opt && opts[i].lvl == lvl)
break;
}
if (i == SOPT_CNT)
return;
/* not found or overridable */
if (i == *copts || ovr) {
opts[i].opt = opt;
opts[i].val = val;
opts[i].lvl = lvl;
}
if (i == *copts)
(*copts)++;
}
void sp_delsobyidx(struct nopts_s *opts, int *copts, int idx)
{
if (!copts || !*copts || idx < 0 || idx >= *copts - 1)
return;
memmove(opts + idx, opts + idx + 1, sizeof(struct nopts_s)*(size_t)(*copts - 1 - idx));
(*copts)--;
}
void sp_addsodefs(struct nopts_s *opts, int *copts, int dom)
{
sp_addsobyint(opts, copts, SO_REUSEADDR, SOL_SOCKET, 1, 0);
sp_addsobyint(opts, copts, IP_TOS, IPPROTO_IP, IPTOS_THROUGHPUT, 0);
if (dom == IPPROTO_TCP) {
#if defined(h_mingw) && (_WIN32_WINNT < 0x0600)
sp_addsobyint(opts, copts, SO_SNDBUF, SOL_SOCKET, 6*65536, 0);
sp_addsobyint(opts, copts, SO_RCVBUF, SOL_SOCKET, 6*65536, 0);
#endif
} else {
sp_addsobyint(opts, copts, SO_SNDBUF, SOL_SOCKET, 18000 - 2*28, 0);
#if defined(h_mingw) && (_WIN32_WINNT < 0x0600)
sp_addsobyint(opts, copts, SO_RCVBUF, SOL_SOCKET, 65535, 0);
#endif
}
}
/*
* TODO move this mess to flex + bison
*/
int sp_addr_parse(struct netpnt_s *a, const char *spec, int dom)
{
char *idx, *idx2, *idx3, *idx4;
char ostr[SOPT_LEN];
ssize_t len, len2;
int val, opt, lvl, ret = -1;
if (!spec)
return -1;
memset(a, 0, sizeof *a);
a->port = DEF_PORT;
switch (dom) {
case SPEC_IS_TCP:
a->dom = IPPROTO_TCP;
break;
case SPEC_IS_UDP:
a->dom = IPPROTO_UDP;
break;
default:
ret = baddom;
goto out;
}
idx = strchr(spec,':');
/* if we have port separator */
if (idx) {
len = idx - spec;
idx++;
val = (int)get_ul(idx);
if (errno) {
ret = badcnv;
goto out;
}
if (val)
a->port = (unsigned short int)val;
} else {
len = strlen(spec);
}
if (len >= HOST_LEN) {
ret = badstr;
goto out;
} else if (!len) {
len++;
a->host[0] = '*';
} else
memcpy(a->host, spec, len);
a->host[len] = 0;
if (!idx)
/* no port separator => ok out */
goto ok;
spec = strchr(idx, ':');
#if 0
if (!spec)
/* no proto separator => ok out */
goto ok;
spec++;
idx = strchr(spec,':');
if (idx) {
len = idx - spec;
} else {
len = strlen(spec);
}
len2 = len <= 3 ? 3 : len;
if (!strncasecmp("udp", spec, len2))
a->dom = IPPROTO_UDP;
else if (len && strncasecmp("tcp", spec, len2)) {
ret = baddom;
goto out;
}
spec = idx;
#endif
if (!spec || !spec[1])
/* no opts separator or empty => bail out */
goto ok;
do {
spec++;
idx = strchr(spec, ',');
idx2 = strchr(spec, '=');
if (idx2 && (!idx || idx2 < idx)) {
len = idx2 - spec;
idx3 = idx2;
val = 0;
do {
idx3++;
idx4 = strchr(idx3, '|');
if (idx4 && (!idx || idx4 < idx))
len2 = idx4 - idx3;
else if (idx)
len2 = idx - idx3;
else
len2 = strlen(idx3);
if (len2 >= SOPT_LEN) {
len = len2; spec = idx3;
ret = badstr;
goto out;
}
memcpy(ostr, idx3, len2);
ostr[len2] = 0;
val |= sp_getvalbystr(ostr);
} while ((idx3 = idx4) && (!idx || idx3 < idx));
} else {
if (idx)
len = idx - spec;
else
len = strlen(spec);
val = 1;
}
if (len >= SOPT_LEN) {
ret = badstr;
goto out;
}
memcpy(ostr, spec, len);
ostr[len] = 0;
opt = sp_getsobystr(ostr, &lvl);
if (opt < 0) {
spec = ostr;
ret = badopt;
goto out;
}
sp_addsobyint(a->opts, &a->copts, opt, lvl, val, 1);
} while (a->copts < SOPT_CNT && (spec = idx));
ok:
sp_addsodefs(a->opts, &a->copts, a->dom);
return 0;
out:
fputs("sp_addr_parse(): ", stderr);
switch (ret) {
case badstr:
fprintf(stderr,"string too long: %.*s\n", (int)len, spec);
break;
case badopt:
fprintf(stderr,"unknown option: %.*s\n", (int)len, spec);
break;
case nomem:
fputs("not enough memory ....\n", stderr);
break;
case baddom:
fputs("unknown protocol\n", stderr);
break;
case badcnv:
fputs("can't convert\n", stderr);
break;
default:
fputs("unspecified error\n", stderr);
}
return ret;
}