File: ClntOptTA.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 (277 lines) | stat: -rw-r--r-- 7,780 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
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 *
 * released under GNU GPL v2 only licence
 *
 */

#include "AddrIA.h"
#include "ClntOptTA.h"
#include "ClntOptIAAddress.h"
#include "ClntOptStatusCode.h"
#include "ClntOptIAAddress.h"
#include "OptDUID.h"
#include "ClntAddrMgr.h"
#include "ClntIfaceMgr.h"
#include "Logger.h"

/**
 * Constructor used during
 *
 * @param iaid
 * @param parent
 */
TClntOptTA::TClntOptTA(unsigned int iaid, TMsg* parent)
    :TOptTA(iaid, parent), Iface(-1)
{
    // don't put any suboptions
}

/**
 * Constructor used during option reception
 *
 * @param buf
 * @param bufsize
 * @param parent
 */
TClntOptTA::TClntOptTA(char * buf,int bufsize, TMsg* parent)
    :TOptTA(buf,bufsize, parent), Iface(-1)
{
    int pos=0, length=0;
    while(pos<bufsize)
    {
        pos+=length;
        int code= buf[pos]*256+buf[pos+1];
        pos+=2;
        int length=buf[pos]*256+buf[pos+1];
        pos+=2;
        SPtr<TOpt> opt;

        if(!allowOptInOpt(parent->getType(),OPTION_IA_TA,code)) {
            Log(Warning) << "Option " << code << " is not allowed as suboption of "
                         << OPTION_IA_TA << LogEnd;
            continue;
        }

        switch (code) {
        case OPTION_IAADDR:
            opt = new TClntOptIAAddress(buf+pos,length,this->Parent);
            break;
        case OPTION_STATUS_CODE:
            opt = new TClntOptStatusCode(buf+pos,length,this->Parent);
            break;
        default:
            Log(Warning) <<"Option " << code<< " in message (type=" << parent->getType()
                         <<") is currently not supported."<<LogEnd;
            break;
        }
        if( (opt) && (opt->isValid()) )
            SubOptions.append(opt);
        pos+=length;
    }
}

TClntOptTA::TClntOptTA(SPtr<TAddrIA> ta, TMsg* parent)
    :TOptTA(ta->getIAID(), parent), Iface(-1)
{
    ta->firstAddr();
    SPtr<TAddrAddr> addr;
    while (addr=ta->getAddr()) {
        SubOptions.append(new TClntOptIAAddress(addr->get(), 0, 0, parent));
    }
}

void TClntOptTA::firstAddr()
{
    SubOptions.first();
}

SPtr<TClntOptIAAddress> TClntOptTA::getAddr()
{
    SPtr<TClntOptIAAddress> ptr;
    do {
        ptr = (Ptr*) SubOptions.get();
        if (ptr)
            if (ptr->getOptType()==OPTION_IAADDR)
                return ptr;
    } while (ptr);

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

int TClntOptTA::countAddr()
{
    SPtr<TOpt> ptr;
    SubOptions.first();
    int count = 0;
    while ( ptr = SubOptions.get() ) {
        if (ptr->getOptType() == OPTION_IAADDR)
            count++;
    }
    return count;
}

int TClntOptTA::getStatusCode()
{
    SPtr<TOpt> option;
    if (option=getOption(OPTION_STATUS_CODE))
    {
        SPtr<TClntOptStatusCode> statOpt=(Ptr*) option;
        return statOpt->getCode();
    }
    return STATUSCODE_SUCCESS;
}

TClntOptTA::~TClntOptTA()
{

}

/**
 * This function sets everything in motion, i.e. sets up temporary addresses
 *
 * @return
 */bool TClntOptTA::doDuties()
{
    // find this TA in addrMgr...
    SPtr<TAddrIA> ta = ClntAddrMgr().getTA(this->getIAID());

    SPtr<TClntCfgIface> cfgIface;
    if (! (cfgIface = ClntCfgMgr().getIface(this->Iface)) ) {
        Log(Error) << "Unable to find TA class in the CfgMgr, on the "
                   << this->Iface << " interface." << LogEnd;
        return true;
    }

    SPtr<TOptDUID> duid = (Ptr*)Parent->getOption(OPTION_SERVERID);
    if (!duid) {
        Log(Error) << "Unable to find proper DUID while setting TA." << LogEnd;
        return false;
    }

    if (!ta) {
        Log(Debug) << "Creating TA (iaid=" << this->getIAID() << ") in the addrDB."
                   << LogEnd;
        ta = new TAddrIA(cfgIface->getName(), this->Iface, IATYPE_TA,
                         SPtr<TIPv6Addr>() /*if unicast, then this->Addr*/,
                         duid->getDUID(), DHCPV6_INFINITY,
                         DHCPV6_INFINITY, this->getIAID());
        ClntAddrMgr().addTA(ta);
    }

    // IAID found, set up new received options.
    SPtr<TAddrAddr> addr;
    SPtr<TClntOptIAAddress> optAddr;

    SPtr<TIfaceIface> ptrIface;
    ptrIface = ClntIfaceMgr().getIfaceByID(this->Iface);
    if (!ptrIface)
    {
        Log(Error) << "Interface " << this->Iface << " not found." << LogEnd;
        return true;
    }

    // for each address in IA option...
    this->firstAddr();
    while (optAddr = this->getAddr() ) {
        addr = ta->getAddr( optAddr->getAddr() );

        if (!addr) { // - no such address in DB -
            if (!optAddr->getValid()) {
                Log(Warning) << "Server send new addr with valid=0." << LogEnd;
                continue;
            }
            // add this address in addrDB...
            ta->addAddr(optAddr->getAddr(), optAddr->getPref(), optAddr->getValid());
            ta->setDUID(duid->getDUID()
);
            // ... and in IfaceMgr -
            ptrIface->addAddr(optAddr->getAddr(), optAddr->getPref(), optAddr->getValid(), ptrIface->getPrefixLength());
            Log(Notice) << "Temp. address " << *optAddr->getAddr() << " has been added to "
                        << ptrIface->getName() << "/" << ptrIface->getID()
                        << " interface." << LogEnd;

        } else { // - we have this addr in DB -

            if ( optAddr->getValid() == 0 ) {
                // valid=0, release this address and delete address from addrDB
                ta->delAddr(optAddr->getAddr());
                // delete address from IfaceMgr
                ptrIface->delAddr(optAddr->getAddr(), ptrIface->getPrefixLength());
                continue; // analyze next option OPTION_IA
            }

            // set up new options in IfaceMgr
            ptrIface->updateAddr(optAddr->getAddr(), optAddr->getPref(), optAddr->getValid());
            // set up new options in addrDB
            addr->setPref(optAddr->getPref());
            addr->setValid(optAddr->getValid());
            addr->setTimestamp();
        }
    }

    // mark this TA as configured
    SPtr<TClntCfgTA> cfgTA;
    cfgIface->firstTA();
    cfgTA = cfgIface->getTA();
    cfgTA->setState(STATE_CONFIGURED);

    ta->setState(STATE_CONFIGURED);
    return true;
}

SPtr<TClntOptIAAddress> TClntOptTA::getAddr(SPtr<TIPv6Addr> addr)
{
    SPtr<TClntOptIAAddress> optAddr;
    this->firstAddr();
    while(optAddr=this->getAddr())
    {
        //!memcmp(optAddr->getAddr(),addr,16)
        if ((*addr)==(*optAddr->getAddr()))
            return optAddr;
    };
    return SPtr<TClntOptIAAddress>();
}

void TClntOptTA::releaseAddr(long IAID, SPtr<TIPv6Addr> addr )
{
    SPtr<TAddrIA> ptrIA = ClntAddrMgr().getIA(IAID);
    if (ptrIA)
        ptrIA->delAddr(addr);
    else
        Log(Warning) << "Unable to release addr: IA ("
                     << IAID << ") not present in addrDB." << LogEnd;
}

bool TClntOptTA::isValid() const
{

    const TOptList& opts = SubOptions.getSTL();

    for (TOptList::const_iterator it = opts.begin(); it != opts.end(); ++it) {
        if ((*it)->getOptType() != OPTION_IAADDR)
            continue;
        const TOptIAAddress* addr = (const TOptIAAddress*)it->get();

        if (addr->getAddr()->linkLocal()) {
            Log(Warning) << "Address " << addr->getAddr()->getPlain() << " used in IA_NA (IAID="
                         << IAID_ << ") is link local. The whole IA option is considered invalid."
                         << LogEnd;
            return false;
        }
    }

    return true;
}

void TClntOptTA::setContext(int iface, SPtr<TIPv6Addr> clntAddr) {
    this->Iface    = iface;
    this->Addr     = clntAddr;
}

void TClntOptTA::setIface(int iface) {
    this->Iface    = iface;
}