Upstream tarball 10010
[amule.git] / src / NetworkFunctions.h
blobc78950f5c2b4c89cd117797f4caa9f654ba48715
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 #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 take 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);
46 // These functions take IPs in host-order
47 inline wxString KadIPToString(uint32_t ip)
49 return wxString::Format(wxT("%u.%u.%u.%u"), (uint8_t)(ip >> 24), (uint8_t)(ip >> 16), (uint8_t)(ip >> 8), (uint8_t)ip);
52 inline wxString KadIPPortToString(uint32_t ip, uint16_t port)
54 return wxString::Format(wxT("%u.%u.%u.%u:%u"), (uint8_t)(ip >> 24), (uint8_t)(ip >> 16), (uint8_t)(ip >> 8), (uint8_t)ip, port);
57 /**
58 * Parses a String-IP and saves the IP in the referenced variable.
60 * @param strIP A string-ip in the format "a.b.c.d".
61 * @param Ip The value to save the result in.
62 * @return True if the string was parsed, false otherwise.
64 * When parsing the IP address, whitespace before or after the
65 * ip-address is ignored and the resulting IP is saved in
66 * anti-host order.
68 * The reason for the existance of this function is the fact that
69 * the standard inet_aton function treats numbers with 0 prefixed
70 * as octals, which is desirable.
72 * Note: The reference value will not be changed unless the string
73 * contains a valid IP adress.
75 bool StringIPtoUint32(const wxString &strIP, uint32& Ip);
78 /**
79 * Parses a String-IP and returns the IP or 0 if it was invalid.
81 * @param strIP A string-ip in the format "a.b.c.d".
82 * @return The resulting IP-address or zero if invalid (or 0.0.0.0).
84 * The IP will be saved in anti-host order.
86 inline uint32 StringIPtoUint32(const wxString &strIP)
88 uint32 ip = 0;
89 StringIPtoUint32( strIP, ip );
91 return ip;
93 /**
94 * Parses a String-IHost and returns the IP or 0 if it was invalid.
96 * @param Host A string with the Host to convert.
97 * @return The resulting IP-address or zero if invalid (or 0.0.0.0).
99 * The IP will be saved in anti-host order.
101 uint32 StringHosttoUint32(const wxString &Host);
104 * Checks for invalid IP-values.
106 * @param IP the IP-address to check.
107 * @param filterLAN Specifies if LAN IP-ranges should be filtered.
108 * @return True if it was valid, false otherwise.
110 * Note: IP must be in anti-host order (BE on LE platform, LE on BE platform).
112 bool IsGoodIP( uint32 IP, bool filterLAN );
114 inline bool IsGoodIPPort(uint32 nIP, uint16 nPort)
116 return IsGoodIP(nIP, true) && nPort!=0;
119 #define HIGHEST_LOWID_ED2K_KAD 16777216
121 inline bool IsLowID(uint32 id) {
122 return (id < HIGHEST_LOWID_ED2K_KAD);
126 * Checks for LAN IPs.
128 * @param ip The IP-address to check.
129 * @return True if it was a LAN IP, false otherwise.
131 * @note IP must be in anti-host order.
133 bool IsLanIP(uint32_t ip) throw();
135 #endif // NETWORK_FUNCTIONS_H
136 // File_checked_for_headers