|
@@ -16,13 +16,15 @@
|
|
|
#include <dhcp/pkt4.h>
|
|
|
#include <dhcp/iface_mgr.h>
|
|
|
#include <dhcp4/dhcp4_srv.h>
|
|
|
+#include <dhcp4/dhcp4_log.h>
|
|
|
#include <asiolink/io_address.h>
|
|
|
#include <dhcp/option4_addrlst.h>
|
|
|
|
|
|
-using namespace std;
|
|
|
using namespace isc;
|
|
|
-using namespace isc::dhcp;
|
|
|
using namespace isc::asiolink;
|
|
|
+using namespace isc::dhcp;
|
|
|
+using namespace isc::log;
|
|
|
+using namespace std;
|
|
|
|
|
|
// These are hardcoded parameters. Currently this is a skeleton server that only
|
|
|
// grants those options and a single, fixed, hardcoded lease.
|
|
@@ -35,8 +37,7 @@ const std::string HARDCODED_DOMAIN_NAME = "isc.example.com";
|
|
|
const std::string HARDCODED_SERVER_ID = "192.0.2.1";
|
|
|
|
|
|
Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
|
|
|
- cout << "Initialization: opening sockets on port " << port << endl;
|
|
|
-
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET).arg(port);
|
|
|
try {
|
|
|
// first call to instance() will create IfaceMgr (it's a singleton)
|
|
|
// it may throw something if things go wrong
|
|
@@ -47,8 +48,9 @@ Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
|
|
|
IfaceMgr::instance().openSockets4(port);
|
|
|
|
|
|
setServerID();
|
|
|
+
|
|
|
} catch (const std::exception &e) {
|
|
|
- cerr << "Error during DHCPv4 server startup: " << e.what() << endl;
|
|
|
+ LOG_ERROR(dhcp4_logger, DHCP4_SRV_CONSTRUCT_ERROR).arg(e.what());
|
|
|
shutdown_ = true;
|
|
|
return;
|
|
|
}
|
|
@@ -57,12 +59,11 @@ Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
|
|
|
}
|
|
|
|
|
|
Dhcpv4Srv::~Dhcpv4Srv() {
|
|
|
- cout << "b10-dhcp4: DHCPv4 server terminating." << endl;
|
|
|
IfaceMgr::instance().closeSockets();
|
|
|
}
|
|
|
|
|
|
void Dhcpv4Srv::shutdown() {
|
|
|
- cout << "b10-dhcp4: DHCPv4 server shutdown." << endl;
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_SHUTDOWN_REQUEST);
|
|
|
shutdown_ = true;
|
|
|
}
|
|
|
|
|
@@ -79,39 +80,43 @@ Dhcpv4Srv::run() {
|
|
|
if (query) {
|
|
|
try {
|
|
|
query->unpack();
|
|
|
+
|
|
|
} catch (const std::exception& e) {
|
|
|
- /// TODO: Printout reasons of failed parsing
|
|
|
- cout << "Failed to parse incoming packet " << endl;
|
|
|
+ // Failed to parse the packet.
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
|
|
|
+ DHCP4_PACKET_PARSE_FAIL).arg(e.what());
|
|
|
continue;
|
|
|
}
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_QUERY_DATA)
|
|
|
+ .arg(query->toText());
|
|
|
+
|
|
|
|
|
|
switch (query->getType()) {
|
|
|
case DHCPDISCOVER:
|
|
|
rsp = processDiscover(query);
|
|
|
break;
|
|
|
+
|
|
|
case DHCPREQUEST:
|
|
|
rsp = processRequest(query);
|
|
|
break;
|
|
|
+
|
|
|
case DHCPRELEASE:
|
|
|
processRelease(query);
|
|
|
break;
|
|
|
+
|
|
|
case DHCPDECLINE:
|
|
|
processDecline(query);
|
|
|
break;
|
|
|
+
|
|
|
case DHCPINFORM:
|
|
|
processInform(query);
|
|
|
break;
|
|
|
+
|
|
|
default:
|
|
|
- cout << "Unknown pkt type received:"
|
|
|
- << query->getType() << endl;
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_UNKNOWN)
|
|
|
+ .arg(query->getType()).arg(query->getIface());
|
|
|
}
|
|
|
|
|
|
- cout << "Received message type " << int(query->getType()) << endl;
|
|
|
-
|
|
|
- // TODO: print out received packets only if verbose (or debug)
|
|
|
- // mode is enabled
|
|
|
- cout << query->toText();
|
|
|
-
|
|
|
if (rsp) {
|
|
|
if (rsp->getRemoteAddr().toText() == "0.0.0.0") {
|
|
|
rsp->setRemoteAddr(query->getRemoteAddr());
|
|
@@ -127,12 +132,11 @@ Dhcpv4Srv::run() {
|
|
|
rsp->setIface(query->getIface());
|
|
|
rsp->setIndex(query->getIndex());
|
|
|
|
|
|
- cout << "Replying with message type "
|
|
|
- << static_cast<int>(rsp->getType()) << ":" << endl;
|
|
|
- cout << rsp->toText();
|
|
|
- cout << "----" << endl;
|
|
|
- if (rsp->pack()) {
|
|
|
- cout << "Packet assembled correctly." << endl;
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA,
|
|
|
+ DHCP4_RESPONSE_DATA)
|
|
|
+ .arg(rsp->getType()).arg(rsp->toText());
|
|
|
+ if (!rsp->pack()) {
|
|
|
+ LOG_ERROR(dhcp4_logger, DHCP4_PACK_FAIL);
|
|
|
}
|
|
|
IfaceMgr::instance().send(rsp);
|
|
|
}
|
|
@@ -239,6 +243,8 @@ void Dhcpv4Srv::tryAssignLease(Pkt4Ptr& msg) {
|
|
|
}
|
|
|
|
|
|
Pkt4Ptr Dhcpv4Srv::processDiscover(Pkt4Ptr& discover) {
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DISCOVER)
|
|
|
+ .arg(discover->getIface());
|
|
|
Pkt4Ptr offer = Pkt4Ptr
|
|
|
(new Pkt4(DHCPOFFER, discover->getTransid()));
|
|
|
|
|
@@ -252,6 +258,8 @@ Pkt4Ptr Dhcpv4Srv::processDiscover(Pkt4Ptr& discover) {
|
|
|
}
|
|
|
|
|
|
Pkt4Ptr Dhcpv4Srv::processRequest(Pkt4Ptr& request) {
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_REQUEST)
|
|
|
+ .arg(request->getIface());
|
|
|
Pkt4Ptr ack = Pkt4Ptr
|
|
|
(new Pkt4(DHCPACK, request->getTransid()));
|
|
|
|
|
@@ -265,16 +273,20 @@ Pkt4Ptr Dhcpv4Srv::processRequest(Pkt4Ptr& request) {
|
|
|
}
|
|
|
|
|
|
void Dhcpv4Srv::processRelease(Pkt4Ptr& release) {
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_RELEASE)
|
|
|
+ .arg(release->getIface());
|
|
|
/// TODO: Implement this.
|
|
|
- cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
|
|
|
}
|
|
|
|
|
|
void Dhcpv4Srv::processDecline(Pkt4Ptr& decline) {
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_DECLINE)
|
|
|
+ .arg(decline->getIface());
|
|
|
/// TODO: Implement this.
|
|
|
- cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
|
|
|
}
|
|
|
|
|
|
Pkt4Ptr Dhcpv4Srv::processInform(Pkt4Ptr& inform) {
|
|
|
+ LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_INFORM)
|
|
|
+ .arg(inform->getIface());
|
|
|
/// TODO: Currently implemented echo mode. Implement this for real
|
|
|
return (inform);
|
|
|
}
|