1 diff -Nur tcpproxy-1.2.orig/src/cfg_parser.c tcpproxy-1.2/src/cfg_parser.c
2 --- tcpproxy-1.2.orig/src/cfg_parser.c 1970-01-01 01:00:00.000000000 +0100
3 +++ tcpproxy-1.2/src/cfg_parser.c 2015-05-17 18:10:54.933307372 +0200
6 +#line 1 "cfg_parser.rl"
10 + * tcpproxy is a simple tcp connection proxy which combines the
11 + * features of rinetd and 6tunnel. tcpproxy supports IPv4 and
12 + * IPv6 and also supports connections from IPv6 to IPv4
13 + * endpoints and vice versa.
16 + * Copyright (C) 2010-2015 Christian Pointner <equinox@spreadspace.org>
18 + * This file is part of tcpproxy.
20 + * tcpproxy is free software: you can redistribute it and/or modify
21 + * it under the terms of the GNU General Public License as published by
22 + * the Free Software Foundation, either version 3 of the License, or
23 + * any later version.
25 + * tcpproxy is distributed in the hope that it will be useful,
26 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 + * GNU General Public License for more details.
30 + * You should have received a copy of the GNU General Public License
31 + * along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
37 +#include <sys/types.h>
38 +#include <sys/stat.h>
41 +#include <sys/mman.h>
43 +#include "datatypes.h"
47 +#include "listener.h"
59 +static void init_listener_struct(struct listener* l)
72 +static void clear_listener_struct(struct listener* l)
87 + init_listener_struct(l);
90 +static int owrt_string(char** dest, char* start, char* end)
92 + if(!dest || start >= end)
95 + if(*dest) free(*dest);
96 + int n = end - start;
97 + *dest = malloc(n+1);
101 + memcpy(*dest, start, n);
108 +#line 162 "cfg_parser.rl"
112 +int parse_listener(char* p, char* pe, listeners_t* listener)
114 + int cs, ret = 0, cur_line = 1;
117 +#line 114 "cfg_parser.c"
118 +static const int cfg_parser_start = 67;
119 +static const int cfg_parser_first_final = 67;
120 +static const int cfg_parser_error = 0;
122 +static const int cfg_parser_en_main = 67;
125 +#line 170 "cfg_parser.rl"
127 +#line 124 "cfg_parser.c"
129 + cs = cfg_parser_start;
132 +#line 171 "cfg_parser.rl"
134 + char* cpy_start = NULL;
135 + struct listener lst;
136 + init_listener_struct(&lst);
140 +#line 137 "cfg_parser.c"
148 + case 67: goto st67;
159 + case 10: goto st10;
160 + case 11: goto st11;
161 + case 12: goto st12;
162 + case 13: goto st13;
163 + case 14: goto st14;
164 + case 15: goto st15;
165 + case 16: goto st16;
166 + case 17: goto st17;
167 + case 18: goto st18;
168 + case 19: goto st19;
169 + case 20: goto st20;
170 + case 21: goto st21;
171 + case 22: goto st22;
172 + case 23: goto st23;
173 + case 24: goto st24;
174 + case 25: goto st25;
175 + case 26: goto st26;
176 + case 27: goto st27;
177 + case 28: goto st28;
178 + case 29: goto st29;
179 + case 30: goto st30;
180 + case 31: goto st31;
181 + case 32: goto st32;
182 + case 33: goto st33;
183 + case 34: goto st34;
184 + case 35: goto st35;
185 + case 36: goto st36;
186 + case 37: goto st37;
187 + case 38: goto st38;
188 + case 39: goto st39;
189 + case 40: goto st40;
190 + case 41: goto st41;
191 + case 42: goto st42;
192 + case 43: goto st43;
193 + case 44: goto st44;
194 + case 45: goto st45;
195 + case 46: goto st46;
196 + case 47: goto st47;
197 + case 48: goto st48;
198 + case 49: goto st49;
199 + case 50: goto st50;
200 + case 51: goto st51;
201 + case 52: goto st52;
202 + case 53: goto st53;
203 + case 54: goto st54;
204 + case 55: goto st55;
205 + case 56: goto st56;
206 + case 57: goto st57;
207 + case 58: goto st58;
208 + case 59: goto st59;
209 + case 60: goto st60;
210 + case 61: goto st61;
211 + case 62: goto st62;
212 + case 63: goto st63;
213 + case 64: goto st64;
214 + case 65: goto st65;
215 + case 66: goto st66;
225 +#line 127 "cfg_parser.rl"
229 +#line 114 "cfg_parser.rl"
231 + ret = listeners_add(listener, lst.la_, lst.lrt_, lst.lp_, lst.ra_, lst.rrt_, lst.rp_, lst.sa_);
232 + clear_listener_struct(&lst);
239 +#line 236 "cfg_parser.c"
242 + case 32: goto st67;
244 + case 108: goto st2;
246 + if ( 9 <= (*p) && (*p) <= 13 )
250 +#line 118 "cfg_parser.rl"
253 + log_printf(ERROR, "config file syntax error: unexpected end of file");
255 + log_printf(ERROR, "config file syntax error at line %d", cur_line);
257 + {cs = (cfg_parser_error); goto _again;}
260 +#line 257 "cfg_parser.c"
323 + case 58: goto tr12;
327 + if ( 48 <= (*p) && (*p) <= 57 )
329 + } else if ( (*p) >= 45 )
331 + } else if ( (*p) > 70 ) {
333 + if ( 71 <= (*p) && (*p) <= 90 )
335 + } else if ( (*p) > 102 ) {
336 + if ( 103 <= (*p) && (*p) <= 122 )
349 + case 32: goto st10;
353 +#line 105 "cfg_parser.rl"
354 + { ret = owrt_string(&(lst.la_), cpy_start, p); cpy_start = NULL; }
360 +#line 357 "cfg_parser.c"
363 + case 32: goto st10;
364 + case 45: goto tr14;
367 + if ( 48 <= (*p) && (*p) <= 57 )
369 + } else if ( (*p) > 90 ) {
370 + if ( 97 <= (*p) && (*p) <= 122 )
376 +#line 104 "cfg_parser.rl"
383 +#line 380 "cfg_parser.c"
385 + case 10: goto tr16;
386 + case 32: goto tr15;
387 + case 35: goto tr17;
388 + case 45: goto st11;
389 + case 123: goto tr19;
392 + if ( 9 <= (*p) && (*p) <= 13 )
394 + } else if ( (*p) > 57 ) {
396 + if ( 97 <= (*p) && (*p) <= 122 )
398 + } else if ( (*p) >= 65 )
404 +#line 127 "cfg_parser.rl"
408 +#line 106 "cfg_parser.rl"
409 + { ret = owrt_string(&(lst.lp_), cpy_start, p); cpy_start = NULL; }
412 +#line 106 "cfg_parser.rl"
413 + { ret = owrt_string(&(lst.lp_), cpy_start, p); cpy_start = NULL; }
414 +#line 127 "cfg_parser.rl"
421 +#line 418 "cfg_parser.c"
423 + case 10: goto tr21;
424 + case 32: goto st12;
425 + case 35: goto st13;
426 + case 123: goto st14;
428 + if ( 9 <= (*p) && (*p) <= 13 )
432 +#line 106 "cfg_parser.rl"
433 + { ret = owrt_string(&(lst.lp_), cpy_start, p); cpy_start = NULL; }
439 +#line 436 "cfg_parser.c"
444 +#line 127 "cfg_parser.rl"
448 +#line 106 "cfg_parser.rl"
449 + { ret = owrt_string(&(lst.lp_), cpy_start, p); cpy_start = NULL; }
452 +#line 110 "cfg_parser.rl"
453 + { ret = owrt_string(&(lst.rp_), cpy_start, p); cpy_start = NULL; }
456 +#line 113 "cfg_parser.rl"
457 + { ret = owrt_string(&(lst.sa_), cpy_start, p); cpy_start = NULL; }
463 +#line 460 "cfg_parser.c"
465 + case 10: goto tr24;
466 + case 32: goto st14;
467 + case 35: goto st15;
468 + case 114: goto st16;
469 + case 115: goto st52;
470 + case 125: goto st63;
472 + if ( 9 <= (*p) && (*p) <= 13 )
494 + case 109: goto st18;
495 + case 115: goto st43;
525 + case 32: goto st22;
526 + case 45: goto st31;
527 + case 58: goto st23;
536 + case 32: goto st22;
537 + case 58: goto st23;
546 + case 32: goto st24;
555 + case 32: goto st24;
556 + case 58: goto tr41;
560 + if ( 48 <= (*p) && (*p) <= 57 )
562 + } else if ( (*p) >= 45 )
564 + } else if ( (*p) > 70 ) {
566 + if ( 71 <= (*p) && (*p) <= 90 )
568 + } else if ( (*p) > 102 ) {
569 + if ( 103 <= (*p) && (*p) <= 122 )
577 +#line 104 "cfg_parser.rl"
584 +#line 581 "cfg_parser.c"
587 + case 32: goto tr42;
590 + if ( 45 <= (*p) && (*p) <= 46 )
592 + } else if ( (*p) > 57 ) {
594 + if ( 97 <= (*p) && (*p) <= 122 )
596 + } else if ( (*p) >= 65 )
602 +#line 109 "cfg_parser.rl"
603 + { ret = owrt_string(&(lst.ra_), cpy_start, p); cpy_start = NULL; }
609 +#line 606 "cfg_parser.c"
612 + case 32: goto st26;
613 + case 45: goto tr45;
616 + if ( 48 <= (*p) && (*p) <= 57 )
618 + } else if ( (*p) > 90 ) {
619 + if ( 97 <= (*p) && (*p) <= 122 )
625 +#line 104 "cfg_parser.rl"
632 +#line 629 "cfg_parser.c"
635 + case 32: goto tr46;
636 + case 45: goto st27;
637 + case 59: goto tr48;
640 + if ( 48 <= (*p) && (*p) <= 57 )
642 + } else if ( (*p) > 90 ) {
643 + if ( 97 <= (*p) && (*p) <= 122 )
649 +#line 110 "cfg_parser.rl"
650 + { ret = owrt_string(&(lst.rp_), cpy_start, p); cpy_start = NULL; }
653 +#line 111 "cfg_parser.rl"
654 + { lst.rrt_ = IPV4_ONLY; }
657 +#line 112 "cfg_parser.rl"
658 + { lst.rrt_ = IPV6_ONLY; }
661 +#line 107 "cfg_parser.rl"
662 + { lst.lrt_ = IPV4_ONLY; }
665 +#line 108 "cfg_parser.rl"
666 + { lst.lrt_ = IPV6_ONLY; }
669 +#line 113 "cfg_parser.rl"
670 + { ret = owrt_string(&(lst.sa_), cpy_start, p); cpy_start = NULL; }
676 +#line 673 "cfg_parser.c"
679 + case 32: goto st28;
680 + case 59: goto st14;
684 +#line 104 "cfg_parser.rl"
691 +#line 688 "cfg_parser.c"
694 + case 32: goto tr42;
695 + case 58: goto st30;
699 + if ( 48 <= (*p) && (*p) <= 57 )
701 + } else if ( (*p) >= 45 )
703 + } else if ( (*p) > 70 ) {
705 + if ( 71 <= (*p) && (*p) <= 90 )
707 + } else if ( (*p) > 102 ) {
708 + if ( 103 <= (*p) && (*p) <= 122 )
716 +#line 104 "cfg_parser.rl"
723 +#line 720 "cfg_parser.c"
726 + case 32: goto tr42;
729 + if ( 48 <= (*p) && (*p) <= 58 )
731 + } else if ( (*p) > 70 ) {
732 + if ( 97 <= (*p) && (*p) <= 102 )
785 + case 32: goto st37;
786 + case 58: goto st38;
795 + case 32: goto st39;
804 + case 32: goto st39;
805 + case 73: goto st40;
806 + case 105: goto st40;
814 + case 80: goto st41;
815 + case 112: goto st41;
823 + case 86: goto st42;
824 + case 118: goto st42;
832 + case 52: goto tr63;
833 + case 54: goto tr64;
863 + case 32: goto st46;
864 + case 58: goto st47;
873 + case 32: goto st48;
882 + case 32: goto st48;
883 + case 73: goto st49;
884 + case 105: goto st49;
892 + case 80: goto st50;
893 + case 112: goto st50;
901 + case 86: goto st51;
902 + case 118: goto st51;
910 + case 52: goto tr73;
911 + case 54: goto tr74;
955 + case 32: goto st57;
956 + case 58: goto st58;
965 + case 32: goto st59;
974 + case 32: goto st59;
975 + case 58: goto tr84;
979 + if ( 48 <= (*p) && (*p) <= 57 )
981 + } else if ( (*p) >= 45 )
983 + } else if ( (*p) > 70 ) {
985 + if ( 71 <= (*p) && (*p) <= 90 )
987 + } else if ( (*p) > 102 ) {
988 + if ( 103 <= (*p) && (*p) <= 122 )
996 +#line 104 "cfg_parser.rl"
1003 +#line 1000 "cfg_parser.c"
1005 + case 9: goto tr85;
1006 + case 32: goto tr85;
1007 + case 59: goto tr87;
1009 + if ( (*p) < 48 ) {
1010 + if ( 45 <= (*p) && (*p) <= 46 )
1012 + } else if ( (*p) > 57 ) {
1013 + if ( (*p) > 90 ) {
1014 + if ( 97 <= (*p) && (*p) <= 122 )
1016 + } else if ( (*p) >= 65 )
1022 +#line 104 "cfg_parser.rl"
1023 + { cpy_start = p; }
1029 +#line 1026 "cfg_parser.c"
1031 + case 9: goto tr85;
1032 + case 32: goto tr85;
1033 + case 58: goto st62;
1034 + case 59: goto tr87;
1036 + if ( (*p) < 65 ) {
1037 + if ( (*p) > 46 ) {
1038 + if ( 48 <= (*p) && (*p) <= 57 )
1040 + } else if ( (*p) >= 45 )
1042 + } else if ( (*p) > 70 ) {
1043 + if ( (*p) < 97 ) {
1044 + if ( 71 <= (*p) && (*p) <= 90 )
1046 + } else if ( (*p) > 102 ) {
1047 + if ( 103 <= (*p) && (*p) <= 122 )
1055 +#line 104 "cfg_parser.rl"
1056 + { cpy_start = p; }
1062 +#line 1059 "cfg_parser.c"
1064 + case 9: goto tr85;
1065 + case 32: goto tr85;
1066 + case 59: goto tr87;
1068 + if ( (*p) < 65 ) {
1069 + if ( 48 <= (*p) && (*p) <= 58 )
1071 + } else if ( (*p) > 70 ) {
1072 + if ( 97 <= (*p) && (*p) <= 102 )
1085 +#line 104 "cfg_parser.rl"
1086 + { cpy_start = p; }
1092 +#line 1089 "cfg_parser.c"
1094 + case 9: goto tr91;
1095 + case 32: goto tr91;
1097 + if ( (*p) < 48 ) {
1098 + if ( 45 <= (*p) && (*p) <= 46 )
1100 + } else if ( (*p) > 57 ) {
1101 + if ( (*p) > 90 ) {
1102 + if ( 97 <= (*p) && (*p) <= 122 )
1104 + } else if ( (*p) >= 65 )
1110 +#line 104 "cfg_parser.rl"
1111 + { cpy_start = p; }
1117 +#line 1114 "cfg_parser.c"
1119 + case 9: goto tr91;
1120 + case 32: goto tr91;
1121 + case 58: goto st66;
1123 + if ( (*p) < 65 ) {
1124 + if ( (*p) > 46 ) {
1125 + if ( 48 <= (*p) && (*p) <= 57 )
1127 + } else if ( (*p) >= 45 )
1129 + } else if ( (*p) > 70 ) {
1130 + if ( (*p) < 97 ) {
1131 + if ( 71 <= (*p) && (*p) <= 90 )
1133 + } else if ( (*p) > 102 ) {
1134 + if ( 103 <= (*p) && (*p) <= 122 )
1142 +#line 104 "cfg_parser.rl"
1143 + { cpy_start = p; }
1149 +#line 1146 "cfg_parser.c"
1151 + case 9: goto tr91;
1152 + case 32: goto tr91;
1154 + if ( (*p) < 65 ) {
1155 + if ( 48 <= (*p) && (*p) <= 58 )
1157 + } else if ( (*p) > 70 ) {
1158 + if ( 97 <= (*p) && (*p) <= 102 )
1164 + _test_eof67: cs = 67; goto _test_eof;
1165 + _test_eof1: cs = 1; goto _test_eof;
1166 + _test_eof2: cs = 2; goto _test_eof;
1167 + _test_eof3: cs = 3; goto _test_eof;
1168 + _test_eof4: cs = 4; goto _test_eof;
1169 + _test_eof5: cs = 5; goto _test_eof;
1170 + _test_eof6: cs = 6; goto _test_eof;
1171 + _test_eof7: cs = 7; goto _test_eof;
1172 + _test_eof8: cs = 8; goto _test_eof;
1173 + _test_eof9: cs = 9; goto _test_eof;
1174 + _test_eof10: cs = 10; goto _test_eof;
1175 + _test_eof11: cs = 11; goto _test_eof;
1176 + _test_eof12: cs = 12; goto _test_eof;
1177 + _test_eof13: cs = 13; goto _test_eof;
1178 + _test_eof14: cs = 14; goto _test_eof;
1179 + _test_eof15: cs = 15; goto _test_eof;
1180 + _test_eof16: cs = 16; goto _test_eof;
1181 + _test_eof17: cs = 17; goto _test_eof;
1182 + _test_eof18: cs = 18; goto _test_eof;
1183 + _test_eof19: cs = 19; goto _test_eof;
1184 + _test_eof20: cs = 20; goto _test_eof;
1185 + _test_eof21: cs = 21; goto _test_eof;
1186 + _test_eof22: cs = 22; goto _test_eof;
1187 + _test_eof23: cs = 23; goto _test_eof;
1188 + _test_eof24: cs = 24; goto _test_eof;
1189 + _test_eof25: cs = 25; goto _test_eof;
1190 + _test_eof26: cs = 26; goto _test_eof;
1191 + _test_eof27: cs = 27; goto _test_eof;
1192 + _test_eof28: cs = 28; goto _test_eof;
1193 + _test_eof29: cs = 29; goto _test_eof;
1194 + _test_eof30: cs = 30; goto _test_eof;
1195 + _test_eof31: cs = 31; goto _test_eof;
1196 + _test_eof32: cs = 32; goto _test_eof;
1197 + _test_eof33: cs = 33; goto _test_eof;
1198 + _test_eof34: cs = 34; goto _test_eof;
1199 + _test_eof35: cs = 35; goto _test_eof;
1200 + _test_eof36: cs = 36; goto _test_eof;
1201 + _test_eof37: cs = 37; goto _test_eof;
1202 + _test_eof38: cs = 38; goto _test_eof;
1203 + _test_eof39: cs = 39; goto _test_eof;
1204 + _test_eof40: cs = 40; goto _test_eof;
1205 + _test_eof41: cs = 41; goto _test_eof;
1206 + _test_eof42: cs = 42; goto _test_eof;
1207 + _test_eof43: cs = 43; goto _test_eof;
1208 + _test_eof44: cs = 44; goto _test_eof;
1209 + _test_eof45: cs = 45; goto _test_eof;
1210 + _test_eof46: cs = 46; goto _test_eof;
1211 + _test_eof47: cs = 47; goto _test_eof;
1212 + _test_eof48: cs = 48; goto _test_eof;
1213 + _test_eof49: cs = 49; goto _test_eof;
1214 + _test_eof50: cs = 50; goto _test_eof;
1215 + _test_eof51: cs = 51; goto _test_eof;
1216 + _test_eof52: cs = 52; goto _test_eof;
1217 + _test_eof53: cs = 53; goto _test_eof;
1218 + _test_eof54: cs = 54; goto _test_eof;
1219 + _test_eof55: cs = 55; goto _test_eof;
1220 + _test_eof56: cs = 56; goto _test_eof;
1221 + _test_eof57: cs = 57; goto _test_eof;
1222 + _test_eof58: cs = 58; goto _test_eof;
1223 + _test_eof59: cs = 59; goto _test_eof;
1224 + _test_eof60: cs = 60; goto _test_eof;
1225 + _test_eof61: cs = 61; goto _test_eof;
1226 + _test_eof62: cs = 62; goto _test_eof;
1227 + _test_eof63: cs = 63; goto _test_eof;
1228 + _test_eof64: cs = 64; goto _test_eof;
1229 + _test_eof65: cs = 65; goto _test_eof;
1230 + _test_eof66: cs = 66; goto _test_eof;
1302 +#line 118 "cfg_parser.rl"
1305 + log_printf(ERROR, "config file syntax error: unexpected end of file");
1307 + log_printf(ERROR, "config file syntax error at line %d", cur_line);
1309 + {cs = (cfg_parser_error); goto _again;}
1312 +#line 1309 "cfg_parser.c"
1319 +#line 178 "cfg_parser.rl"
1321 + if(cs == cfg_parser_error) {
1322 + listeners_revert(listener);
1326 + ret = listeners_update(listener);
1328 + clear_listener_struct(&lst);
1333 +int read_configfile(const char* filename, listeners_t* listener)
1335 + int fd = open(filename, 0);
1337 + log_printf(ERROR, "open('%s') failed: %s", filename, strerror(errno));
1342 + if(fstat(fd, &sb) == -1) {
1343 + log_printf(ERROR, "fstat() error: %s", strerror(errno));
1349 + log_printf(ERROR, "config file %s is empty", filename);
1354 + if(!S_ISREG(sb.st_mode)) {
1355 + log_printf(ERROR, "config file %s is not a regular file", filename);
1360 + char* p = (char*)mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
1361 + if(p == MAP_FAILED) {
1362 + log_printf(ERROR, "mmap() error: %s", strerror(errno));
1368 + log_printf(DEBUG, "mapped %ld bytes from file %s at address 0x%08lX", sb.st_size, filename, p);
1369 + int ret = parse_listener(p, p + sb.st_size, listener);
1371 + if(munmap(p, sb.st_size) == -1) {
1372 + log_printf(ERROR, "munmap() error: %s", strerror(errno));
1375 + log_printf(DEBUG, "unmapped file %s", filename);