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

#include <iostream>
#include <stdlib.h>
#include "SmartPtr.h"
#include "DHCPClient.h"
#include "ClntTransMgr.h"

#include "IfaceMgr.h"
#include "ClntIfaceMgr.h"
#include "Logger.h"
#include "Portable.h"

using namespace std;

volatile int serviceShutdown;
volatile int linkstateChange;

TDHCPClient::TDHCPClient(const std::string& config)
  :IsDone_(false)
{
    serviceShutdown = 0;
    linkstateChange = 0;
    srand((uint32_t)time(NULL));

    TClntIfaceMgr::instanceCreate(CLNTIFACEMGR_FILE);
    if ( ClntIfaceMgr().isDone() ) {
        Log(Crit) << "Fatal error during IfaceMgr initialization." << LogEnd;
        IsDone_ = true;
        return;
    }

    TClntCfgMgr::instanceCreate(config);
    if ( ClntCfgMgr().isDone() ) {
        Log(Crit) << "Fatal error during CfgMgr initialization." << LogEnd;
        IsDone_ = true;
        return;
    }

        TClntAddrMgr::instanceCreate(ClntCfgMgr().getDUID(), ClntCfgMgr().useConfirm(),
                                     CLNTADDRMGR_FILE, false);
    if ( ClntAddrMgr().isDone() ) {
        Log(Crit) << "Fatal error during AddrMgr initialization." << LogEnd;
            IsDone_ = true;
            return;
    }

    TClntTransMgr::instanceCreate(CLNTTRANSMGR_FILE);
    if ( TClntTransMgr::instance().isDone() ) {
        Log(Crit) << "Fatal error during TransMgr initialization." << LogEnd;
        IsDone_ = true;
        return;
    }

    if ( !ClntTransMgr().sanitizeAddrDB() ) {
        Log(Crit) << "Loaded address database failed sanitization checks."
                  << LogEnd;
        IsDone_ = true;
        return;
    }


    if (ClntCfgMgr().useConfirm())
        initLinkStateChange();
    else
        Log(Debug) << "Confirm disabled, skipping link change detection." << LogEnd;
}

/**
 * initializes low-level link state change detection mechanism
 *
 */
void TDHCPClient::initLinkStateChange()
{
    // Log(Debug) << "LinkState change detection not fully supported (disabled for now)." << LogEnd;
    // return; // disable this for now

    memset((void*)&this->linkstates, 0, sizeof(linkstates));

    ClntCfgMgr().firstIface();
    SPtr<TClntCfgIface> iface;
    Log(Debug) << "Initialising link-state detection for interfaces: ";
    while (iface = ClntCfgMgr().getIface())
    {
        linkstates.ifindex[linkstates.cnt++] = iface->getID();
        Log(Cont) << iface->getFullName() << " ";
    }
    Log(Cont) << LogEnd;

    link_state_change_init(&linkstates, &linkstateChange);
}

void TDHCPClient::stop() {
    serviceShutdown = 1;

#ifdef MOD_CLNT_CONFIRM
    if (ClntCfgMgr().useConfirm())
        link_state_change_cleanup();
#endif

#ifdef WIN32
    // just to break select() in WIN32 systems

    if (ClntTransMgr().getCtrlIface() < 0) {
        return; // no interfaces configured yet
    }

    SPtr<TIfaceIface> iface = ClntIfaceMgr().getIfaceByID(ClntTransMgr().getCtrlIface());
    Log(Warning) << "Sending SHUTDOWN packet on the " << iface->getName()
        << "/" << iface->getID() << " (addr=" << ClntTransMgr().getCtrlAddr() << ")." << LogEnd;
    int fd = sock_add("", ClntTransMgr().getCtrlIface(),"::",0,true, false);
    char buf = CONTROL_MSG;
    int cnt = sock_send(fd,ClntTransMgr().getCtrlAddr(),&buf,1,DHCPCLIENT_PORT,ClntTransMgr().getCtrlIface());
    if (cnt<0) {
        Log(Error) << "Failed to send shutdown command" << LogEnd;
        exit(EXIT_FAILURE);
    }
    sock_del(fd);
#endif
}

/* Function removed. Please use link_state_changed() instead */
/* void TDHCPClient::changeLinkstate(int iface_id) */

void TDHCPClient::resetLinkstate() {
    linkstates.cnt = 0;
    for (int i = 0; i<MAX_LINK_STATE_CHANGES_AT_ONCE; i++)
        linkstates.ifindex[i]=0; // ugly, but safe way of zeroing table
    linkstateChange = 0;
}


#ifdef MOD_CLNT_CONFIRM
void TDHCPClient::requestLinkstateChange(){
    linkstateChange = 1;
}
#endif

char* TDHCPClient::getCtrlIface(){
    SPtr<TIfaceIface> iface = ClntIfaceMgr().getIfaceByID(ClntTransMgr().getCtrlIface());
    return iface->getName();
;
}

void TDHCPClient::run()
{
    SPtr<TMsg> msg;
    while ( (!this->isDone()) && !ClntTransMgr().isDone() )
    {
        if (serviceShutdown)
            ClntTransMgr().shutdown();

#ifdef MOD_CLNT_CONFIRM
        if (linkstateChange) {
          ClntAddrMgr().setIA2Confirm(&linkstates);
          this->resetLinkstate();
        }
#endif

        ClntTransMgr().doDuties();

        unsigned int timeout = ClntTransMgr().getTimeout();

        if (timeout == 0)
            timeout = 1;

        Log(Debug) << "Sleeping for " << timeout << " second(s)." << LogEnd;
        SPtr<TClntMsg> msg=ClntIfaceMgr().select(timeout);

        if (msg) {
            int iface = msg->getIface();
            SPtr<TIfaceIface> ptrIface;
            ptrIface = ClntIfaceMgr().getIfaceByID(iface);
            Log(Info) << "Received " << msg->getName() << " on " << ptrIface->getName()
                      << "/" << iface	<< hex << ",trans-id=0x" << msg->getTransID()
                      << dec << ", " << msg->countOption() << " opts:";
            SPtr<TOpt> ptrOpt;
            msg->firstOption();
            while (ptrOpt = msg->getOption() )
                Log(Cont) << " " << ptrOpt->getOptType();
            Log(Cont) << LogEnd;

            ClntTransMgr().relayMsg(msg);
        }
    }
    Log(Notice) << "Bye bye." << LogEnd;
}

bool TDHCPClient::isDone() const {
    return IsDone_;
}

bool TDHCPClient::checkPrivileges() {
    /// @todo: check privileges
    return true;
}

void TDHCPClient::setWorkdir(const std::string& workdir) {
    ClntCfgMgr().setWorkdir(workdir);
    ClntCfgMgr().dump();
}

TDHCPClient::~TDHCPClient()
{
}