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-2011 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.
11//
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.
16//
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
25//
26
27#ifndef NETWORK_FUNCTIONS_H
28#define NETWORK_FUNCTIONS_H
29
30#include "Types.h"		// Needed for uint16 and uint32
31#include <common/Format.h>	// Needed for CFormat
32#include <wx/socket.h>
33
34// Network ip/host handling functions
35// These functions take IPs in anti-host order
36
37inline wxString Uint32toStringIP(uint32 ip)
38{
39	return CFormat(wxT("%u.%u.%u.%u")) % (uint8)ip % (uint8)(ip>>8) % (uint8)(ip>>16) % (uint8)(ip>>24);
40}
41
42inline wxString Uint32_16toStringIP_Port(uint32 ip, uint16 port)
43{
44	return CFormat(wxT("%u.%u.%u.%u:%u")) % (uint8)ip % (uint8)(ip>>8) % (uint8)(ip>>16) % (uint8)(ip>>24) % port;
45}
46
47// These functions take IPs in host-order
48inline wxString KadIPToString(uint32_t ip)
49{
50	return CFormat(wxT("%u.%u.%u.%u")) % (uint8_t)(ip >> 24) % (uint8_t)(ip >> 16) % (uint8_t)(ip >> 8) % (uint8_t)ip;
51}
52
53inline wxString KadIPPortToString(uint32_t ip, uint16_t port)
54{
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;
56}
57
58/**
59 * Parses a String-IP and saves the IP in the referenced variable.
60 *
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.
64 *
65 * When parsing the IP address, whitespace before or after the
66 * ip-address is ignored and the resulting IP is saved in
67 * anti-host order.
68 *
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.
72 *
73 * Note: The reference value will not be changed unless the string
74 *       contains a valid IP adress.
75 */
76bool	StringIPtoUint32(const wxString &strIP, uint32& Ip);
77
78
79/**
80 * Parses a String-IP and returns the IP or 0 if it was invalid.
81 *
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).
84 *
85 * The IP will be saved in anti-host order.
86 */
87inline uint32 StringIPtoUint32(const wxString &strIP)
88{
89	uint32 ip = 0;
90	StringIPtoUint32( strIP, ip );
91
92	return ip;
93}
94/**
95 * Parses a String-IHost and returns the IP or 0 if it was invalid.
96 *
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).
99 *
100 * The IP will be saved in anti-host order.
101 */
102uint32 StringHosttoUint32(const wxString &Host);
103
104/**
105 * Checks for invalid IP-values.
106 *
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.
110 *
111 * Note: IP must be in anti-host order (BE on LE platform, LE on BE platform).
112 */
113bool IsGoodIP( uint32 IP, bool filterLAN );
114
115inline bool IsGoodIPPort(uint32 nIP, uint16 nPort)
116{
117	return IsGoodIP(nIP, true) && nPort!=0;
118}
119
120#define HIGHEST_LOWID_ED2K_KAD		16777216
121
122inline bool IsLowID(uint32 id) {
123	return (id < HIGHEST_LOWID_ED2K_KAD);
124}
125
126/**
127 * Checks for LAN IPs.
128 *
129 * @param ip The IP-address to check.
130 * @return True if it was a LAN IP, false otherwise.
131 *
132 * @note IP must be in anti-host order.
133 */
134bool IsLanIP(uint32_t ip) throw();
135
136#endif // NETWORK_FUNCTIONS_H
137// File_checked_for_headers
138