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
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* Marek Senderski <msend@o2.pl>
*
* released under GNU GPL v2 only licence
*
* $Id: DHCPClient.cpp,v 1.33 2008-11-13 22:18:17 thomson Exp $
*
*/
#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"
volatile int serviceShutdown;
volatile int linkstateChange;
TDHCPClient::TDHCPClient(string config)
{
serviceShutdown = 0;
linkstateChange = 0;
srand(now());
this->IsDone = false;
IfaceMgr = new TClntIfaceMgr(CLNTIFACEMGR_FILE);
if ( this->IfaceMgr->isDone() ) {
Log(Crit) << "Fatal error during IfaceMgr initialization." << LogEnd;
this->IsDone = true;
return;
}
this->CfgMgr = new TClntCfgMgr(IfaceMgr, config);
if ( this->CfgMgr->isDone() ) {
Log(Crit) << "Fatal error during CfgMgr initialization." << LogEnd;
this->IsDone = true;
return;
}
this->AddrMgr = new TClntAddrMgr(CfgMgr, CLNTADDRMGR_FILE, false);
if ( this->AddrMgr->isDone() ) {
Log(Crit) << "Fatal error during AddrMgr initialization." << LogEnd;
this->IsDone = true;
return;
}
this->TransMgr = new TClntTransMgr(IfaceMgr, AddrMgr, CfgMgr, CLNTTRANSMGR_FILE);
if ( this->TransMgr->isDone() ) {
Log(Crit) << "Fatal error during TransMgr initialization." << LogEnd;
this->IsDone = true;
return;
}
if (CfgMgr->useConfirm())
initLinkStateChange();
TransMgr->setContext(TransMgr);
}
/**
* 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));
CfgMgr->firstIface();
SPtr<TClntCfgIface> iface;
Log(Debug) << "Initialising link-state detection for interfaces: ";
while (iface = CfgMgr->getIface())
{
linkstates.ifindex[linkstates.cnt++] = iface->getID();
Log(Cont) << iface->getID() << " ";
}
Log(Cont) << LogEnd;
link_state_change_init(&linkstates, &linkstateChange);
}
void TDHCPClient::stop() {
serviceShutdown = 1;
if (CfgMgr->useConfirm())
link_state_change_cleanup();
#ifdef WIN32
// just to break select() in WIN32 systems
SmartPtr<TIfaceIface> iface = IfaceMgr->getIfaceByID(TransMgr->getCtrlIface());
Log(Warning) << "Sending SHUTDOWN packet on the " << iface->getName()
<< "/" << iface->getID() << " (addr=" << TransMgr->getCtrlAddr() << ")." << LogEnd;
int fd = sock_add("", TransMgr->getCtrlIface(),"::",0,true, false);
char buf = CONTROL_MSG;
int cnt=sock_send(fd,TransMgr->getCtrlAddr(),&buf,1,DHCPCLIENT_PORT,TransMgr->getCtrlIface());
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;
}
char* TDHCPClient::getCtrlIface(){
SmartPtr<TIfaceIface> iface = IfaceMgr->getIfaceByID(TransMgr->getCtrlIface());
return iface->getName();
;
}
void TDHCPClient::run()
{
SmartPtr<TMsg> msg;
while ( (!this->isDone()) && !TransMgr->isDone() )
{
if (serviceShutdown)
TransMgr->shutdown();
if (linkstateChange) {
AddrMgr->setIA2Confirm(&linkstates);
this->resetLinkstate();
}
TransMgr->doDuties();
unsigned int timeout = TransMgr->getTimeout();
if (timeout == 0)
timeout = 1;
Log(Debug) << "Sleeping for " << timeout << " second(s)." << LogEnd;
SmartPtr<TClntMsg> msg=IfaceMgr->select(timeout);
if (msg) {
int iface = msg->getIface();
SmartPtr<TIfaceIface> ptrIface;
ptrIface = IfaceMgr->getIfaceByID(iface);
Log(Info) << "Received " << msg->getName() << " on " << ptrIface->getName()
<< "/" << iface << hex << ",TransID=0x" << msg->getTransID()
<< dec << ", " << msg->countOption() << " opts:";
SmartPtr<TOpt> ptrOpt;
msg->firstOption();
while (ptrOpt = msg->getOption() )
Log(Cont) << " " << ptrOpt->getOptType();
Log(Cont) << LogEnd;
TransMgr->relayMsg(msg);
}
}
Log(Notice) << "Bye bye." << LogEnd;
}
bool TDHCPClient::isDone() {
return IsDone;
}
bool TDHCPClient::checkPrivileges() {
// FIXME: check privileges
return true;
}
void TDHCPClient::setWorkdir(std::string workdir) {
if (this->CfgMgr) {
this->CfgMgr->setWorkdir(workdir);
this->CfgMgr->dump();
}
}
SmartPtr<TClntAddrMgr> TDHCPClient::getAddrMgr() {
return this->AddrMgr;
}
SmartPtr<TClntCfgMgr> TDHCPClient::getCfgMgr() {
return this->CfgMgr;
}
TDHCPClient::~TDHCPClient()
{
if (TransMgr)
TransMgr->setContext(0);
this->TransMgr = 0;
this->AddrMgr = 0;
this->CfgMgr = 0;
this->IfaceMgr = 0;
}
|