summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/core/kmfprotocolusage.cpp
blob: e9a8ae623d177b5ebce2e40daa3ea3d6a167c9cb (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
//
// C++ Implementation: kmfprotocolusage
//
// Description: 
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kmfprotocolusage.h"

// TQt includes
#include <tqfile.h>
#include <tqdir.h>
#include <tqdom.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>

// KDE includes
#include <kdebug.h>
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <tdeio/netaccess.h>
#include <tdeio/job.h>
#include <ktrader.h>
#include <klibloader.h>
#include <tdetempfile.h>
#include <tdefileitem.h>

// project includes
#include "../version.h"
#include "kmfprotocol.h"
#include "kmfprotocollibrary.h"
#include "kmferror.h"
#include "kmferrorhandler.h"
#include "kmfgenericdoc.h"
#include "kmfnetzone.h"
#include "kmfnethost.h"
#include "xmlnames.h"

namespace KMF {

KMFProtocolUsage::KMFProtocolUsage( NetfilterObject* parent, const char* name )  : NetfilterObject( parent, name ) {
	m_protocol = 0;
	m_limit_interval = "minute";
	m_limit = -1;
	m_log = false;
}


KMFProtocolUsage::~KMFProtocolUsage()
{
}

const TQString& KMFProtocolUsage::name() {
//	kdDebug() << "KMFProtocolUsage::name()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "ERROR: No Protocol Stored in KMFProtocolUsage instance!" << endl;
		return *( new TQString( "ERROR: No Protocol Stored in KMFProtocolUsage instance!") );
	}
	return protocol()->name();
}

const TQString& KMFProtocolUsage::description() {
//	kdDebug() << "KMFProtocolUsage::description()" << endl;
	if ( ! m_protocol ) {
		kdDebug() << "ERROR: No Protocol Stored in KMFProtocolUsage instance!" << endl;
		return *( new TQString( "ERROR: No Protocol Stored in KMFProtocolUsage instance!") );
	}
	return protocol()->description();
}

int KMFProtocolUsage::type() {
	//kdDebug() << "KMFProtocolUsage::type()" << endl;
	return NetfilterObject::PROTOCOLUSAGE;
}
void KMFProtocolUsage::clear() {
	m_limit_interval = "minute";
	m_limit = -1;
	m_log = false;
}

void KMFProtocolUsage::setProtocol( KMFProtocol* p ) {
	// kdDebug() << "KMFProtocolUsage::setProtocol( KMFProtocol* " << p->name() << " )" << endl;
	m_protocol = p;
	
	disconnect( m_protocol, TQ_SIGNAL( destroyed( TQObject* ) ),
		this, TQ_SLOT( slotOnProtocolDeleted( TQObject* ) ) );
	connect( m_protocol, TQ_SIGNAL( destroyed( TQObject* ) ),
		this, TQ_SLOT( slotOnProtocolDeleted( TQObject* ) ) );
}

void KMFProtocolUsage::slotOnProtocolDeleted( TQObject* ) {
	kdDebug() << "KMFProtocolUsage::slotOnProtocolDeleted( TQObject* )" << endl;
	deleteLater();
}


bool KMFProtocolUsage::validUsage() {

	return m_protocol ? true : false;
}

void KMFProtocolUsage::setIO ( int io ) {
	m_io = io;
}

void KMFProtocolUsage::setLimitInterval( const TQString& interval ) {
	// kdDebug() << "void KMFProtocolUsage::setLimitInterval( const TQString& )" << endl;
	if ( m_limit_interval == interval ) {
		return;
	}
	m_limit_interval = interval;
	changed();
}

void KMFProtocolUsage::setLimit( int limit ){
	// kdDebug() << "void KMFProtocolUsage::setLimit( int )" << endl;
	if ( m_limit == limit ) {
		return;
	}
	m_limit = limit;
	changed();
}

void KMFProtocolUsage::setLogging( bool onoff ) {
	// kdDebug() << "void KMFProtocolUsage::setLimit( int )" << endl;
	if ( m_log == onoff ) {
		return;
	}
	m_log = onoff;
	changed();
}

const TQDomDocument& KMFProtocolUsage::getDOMTree() {
	// kdDebug() << "const TQDomDocument& KMFProtocolUsage::getDOMTree()" << endl;
	TQDomDocument doc;
	TQDomElement root = doc.createElement( XML::ProtocolUsage_Element );
	NetfilterObject::saveUuid( root );
	// root.setAttribute( "protocolName", protocol()->name() );
	root.setAttribute( XML::ProtocolUuid_Attribute, protocol()->uuid().toString() );
	
	if ( logging() ) {
		root.setAttribute( XML::Logging_Attribute, XML::Yes_Value );
	} else {
		root.setAttribute( XML::Logging_Attribute, XML::No_Value );
	}
	
	if ( io() == INCOMING ) {
		root.setAttribute( XML::IO_Attribute, XML::Incoming_Value );
	}
	if (  io() == OUTGOING ) {
		root.setAttribute( XML::IO_Attribute, XML::Outgoing_Value );
	}

	TQString lim = "";
	lim = lim.setNum( m_limit );
	lim  += "/" + m_limit_interval;
	root.setAttribute( XML::Limit_Attribute, lim );
	
	doc.appendChild( root );
	return *( new TQDomDocument( doc ) );
}

void KMFProtocolUsage::loadXML( const TQDomDocument& doc, TQStringList& errors ) {
	// kdDebug() << "void KMFProtocolUsage::loadXML( const TQDomDocument& )" << endl;
 	TQDomElement root = doc.documentElement();
 	loadXML( root, errors );
}
void KMFProtocolUsage::loadXML( TQDomNode root, TQStringList& errors ) {
	kdDebug() << "void KMFProtocolUsage::loadXML( TQDomNode root )" << endl;
	NetfilterObject::loadUuid ( root, errors );
	TQString protocolName = "";
	TQString protocolUuid = "";
	TQString logging = "";
	TQString desc = "";
	TQString limit = "";

	TQString io = "";
	// protocolName = "";
	

	if ( root.toElement().hasAttribute( XML::ProtocolUuid_Attribute ) ) {
		protocolUuid = root.toElement().attribute( XML::ProtocolUuid_Attribute );
	} /*else if ( root.toElement().hasAttribute( "protocolName" ) ) {
		protocolName = root.toElement().attribute( "protocolName" );
	} */else if ( root.toElement().hasAttribute(  XML::Name_Attribute ) ) {
		protocolName = root.toElement().attribute(  XML::Name_Attribute );
	} else {
		kdDebug() << "ERROR: NO Protocol name found for Protocol usage!" << endl;
		errors.append( KMFError::getAsString( KMFError::NORMAL, i18n( "No Protocol reference found for ProtocolUsage !" ) ) );
		return;
	}
	
	
	if ( ! protocolUuid.isEmpty() ) {
// 		kdDebug() << "Using Protocol Uuid!" << endl;
		KMFProtocol* prot = KMFProtocolLibrary::instance()->findProtocolByUuid( *( new TQUuid( protocolUuid ) ) );
		if ( ! prot ) {
			errors.append( KMFError::getAsString( KMFError::NORMAL, i18n( "Could not find Protocol: %1 in protocol Library" ).arg( protocolUuid ) ) );
			kdDebug() << "ERROR: Could not find Protocol: " << protocolUuid << " in protocol Library" << endl;
			return;
		}
		setProtocol( prot );
	} else {
	// 	kdDebug() << "Using Protocol Name!" << endl;
		errors.append( KMFError::getAsString( KMFError::WARNING, i18n( "Using Protocol Name! As Reference in Protocol Usage." ) ) );
		
		KMFProtocol* prot = KMFProtocolLibrary::instance()->findProtocolByName( protocolName );
		if ( ! prot ) {
			errors.append( KMFError::getAsString( KMFError::NORMAL, i18n( "Could not find Protocol: %1 in protocol Library" ).arg( protocolName ) ) );
			kdDebug() << "ERROR: Could not find Protocol: " << protocolName << " in protocol Library" << endl;
			return;
		}
		setProtocol( prot );
	}
	
	
	
	if ( root.toElement().hasAttribute(  XML::Logging_Attribute ) ) {
		logging = root.toElement().attribute( XML::Logging_Attribute );
		if ( logging == XML::Yes_Value ) {
			setLogging(true);
		} else {
			setLogging(false);
		}
	}
	if ( root.toElement().hasAttribute( XML::IO_Attribute ) ) {
		io = root.toElement().attribute( XML::IO_Attribute );
	
		if ( io == XML::Incoming_Value) {
			m_io = INCOMING;
		} else {
			m_io = OUTGOING;
		}
	}
	
	if ( root.toElement().hasAttribute( XML::Limit_Attribute ) ) {
		limit = root.toElement().attribute( XML::Limit_Attribute );
		int pos = limit.find('/');
		TQString limi = limit.left( pos );
		TQString limit_interval = limit.right( limit.length() - pos  -1 );
		
		bool ok;
		int lim = limi.toInt( &ok );
		if ( ok ) {
			m_limit = lim;
		}
		m_limit_interval = limit_interval;
	}
	changed();
}

}