1 %{
2 //
3 // This file is part of the aMule Project.
4 //
5 // Copyright (c) 2009-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2009-2011 Stu Redman ( sturedman@amule.org )
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 //
26
27 //
28 // Read IP filters
29 //
30 // These have a silly "grammar". For example:
31 // 1.2.3.4-1.2.3.5,100,Duh:2.3.4.5-2.3.4.6
32 // Now - which format is it, and what range should it take?
33 //
34 // So just use the lexer to read the file and assure the line format.
35 // The actual IP reading is done with the ScanIP() function.
36 //
37
38 #include <stdio.h>
39 #include "Types.h"
40 #include "Logger.h"
41 #define IPFS_EXTERN
42 #include "IPFilterScanner.h"
43 #include <common/Format.h>
44 #include <common/StringFunctions.h>
45
46 #ifdef _MSC_VER
47 #define isatty(DUMMY) 0
48 #define YY_NO_UNISTD_H
49 #pragma warning(disable:4003)
50 #endif
51
52 #define YY_NEVER_INTERACTIVE 1
53
54 // When we get here the IP has been lexed nicely,
55 // so we can blaze through without any checks.
56 // The total lexing time is more than twice as fast
57 // with this than when using sscanf.
ScanIP(const char * buf,uint32 & ip)58 static bool ScanIP(const char * buf, uint32 & ip)
59 {
60 while (*buf < '0') {
61 buf++; // skip whitespace
62 }
63 ip = 0;
64 int a = 0;
65 for (int i = 0; i < 4; buf++) {
66 if (*buf < '0' || *buf > '9') {
67 // finished a number, check it and add to the ip
68 if (a > 255) {
69 return false;
70 }
71 ip = (ip << 8) | a;
72 a = 0;
73 i++;
74 } else {
75 // build number
76 a = a * 10 + *buf - '0';
77 }
78 }
79 return true;
80 }
81
ScanInt(const char * buf,uint32 & a)82 static bool ScanInt(const char * buf, uint32 & a)
83 {
84 while (*buf < '0') {
85 buf++; // skip whitespace
86 }
87 a = 0;
88 while (*buf >= '0' && *buf <= '9') {
89 a = a * 10 + *buf - '0';
90 if (a > 255) {
91 return false;
92 }
93 buf++;
94 }
95 return true;
96 }
97
98 %}
99
100 %option noyywrap
101
102 NO [0-9]{1,3}
103 IP {NO}"."{NO}"."{NO}"."{NO}
104 WS [ \t\r]*
105
106 %%
107
108 ^{WS}{IP}{WS}-{WS}{IP}{WS},{WS}{NO}{WS},.* {
109 /* PeerGuardian filter line
110 <IPStart> - <IPEnd> , <AccessLevel> , <Description>
111 */
112 char * ip1 = yytext;
113 char * ip2 = strchr(ip1 + 7, '-') + 1;
114 char * acc = strchr(ip2 + 7, ',') + 1;
115 char * dsc = strchr(acc + 1, ',') + 1;
116 if (!ScanIP(ip1, IPStart) || !ScanIP(ip2, IPEnd)
117 || !ScanInt(acc, IPLevel)) {
118 yyip_Bad++;
119 } else {
120 IPDescription = dsc;
121 return 1;
122 }
123 }
124
125 ^{WS}#.* {
126 /* Comment */
127 }
128
129 ^.*:{WS}{IP}{WS}-{WS}{IP}{WS} {
130 /* AntiP2P filter line
131 <Description> : <IPStart> - <IPEnd>
132 */
133 char * ip1 = strrchr(yytext, ':');
134 *ip1++ = 0; // remove : and terminate comment
135 char * ip2 = strchr(ip1 + 7, '-') + 1;
136 if (!ScanIP(ip1, IPStart) || !ScanIP(ip2, IPEnd)) {
137 yyip_Bad++;
138 } else {
139 IPLevel = 0;
140 IPDescription = yytext;
141 return 1;
142 }
143 }
144
145 {WS}"\n" {
146 yyip_Line++;
147 }
148
149 ^. {
150 /* Bad line */
151 yyip_Bad++;
152 AddDebugLogLineN(logIPFilter, CFormat(wxT("error in line %d: %s")) % yyip_Line % wxString(char2unicode(yytext)));
153 }
154
155 %%
156