Fix compilation with wxWidgets 2.8.12
[amule.git] / src / NetworkFunctions.cpp
blob53845eb55d976835fe5ddc2a513a86ce98dbe2e7
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2003-2016 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2002-2011 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.
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
28 #include "amuleIPV4Address.h"
29 #include <common/Macros.h> // Needed for itemsof()
32 bool StringIPtoUint32(const wxString &strIP, uint32& Ip)
34 // The current position in the current field, used to detect malformed fields (x.y..z).
35 unsigned digit = 0;
37 // The current field, used to ensure only IPs that looks like a.b.c.d are supported
38 unsigned field = 0;
40 // The value of the current field
41 unsigned value = 0;
43 // Stores the work-value of the IP, reference is not changed unless the str was valid
44 uint32 tmp_ip = 0;
46 wxString str = strIP.Strip( wxString::both );
47 for (size_t i = 0; i < str.Length(); i++) {
48 wxChar c = str.GetChar( i );
50 if ( c >= wxT('0') && c <= wxT('9') && (value >> 8) == 0) {
51 value = ( value * 10 ) + ( c - wxT('0') );
52 ++digit;
53 } else if ( c == wxT('.') ) {
54 if ( digit && (value >> 8) == 0) {
55 tmp_ip = tmp_ip | value << ( field * 8 );
57 // Rest the current field values
58 value = digit = 0;
59 ++field;
60 } else {
61 return false;
63 } else {
64 return false;
68 // Only set the referenced value if it was a valid IP
69 if ( field == 3 && digit && (value >> 8) == 0) {
70 Ip = tmp_ip | value << 24;
71 return true;
74 return false;
78 uint32 StringHosttoUint32(const wxString &Host)
80 if (Host.IsEmpty()) {
81 return 0;
83 amuleIPV4Address solver;
84 if (solver.Hostname(Host)) {
85 uint32 result = StringIPtoUint32(solver.IPAddress());
86 if (result != (uint32)-1) { // should not happen
87 return result;
90 // This actually happens on wrong hostname
91 return 0;
95 struct filter_st {
96 uint32 addr; // Address in anti-host order.
97 uint32 mask; // Mask in anti-host order.
101 static filter_st reserved_ranges[] = {
102 // Here are the reserved blocks from RFC 3330 at http://www.rfc-editor.org/rfc/rfc3330.txt
104 // Address Block Present Use Reference
105 //---------------------------------------------------------------------------------------------------------------------
106 { 0x00000000, 0x000000ff }, // 0.0.0.0/8 "This" Network [RFC1700, page 4]
107 // According to RFC3330, 14.* and 24.* must be parsed as normal IPs.
108 // { 0x0000000e, 0x000000ff }, // 14.0.0.0/8 Public-Data Networks [RFC1700, page 181]
109 // { 0x00000018, 0x000000ff }, // 24.0.0.0/8 Cable Television Networks --
110 { 0x00000027, 0x000000ff }, // 39.0.0.0/8 Reserved but subject to allocation [RFC1797]
111 { 0x0000007f, 0x000000ff }, // 127.0.0.0/8 Loopback [RFC1700, page 5]
112 { 0x00000080, 0x0000ffff }, // 128.0.0.0/16 Reserved but subject to allocation --
113 { 0x0000fea9, 0x0000ffff }, // 169.254.0.0/16 Link Local --
114 { 0x0000ffbf, 0x0000ffff }, // 191.255.0.0/16 Reserved but subject to allocation --
115 { 0x000000c0, 0x00ffffff }, // 192.0.0.0/24 Reserved but subject to allocation --
116 { 0x000200c0, 0x00ffffff }, // 192.0.2.0/24 Test-Net
117 { 0x006358c0, 0x00ffffff }, // 192.88.99.0/24 6to4 Relay Anycast [RFC3068]
118 { 0x000012c6, 0x0000feff }, // 198.18.0.0/15 Network Interconnect Device Benchmark Testing [RFC2544]
119 { 0x00ffffdf, 0x00ffffff }, // 223.255.255.0/24 Reserved but subject to allocation --
120 { 0x000000e0, 0x000000f0 }, // 224.0.0.0/4 Multicast [RFC3171]
121 { 0x000000f0, 0x000000f0 } // 240.0.0.0/4 Reserved for Future Use [RFC1700, page 4]
125 // Private-Use Networks [RFC1918]
126 static filter_st lan_ranges[] = {
127 { 0x0000000a, 0x000000ff }, // 10.0.0.0/8
128 { 0x000010ac, 0x0000f0ff }, // 172.16.0.0/12
129 { 0x0000a8c0, 0x0000ffff } // 192.168.0.0/16
133 bool IsGoodIP(uint32 ip, bool filterLAN) throw()
135 for (unsigned int i = 0; i < itemsof(reserved_ranges); ++i) {
136 if (((ip ^ reserved_ranges[i].addr) & reserved_ranges[i].mask) == 0) {
137 return false;
141 return !(filterLAN && IsLanIP(ip));
144 bool IsLanIP(uint32_t ip) throw()
146 for (unsigned int i = 0; i < itemsof(lan_ranges); ++i) {
147 if (((ip ^ lan_ranges[i].addr) & lan_ranges[i].mask) == 0) {
148 return true;
151 return false;