File: ClntMsgRequest.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 (360 lines) | stat: -rw-r--r-- 9,274 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
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 * changes: Krzysztof Wnuk <keczi@poczta.onet.pl>
 *          Michal Kowalczuk <michal@kowalczuk.eu>
 *
 * released under GNU GPL v2 only licence
 *
 * $Id: ClntMsgRequest.cpp,v 1.29 2009-03-24 23:17:17 thomson Exp $
 *
 */

#include "ClntMsgRequest.h"
#include "SmartPtr.h"
#include "DHCPConst.h"
#include "OptDUID.h"
#include "OptAddr.h"
#include "ClntIfaceMgr.h"
#include "ClntMsgAdvertise.h"
#include "ClntOptIA_NA.h"
#include "ClntOptTA.h"
#include "ClntOptIA_PD.h"
#include "ClntOptElapsed.h"
#include "ClntOptOptionRequest.h"
#include "ClntOptStatusCode.h"
#include "ClntTransMgr.h"
#include <cmath>
#include "Logger.h"

/*
 * opts - options list WITHOUT serverDUID
 */
TClntMsgRequest::TClntMsgRequest(TOptList opts, int iface)
    :TClntMsg(iface, SPtr<TIPv6Addr>(), REQUEST_MSG) {
    IRT = REQ_TIMEOUT;
    MRT = REQ_MAX_RT;
    MRC = REQ_MAX_RC;
    MRD = 0;   
    RT=0;

    Iface=iface;
    IsDone=false;

    int backupCount = ClntTransMgr().getAdvertiseLstCount();
    if (!backupCount) 
    {
	Log(Error) << "Unable to send REQUEST. There are no backup servers left." << LogEnd;
	setState( opts, STATE_NOTCONFIGURED);

        this->IsDone = true;
        return;
    }
    Log(Info) << "Creating REQUEST. Backup server list contains " 
	      << backupCount << " server(s)." << LogEnd;
    ClntTransMgr().printAdvertiseLst();

    // get server DUID from the first advertise
    SPtr<TOpt> srvDUID = ClntTransMgr().getAdvertiseDUID();

    ClntTransMgr().firstAdvertise();
    SPtr<TClntMsgAdvertise> advertise = (Ptr*) ClntTransMgr().getAdvertise();
    this->copyAAASPI((SPtr<TClntMsg>)advertise);

    // remove just used server
    ClntTransMgr().delFirstAdvertise();

    // copy whole list from SOLICIT ...
    Options = opts;

    // set proper Parent in copied options
    TOptList::iterator opt=Options.begin();
    while (opt!=Options.end())
    {
        if ( (*opt)->getOptType() == OPTION_ELAPSED_TIME)
	          opt = Options.erase(opt);
        else {
	          (*opt)->setParent(this);
            ++opt;
        }
    }

    // copy addresses offered in ADVERTISE
    copyAddrsFromAdvertise((Ptr*) advertise);

    copyPrefixesFromAdvertise((Ptr*) advertise);

    // does this server support unicast?
    SPtr<TClntCfgIface> cfgIface = ClntCfgMgr().getIface(iface);
    if (!cfgIface) {
	Log(Error) << "Unable to find interface with ifindex " << iface << "." << LogEnd;    
	IsDone = true;
	return;
    }
    SPtr<TOptAddr> unicast = (Ptr*) advertise->getOption(OPTION_UNICAST);
    if (unicast && !cfgIface->getUnicast()) {
	Log(Info) << "Server offers unicast (" << *unicast->getAddr() 
		  << ") communication, but this client is not configured to so." << LogEnd;
    }
    if (unicast && cfgIface->getUnicast()) {
	Log(Debug) << "Server supports unicast on address " << *unicast->getAddr() << "." << LogEnd;

	//this->PeerAddr = unicast->getAddr();
	firstOption();
	SPtr<TOpt> opt;
	// set this unicast address in each IA in AddrMgr
	while ( opt = getOption() ) {
	  /// @todo: add support for unicast in IA_TA and IA_PD
	    if (opt->getOptType()!=OPTION_IA_NA)
		continue;
	    SPtr<TClntOptIA_NA> ptrOptIA = (Ptr*) opt;
	    SPtr<TAddrIA> ptrAddrIA = ClntAddrMgr().getIA(ptrOptIA->getIAID());
	  
	    if (!ptrAddrIA) {
		Log(Crit) << "IA with IAID=" << ptrOptIA->getIAID() << " not found." << LogEnd;
		continue;
	    }
	    ptrAddrIA->setUnicast(unicast->getAddr());
	}
	
    }
    
    // ... and append server's DUID from ADVERTISE
    Options.push_back( srvDUID );
    
    appendElapsedOption();
    appendAuthenticationOption();
    IsDone = false;
    send();
}

TClntMsgRequest::TClntMsgRequest(List(TAddrIA) IAs,
				 SPtr<TDUID> srvDUID, int iface)
    :TClntMsg(iface, SPtr<TIPv6Addr>(), REQUEST_MSG) {
    IRT = REQ_TIMEOUT;
    MRT = REQ_MAX_RT;
    MRC = REQ_MAX_RC;
    MRD = 0;   
    RT=0;

    Iface=iface;
    IsDone=false;
    SPtr<TOpt> ptr;
    ptr = new TOptDUID(OPTION_CLIENTID, ClntCfgMgr().getDUID(), this );
    Options.push_back( ptr );

    if (!srvDUID) {
	Log(Error) << "Unable to send REQUEST: ServerId not specified.\n" << LogEnd;
	IsDone = true;
	return;
    }
    
    ptr = (Ptr*) new TOptDUID(OPTION_SERVERID, srvDUID,this);
    // all IAs provided by checkSolicit
    SPtr<TAddrIA> ClntAddrIA;
    Options.push_back( ptr );
	
    IAs.first();
    while (ClntAddrIA = IAs.get()) 
    {
        SPtr<TClntCfgIA> ClntCfgIA = ClntCfgMgr().getIA(ClntAddrIA->getIAID());
        SPtr<TClntOptIA_NA> IA_NA = new TClntOptIA_NA(ClntCfgIA, ClntAddrIA, this);
        Options.push_back((Ptr*)IA_NA);
    }

    appendElapsedOption();
    appendAuthenticationOption();

    this->IsDone = false;
    this->send();
}

/*
 * analyse REPLY received for this REQUEST
 */
void TClntMsgRequest::answer(SPtr<TClntMsg> msg)
{
    this->copyAAASPI(msg);

#ifdef MOD_CLNT_CONFIRM    
    /* CHANGED here: When the client receives a NotOnLink status from the server in
     * response to a Request, the client can either re-issue the Request
     * without specifying any addresses or restart the DHCP server discovery
     * process.
     */  
    SPtr<TOptStatusCode> optStateCode = (Ptr*)msg->getOption(OPTION_STATUS_CODE);
    if( optStateCode && STATUSCODE_NOTONLINK == optStateCode->getCode()){
	SPtr<TOpt> opt;
	firstOption();
	while(opt = getOption()){
	    if(opt->getOptType() != OPTION_IA_NA){
		continue;
	    }
	    SPtr<TClntOptIA_NA> IA_NA = (Ptr*)opt;
            IA_NA->delOption(OPTION_IAADDR);
	} 
	return;   
    }
#endif
     
    TClntMsg::answer(msg);
}

void TClntMsgRequest::doDuties()
{
    // timeout is reached and we still don't have answer, retransmit
    if (RC>MRC) 
    {
        ClntTransMgr().sendRequest(Options, Iface);

        IsDone = true;
        return;
    }
    send();
    return;
}

bool TClntMsgRequest::check()
{
    return false;
}

std::string TClntMsgRequest::getName() const {
    return "REQUEST";
}

/** 
 * method is used when REQUEST transmission was attempted, but there are no more servers
 * on the backup list. When this method is called, it is sure that some IAs, TA or PDs will
 * remain not configured (as there are no servers available, which could configure it)
 * 
 * @param options list of still unconfigured options 
 * @param state state to be set in CfgMgr
 */
void TClntMsgRequest::setState(TOptList options, EState state)
{
    SPtr<TOpt>          opt;
    SPtr<TClntOptIA_NA> ia;
    SPtr<TClntOptTA>    ta;
    SPtr<TClntOptIA_PD> pd;

    SPtr<TClntCfgIA> cfgIa;
    SPtr<TClntCfgTA> cfgTa;
    SPtr<TClntCfgPD> cfgPd;

    SPtr<TClntCfgIface> iface = ClntCfgMgr().getIface(Iface);
    if (!iface) {
	      Log(Error) << "Unable to find interface with ifindex=" << Iface << LogEnd;
	      return;
    }

    for (TOptList::iterator opt = options.begin(); opt!=options.end(); ++opt)
    {
	switch ( (*opt)->getOptType()) {
	case OPTION_IA_NA:
	{
	    ia = (Ptr*) (*opt);
	    cfgIa = iface->getIA(ia->getIAID());
	    if (cfgIa)
		cfgIa->setState(state);
	    break;
	}
	case OPTION_IA_TA:
	{
	    ia = (Ptr*) (*opt);
	    iface->firstTA();
	    cfgTa = iface->getTA();
	    if (cfgTa)
		cfgTa->setState(state);
	    break;
	}
	case OPTION_IA_PD:
	{
	    pd = (Ptr*) (*opt);
	    cfgPd = iface->getPD(pd->getIAID());
	    if (cfgPd)
		cfgPd->setState(state);
	    break;
	}
      	default:
	    continue;
	}
    }
}

void TClntMsgRequest::copyAddrsFromAdvertise(SPtr<TClntMsg> adv)
{
    if (ClntCfgMgr().insistMode())
        return; // insist-mode on, so ask for the address user specified again

    SPtr<TOpt> opt1, opt2, opt3;
    SPtr<TClntOptIA_NA> ia1, ia2;

    this->copyAAASPI(adv);

    firstOption();
    while (opt1 = getOption()) {
	if (opt1->getOptType()!=OPTION_IA_NA)
	    continue; // ignore all options except IA_NA
	adv->firstOption();
	while (opt2 = adv->getOption()) {
	    if (opt2->getOptType() != OPTION_IA_NA)
		continue;
	    ia1 = (Ptr*) opt1;
	    ia2 = (Ptr*) opt2;
	    if (ia1->getIAID() != ia2->getIAID())
		continue;

	    // found IA in ADVERTISE, now copy all addrs

	    ia1->delAllOptions();
	    ia2->firstOption();
	    while (opt3 = ia2->getOption()) {
		if (opt3->getOptType() == OPTION_IAADDR)
		    ia1->addOption(opt3);
	    }
	}
    }
}

void TClntMsgRequest::copyPrefixesFromAdvertise(SPtr<TClntMsg> adv)
{
    if (ClntCfgMgr().insistMode())
        return; // insist-mode on, so ask for the address user specified again

    SPtr<TOpt> opt1, opt2, opt3;
    SPtr<TClntOptIA_PD> ia1, ia2;

    this->copyAAASPI(adv);

    firstOption();
    while (opt1 = getOption()) {
	if (opt1->getOptType()!=OPTION_IA_PD)
	    continue; // ignore all options except IA_NA
	adv->firstOption();
	while (opt2 = adv->getOption()) {
	    if (opt2->getOptType() != OPTION_IA_PD)
		continue;
	    ia1 = (Ptr*) opt1;
	    ia2 = (Ptr*) opt2;
	    if (ia1->getIAID() != ia2->getIAID())
		continue;

	    // found IA_PD in ADVERTISE, now copy all addrs
	    ia1->delAllOptions();
	    ia2->firstOption();
	    while (opt3 = ia2->getOption()) {
		if (opt3->getOptType() == OPTION_IAPREFIX)
		    ia1->addOption(opt3);
	    }

	}
    }
    

}

TClntMsgRequest::~TClntMsgRequest() {
}