summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/core/ipaddress.h
blob: b9a71ccd11a8a6d654671315ab7313c2bc13ac8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// C++ Interface: ipaddress
//
// Description: 
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#ifndef IPADDRESS_H
#define IPADDRESS_H

/**
@author Christian Hubinger
*/

// QT includes
#include <qstring.h>
#include <qvaluelist.h>

// KDE includes
#include <kdemacros.h>

// Project includes

namespace KMF {
class KMFError;
class KMFCheckInput;

#define NUMDIGITS 4


class KDE_EXPORT IPAddress {
public:
	IPAddress( int = 0, int = 0, int = 0, int = 0 );
	~IPAddress();

	/** Sets the address to the given 4 int representation.
		Returns false if the address is invalid */
	bool setAddress( int, int, int, int );
	
	/** Set the address to the address given in the string.
		Returns false if the address is invalid */
	bool setAddress( const QString& );
	
	
	IPAddress&  plus(int); // FIXME: remove that!!!
	
	/** check if the address is equal to the other */	
	int operator==( const IPAddress& );

	/** Return the int for the given posintion in the address */
	int getDigit( int num ) const;
	
	/** returns a QString holding the address e.g. 123.234.123.234 */
	const QString& toString() const;

	/** Return values for the comparision operator== */
	enum { EQUAL, BIGGER, SMALLER };
	
private:
	int m_digits[ NUMDIGITS ];
	KMFCheckInput *m_checkInput;
	KMFError *m_err;



// static stuff
public:	
	/** Calculates the netmask from the mask length
		e.g. 255.255.255.0 -> 24 */
	static IPAddress& calcNetworkMaskFromLength( int maskLen );
	
	/** Calculates the given netmask length to the net mask.
		e.g. 24 -> 255.255.255.0 */
	static int calcLenthToMask( IPAddress& );
	
	/** Returns true it the given adddress is a valid net mask */
	static bool isValidMask( IPAddress& );
	
	/** Returns true it the given adddress is a valid net mask */
	static bool isValidAddress( IPAddress& );
	
	/** Checks if the the addresses are on the same network. */
	static bool hostsOnSameNetwork( IPAddress&, IPAddress&, int maskLen );
	
	/** Checks if the the addresses are on the same network. */
	static bool hostsOnSameNetwork( IPAddress&, IPAddress&, IPAddress& mask );

private:	
	private: static int calcLenthToMaskDigit( int nMask, int *nextOne );

};
}
#endif