File: ClntAddrMgr.cpp

package info (click to toggle)
dibbler 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,352 kB
  • sloc: cpp: 60,323; ansic: 12,235; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (376 lines) | stat: -rw-r--r-- 9,063 bytes parent folder | download | duplicates (3)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 *
 * released under GNU GPL v2 only licence
 *
 */

#include "SmartPtr.h"
#include "AddrIA.h"
#include "ClntAddrMgr.h"
#include "AddrClient.h"
#include "Logger.h"

using namespace std;

TClntAddrMgr * TClntAddrMgr::Instance = 0;

void TClntAddrMgr::instanceCreate(SPtr<TDUID> clientDUID, bool useConfirm,
                                  const std::string& xmlFile, bool loadDB) {
  if (Instance) {
      Log(Crit) << "Attempt to create another instance of TClntAddrMgr!" << LogEnd;
      return;
  }
  Instance = new TClntAddrMgr(clientDUID, useConfirm, xmlFile, loadDB);
}

TClntAddrMgr& TClntAddrMgr::instance()
{
  return *Instance;
}

/**
 * @brief Client Address Manager constructor
 *
 * constructor used for creation of client version of address manager
 *
 * @param clientDUID pointer to client DUID
 * @param useConfirm should confirm be used or not?
 * @param xmlFile XML filename (AddrMgr dumps will be stored there)
 * @param loadDB should existing dump be loaded?
 *
 */
TClntAddrMgr::TClntAddrMgr(SPtr<TDUID> clientDUID, bool useConfirm,
                           const std::string& xmlFile, bool loadDB)
    :TAddrMgr(xmlFile, useConfirm)
{
    // client may have been already loaded from client-AddrMgr.xml file
    firstClient();
    if (!getClient()) {
	// add this client (with proper duid)
	SPtr<TAddrClient> client = new TAddrClient(clientDUID);
	addClient(client);
    }

    // set Client field
    DeleteEmptyClient = false; // don't delete this client, even when IAs or PD has been removed
    firstClient();
    Client = getClient();

    if (useConfirm)
	processLoadedDB();
}

void TClntAddrMgr::processLoadedDB() {
    SPtr<TAddrIA> ia;
    Client->firstIA();
    while (ia=Client->getIA()) {
	ia->setState(STATE_CONFIRMME);
    }

    Client->firstPD();
    while (ia=Client->getPD()) {
	ia->setState(STATE_CONFIRMME);
    }
}

unsigned long TClntAddrMgr::getT1Timeout() {
    return Client->getT1Timeout();
}
unsigned long TClntAddrMgr::getT2Timeout() {
    return Client->getT2Timeout();
}
unsigned long TClntAddrMgr::getPrefTimeout() {
    return Client->getPrefTimeout();
}
unsigned long TClntAddrMgr::getValidTimeout() {
    return Client->getValidTimeout();
}

unsigned long TClntAddrMgr::getTimeout()
{
    unsigned long val, val2;
    val = this->getT1Timeout();
    if ( (val2 = this->getT2Timeout()) < val)
	val = val2;

    // no special action is required after preferred timeout reached
    // (except perhaps logging that we are screwed, as no new connections could
    // be created)
    //if ( (val2 = this->getPrefTimeout()) < val)
    //  val = val2;
    if ( (val2 = this->getValidTimeout()) < val)
	val = val2;
    return val;
}

unsigned long TClntAddrMgr::getTentativeTimeout()
{
    SPtr<TAddrIA> ptrIA;
    Client->firstIA();
    uint32_t min = DHCPV6_INFINITY;

    while(ptrIA=Client->getIA())
    {
        uint32_t tmp = ptrIA->getTentativeTimeout();
        if (min > tmp)
            min = tmp;
    }

    return min;
}

/**
 * @brief Removes outdated addresses
 *
 * removes addresses that already expired. May also perform
 * other duties. This method should be called periodically.
 *
 */
void TClntAddrMgr::doDuties()
{
    SPtr<TAddrIA> ptrIA;
    SPtr<TAddrAddr> ptrAddr;
    
    firstIA();
    while ( ptrIA = this->getIA() ) {

	ptrIA->firstAddr();
	while( ptrAddr=ptrIA->getAddr()) {
	    //Removing outdated addresses
	    if(!ptrAddr->getValidTimeout()) {
		ptrIA->delAddr(ptrAddr->get());
		Log(Info) << "Expired address " << ptrAddr->get()->getPlain()
			  << " from IA " << ptrIA->getIAID()
			  << " has been removed from addrDB." << LogEnd;
	    }
	}

	if ( ((ptrIA->getState() == STATE_CONFIGURED) || (ptrIA->getState() == STATE_CONFIRMME))
		 && !ptrIA->countAddr()) {
	    ptrIA->setState(STATE_NOTCONFIGURED);
	}
    }
}

void TClntAddrMgr::firstIA() {
    Client->firstIA();
}

SPtr<TAddrIA> TClntAddrMgr::getIA() {
    return Client->getIA();
}

bool TClntAddrMgr::delIA(long IAID) {
    return Client->delIA(IAID);
}

void TClntAddrMgr::addIA(SPtr<TAddrIA> ptr) {
    Client->addIA(ptr);
}

int TClntAddrMgr::countIA() {
    return Client->countIA();
}

TClntAddrMgr::~TClntAddrMgr() {
    Client.reset();
    Log(Debug) << "ClntAddrMgr cleanup." << LogEnd;
}

SPtr<TAddrIA> TClntAddrMgr::getIA(unsigned long IAID)
{
    SPtr<TAddrIA> ptrIA;
    Client->firstIA();
    while (ptrIA = Client->getIA() ) {
	if (ptrIA->getIAID() == IAID)
	    return ptrIA;
    }
    return SPtr<TAddrIA>();
}

/**
 * sets specified interface to CONFIRMME state
 * when network switch off signal received, the funtion will be invoked to
 * set valid IA to CONFIRMME state.
 *
 * @param changedLinks structure containing interface indexes to be confirmed
 */
void TClntAddrMgr::setIA2Confirm(volatile link_state_notify_t * changedLinks)
{
    SPtr<TAddrIA> ptrIA;
    this->firstIA();
    while(ptrIA = this->getIA()){

	bool found = false;
	int ifindex = ptrIA->getIfindex(); // interface index of this IA

	// is this index on the list of interfaces to be confirmed?
	for (int i=0; i < MAX_LINK_STATE_CHANGES_AT_ONCE; i++)
	    if (changedLinks->ifindex[i]==ifindex)
		found = true;

	if (!found)
	    continue;

	if( (ptrIA->getState() == STATE_CONFIGURED || ptrIA->getState() == STATE_INPROCESS) )
	{
	    ptrIA->setState(STATE_CONFIRMME);
	    Log(Notice) << "Network switch off event detected. do Confirmming." << LogEnd;
	}
    }

}

// pd functions

void TClntAddrMgr::firstPD() {
    Client->firstPD();
}

SPtr<TAddrIA> TClntAddrMgr::getPD() {
    return Client->getPD();
}

bool TClntAddrMgr::delPD(long IAID) {
    return Client->delPD(IAID);
}

void TClntAddrMgr::addPD(SPtr<TAddrIA> ptr) {
    Client->addPD(ptr);
}

int TClntAddrMgr::countPD() {
    return Client->countPD();
}

SPtr<TAddrIA> TClntAddrMgr::getPD(unsigned long IAID)
{
    SPtr<TAddrIA> ptrPD;
    this->Client->firstPD();
    while (ptrPD = this->Client->getPD() ) {
	if (ptrPD->getIAID() == IAID)
	    return ptrPD;
    }
    return SPtr<TAddrIA>(); // NULL
}

void TClntAddrMgr::firstTA()
{
    Client->firstTA();
}

SPtr<TAddrIA> TClntAddrMgr::getTA()
{
    return Client->getTA();
}

SPtr<TAddrIA> TClntAddrMgr::getTA(unsigned long iaid)
{
    SPtr<TAddrIA> ta;
    this->Client->firstTA();
    while (ta = this->Client->getTA() ) {
	if (ta->getIAID() == iaid)
	    return ta;
    }
    return SPtr<TAddrIA>(); // NULL
}

void TClntAddrMgr::addTA(SPtr<TAddrIA> ptr)
{
    Client->addTA(ptr);
}

bool TClntAddrMgr::delTA(unsigned long iaid)
{
    return Client->delTA(iaid);
}

int TClntAddrMgr::countTA()
{
    return Client->countTA();
}

void TClntAddrMgr::print(std::ostream &) {

}

/**
 * @brief Adds a prefix.
 *
 * @param srvDuid Server DUID
 * @param srvAddr Server address.
 * @param iface   interface index
 * @param IAID    IAID
 * @param T1      T1 timer
 * @param T2      T2 timer
 * @param prefix  prefix
 * @param pref    prefered lifetime
 * @param valid   valid lifetime
 * @param length  prefix length
 * @param quiet   quiet mode (true=be quiet)
 *
 * @return true if successful, false otherwise
 */
bool TClntAddrMgr::addPrefix(SPtr<TDUID> srvDuid , SPtr<TIPv6Addr> srvAddr,
			     const std::string& ifacename, int iface, unsigned long IAID, unsigned long T1, unsigned long T2,
			     SPtr<TIPv6Addr> prefix, unsigned long pref, unsigned long valid,
			     int length, bool quiet)
{
    if (!prefix) {
	Log(Error) << "Attempt to add null prefix failed." << LogEnd;
	return false;
    }

    // find this client
    SPtr <TAddrClient> ptrClient;
    this->firstClient();
    ptrClient = getClient();

    return TAddrMgr::addPrefix(ptrClient, srvDuid, srvAddr, ifacename,
                               iface, IAID, T1, T2, prefix,
			       pref, valid, length, quiet);
}

bool TClntAddrMgr::updatePrefix(SPtr<TDUID> srvDuid , SPtr<TIPv6Addr> srvAddr,
                                const std::string& ifname,
				int iface, unsigned long IAID, unsigned long T1, unsigned long T2,
				SPtr<TIPv6Addr> prefix, unsigned long pref, unsigned long valid,
				int length, bool quiet)
{
    if (!prefix) {
	Log(Error) << "Attempt to update null prefix failed." << LogEnd;
	return false;
    }

    // find this client
    SPtr <TAddrClient> ptrClient;
    this->firstClient();
    ptrClient = getClient();

    return TAddrMgr::updatePrefix(ptrClient, srvDuid, srvAddr, iface, IAID, T1, T2, prefix,
				  pref, valid, length, quiet);
}

SPtr<TIPv6Addr> TClntAddrMgr::getPreferredAddr() {
    SPtr<TAddrIA> ia;
    SPtr<TAddrAddr> addr;

    firstIA();
    while ( ia = getIA() ) {
	if (ia->getTentative() != ADDRSTATUS_NO)
	    continue;

	ia->firstAddr();
	while (addr = ia->getAddr()) {
	    if (addr->getTentative() == ADDRSTATUS_NO)
		return addr->get(); // return the first address from first non-tentative
	    //if (is_addr_tentative(NULL, ia->getIface(), addr->get()->getPlain()) == LOWLEVEL_TENTATIVE_NO)
	}
    }

    return SPtr<TIPv6Addr>(); // NULL
}