3 // This file is part of the aMule Project.
5 // Copyright (c) 2009-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2009-2011 Stu Redman ( sturedman@amule.org )
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.
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.
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.
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
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?
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.
42 #include "IPFilterScanner.h"
43 #include <common/Format.h>
44 #include <common/StringFunctions.h>
47 #define isatty(DUMMY) 0
48 #define YY_NO_UNISTD_H
49 #pragma warning(disable:4003)
52 #define YY_NEVER_INTERACTIVE 1
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.
58 static bool ScanIP(const char * buf, uint32 & ip)
61 buf++; // skip whitespace
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
76 a = a * 10 + *buf - '0';
82 static bool ScanInt(const char * buf, uint32 & a)
85 buf++; // skip whitespace
88 while (*buf >= '0' && *buf <= '9') {
89 a = a * 10 + *buf - '0';
103 IP {NO}"."{NO}"."{NO}"."{NO}
108 ^{WS}{IP}{WS}-{WS}{IP}{WS},{WS}{NO}{WS},.* {
109 /* PeerGuardian filter line
110 <IPStart> - <IPEnd> , <AccessLevel> , <Description>
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)) {
129 ^.*:{WS}{IP}{WS}-{WS}{IP}{WS} {
130 /* AntiP2P filter line
131 <Description> : <IPStart> - <IPEnd>
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)) {
140 IPDescription = yytext;
152 AddDebugLogLineN(logIPFilter, CFormat(wxT("error in line %d: %s")) % yyip_Line % wxString(char2unicode(yytext)));