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 <common/Format.h> // Needed for CFormat
32 #include <wx/socket.h>
34 // Network ip/host handling functions
35 // These functions take IPs in anti-host order
37 inline wxString
Uint32toStringIP(uint32 ip
)
39 return CFormat(wxT("%u.%u.%u.%u")) % (uint8
)ip
% (uint8
)(ip
>>8) % (uint8
)(ip
>>16) % (uint8
)(ip
>>24);
42 inline wxString
Uint32_16toStringIP_Port(uint32 ip
, uint16 port
)
44 return CFormat(wxT("%u.%u.%u.%u:%u")) % (uint8
)ip
% (uint8
)(ip
>>8) % (uint8
)(ip
>>16) % (uint8
)(ip
>>24) % port
;
47 // These functions take IPs in host-order
48 inline wxString
KadIPToString(uint32_t ip
)
50 return CFormat(wxT("%u.%u.%u.%u")) % (uint8_t)(ip
>> 24) % (uint8_t)(ip
>> 16) % (uint8_t)(ip
>> 8) % (uint8_t)ip
;
53 inline wxString
KadIPPortToString(uint32_t ip
, uint16_t port
)
55 return CFormat(wxT("%u.%u.%u.%u:%u")) % (uint8_t)(ip
>> 24) % (uint8_t)(ip
>> 16) % (uint8_t)(ip
>> 8) % (uint8_t)ip
% port
;
59 * Parses a String-IP and saves the IP in the referenced variable.
61 * @param strIP A string-ip in the format "a.b.c.d".
62 * @param Ip The value to save the result in.
63 * @return True if the string was parsed, false otherwise.
65 * When parsing the IP address, whitespace before or after the
66 * ip-address is ignored and the resulting IP is saved in
69 * The reason for the existance of this function is the fact that
70 * the standard inet_aton function treats numbers with 0 prefixed
71 * as octals, which is desirable.
73 * Note: The reference value will not be changed unless the string
74 * contains a valid IP adress.
76 bool StringIPtoUint32(const wxString
&strIP
, uint32
& Ip
);
80 * Parses a String-IP and returns the IP or 0 if it was invalid.
82 * @param strIP A string-ip in the format "a.b.c.d".
83 * @return The resulting IP-address or zero if invalid (or 0.0.0.0).
85 * The IP will be saved in anti-host order.
87 inline uint32
StringIPtoUint32(const wxString
&strIP
)
90 StringIPtoUint32( strIP
, ip
);
95 * Parses a String-IHost and returns the IP or 0 if it was invalid.
97 * @param Host A string with the Host to convert.
98 * @return The resulting IP-address or zero if invalid (or 0.0.0.0).
100 * The IP will be saved in anti-host order.
102 uint32
StringHosttoUint32(const wxString
&Host
);
105 * Checks for invalid IP-values.
107 * @param IP the IP-address to check.
108 * @param filterLAN Specifies if LAN IP-ranges should be filtered.
109 * @return True if it was valid, false otherwise.
111 * Note: IP must be in anti-host order (BE on LE platform, LE on BE platform).
113 bool IsGoodIP( uint32 IP
, bool filterLAN
);
115 inline bool IsGoodIPPort(uint32 nIP
, uint16 nPort
)
117 return IsGoodIP(nIP
, true) && nPort
!=0;
120 #define HIGHEST_LOWID_ED2K_KAD 16777216
122 inline bool IsLowID(uint32 id
) {
123 return (id
< HIGHEST_LOWID_ED2K_KAD
);
127 * Checks for LAN IPs.
129 * @param ip The IP-address to check.
130 * @return True if it was a LAN IP, false otherwise.
132 * @note IP must be in anti-host order.
134 bool IsLanIP(uint32_t ip
) throw();
136 #endif // NETWORK_FUNCTIONS_H
137 // File_checked_for_headers