summaryrefslogtreecommitdiffstats
path: root/plugins/ipfilter/antip2p.h
blob: ce54c5384e88013c85425c60b6289ea18cc0474a (plain)
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
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef ANTIP2P_H
#define ANTIP2P_H

#include <util/mmapfile.h>
#include <util/constants.h>

#include <tqvaluelist.h>
#include <tqstring.h>

namespace kt
{
	typedef struct
	{
		bt::Uint32 ip1;
		bt::Uint32 ip2;
		bt::Uint64 offset;
		bt::Uint32 nrEntries;
	} HeaderBlock;
	
	struct IPBlock
	{
		bt::Uint32 ip1;
		bt::Uint32 ip2;
		
		bool operator < (const IPBlock & b) const;
	};
	
	/**
	 * @author Ivan Vasic <ivasic@gmail.com>
	 * @brief This class is used to manage anti-p2p filter list, so called level1.
	 */
	class AntiP2P
	{
		public:
    		AntiP2P();
    		~AntiP2P();
			
			/**
			 * Checks if anti-p2p file is present. Used to check if we should use level1 list
			 **/
			bool exists();
			
			
			/**
			 * Creates and loads the header from antip2p filter file.
			 **/
			void loadHeader();
			
			
			/**
			 * Checks if specified IP is listed in filter file.
			 * @return TRUE if IP should be blocked, FALSE otherwise
			 * @param ip TQString representation of IP to be checked
			 **/
			bool isBlockedIP(const TQString& ip);
			
			/**
			 * Overloaded function. Uses Uint32 IP to be checked
			 **/
			bool isBlockedIP(bt::Uint32& ip);
			
			/**
			 * This function converts TQString IP to Uint32 format.
			 **/
			static bt::Uint32 toUint32(const TQString& ip);
			
		private:
			bt::MMapFile* file;
			TQValueList<HeaderBlock> blocks;
			
			///Is AntiP2P header loaded
			bool header_loaded;
			
			/**
			 * Loads filter file
			*/
			void load();
			
			/**
			 * Binary searches AntiP2P::blocks to find range where IP could be.
			 * @returns 
			 * 		-1 if IP cannot be in the list
			 * 		-2 if IP is already found in blocks
			 * 		or index of HeaderBlock in AntiP2P::blocks which will be used for direct file search.
			 **/
			int searchHeader(bt::Uint32& ip, int start, int end);
			
			
			/**
			 * Binary searches AntiP2P::file to find IP.
			 * @returns TRUE if IP should be blocked FALSE otherwise
			 **/
			bool searchFile(IPBlock* file_blocks, bt::Uint32& ip, int start, int end);
			
	};
}
#endif