Browse Source

[1238] Generic dhcp6::iface_mgr class improvements.

- IfaceMgr does not open sockets by default. That will make running
  tests easier.
- sockets are now stored as uint16_t (see 1282)
- tests sockets6, sockets6Mcast and sendReceive6 are now reenabled
  (I'm going to regret this step very soon)
Tomek Mrugalski 13 years ago
parent
commit
5371b694b6

+ 14 - 7
src/bin/dhcp6/dhcp6_srv.cc

@@ -12,13 +12,14 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#include "dhcp/dhcp6.h"
-#include "dhcp/pkt6.h"
-#include "dhcp6/iface_mgr.h"
-#include "dhcp6/dhcp6_srv.h"
-#include "dhcp/option6_ia.h"
-#include "dhcp/option6_iaaddr.h"
-#include "asiolink/io_address.h"
+#include <dhcp/dhcp6.h>
+#include <dhcp/pkt6.h>
+#include <dhcp6/iface_mgr.h>
+#include <dhcp6/dhcp6_srv.h>
+#include <dhcp/option6_ia.h>
+#include <dhcp/option6_iaaddr.h>
+#include <asiolink/io_address.h>
+#include <exceptions/exceptions.h>
 
 using namespace std;
 using namespace isc;
@@ -32,6 +33,12 @@ Dhcpv6Srv::Dhcpv6Srv() {
     // it may throw something if things go wrong
     IfaceMgr::instance();
 
+    try {
+        IfaceMgr::instance().openSockets();
+    } catch (const Exception& e) {
+
+    }
+
     /// @todo: instantiate LeaseMgr here once it is imlpemented.
 
     setServerID();

+ 8 - 12
src/bin/dhcp6/iface_mgr.cc

@@ -95,9 +95,6 @@ IfaceMgr::IfaceMgr()
 
         detectIfaces();
 
-        if (!openSockets()) {
-            isc_throw(Unexpected, "Failed to open/bind sockets.");
-        }
     } catch (const std::exception& ex) {
         cout << "IfaceMgr creation failed:" << ex.what() << endl;
 
@@ -154,7 +151,7 @@ IfaceMgr::detectIfaces() {
     }
 }
 
-bool
+void
 IfaceMgr::openSockets() {
     int sock;
 
@@ -171,8 +168,8 @@ IfaceMgr::openSockets() {
             sock = openSocket(iface->getName(), *addr,
                               DHCP6_SERVER_PORT);
             if (sock<0) {
-                cout << "Failed to open unicast socket." << endl;
-                return (false);
+                isc_throw(Unexpected, "Failed to open unicast socket on "
+                          << " interface " << iface->getFullName());
             }
             sendsock_ = sock;
 
@@ -180,15 +177,14 @@ IfaceMgr::openSockets() {
                               IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
                               DHCP6_SERVER_PORT);
             if (sock<0) {
-                cout << "Failed to open multicast socket." << endl;
+                isc_throw(Unexpected, "Failed to open multicast socket on "
+                          << " interface " << iface->getFullName());
                 close(sendsock_);
-                return (false);
+                sendsock_ = 0;
             }
             recvsock_ = sock;
         }
     }
-
-    return (true);
 }
 
 void
@@ -252,13 +248,13 @@ IfaceMgr::openSocket(const std::string& ifname,
     }
 }
 
-int
+uint16_t
 IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
     isc_throw(NotImplemented, "Sorry. Try again in 2 weeks");
     cout << iface.getFullName() << addr.toText() << port; // just to disable unused warning
 }
 
-int
+uint16_t
 IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
     struct sockaddr_in6 addr6;
 

+ 9 - 12
src/bin/dhcp6/iface_mgr.h

@@ -86,10 +86,10 @@ public:
         int mac_len_;
 
         /// socket used to sending data
-        int sendsock_;
+        uint16_t sendsock_;
 
         /// socket used for receiving data
-        int recvsock_;
+        uint16_t recvsock_;
     };
 
     // TODO performance improvement: we may change this into
@@ -157,11 +157,12 @@ public:
     /// (e.g. remove expired leases)
     ///
     /// @return Pkt6 object representing received packet (or NULL)
-    boost::shared_ptr<Pkt6>
-    receive6();
+    boost::shared_ptr<Pkt6> receive6();
 
-    boost::shared_ptr<Pkt4>
-    receive4();
+    boost::shared_ptr<Pkt4> receive4();
+
+    /// Opens sockets on detected interfaces.
+    void openSockets();
 
     // don't use private, we need derived classes in tests
 protected:
@@ -174,10 +175,9 @@ protected:
 
     ~IfaceMgr();
 
-    int openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr, int port);
-
-    int openSocket6(Iface& iface, const isc::asiolink::IOAddress& addr, int port);
+    uint16_t openSocket4(Iface& iface, const isc::asiolink::IOAddress& addr, int port);
 
+    uint16_t openSocket6(Iface& iface, const isc::asiolink::IOAddress& addr, int port);
 
     void addInterface(const Iface& iface) {
         ifaces_.push_back(iface);
@@ -229,9 +229,6 @@ protected:
     boost::scoped_array<char> control_buf_;
 
 private:
-    /// Opens sockets on detected interfaces.
-    bool
-    openSockets();
 
     /// creates a single instance of this class (a singleton implementation)
     static void

+ 12 - 4
src/bin/dhcp6/tests/iface_mgr_unittest.cc

@@ -48,7 +48,6 @@ public:
                    int port) {
         return IfaceMgr::openSocket(ifname, addr, port);
     }
-
 };
 
 // dummy class for now, but this will be expanded when needed
@@ -56,6 +55,13 @@ class IfaceMgrTest : public ::testing::Test {
 public:
     IfaceMgrTest() {
     }
+
+    void createLoInterfacesTxt() {
+        unlink(INTERFACE_FILE);
+        fstream fakeifaces(INTERFACE_FILE, ios::out|ios::trunc);
+        fakeifaces << LOOPBACK << " ::1";
+        fakeifaces.close();
+    }
 };
 
 // We need some known interface to work reliably. Loopback interface
@@ -246,10 +252,12 @@ TEST_F(IfaceMgrTest, detectIfaces) {
 // (lo in Linux, lo0 in BSD systems)
 // Fix for this is available on 1186 branch, will reenable
 // this test once 1186 is merged
-TEST_F(IfaceMgrTest, DISABLED_sockets) {
+TEST_F(IfaceMgrTest, sockets6) {
     // testing socket operation in a portable way is tricky
     // without interface detection implemented
 
+    createLoInterfacesTxt();
+
     NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
 
     IOAddress loAddr("::1");
@@ -283,7 +291,7 @@ TEST_F(IfaceMgrTest, DISABLED_sockets) {
 
 // TODO: disabled due to other naming on various systems
 // (lo in Linux, lo0 in BSD systems)
-TEST_F(IfaceMgrTest, DISABLED_socketsMcast) {
+TEST_F(IfaceMgrTest, sockets6Mcast) {
     // testing socket operation in a portable way is tricky
     // without interface detection implemented
 
@@ -316,7 +324,7 @@ TEST_F(IfaceMgrTest, DISABLED_socketsMcast) {
 // (lo in Linux, lo0 in BSD systems)
 // Fix for this is available on 1186 branch, will reenable
 // this test once 1186 is merged
-TEST_F(IfaceMgrTest, DISABLED_sendReceive6) {
+TEST_F(IfaceMgrTest, sendReceive6) {
     // testing socket operation in a portable way is tricky
     // without interface detection implemented