Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
cout << "Initialization: opening sockets on port " << port << endl;
- // first call to instance() will create IfaceMgr (it's a singleton)
- // it may throw something if things go wrong
- IfaceMgr::instance();
+ try {
+ // first call to instance() will create IfaceMgr (it's a singleton)
+ // it may throw something if things go wrong
+ IfaceMgr::instance();
- /// @todo: instantiate LeaseMgr here once it is imlpemented.
- IfaceMgr::instance().printIfaces();
+ /// @todo: instantiate LeaseMgr here once it is imlpemented.
- IfaceMgr::instance().openSockets4(port);
+ IfaceMgr::instance().openSockets4(port);
- setServerID();
+ setServerID();
+ } catch (const std::exception &e) {
+ cerr << "Error during DHCPv4 server startup: " << e.what() << endl;
+ shutdown_ = true;
+ return;
+ }
shutdown_ = false;
}
#include <log/dummylog.h>
#include <log/logger_support.h>
#include <dhcp4/ctrl_dhcp4_srv.h>
+#include <boost/lexical_cast.hpp>
using namespace std;
using namespace isc::dhcp;
stand_alone = true;
break;
case 'p':
- port_number = strtol(optarg, NULL, 10);
- if (port_number == 0) {
+ try {
+ port_number = boost::lexical_cast<int>(optarg);
+ } catch (const boost::bad_lexical_cast &) {
+ cerr << "Failed to parse port number: [" << optarg
+ << "], 1-65535 allowed." << endl;
+ usage();
+ }
+ if (port_number <= 0 || port_number > 65535) {
cerr << "Failed to parse port number: [" << optarg
<< "], 1-65535 allowed." << endl;
usage();
cout << "[b10-dhcp4] Initiating DHCPv4 server operation." << endl;
/// @todo: pass verbose to the actul server once logging is implemented
- ControlledDhcpv4Srv* server = new ControlledDhcpv4Srv(port_number);
+ ControlledDhcpv4Srv server(port_number);
if (!stand_alone) {
try {
- server->establishSession();
+ server.establishSession();
} catch (const std::exception& ex) {
cerr << "Failed to establish BIND10 session. "
"Running in stand-alone mode:" << ex.what() << endl;
cout << "Skipping connection to the BIND10 msgq." << endl;
}
- server->run();
- delete server;
- server = NULL;
-
+ server.run();
} catch (const std::exception& ex) {
cerr << "[b10-dhcp4] Server failed: " << ex.what() << endl;
ret = EXIT_FAILURE;
def tearDown(self):
pass
- def runDhcp4(self, params, wait=1):
+ def runCommand(self, params, wait=1):
"""
This method runs dhcp4 and returns a touple: (returncode, stdout, stderr)
"""
print("Note: Purpose of some of the tests is to check if DHCPv4 server can be started,")
print(" not that is can bind sockets correctly. Please ignore binding errors.")
- (returncode, output, error) = self.runDhcp4(["../b10-dhcp4", "-v"])
+ (returncode, output, error) = self.runCommand(["../b10-dhcp4", "-v"])
self.assertEqual( str(output).count("[b10-dhcp4] Initiating DHCPv4 server operation."), 1)
def test_portnumber_0(self):
print("Check that specifying port number 0 is not allowed.")
- (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-p', '0'])
+ (returncode, output, error) = self.runCommand(['../b10-dhcp4', '-p', '0'])
# When invalid port number is specified, return code must not be success
self.assertTrue(returncode != 0)
def test_portnumber_missing(self):
print("Check that -p option requires a parameter.")
- (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-p'])
+ (returncode, output, error) = self.runCommand(['../b10-dhcp4', '-p'])
# When invalid port number is specified, return code must not be success
self.assertTrue(returncode != 0)
# Check that there is an error message about invalid port number printed on stderr
self.assertEqual( str(error).count("option requires an argument"), 1)
+ def test_portnumber_invalid1(self):
+ print("Check that -p option is check against bogus port number (999999).")
+
+ (returncode, output, error) = self.runCommand(['../b10-dhcp4', '-p','999999'])
+
+ # When invalid port number is specified, return code must not be success
+ self.assertTrue(returncode != 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(error).count("Failed to parse port number"), 1)
+
+ def test_portnumber_invalid2(self):
+ print("Check that -p option is check against bogus port number (123garbage).")
+
+ (returncode, output, error) = self.runCommand(['../b10-dhcp4', '-p','123garbage'])
+
+ # When invalid port number is specified, return code must not be success
+ self.assertTrue(returncode != 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(error).count("Failed to parse port number"), 1)
+
def test_portnumber_nonroot(self):
print("Check that specifying unprivileged port number will work.")
- (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-p', '10057'])
+ (returncode, output, error) = self.runCommand(['../b10-dhcp4', '-s', '-p', '10057'])
# When invalid port number is specified, return code must not be success
# TODO: Temporarily commented out as socket binding on systems that do not have
def test_skip_msgq(self):
print("Check that connection to BIND10 msgq can be disabled.")
- (returncode, output, error) = self.runDhcp4(['../b10-dhcp4', '-s', '-p', '10057'])
+ (returncode, output, error) = self.runCommand(['../b10-dhcp4', '-s', '-p', '10057'])
# When invalid port number is specified, return code must not be success
# TODO: Temporarily commented out as socket binding on systems that do not have
// first call to instance() will create IfaceMgr (it's a singleton)
// it may throw something if things go wrong
try {
- IfaceMgr::instance();
- } catch (const std::exception &e) {
- cout << "Failed to instantiate InterfaceManager:" << e.what() << ". Aborting." << endl;
- shutdown_ = true;
- }
- if (IfaceMgr::instance().countIfaces() == 0) {
- cout << "Failed to detect any network interfaces. Aborting." << endl;
- shutdown_ = true;
- }
+ if (IfaceMgr::instance().countIfaces() == 0) {
+ cout << "Failed to detect any network interfaces. Aborting." << endl;
+ shutdown_ = true;
+ return;
+ }
- // Now try to open IPv6 sockets on detected interfaces.
- IfaceMgr::instance().openSockets6(port);
+ IfaceMgr::instance().openSockets6(port);
- /// @todo: instantiate LeaseMgr here once it is imlpemented.
+ setServerID();
- setServerID();
+ /// @todo: instantiate LeaseMgr here once it is imlpemented.
+
+ } catch (const std::exception &e) {
+ cerr << "Error during DHCPv4 server startup: " << e.what() << endl;
+ shutdown_ = true;
+ return;
+ }
shutdown_ = false;
}
#include <log/dummylog.h>
#include <log/logger_support.h>
#include <dhcp6/ctrl_dhcp6_srv.h>
+#include <boost/lexical_cast.hpp>
using namespace std;
using namespace isc::dhcp;
stand_alone = true;
break;
case 'p':
- port_number = strtol(optarg, NULL, 10);
- if (port_number == 0) {
+ try {
+ port_number = boost::lexical_cast<int>(optarg);
+ } catch (const boost::bad_lexical_cast &) {
+ cerr << "Failed to parse port number: [" << optarg
+ << "], 1-65535 allowed." << endl;
+ usage();
+ }
+ if (port_number <= 0 || port_number > 65535) {
cerr << "Failed to parse port number: [" << optarg
<< "], 1-65535 allowed." << endl;
usage();
cout << "b10-dhcp6: Initiating DHCPv6 server operation." << endl;
/// @todo: pass verbose to the actual server once logging is implemented
- ControlledDhcpv6Srv* server = new ControlledDhcpv6Srv(port_number);
+ ControlledDhcpv6Srv server(port_number);
if (!stand_alone) {
try {
- server->establishSession();
+ server.establishSession();
} catch (const std::exception& ex) {
cerr << "Failed to establish BIND10 session. "
"Running in stand-alone mode:" << ex.what() << endl;
cout << "Skipping connection to the BIND10 msgq." << endl;
}
- server->run();
- delete server;
- server = NULL;
+ server.run();
} catch (const std::exception& ex) {
cerr << "[b10-dhcp6] Server failed: " << ex.what() << endl;
# Check that there is an error message about invalid port number printed on stderr
self.assertEqual( str(error).count("option requires an argument"), 1)
+ def test_portnumber_invalid1(self):
+ print("Check that -p option is check against bogus port number (999999).")
+
+ (returncode, output, error) = self.runCommand(['../b10-dhcp6', '-p','999999'])
+
+ # When invalid port number is specified, return code must not be success
+ self.assertTrue(returncode != 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(error).count("Failed to parse port number"), 1)
+
+ def test_portnumber_invalid2(self):
+ print("Check that -p option is check against bogus port number (123garbage).")
+
+ (returncode, output, error) = self.runCommand(['../b10-dhcp6', '-p','123garbage'])
+
+ # When invalid port number is specified, return code must not be success
+ self.assertTrue(returncode != 0)
+
+ # Check that there is an error message about invalid port number printed on stderr
+ self.assertEqual( str(error).count("Failed to parse port number"), 1)
+
def test_portnumber_nonroot(self):
print("Check that specifying unprivileged port number will work.")
- (returncode, output, error) = self.runCommand(['../b10-dhcp6', '-p', '10547'])
+ (returncode, output, error) = self.runCommand(['../b10-dhcp6', '-s', '-p', '10547'])
# When invalid port number is specified, return code must not be success
# TODO: Temporarily commented out as socket binding on systems that do not have
# interface detection implemented currently fails.
# self.assertTrue(returncode == 0)
- # Check that there is a message on stdout about opening proper port
self.assertEqual( str(output).count("opening sockets on port 10547"), 1)
def test_skip_msgq(self):
# interface detection implemented currently fails.
# self.assertTrue(returncode == 0)
- # Check that there is an error message about invalid port number printed on stderr
self.assertEqual( str(output).count("Skipping connection to the BIND10 msgq."), 1)