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
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
*
* released under GNU GPL v2 only licence
*
*/
#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include "IfaceMgr.h"
#include "RelIfaceMgr.h"
#include "RelCfgIface.h"
using namespace std;
#include "FlexLexer.h"
#include "RelParser.h"
#include "RelCfgMgr.h"
int TRelCfgMgr::NextRelayID = RELAY_MIN_IFINDEX;
TRelCfgMgr * TRelCfgMgr::Instance = 0;
TRelCfgMgr::TRelCfgMgr(const std::string& cfgFile, const std::string& xmlFile)
:TCfgMgr(), XmlFile(xmlFile), ClientLinkLayerAddress_(false)
{
// load config file
if (!this->parseConfigFile(cfgFile)) {
this->IsDone = true;
return;
}
this->dump();
this->IsDone = false;
}
bool TRelCfgMgr::parseConfigFile(const std::string& cfgFile) {
int result;
ifstream f;
// parse config file
f.open( cfgFile.c_str() );
if ( ! f.is_open() ) {
Log(Crit) << "Unable to open " << cfgFile << " file." << LogEnd;
this->IsDone = true;
return false;
} else {
Log(Notice) << "Parsing " << cfgFile << " config file..." << LogEnd;
}
yyFlexLexer lexer(&f,&clog);
RelParser parser(&lexer);
parser.CfgMgr = this; // just a workaround to access CfgMgr while still being in constructor
result = parser.yyparse();
Log(Debug) << "Parsing config done." << LogEnd;
f.close();
this->LogLevel = logger::getLogLevel();
this->LogName = logger::getLogName();
if (result) {
Log(Crit) << "Fatal error during config parsing." << LogEnd;
return false;
}
// setup global options
this->setupGlobalOpts(parser.ParserOptStack.getLast());
// analyse interfaces mentioned in config file
if (!this->matchParsedSystemInterfaces(&parser.RelCfgIfaceLst)) {
return false;
}
// check if config is sane
if(!this->validateConfig()) {
return false;
}
if (guessMode())
Log(Debug) << "Guess-mode enabled. RELAY-REPL from the server without interface-id option will be accepted." << LogEnd;
else
Log(Debug) << "Guess-mode disabled. RELAY-REPL from the server without interface-id option will be dropped." << LogEnd;
return true;
}
void TRelCfgMgr::dump() {
std::ofstream xmlDump;
xmlDump.open(this->XmlFile.c_str());
xmlDump << *this;
xmlDump.close();
}
bool TRelCfgMgr::setupGlobalOpts(SPtr<TRelParsGlobalOpt> opt) {
this->Workdir = opt->getWorkDir();
this->GuessMode = opt->getGuessMode();
this->InterfaceIDOrder = opt->getInterfaceIDOrder();
this->RemoteID = opt->getRemoteID();
this->Echo = opt->getEcho();
return true;
}
/*
* Now parsed information should be placed in config manager
* in accordance with information provided by interface manager
*/
bool TRelCfgMgr::matchParsedSystemInterfaces(List(TRelCfgIface) * lst) {
int cfgIfaceCnt;
cfgIfaceCnt = lst->count();
Log(Debug) << cfgIfaceCnt << " interface(s) specified in " << RELCONF_FILE << LogEnd;
SPtr<TRelCfgIface> cfgIface;
SPtr<TIfaceIface> ifaceIface;
lst->first();
while(cfgIface=lst->get()) {
// physical interface
if (cfgIface->getID()==-1) {
// ID==-1 means that user referenced to interface by name
ifaceIface = RelIfaceMgr().getIfaceByName(cfgIface->getName());
} else {
ifaceIface = RelIfaceMgr().getIfaceByID(cfgIface->getID());
}
if (!ifaceIface) {
Log(Crit) << "Interface " << cfgIface->getName() << "/" << cfgIface->getID()
<< " is not present in the system or does not support IPv6."
<< LogEnd;
return false;
}
if (!ifaceIface->countLLAddress()) {
Log(Crit) << "Interface " << ifaceIface->getName() << "/" << ifaceIface->getID()
<< " is down or doesn't have any link-layer address. " << LogEnd;
return false;
}
cfgIface->setName(ifaceIface->getName());
cfgIface->setID(ifaceIface->getID());
this->addIface(cfgIface);
Log(Info) << "Interface " << cfgIface->getName() << "/" << cfgIface->getID()
<< " configuration has been loaded." << LogEnd;
}
return true;
}
SPtr<TRelCfgIface> TRelCfgMgr::getIface() {
return this->IfaceLst.get();
}
void TRelCfgMgr::addIface(SPtr<TRelCfgIface> ptr) {
IfaceLst.append(ptr);
}
void TRelCfgMgr::firstIface() {
IfaceLst.first();
}
long TRelCfgMgr::countIface() {
return IfaceLst.count();
}
TRelCfgMgr::~TRelCfgMgr() {
Log(Debug) << "RelCfgMgr cleanup." << LogEnd;
}
bool TRelCfgMgr::isDone() {
return IsDone;
}
bool TRelCfgMgr::guessMode() {
return GuessMode;
}
bool TRelCfgMgr::validateConfig() {
SPtr<TRelCfgIface> ptrIface;
if (!this->countIface()) {
Log(Crit) << "Config problem: No interface defined." << LogEnd;
return false;
}
if ( this->countIface()<2 ) {
Log(Warning) << "There is only one interface defined. At least two is a sanite configuration." << LogEnd;
}
firstIface();
while(ptrIface=getIface()) {
if (!this->validateIface(ptrIface))
return false;
}
return true;
}
bool TRelCfgMgr::validateIface(SPtr<TRelCfgIface> ptrIface)
{
if ( (ptrIface->getInterfaceID() == (signed int)DHCPV6_INFINITY) &&
(ptrIface->getClientUnicast() || ptrIface->getClientMulticast()) ) {
Log(Crit) << "Client address defined without Interface-ID on the " << ptrIface->getFullName() << " interface." << LogEnd;
return false;
}
if ( !ptrIface->getServerUnicast() && !ptrIface->getServerMulticast() &&
!ptrIface->getClientUnicast() && !ptrIface->getClientMulticast() ) {
Log(Warning) << "Both server and client support is defined on the " << ptrIface->getName()
<< "/" << ptrIface->getID() << " interface." << LogEnd;
}
if ( (ptrIface->getServerUnicast() || ptrIface->getServerMulticast()) &&
(ptrIface->getClientUnicast() || ptrIface->getClientMulticast()) ) {
Log(Warning) << "Both server and client support is defined on the " << ptrIface->getName()
<< "/" << ptrIface->getID() << " interface." << LogEnd;
}
return true;
}
SPtr<TRelCfgIface> TRelCfgMgr::getIfaceByID(int iface)
{
SPtr<TRelCfgIface> ptrIface;
this->firstIface();
while ( ptrIface = this->getIface() ) {
if ( ptrIface->getID()==iface )
return ptrIface;
}
Log(Error) << "There is no interface with ifindex=" << iface << " in the CfgMgr." << LogEnd;
return SPtr<TRelCfgIface>(); // NULL
}
SPtr<TRelCfgIface> TRelCfgMgr::getIfaceByInterfaceID(int iface)
{
SPtr<TRelCfgIface> ptrIface;
this->firstIface();
while ( ptrIface = this->getIface() ) {
if ( ptrIface->getInterfaceID()==iface )
return ptrIface;
}
Log(Error) << "There is no interface with interfaceID=" << iface
<< " in the CfgMgr." << LogEnd;
return SPtr<TRelCfgIface>(); // NULL
}
ERelIfaceIdOrder TRelCfgMgr::getInterfaceIDOrder()
{
return InterfaceIDOrder;
}
SPtr<TOptVendorData> TRelCfgMgr::getRemoteID()
{
return RemoteID;
}
SPtr<TRelOptEcho> TRelCfgMgr::getEcho()
{
return Echo;
}
void TRelCfgMgr::instanceCreate(const std::string& cfgFile, const std::string& xmlFile )
{
if (Instance)
Log(Crit) << "RelCfgMgr instance already created. Application error!" << LogEnd;
Instance = new TRelCfgMgr(cfgFile, xmlFile);
}
TRelCfgMgr& TRelCfgMgr::instance()
{
if (!Instance) {
Log(Crit) << "RelCfgMgr instance not created yet. Application error. Emergency shutdown." << LogEnd;
exit(EXIT_FAILURE);
}
return *Instance;
}
// --------------------------------------------------------------------
// --- operators ------------------------------------------------------
// --------------------------------------------------------------------
ostream & operator<<(ostream &out, TRelCfgMgr &x) {
out << "<RelCfgMgr>" << std::endl;
out << " <workdir>" << x.getWorkDir() << "</workdir>" << endl;
out << " <LogName>" << x.getLogName() << "</LogName>" << endl;
out << " <LogLevel>" << x.getLogLevel() << "</LogLevel>" << endl;
out << " <InterfaceIDOrder>";
switch (x.InterfaceIDOrder) {
case REL_IFACE_ID_ORDER_BEFORE:
out << "before";
break;
case REL_IFACE_ID_ORDER_AFTER:
out << "after";
break;
case REL_IFACE_ID_ORDER_NONE:
out << "none";
break;
default:
out << "unknown(" << x.InterfaceIDOrder << ")";
break;
}
out << "</InterfaceIDOrder>" << endl;
if (SPtr<TOptVendorData> r = x.getRemoteID()) {
out << " <RemoteID enterprise=\"" << r->getVendor() << "\" length=\"" << r->getVendorDataLen()
<< "\">" << r->getVendorDataPlain() << "</RemoteID>" << endl;
} else {
out << " <!-- <RemoteID/> -->" << endl;
}
if (x.getEcho()) {
SPtr<TRelOptEcho> e = x.getEcho();
out << " <EchoRequest count=\"" << e->count() << "\">";
for (int i=0;i<e->count();i++) {
out << e->getReqOpt(i) << " ";
}
out << "</EchoRequest>" << endl;
} else {
out << " <!-- <EchoRequest/> -->" << endl;
}
SPtr<TRelCfgIface> ptrIface;
x.firstIface();
while (ptrIface = x.getIface()) {
out << *ptrIface;
}
out << "</RelCfgMgr>" << std::endl;
return out;
}
void TRelCfgMgr::setRelayID(SPtr<TOpt> relayID)
{
RelayID_ = relayID;
}
SPtr<TOpt> TRelCfgMgr::getRelayID()
{
return RelayID_;
}
void TRelCfgMgr::setClientLinkLayerAddress(bool enabled) {
ClientLinkLayerAddress_ = enabled;
}
bool TRelCfgMgr::getClientLinkLayerAddress() {
return ClientLinkLayerAddress_;
}
|