summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/core/ipaddress.cpp
blob: b2eb58d16d63d1755c64d55a114f4270d6fbdd5a (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// C++ Implementation: 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.                                   *
 *                                                                         *
 ***************************************************************************/


#include "ipaddress.h"

// TQt includes

// KDE includes
#include <kdebug.h>

// Project includes
#include "kmfcheckinput.h"
#include "kmferror.h"
#include "xmlnames.h"
using namespace std;

namespace KMF {

IPAddress::IPAddress( int fi, int se, int th, int fo ) {
	m_checkInput = new KMFCheckInput();
	m_err = new KMFError();
	for ( int i = 0; i < NUMDIGITS; i++ ) {
		m_digits[i] = 0;
	}
	if ( !setAddress( fi, se, th, fo ) )
		kdDebug() << "ERROR: Tried to initialise IPAddress with invalid parameters." << endl;
}


IPAddress::~IPAddress() {
	delete m_checkInput;
	delete m_err;
}

int IPAddress::getDigit( int num ) const {
	if ( num >= 4 || num < 0 )
		return -1;
	return m_digits[ num ];
}

IPAddress&  IPAddress::plus(int num) {
	// 	kdDebug() << "IPAddress&  IPAddress::operator+( int " << num << " )" << endl;
	if ( m_digits[3] + num  < 256 &&  m_digits[3] + num  > -1 ) {
		m_digits[3] = m_digits[3] + num;
	}
	return *this;
}

int IPAddress::operator==( const IPAddress& addr ) {
	// 	kdDebug() << "IPAddress&  IPAddress::operator==( const IPAddress& addr " << addr.toString()<< " )" << endl;
	bool ident = true;
	int first_diff = 0;
	for ( int i = 0; i < NUMDIGITS && ident; i++ ) {
		if ( m_digits[i] != addr.getDigit(i) ) {
			ident = false;
			first_diff = i;
		}
	}
	if ( ident )
		return EQUAL;

	if ( m_digits[ first_diff ] > addr.getDigit( first_diff )  )
		return SMALLER;
	else
		return BIGGER;
}

bool IPAddress::setAddress( int fi, int se, int th, int fo ) {
	if ( fi < 0 || fi > 255 || se < 0 || se > 255 || th < 0 || th > 255 || fo < 0 || fo > 255 )
		return false;

	m_digits[0] = fi;
	m_digits[1] = se;
	m_digits[2] = th;
	m_digits[3] = fo;

	return true;
}

bool IPAddress::setAddress( const TQString& input ) {
	TQString inp = input;
	m_checkInput->checkInput( inp, "IP", m_err );
	if ( m_err->errType() != KMFError::OK ) {
		kdDebug() << "WARNING: Given wron IP address string: " << inp << endl;
		return false;
	}
	int pos;
	TQString str_num;
	bool valid = true;
	int counter = 0;
	while ( !inp.isEmpty() ) {
		pos = inp.find( "." );
		if ( pos > -1 ) {
			str_num = inp.left( pos );
			// 			kdDebug() << "IP Num Part: " << str_num << endl;
			inp = inp.right( inp.length() - ( pos + 1 ) );
			// 			kdDebug() << "Digit: " << str_num << endl;
			int val = str_num.toInt();
			//				kdDebug() << "Val: " << val << endl;
			if ( val < 0 || val > 255 ) {
				valid = false;
			} else {
				// 				kdDebug() << "IPAddress: Setting digit: " << counter  << " to "  << val << endl;
				m_digits[ counter ] = val;
				counter++;
			}
		} else {
			str_num = inp;
			//				kdDebug() << "IP Num Part: " << str_num << endl;
			inp = "";
			//				kdDebug() << "Rest: " << inp << endl;
			int val = str_num.toInt();
			// 			kdDebug() << "Digit: " << val << endl;
			if ( val < 0 || val > 255 ) {
				valid = false;
			} else {
				// 				kdDebug() << "IPAddress: Setting digit: " << counter  << " to "  << val << endl;
				m_digits[ counter ] = val;
				counter++;
			}
		}
	}
	return true;
}

const TQString& IPAddress::toString() const {
	TQString fi = "";
	TQString se = "";
	TQString th = "";
	TQString fo = "";
	return *( new TQString( fi.setNum( m_digits[0] ) + "." + se.setNum( m_digits[1] ) + "." + th.setNum( m_digits[2] ) + "." + fo.setNum( m_digits[3] ) ) );
}


// static stuff
IPAddress& IPAddress::calcNetworkMaskFromLength( int len ) {
	TQValueList<int> list;
	int nextOne = 0;
	int digit1 = IPAddress::calcLenthToMaskDigit( len, &nextOne );
	int digit2 = IPAddress::calcLenthToMaskDigit( nextOne, &nextOne );
	int digit3 = IPAddress::calcLenthToMaskDigit( nextOne, &nextOne );
	int digit4 = IPAddress::calcLenthToMaskDigit( nextOne, &nextOne );
	list.append( digit1 );
	list.append( digit2 );
	list.append( digit3 );
	list.append( digit4 );
	IPAddress *addr = new IPAddress( digit1,digit2,digit3,digit4 );

	return *addr;
}


int IPAddress::calcLenthToMaskDigit( int nMask, int *nextOne ) {
	if (nMask < 1 || nMask > 32 ) {
		return 0;
	}
	int nCalc = 0;
	for (int nX = 7;nX > -1 ; nX--) {
		int total=1;
		for ( int j=0; j < nX; j++) {
			total*=2;
		}

		nCalc=nCalc + total;

		nMask = nMask -1;
		*nextOne=nMask;
		if (nMask <1) {
			return nCalc;
		}
	}
	return nCalc;
}

int IPAddress::calcLenthToMask( IPAddress& addr) {
	// kdDebug() << "calcLenthToMask( " << addr.toString() << " )" << endl;
	if ( ! IPAddress::isValidMask( addr ) ) {
		kdDebug() << "Netmaks is not Valid!!!" << endl;
		return -1;
	}

	int m[4];
	for ( int i = 0; i< 4 ; i++ ) {
		m[i] = addr.getDigit( i );
	}


	int mask = 0;
	for (int loop=0; loop<4; loop++) {
		int div = 256;
		while ( div > 1) {
			div = div/2;
			int test = m[loop] - div;
			if ( test >-1) {
				mask=mask+1;
				m[loop]=test;
			} else {
				break;
			}
		}

	}
	// kdDebug()  << "Returning: " << mask << endl;
	return mask;
}

bool IPAddress::isValidAddress( IPAddress& addr) {
	int IP1 = addr.getDigit( 0 );
	int IP2 = addr.getDigit( 1 );
	int IP3 = addr.getDigit( 2 );
	int IP4 = addr.getDigit( 3 );

	if ((IP1 > 255) || (IP1 < 0)) {
		return false;
	}
	if ((IP2 > 255) || (IP2 < 0)) {
		return false;
	}
	if ((IP3 > 255) || (IP3 < 0)) {
		return false;
	}
	if ((IP4 > 255) || (IP4 < 0)) {
		return false;
	}
	return true;
}

bool IPAddress::isValidMask( IPAddress& addr) {
	// alert(IP1+"."+IP2+"."+IP3+"."+IP4)
	int IP1 = addr.getDigit( 0 );
	int IP2 = addr.getDigit( 1 );
	int IP3 = addr.getDigit( 2 );
	int IP4 = addr.getDigit( 3 );

	if ((IP1 > 255) || (IP1 < 0)) {
		return false;
	}
	if ((IP2 > 255) || (IP2 < 0)) {
		return false;
	}
	if ((IP3 > 255) || (IP3 < 0)) {
		return false;
	}
	if ((IP4 > 255) || (IP4 < 0)) {
		return false;
	}

	int IPX =5;
	// find where it changes
	if (IP1 < 255) {
		if((IP2 > 0) || (IP3 > 0) || (IP4 > 0)) {
			return false;
		}
		IPX = IP1;
	} else {
		if (IP2 < 255) {
			if((IP3 > 0) || (IP4 > 0)) {
				return false;
			}
			IPX = IP2;
		} else {
			if (IP3 < 255) {
				if((IP4 > 0)) {
					return false;
				}
				IPX = IP3;
			} else {
				IPX = IP4;
			}
		}
	}

	// determine if IPX is a good
	switch (IPX) {
	case 255:
	case 128:
	case 192:
	case 224:
	case 240:
	case 248:
	case 252:
	case 254:
	case 0:
		return true;
	default:
		return false;
	}

	return true;
}

bool IPAddress::hostsOnSameNetwork(  IPAddress& host1, IPAddress& host2, int len ) {
//	kdDebug() << "IPAddress::hostsOnSameNetwork( IPAddress&, IPAddress&, int )" << endl;
	IPAddress mask = IPAddress::calcNetworkMaskFromLength( len );
	return ( IPAddress::hostsOnSameNetwork( host1, host2, mask ) );
}

bool IPAddress::hostsOnSameNetwork( IPAddress& host1, IPAddress& host2, IPAddress& mask ) {
	kdDebug() << "IPAddress::hostsOnSameNetwork( IPAddress&, IPAddress&, int )" << endl;
	kdDebug() << "Host 1: " << host1.toString() <<  endl; 
	kdDebug() << "Host 2: " << host2.toString() << endl;
	kdDebug() << "Mask: " << mask.toString() << endl; 
//	IPAddress mask = IPAddress::calcNetworkMaskFromLength( len );

	int nOctA1=host1.getDigit(0) & mask.getDigit(0);
	int nOctA2=host1.getDigit(1) & mask.getDigit(1);
	int nOctA3=host1.getDigit(2) & mask.getDigit(2);
	int nOctA4=host1.getDigit(3) & mask.getDigit(3);


	int nOctB1=host2.getDigit(0) & mask.getDigit(0);
	int nOctB2=host2.getDigit(1) & mask.getDigit(1);
	int nOctB3=host2.getDigit(2) & mask.getDigit(2);
	int nOctB4=host2.getDigit(3) & mask.getDigit(3);

	if ((nOctA1==nOctB1) && (nOctA2==nOctB2) && (nOctA3==nOctB3) && (nOctA4==nOctB4)) {
		kdDebug() << "Hosts on same net." << endl;
		return true;
	} else {
		kdDebug() << "Hosts NOT on same net." << endl;
		return false;
	}
}
}