2 // This file is part of the aMule Project.
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 )
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 #ifndef NETWORK_FUNCTIONS_H
28 #define NETWORK_FUNCTIONS_H
30 #include "Types.h" // Needed for uint16 and uint32
31 #include <wx/socket.h>
33 // Network ip/host handling functions
34 // These functions takes IPs in anti-host order and IPs in anti-host order
36 inline wxString
Uint32toStringIP(uint32 ip
)
38 return wxString::Format(wxT("%u.%u.%u.%u"),(uint8
)ip
,(uint8
)(ip
>>8),(uint8
)(ip
>>16),(uint8
)(ip
>>24));
41 inline wxString
Uint32_16toStringIP_Port(uint32 ip
, uint16 port
)
43 return wxString::Format(wxT("%u.%u.%u.%u:%u"),(uint8
)ip
,(uint8
)(ip
>>8),(uint8
)(ip
>>16),(uint8
)(ip
>>24),port
);
48 * Parses a String-IP and saves the IP in the referenced variable.
50 * @param strIP A string-ip in the format "a.b.c.d".
51 * @param Ip The value to save the result in.
52 * @return True if the string was parsed, false otherwise.
54 * When parsing the IP address, whitespace before or after the
55 * ip-address is ignored and the resulting IP is saved in
58 * The reason for the existance of this function is the fact that
59 * the standard inet_aton function treats numbers with 0 prefixed
60 * as octals, which is desirable.
62 * Note: The reference value will not be changed unless the string
63 * contains a valid IP adress.
65 bool StringIPtoUint32(const wxString
&strIP
, uint32
& Ip
);
69 * Parses a String-IP and returns the IP or 0 if it was invalid.
71 * @param strIP A string-ip in the format "a.b.c.d".
72 * @return The resulting IP-address or zero if invalid (or 0.0.0.0).
74 * The IP will be saved in anti-host order.
76 inline uint32
StringIPtoUint32(const wxString
&strIP
)
79 StringIPtoUint32( strIP
, ip
);
84 * Parses a String-IHost and returns the IP or 0 if it was invalid.
86 * @param Host A string with the Host to convert.
87 * @return The resulting IP-address or zero if invalid (or 0.0.0.0).
89 * The IP will be saved in anti-host order.
91 uint32
StringHosttoUint32(const wxString
&Host
);
94 * Checks for invalid IP-values.
96 * @param IP the IP-address to check.
97 * @param filterLAN Specifies if LAN IP-ranges should be filtered.
98 * @return True if it was valid, false otherwise.
100 * Note: IP must be in anti-host order (BE on LE platform, LE on BE platform).
102 bool IsGoodIP( uint32 IP
, bool filterLAN
);
104 inline bool IsGoodIPPort(uint32 nIP
, uint16 nPort
)
106 return IsGoodIP(nIP
, true) && nPort
!=0;
109 #define HIGHEST_LOWID_ED2K_KAD 16777216
111 inline bool IsLowID(uint32 id
) {
112 return (id
< HIGHEST_LOWID_ED2K_KAD
);
116 * Checks for LAN IPs.
118 * @param ip The IP-address to check.
119 * @return True if it was a LAN IP, false otherwise.
121 * @note IP must be in anti-host order.
123 bool IsLanIP(uint32_t ip
) throw();
125 #endif // NETWORK_FUNCTIONS_H
126 // File_checked_for_headers