File: DHCPRelay.cpp

package info (click to toggle)
dibbler 0.7.3-1.3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,148 kB
  • ctags: 8,720
  • sloc: cpp: 54,863; sh: 9,389; ansic: 8,659; yacc: 2,570; makefile: 1,061; lex: 842; perl: 49; xml: 6
file content (146 lines) | stat: -rw-r--r-- 3,763 bytes parent folder | download | duplicates (2)
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
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 *
 * released under GNU GPL v2 licence
 *
 * $Id: DHCPRelay.cpp,v 1.5 2007-03-10 01:42:32 thomson Exp $
 *
 * $Log: DHCPRelay.cpp,v $
 * Revision 1.5  2007-03-10 01:42:32  thomson
 * Guess-mode (and related segfault fix) added to relay.
 *
 * Revision 1.4  2007-02-03 17:50:43  thomson
 * Win32 fixes.
 *
 * Revision 1.3  2005/02/01 00:57:36  thomson
 * no message
 *
 * Revision 1.2  2005/01/13 22:45:55  thomson
 * Relays implemented.
 *
 * Revision 1.1  2005/01/11 22:53:35  thomson
 * Relay skeleton implemented.
 *
 */
      
#include "DHCPRelay.h"
#include "RelCommon.h"
#include "Logger.h"
#include "Portable.h"
#include "RelIfaceMgr.h"
#include "RelCfgMgr.h"
#include "RelTransMgr.h"
#include "RelMsg.h"

volatile int serviceShutdown;

TDHCPRelay::TDHCPRelay(string config)
{
    memset(&this->Ctx, 0, sizeof(TCtx));

    serviceShutdown = 0;
    srand(now());
    this->IsDone = false;

    this->Ctx.IfaceMgr = new TRelIfaceMgr(RELIFACEMGR_FILE);
    if ( this->Ctx.IfaceMgr->isDone() ) {
	Log(Crit) << "Fatal error during IfaceMgr initialization." << LogEnd;
	this->IsDone = true;
	return;
    }
    this->Ctx.IfaceMgr->setContext(&this->Ctx);
    this->Ctx.IfaceMgr->dump();

    this->Ctx.CfgMgr = new TRelCfgMgr(this->Ctx, config, RELCFGMGR_FILE);
    if ( this->Ctx.CfgMgr->isDone() ) {
	Log(Crit) << "Fatal error during CfgMgr initialization." << LogEnd;
	this->IsDone = true;
	return;
    }
    this->Ctx.CfgMgr->dump();

    this->Ctx.TransMgr = new TRelTransMgr(&this->Ctx, RELTRANSMGR_FILE);
    if ( this->Ctx.TransMgr->isDone() ) {
	Log(Crit) << "Fatal error during TransMgr initialization." << LogEnd;
	this->IsDone = true;
	return;
    }
    this->Ctx.IfaceMgr->dump();
    this->Ctx.TransMgr->dump();
}

void TDHCPRelay::run()
{
    bool silent = false;
    while ( (!this->isDone()) && (!this->Ctx.TransMgr->isDone()) ) {
    	if (serviceShutdown)
	    this->Ctx.TransMgr->shutdown();
	
	this->Ctx.TransMgr->doDuties();
	unsigned int timeout = DHCPV6_INFINITY/2;
	if (serviceShutdown)     timeout = 0;
	
	if (!silent)
	    Log(Debug) << "Accepting messages." << LogEnd;

#ifdef WIN32
	// There's no easy way to break select, so just don't sleep for too long.
	if (timeout>5) {
	    silent = true;
	    timeout = 5;
	}
#endif
	
	SmartPtr<TRelMsg> msg = this->Ctx.IfaceMgr->select(timeout);
	if (!msg) 
	    continue;
	silent = false;
	int iface = msg->getIface();
	SmartPtr<TIfaceIface> ptrIface;
	ptrIface = this->Ctx.IfaceMgr->getIfaceByID(iface);
	Log(Notice) << "Received " << msg->getName() << " on " << ptrIface->getName() 
		    << "/" << iface;
	if (msg->getType()!=RELAY_FORW_MSG && msg->getType()!=RELAY_REPL_MSG)
	    Log(Cont) << hex << ",TransID=0x" << msg->getTransID() << dec;
	Log(Cont) << ", " << msg->countOption() << " opts:";
	SmartPtr<TOpt> ptrOpt;
	msg->firstOption();
	while ( ptrOpt = msg->getOption() ) {
	    Log(Cont) << " " << ptrOpt->getOptType(); 
            // uncomment this to get detailed info about option lengths Log(Cont) << "/" << ptrOpt->getSize();
	}
	// Log(Cont) << ", " << msg->getRelayCount() << " relay(s).";
	Log(Cont) << LogEnd;
	this->Ctx.TransMgr->relayMsg(msg);
    }
    Log(Notice) << "Bye bye." << LogEnd;
}

bool TDHCPRelay::isDone() {
    return this->IsDone;
}

bool TDHCPRelay::checkPrivileges() {
    // FIXME: check privileges
    return true;
}

void TDHCPRelay::stop() {
    serviceShutdown = 1;
	Log(Crit) << "Service SHUTDOWN." << LogEnd;

}

void TDHCPRelay::setWorkdir(std::string workdir) {
    if (this->Ctx.CfgMgr) {
        this->Ctx.CfgMgr->setWorkdir(workdir);
        this->Ctx.CfgMgr->dump();
    }
}

TDHCPRelay::~TDHCPRelay() {
}