File: ClntMsgRelease.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 (184 lines) | stat: -rw-r--r-- 5,000 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
/*
 * 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
 *
 */

#include "ClntMsgRelease.h"
#include "DHCPConst.h"
#include "SmartPtr.h"
#include "Container.h"
#include "AddrIA.h"
#include "OptDUID.h"
#include "ClntCfgMgr.h"
#include "ClntOptIA_NA.h"
#include "ClntOptTA.h"
#include "ClntOptIA_PD.h"
#include <cmath>
#include "Logger.h"
#include "ClntAddrMgr.h"
#include "AddrMgr.h"
#include "AddrIA.h"
#include "AddrAddr.h"

using namespace std;

/**
 * create RELEASE message
 *
 * @param iface
 * @param addr
 * @param iaLst - IA_NA list, which are served by on server on one link
 * @param ta    - IA_TA to be released
 * @param pdLst - IA_PD list to be released
 */
TClntMsgRelease::TClntMsgRelease(int iface, SPtr<TIPv6Addr> addr,
                                 List(TAddrIA) iaLst,
                                 SPtr<TAddrIA> ta,
                                 List(TAddrIA) pdLst)
  :TClntMsg(iface, addr, RELEASE_MSG)
{
    SPtr<TDUID> srvDUID;

    IRT=REL_TIMEOUT;
    MRT=0;
    MRC=REL_MAX_RC;
    MRD=0;
    RT=0;

    // obtain IA, TA or PD, so server DUID can be obtained
    SPtr<TAddrIA> x;
    if (iaLst.count()) {
        iaLst.first();
        x = iaLst.get();
    }
    if (!x)
        x = ta;
    if (!x) {
        pdLst.first();
        x = pdLst.get();
    }
    if (!x) {
        Log(Error) << "Unable to send RELEASE. No IA, TA or PD provided." << LogEnd;
        this->IsDone = true;
        return;
    }
    if (!x->getDUID()) {
        Log(Error) << "Unable to send RELEASE. Unable to find DUID. " << LogEnd;
        this->IsDone = true;
        return;
    }
    srvDUID = x->getDUID();

    Options.push_back(new TOptDUID(OPTION_SERVERID, srvDUID,this));
    Options.push_back(new TOptDUID(OPTION_CLIENTID, ClntCfgMgr().getDUID(),this));

    // --- RELEASE IA ---
    iaLst.first();
    while(x=iaLst.get()) {
        Options.push_back(new TClntOptIA_NA(x,this));
        SPtr<TAddrAddr> ptrAddr;
        SPtr<TClntIfaceIface> ptrIface;
        ptrIface = (Ptr*)ClntIfaceMgr().getIfaceByID(x->getIfindex());
        if (!ptrIface) {
            Log(Warning) << "Unable to find interface with ifindex "
                         << x->getIfindex() << " while creating RELEASE." << LogEnd;
            continue;
        }
        x->firstAddr();
        while (ptrAddr = x->getAddr()) {
            ptrIface->delAddr(ptrAddr->get(), ptrAddr->getPrefix());
        }

        // --- DNS Update ---
        SPtr<TIPv6Addr> dns = x->getFQDNDnsServer();
        if (dns) {
            string fqdn = ptrIface->getFQDN();
            ClntIfaceMgr().fqdnDel(ptrIface, x, fqdn);
        }
        // --- DNS Update ---
    }

    // --- RELEASE TA ---
    if (ta) {
        Options.push_back(new TClntOptTA(ta, this));
        SPtr<TClntIfaceIface> ptrIface;
        ptrIface = (Ptr*)ClntIfaceMgr().getIfaceByID(ta->getIfindex());
        if (ptrIface) {
            SPtr<TAddrAddr> addr;
            ta->firstAddr();
            while (addr = ta->getAddr()) {
                ptrIface->delAddr(addr->get(), addr->getPrefix());
            }
        } else {
            Log(Warning) << "Unable to find interface with ifindex "
                         << ta->getIfindex() << " while creating RELEASE." << LogEnd;
        }
    }

    // --- RELEASE PD ---

    SPtr<TAddrIA> pd;

    pdLst.first();
    while(pd=pdLst.get()) {
        SPtr<TClntOptIA_PD> pdOpt = new TClntOptIA_PD(pd,this);
        Options.push_back( (Ptr*)pdOpt );
        pdOpt->setContext(srvDUID, addr, this);
        pdOpt->delPrefixes();

        ClntAddrMgr().delPD(pd->getIAID() );
    }

    appendElapsedOption();
    appendAuthenticationOption();

    IsDone = false;
    send();
}

void TClntMsgRelease::answer(SPtr<TClntMsg> rep)
{
    SPtr<TOptDUID> rspSrvID = (Ptr*) rep->getOption(OPTION_SERVERID);
    SPtr<TOptDUID> msgSrvID = (Ptr*) getOption(OPTION_SERVERID);
    if (!rspSrvID) {
        Log(Warning) << "Received reply to RELEASE without SERVER-ID option." << LogEnd;
    }

    if (!msgSrvID) {
        Log(Error) << "Sent RELEASE did not contain SERVER-ID. That seems to be my fault. Sorry." << LogEnd;
    }

    if (rspSrvID && msgSrvID &&
        (!(*msgSrvID->getDUID()==*rspSrvID->getDUID()))) {
        Log(Error) << "Internal error. RELEASE sent to server with DUID=" << msgSrvID->getPlain()
                   << ", but response returned with " << rspSrvID->getPlain() << LogEnd;
    }

    IsDone = true; // we don't care. We sent release, we are shutting down anyway.
}

void TClntMsgRelease::doDuties() {
    if (RC!=MRC)
        send();
    else
       IsDone=true;
    return;
}

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

std::string TClntMsgRelease::getName() const {
    return "RELEASE";
}

TClntMsgRelease::~TClntMsgRelease() {
}