Upstream tarball 9959
[amule.git] / src / NetworkFunctions.cpp
blob31b4f9cf5acc18c2834e809743694179744d315b
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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.
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.
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
27 #include "NetworkFunctions.h" // Interface declaration
29 bool StringIPtoUint32(const wxString &strIP, uint32& Ip)
31 // The current position in the current field, used to detect malformed fields (x.y..z).
32 unsigned digit = 0;
34 // The current field, used to ensure only IPs that looks like a.b.c.d are supported
35 unsigned field = 0;
37 // The value of the current field
38 unsigned value = 0;
40 // Stores the work-value of the IP, reference is not changed unless the str was valid
41 uint32 tmp_ip = 0;
43 wxString str = strIP.Strip( wxString::both );
44 for (size_t i = 0; i < str.Length(); i++) {
45 wxChar c = str.GetChar( i );
47 if ( c >= wxT('0') && c <= wxT('9') && (value >> 8) == 0) {
48 value = ( value * 10 ) + ( c - wxT('0') );
49 ++digit;
50 } else if ( c == wxT('.') ) {
51 if ( digit && (value >> 8) == 0) {
52 tmp_ip = tmp_ip | value << ( field * 8 );
54 // Rest the current field values
55 value = digit = 0;
56 ++field;
57 } else {
58 return false;
60 } else {
61 return false;
65 // Only set the referenced value if it was a valid IP
66 if ( field == 3 && digit && (value >> 8) == 0) {
67 Ip = tmp_ip | value << 24;
68 return true;
71 return false;
75 uint32 StringHosttoUint32(const wxString &Host)
77 if (Host.IsEmpty()) {
78 return 0;
80 // Why using native things when we have a wrapper for it :)
81 wxIPV4address solver;
82 solver.Hostname(Host);
83 uint32 result = StringIPtoUint32(solver.IPAddress());
84 if (result != (uint32)-1) {
85 return result;
86 } else {
87 // This actually happens on wrong hostname
88 return 0;
92 /**
93 * Used to store the ranges.
95 struct IPRange
97 const wxChar *addr;
98 unsigned int mask;
99 bool isLAN;
103 const IPRange ranges[] = {
104 // Here is reserved blocks from RFC 3330 at http://www.rfc-editor.org/rfc/rfc3330.txt
106 //Address Block Present Use Reference
107 //----------------------------------------------------------------------------------
108 { wxT("0.0.0.0"), 8, false }, // "This" Network [RFC1700, page 4]
109 { wxT("10.0.0.0"), 8, true }, // Private-Use Networks [RFC1918]
110 // Acording to RFC3330, 24.* and 14.* must be parsed as normal ips.
111 //{ wxT("14.0.0.0"), 8, false }, // Public-Data Networks [RFC1700, page 181]
112 //{ wxT("24.0.0.0"), 8, false }, // Cable Television Networks --
113 { wxT("39.0.0.0"), 8, false }, // Reserved but subject
114 // to allocation [RFC1797]
115 { wxT("127.0.0.0"), 8, false }, // Loopback [RFC1700, page 5]
116 { wxT("128.0.0.0"), 16, false }, // Reserved but subject
117 // to allocation --
118 { wxT("169.254.0.0"), 16, false }, // Link Local --
119 { wxT("172.16.0.0"), 12, true }, // Private-Use Networks [RFC1918]
120 { wxT("191.255.0.0"), 16, false }, // Reserved but subject
121 // to allocation --
122 { wxT("192.0.0.0"), 24, false }, // Reserved but subject
123 // to allocation --
124 { wxT("192.0.2.0"), 24, false }, // Test-Net
125 { wxT("192.88.99.0"), 24, false }, // 6to4 Relay Anycast [RFC3068]
126 { wxT("192.168.0.0"), 16, true }, // Private-Use Networks [RFC1918]
127 { wxT("198.18.0.0"), 15, false }, // Network Interconnect
128 // Device Benchmark Testing [RFC2544]
129 { wxT("223.255.255.0"), 24, false }, // Reserved but subject
130 // to allocation --
131 { wxT("224.0.0.0"), 4, false }, // Multicast [RFC3171]
132 { wxT("240.0.0.0"), 4, false } // Reserved for Future Use [RFC1700, page 4]
136 struct filter_st {
137 uint32 addr; // Address and mask in anti-host order.
138 uint32 mask;
141 const unsigned int number_of_ranges = sizeof(ranges) / sizeof(IPRange);
142 static filter_st filters[number_of_ranges];
145 // This function is used to initialize the IP filter
146 bool SetupFilter()
148 for (unsigned int i = 0; i < number_of_ranges; ++i) {
149 filters[i].addr = StringIPtoUint32( ranges[i].addr );
150 filters[i].mask = ~wxUINT32_SWAP_ALWAYS((1 << (32 - ranges[i].mask)) - 1);
152 return true;
156 // This is a little trick to ensure that the filter-list is initialized before
157 // it gets used, while not risking threading problems.
158 static bool filterSetup = SetupFilter();
161 bool IsGoodIP(uint32 IP, bool filterLAN)
163 for (unsigned int i = 0; i < number_of_ranges; ++i) {
164 if (((IP ^ filters[i].addr) & filters[i].mask) == 0) {
165 if ( filterLAN || !ranges[i].isLAN ) {
166 return false;
171 return true;
174 bool IsLanIP(uint32_t ip) throw()
176 for (unsigned int i = 0; i < number_of_ranges; i++) {
177 if (((ip ^ filters[i].addr) & filters[i].mask) == 0) {
178 return ranges[i].isLAN;
181 return false;
183 // File_checked_for_headers