Browse Source

[4106] Represent DHCPv4o6 endpoint with enum.

Marcin Siodelski 9 years ago
parent
commit
7c43a3ebcf

+ 1 - 1
src/bin/dhcp4/dhcp4_dhcp4o6_ipc.cc

@@ -42,7 +42,7 @@ void Dhcp4o6Ipc::open() {
     }
 
     int old_fd = socket_fd_;
-    socket_fd_ = Dhcp4o6IpcBase::open(static_cast<uint16_t>(port), 4);
+    socket_fd_ = Dhcp4o6IpcBase::open(static_cast<uint16_t>(port), ENDPOINT_TYPE_V4);
     if ((old_fd == -1) && (socket_fd_ != old_fd)) {
         IfaceMgr::instance().addExternalSocket(socket_fd_, Dhcp4o6Ipc::handler);
     }

+ 1 - 1
src/bin/dhcp6/dhcp6_dhcp4o6_ipc.cc

@@ -42,7 +42,7 @@ void Dhcp4o6Ipc::open() {
     }
 
     int old_fd = socket_fd_;
-    socket_fd_ = Dhcp4o6IpcBase::open(static_cast<uint16_t>(port), 6);
+    socket_fd_ = Dhcp4o6IpcBase::open(static_cast<uint16_t>(port), ENDPOINT_TYPE_V6);
     if ((old_fd == -1) && (socket_fd_ != old_fd)) {
         IfaceMgr::instance().addExternalSocket(socket_fd_, Dhcp4o6Ipc::handler);
     }

+ 3 - 3
src/lib/dhcpsrv/dhcp4o6_ipc.cc

@@ -39,7 +39,7 @@ Dhcp4o6IpcBase::~Dhcp4o6IpcBase() {
     close();
 }
 
-int Dhcp4o6IpcBase::open(const uint16_t port, const int side) {
+int Dhcp4o6IpcBase::open(const uint16_t port, const EndpointType& endpoint_type) {
     if (port == port_) {
         // No change: nothing to do
         return (socket_fd_);
@@ -72,7 +72,7 @@ int Dhcp4o6IpcBase::open(const uint16_t port, const int side) {
 #ifdef HAVE_SA_LEN
     local6.sin6_len = sizeof(local6);
 #endif
-    if (side == 6) {
+    if (endpoint_type == ENDPOINT_TYPE_V6) {
         local6.sin6_port = htons(port);
     } else {
         local6.sin6_port = htons(port + 1);
@@ -89,7 +89,7 @@ int Dhcp4o6IpcBase::open(const uint16_t port, const int side) {
 #ifdef HAVE_SA_LEN
     remote6.sin6_len = sizeof(remote6);
 #endif
-    if (side == 6) {
+    if (endpoint_type == ENDPOINT_TYPE_V6) {
         remote6.sin6_port = htons(port + 1);
     } else {
         remote6.sin6_port = htons(port);

+ 10 - 2
src/lib/dhcpsrv/dhcp4o6_ipc.h

@@ -66,6 +66,14 @@ public:
 /// enterprise id. These options are added by the IPC sender and removed
 /// by the IPC receiver.
 class Dhcp4o6IpcBase : public boost::noncopyable {
+public:
+
+    /// @brief Endpoint type: DHCPv4 or DHCPv6 server.
+    enum EndpointType {
+        ENDPOINT_TYPE_V4,
+        ENDPOINT_TYPE_V6
+    };
+
 protected:
     /// @brief Constructor
     ///
@@ -80,10 +88,10 @@ protected:
     /// @param port Port number to use. The socket is bound to this port
     /// if the endpoint type is DHCPv6 server, otherwise the port + 1
     /// value is used.
-    /// @param side Endpoint type (DHCPv4 or DHCPv6 server).
+    /// @param endpoint_type Endpoint type (DHCPv4 or DHCPv6 server).
     ///
     /// @return New socket descriptor.
-    int open(const uint16_t port, const int side);
+    int open(const uint16_t port, const EndpointType& endpoint_type);
 
 public:
 

+ 37 - 36
src/lib/dhcpsrv/tests/dhcp4o6_ipc_unittest.cc

@@ -40,12 +40,6 @@ const uint16_t TEST_PORT = 12345;
 /// @brief Number of iterations used by the tests.
 const uint16_t TEST_ITERATIONS = 10;
 
-/// @brief Defines the DHCPv4 endpoint of IPC.
-const int ENDPOINT_TYPE_V4 = 4;
-
-/// @brief Defines the DHCPv6 endpoint of IPC.
-const int ENDPOINT_TYPE_V6 = 6;
-
 /// @brief Type definition for the function creating DHCP message.
 typedef boost::function<Pkt6Ptr(const uint16_t, const uint16_t)> CreateMsgFun;
 
@@ -57,7 +51,7 @@ public:
     ///
     /// @param port Desired port.
     /// @param endpoint_type Type of the IPC endpoint. It should be 4 or 6.
-    TestIpc(const uint16_t port, const int endpoint_type);
+    TestIpc(const uint16_t port, const EndpointType& endpoint_type);
 
     /// @brief Sets new port to be used with @c open.
     ///
@@ -108,13 +102,13 @@ private:
     uint16_t desired_port_;
 
     /// @brief Endpoint type, i.e. 4 or 6.
-    int endpoint_type_;
+    EndpointType endpoint_type_;
 
     /// @brief Pointer to the last received message.
     Pkt6Ptr pkt_received_;
 };
 
-TestIpc::TestIpc(const uint16_t port, const int endpoint_type)
+TestIpc::TestIpc(const uint16_t port, const EndpointType& endpoint_type)
     : desired_port_(port), endpoint_type_(endpoint_type), pkt_received_() {
 }
 
@@ -213,15 +207,17 @@ protected:
     ///
     /// @param src Type of the source endpoint. It can be 4 or 6.
     /// @return Pointer to the instance of the DHCPv4-query Message option.
-    static OptionPtr createDHCPv4MsgOption(const int src);
+    static OptionPtr createDHCPv4MsgOption(const TestIpc::EndpointType& src);
 
     /// @brief Tests sending and receiving packets over the IPC.
     ///
     /// @param iterations_num Number of packets to be sent over the IPC.
     /// @param src Type of the source IPC endpoint. It can be 4 or 6.
     /// @param dest Type of the destination IPC endpoint. It can be 4 or 6.
-    void testSendReceive(const uint16_t iterations_num, const int src,
-                         const int dest, const CreateMsgFun& create_msg_fun);
+    void testSendReceive(const uint16_t iterations_num,
+                         const TestIpc::EndpointType& src,
+                         const TestIpc::EndpointType& dest,
+                         const CreateMsgFun& create_msg_fun);
 
     /// @brief Tests that error is reported when invalid message is received.
     ///
@@ -267,8 +263,8 @@ Dhcp4o6IpcBaseTest::createDHCPv4o6Message(const uint16_t msg_type,
     pkt->setRemoteAddr(IOAddress(concatenate("2001:db8:1::", postfix)));
 
     // Determine the endpoint type using the message type.
-    const int src = (msg_type ==  DHCPV6_DHCPV4_QUERY) ?
-        ENDPOINT_TYPE_V6 : ENDPOINT_TYPE_V4;
+    const TestIpc::EndpointType src = (msg_type ==  DHCPV6_DHCPV4_QUERY) ?
+        TestIpc::ENDPOINT_TYPE_V6 : TestIpc::ENDPOINT_TYPE_V4;
 
     // Add DHCPv4 Message option to make sure it is conveyed by the IPC.
     pkt->addOption(createDHCPv4MsgOption(src));
@@ -307,9 +303,9 @@ Dhcp4o6IpcBaseTest::createDHCPv4o6MsgWithAnyVendorOption(const uint16_t msg_type
 }
 
 OptionPtr
-Dhcp4o6IpcBaseTest::createDHCPv4MsgOption(const int src) {
+Dhcp4o6IpcBaseTest::createDHCPv4MsgOption(const TestIpc::EndpointType& src) {
     // Create the DHCPv4 message.
-    Pkt4Ptr pkt(new Pkt4(src == ENDPOINT_TYPE_V4 ? DHCPACK : DHCPREQUEST,
+    Pkt4Ptr pkt(new Pkt4(src == TestIpc::ENDPOINT_TYPE_V4 ? DHCPACK : DHCPREQUEST,
                          1234));
     // Make a wire representation of the DHCPv4 message.
     pkt->pack();
@@ -324,7 +320,8 @@ Dhcp4o6IpcBaseTest::createDHCPv4MsgOption(const int src) {
 
 void
 Dhcp4o6IpcBaseTest::testSendReceive(const uint16_t iterations_num,
-                                    const int src, const int dest,
+                                    const TestIpc::EndpointType& src,
+                                    const TestIpc::EndpointType& dest,
                                     const CreateMsgFun& create_msg_fun) {
     // Create IPC instances representing the source and destination endpoints.
     TestIpc ipc_src(TEST_PORT, src);
@@ -337,7 +334,7 @@ Dhcp4o6IpcBaseTest::testSendReceive(const uint16_t iterations_num,
     // Depnding if we're sending from DHCPv6 to DHCPv4 or the opposite
     // direction we use different message type. This is not really required
     // for testing IPC, but it better simulates the real use case.
-    uint16_t msg_type = (src == 6 ? DHCPV6_DHCPV4_QUERY :
+    uint16_t msg_type = (src == TestIpc::ENDPOINT_TYPE_V6 ? DHCPV6_DHCPV4_QUERY :
                          DHCPV6_DHCPV4_RESPONSE);
 
     std::vector<bool> has_vendor_option;
@@ -403,8 +400,8 @@ Dhcp4o6IpcBaseTest::testSendReceive(const uint16_t iterations_num,
 
 void
 Dhcp4o6IpcBaseTest::testReceiveError(const Pkt6Ptr& pkt) {
-    TestIpc ipc_src(TEST_PORT, ENDPOINT_TYPE_V6);
-    TestIpc ipc_dest(TEST_PORT, ENDPOINT_TYPE_V4);
+    TestIpc ipc_src(TEST_PORT, TestIpc::ENDPOINT_TYPE_V6);
+    TestIpc ipc_dest(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
 
     // Open the IPC on both ends.
     ASSERT_NO_THROW(ipc_src.open());
@@ -412,7 +409,7 @@ Dhcp4o6IpcBaseTest::testReceiveError(const Pkt6Ptr& pkt) {
 
     pkt->setIface("eth0");
     pkt->setRemoteAddr(IOAddress("2001:db8:1::1"));
-    pkt->addOption(createDHCPv4MsgOption(ENDPOINT_TYPE_V6));
+    pkt->addOption(createDHCPv4MsgOption(TestIpc::ENDPOINT_TYPE_V6));
 
     OutputBuffer& buf = pkt->getBuffer();
     buf.clear();
@@ -430,22 +427,23 @@ Dhcp4o6IpcBaseTest::testReceiveError(const Pkt6Ptr& pkt) {
 // This test verifies that the IPC can transmit messages between the
 // DHCPv4 and DHCPv6 server.
 TEST_F(Dhcp4o6IpcBaseTest, send4To6) {
-    testSendReceive(TEST_ITERATIONS, ENDPOINT_TYPE_V4, ENDPOINT_TYPE_V6,
-                    &createDHCPv4o6Message);
+    testSendReceive(TEST_ITERATIONS, TestIpc::ENDPOINT_TYPE_V4,
+                    TestIpc::ENDPOINT_TYPE_V6, &createDHCPv4o6Message);
 }
 
 // This test verifies that the IPC can transmit messages between the
 // DHCPv6 and DHCPv4 server.
 TEST_F(Dhcp4o6IpcBaseTest, send6To4) {
-    testSendReceive(TEST_ITERATIONS, ENDPOINT_TYPE_V6, ENDPOINT_TYPE_V4,
-                    &createDHCPv4o6Message);
+    testSendReceive(TEST_ITERATIONS, TestIpc::ENDPOINT_TYPE_V6,
+                    TestIpc::ENDPOINT_TYPE_V4, &createDHCPv4o6Message);
 }
 
 // This test verifies that the IPC will transmit message already containing
 // vendor option with ISC enterprise ID, between the DHCPv6 and DHCPv4
 // server.
 TEST_F(Dhcp4o6IpcBaseTest, send6To4WithISCVendorOption) {
-    testSendReceive(TEST_ITERATIONS, ENDPOINT_TYPE_V6, ENDPOINT_TYPE_V4,
+    testSendReceive(TEST_ITERATIONS, TestIpc::ENDPOINT_TYPE_V6,
+                    TestIpc::ENDPOINT_TYPE_V4,
                     &createDHCPv4o6MsgWithISCVendorOption);
 }
 
@@ -453,7 +451,8 @@ TEST_F(Dhcp4o6IpcBaseTest, send6To4WithISCVendorOption) {
 // vendor option with ISC enterprise ID, between the DHCPv6 and DHCPv4
 // server.
 TEST_F(Dhcp4o6IpcBaseTest, send4To6WithISCVendorOption) {
-    testSendReceive(TEST_ITERATIONS, ENDPOINT_TYPE_V4, ENDPOINT_TYPE_V6,
+    testSendReceive(TEST_ITERATIONS, TestIpc::ENDPOINT_TYPE_V4,
+                    TestIpc::ENDPOINT_TYPE_V6,
                     &createDHCPv4o6MsgWithISCVendorOption);
 }
 
@@ -461,7 +460,8 @@ TEST_F(Dhcp4o6IpcBaseTest, send4To6WithISCVendorOption) {
 // vendor option with enterprise id different than ISC, between the DHCPv6
 // and DHCPv4 server.
 TEST_F(Dhcp4o6IpcBaseTest, send6To4WithAnyVendorOption) {
-    testSendReceive(TEST_ITERATIONS, ENDPOINT_TYPE_V6, ENDPOINT_TYPE_V4,
+    testSendReceive(TEST_ITERATIONS, TestIpc::ENDPOINT_TYPE_V6,
+                    TestIpc::ENDPOINT_TYPE_V4,
                     &createDHCPv4o6MsgWithAnyVendorOption);
 }
 
@@ -469,14 +469,15 @@ TEST_F(Dhcp4o6IpcBaseTest, send6To4WithAnyVendorOption) {
 // vendor option with enterprise id different than ISC, between the DHCPv4
 // and DHCPv6 server.
 TEST_F(Dhcp4o6IpcBaseTest, send4To6WithAnyVendorOption) {
-    testSendReceive(TEST_ITERATIONS, ENDPOINT_TYPE_V4, ENDPOINT_TYPE_V6,
+    testSendReceive(TEST_ITERATIONS, TestIpc::ENDPOINT_TYPE_V4,
+                    TestIpc::ENDPOINT_TYPE_V6,
                     &createDHCPv4o6MsgWithAnyVendorOption);
 }
 
 // This test checks that the values of the socket descriptor are correct
 // when the socket is opened and then closed.
 TEST_F(Dhcp4o6IpcBaseTest, openClose) {
-    TestIpc ipc(TEST_PORT, ENDPOINT_TYPE_V4);
+    TestIpc ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
     EXPECT_EQ(-1, ipc.getSocketFd());
 
     ASSERT_NO_THROW(ipc.open());
@@ -489,7 +490,7 @@ TEST_F(Dhcp4o6IpcBaseTest, openClose) {
 // This test verifies that it is call open() while the socket is already
 // opened. If the port changes, the new socket should be opened.
 TEST_F(Dhcp4o6IpcBaseTest, openMultipleTimes) {
-   TestIpc ipc(TEST_PORT, ENDPOINT_TYPE_V6);
+   TestIpc ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V6);
    ASSERT_NO_THROW(ipc.open());
    int sock_fd = ipc.getSocketFd();
    ASSERT_NE(-1, sock_fd);
@@ -509,12 +510,12 @@ TEST_F(Dhcp4o6IpcBaseTest, openMultipleTimes) {
 // This test verifies that the socket remains open if there is a failure
 // to open a new socket.
 TEST_F(Dhcp4o6IpcBaseTest, openError) {
-    TestIpc ipc(TEST_PORT, ENDPOINT_TYPE_V4);
+    TestIpc ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
     ASSERT_NO_THROW(ipc.open());
     int sock_fd = ipc.getSocketFd();
     ASSERT_NE(-1, sock_fd);
 
-    TestIpc ipc_bound(TEST_PORT + 10, ENDPOINT_TYPE_V4);
+    TestIpc ipc_bound(TEST_PORT + 10, TestIpc::ENDPOINT_TYPE_V4);
     ASSERT_NO_THROW(ipc_bound.open());
     ASSERT_NE(-1, ipc_bound.getSocketFd());
 
@@ -607,7 +608,7 @@ TEST_F(Dhcp4o6IpcBaseTest, receiveWithoutSourceAddressOption) {
 // This test verifies that send method throws exception when the packet
 // is NULL.
 TEST_F(Dhcp4o6IpcBaseTest, sendNullMessage) {
-    TestIpc ipc(TEST_PORT, ENDPOINT_TYPE_V4);
+    TestIpc ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
     ASSERT_NO_THROW(ipc.open());
 
     // NULL message.
@@ -617,7 +618,7 @@ TEST_F(Dhcp4o6IpcBaseTest, sendNullMessage) {
 // This test verifies that send method throws exception when the IPC
 // socket is not opened.
 TEST_F(Dhcp4o6IpcBaseTest, sendOverClosedSocket) {
-    TestIpc ipc(TEST_PORT, ENDPOINT_TYPE_V4);
+    TestIpc ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
 
     // Create a message.
     Pkt6Ptr pkt(createDHCPv4o6Message(DHCPV6_DHCPV4_QUERY));
@@ -629,7 +630,7 @@ TEST_F(Dhcp4o6IpcBaseTest, sendOverClosedSocket) {
 // This test verifies that send method throws exception when the IPC
 // socket has been unexpectedly closed.
 TEST_F(Dhcp4o6IpcBaseTest, sendOverUnexpectedlyClosedSocket) {
-    TestIpc ipc(TEST_PORT, ENDPOINT_TYPE_V4);
+    TestIpc ipc(TEST_PORT, TestIpc::ENDPOINT_TYPE_V4);
     ASSERT_NO_THROW(ipc.open());
 
     // Close the socket behind the scenes. The IPC doesn't know that the