Parcourir la source

[992] Getters/setters implemented in Pkt4.

- Implemented several getters/setters
- Added reason comments for #if 0 code (dependency on other tickets)
- New test for implemented getters/setters
Tomek Mrugalski il y a 13 ans
Parent
commit
0f4dd0cf9c
4 fichiers modifiés avec 109 ajouts et 24 suppressions
  1. 20 17
      src/bin/dhcp4/dhcp4_srv.cc
  2. 3 3
      src/bin/dhcp4/main.cc
  3. 70 3
      src/lib/dhcp/pkt4.h
  4. 16 1
      src/lib/dhcp/tests/pkt4_unittest.cc

+ 20 - 17
src/bin/dhcp4/dhcp4_srv.cc

@@ -48,6 +48,7 @@ Dhcpv4Srv::run() {
         boost::shared_ptr<Pkt4> rsp;   // server's response
 
 #if 0
+        // uncomment this once ticket 1239 is merged.
         query = IfaceMgr::instance().receive4();
 #endif
 
@@ -80,25 +81,26 @@ Dhcpv4Srv::run() {
             cout << "Received " << query->len() << " bytes packet type="
                  << query->getType() << endl;
 
-            /// DEBUG
+            // TODO: print out received packets only if verbose (or debug)
+            // mode is enabled
             cout << query->toText();
 
             if (rsp) {
-#if 0
-                rsp->remote_addr_ = query->remote_addr_;
-                rsp->local_addr_ = query->local_addr_;
-                rsp->remote_port_ = DHCP6_CLIENT_PORT;
-                rsp->local_port_ = DHCP6_SERVER_PORT;
-                rsp->ifindex_ = query->ifindex_;
-                rsp->iface_ = query->iface_;
-#endif
+                rsp->setRemoteAddr(query->getRemoteAddr());
+                rsp->setLocalAddr(query->getLocalAddr());
+                rsp->setRemotePort(DHCP4_CLIENT_PORT);
+                rsp->setLocalPort(DHCP4_SERVER_PORT);
+                rsp->setIface(query->getIface());
+                rsp->setIndex(query->getIndex());
+
                 cout << "Replying with:" << rsp->getType() << endl;
                 cout << rsp->toText();
                 cout << "----" << endl;
                 if (rsp->pack()) {
-                    cout << "#### pack successful." << endl;
+                    cout << "Packet assembled correctly." << endl;
                 }
 #if 0
+                // uncomment this once ticket 1240 is merged.
                 IfaceMgr::instance().send4(rsp);
 #endif
             }
@@ -113,9 +115,11 @@ Dhcpv4Srv::run() {
 
 void
 Dhcpv4Srv::setServerID() {
-    /// TODO implement this for real once interface detection is done.
-    /// Use hardcoded server-id for now
+    /// TODO implement this for real once interface detection (ticket 1237)
+    /// is done. Use hardcoded server-id for now.
+
 #if 0
+    // uncomment this once ticket 1350 is merged.
     IOAddress srvId("127.0.0.1");
     serverid_ = boost::shared_ptr<Option>(
       new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
@@ -124,13 +128,13 @@ Dhcpv4Srv::setServerID() {
 
 boost::shared_ptr<Pkt4>
 Dhcpv4Srv::processDiscover(boost::shared_ptr<Pkt4> discover) {
-    /// TODO: Echo mode. Implement this for real
+    /// TODO: Currently implemented echo mode. Implement this for real
     return (discover);
 }
 
 boost::shared_ptr<Pkt4>
 Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4> request) {
-    /// TODO: Echo mode. Implement this for real
+    /// TODO: Currently implemented echo mode. Implement this for real
     return (request);
 }
 
@@ -138,14 +142,13 @@ void Dhcpv4Srv::processRelease(boost::shared_ptr<Pkt4> release) {
     /// TODO: Implement this.
     cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
 }
- 
+
 void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4> decline) {
     /// TODO: Implement this.
     cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
 }
 
 boost::shared_ptr<Pkt4> processInform(boost::shared_ptr<Pkt4> inform) {
-    /// TODO: Echo mode. Implement this for real
+    /// TODO: Currently implemented echo mode. Implement this for real
     return (inform);
 }
-

+ 3 - 3
src/bin/dhcp4/main.cc

@@ -81,8 +81,8 @@ main(int argc, char* argv[]) {
 
     int ret = 0;
 
-    // TODO remainder of auth to dhcp6 code copy. We need to enable this in
-    //      dhcp6 eventually
+    // TODO remainder of auth to dhcp4 code copy. We need to enable this in
+    //      dhcp4 eventually
 #if 0
     Session* cc_session = NULL;
     Session* statistics_session = NULL;
@@ -104,7 +104,7 @@ main(int argc, char* argv[]) {
         srv->run();
 
     } catch (const std::exception& ex) {
-        cerr << "[b10-dhcp6] Server failed: " << ex.what() << endl;
+        cerr << "[b10-dhcp4] Server failed: " << ex.what() << endl;
         ret = 1;
     }
 

+ 70 - 3
src/lib/dhcp/pkt4.h

@@ -311,6 +311,73 @@ public:
     /// @return interface name
     std::string getIface() { return iface_; };
 
+    /// @brief Sets interface name.
+    ///
+    /// Sets interface name over which packet was received or is
+    /// going to be transmitted.
+    ///
+    /// @return interface name
+    void setIface(const std::string& iface ) { iface_ = iface; };
+
+    /// @brief Sets interface index.
+    ///
+    /// @param ifindex specifies interface index.
+    void setIndex(uint32_t ifindex) { ifindex_ = ifindex; };
+
+    /// @brief Returns interface index.
+    ///
+    /// @return interface index
+    uint32_t getIndex() { return (ifindex_); };
+
+    /// @brief Sets remote address.
+    ///
+    /// @params remote specifies remote address
+    void setRemoteAddr(const isc::asiolink::IOAddress& remote) {
+        remote_addr_ = remote;
+    }
+
+    /// @brief Returns remote address
+    ///
+    /// @return remote address
+    const isc::asiolink::IOAddress& getRemoteAddr() {
+        return (remote_addr_);
+    }
+
+    /// @brief Sets local address.
+    ///
+    /// @params local specifies local address
+    void setLocalAddr(const isc::asiolink::IOAddress& local) {
+        local_addr_ = local;
+    }
+
+    /// @brief Returns local address.
+    ///
+    /// @return local address
+    const isc::asiolink::IOAddress& getLocalAddr() {
+        return (local_addr_);
+    }
+
+    /// @brief Sets local port.
+    ///
+    /// @params local specifies local port
+    void setLocalPort(uint16_t local) { local_port_ = local; }
+
+    /// @brief Returns local port.
+    ///
+    /// @return local port
+    uint16_t getLocalPort() { return (local_port_); }
+
+    /// @brief Sets remote port.
+    ///
+    /// @params remote specifies remote port
+    void setRemotePort(uint16_t remote) { remote_port_ = remote; }
+
+    /// @brief Returns remote port.
+    ///
+    /// @return remote port
+    uint16_t getRemotePort() { return (remote_port_); }
+
+
 protected:
 
     /// converts DHCP message type to BOOTP op type
@@ -335,13 +402,13 @@ protected:
     /// Each network interface has assigned unique ifindex. It is functional
     /// equvalent of name, but sometimes more useful, e.g. when using crazy
     /// systems that allow spaces in interface names e.g. MS Windows)
-    int ifindex_;
+    uint32_t ifindex_;
 
     /// local UDP port
-    int local_port_;
+    uint16_t local_port_;
 
     /// remote UDP port
-    int remote_port_;
+    uint16_t remote_port_;
 
     /// @brief message operation code
     ///

+ 16 - 1
src/lib/dhcp/tests/pkt4_unittest.cc

@@ -504,7 +504,7 @@ TEST(Pkt4Test, unpackOptions) {
 
     vector<uint8_t> expectedFormat = generateTestPacket2();
 
-    for (int i=0; i < sizeof(v4Opts); i++) {
+    for (int i = 0; i < sizeof(v4Opts); i++) {
         expectedFormat.push_back(v4Opts[i]);
     }
 
@@ -559,4 +559,19 @@ TEST(Pkt4Test, unpackOptions) {
     EXPECT_EQ(0, memcmp(&x->getData()[0], v4Opts+22, 3)); // data len=3
 }
 
+TEST(Pkt4Test, metaFields) {
+
+    Pkt4* pkt = new Pkt4(DHCPOFFER, 1234);
+    pkt->setIface("loooopback");
+    pkt->setIndex(42);
+    pkt->setRemoteAddr(IOAddress("1.2.3.4"));
+    pkt->setLocalAddr(IOAddress("4.3.2.1"));
+
+    EXPECT_EQ("loooopback", pkt->getIface());
+    EXPECT_EQ(42, pkt->getIndex());
+    EXPECT_EQ("1.2.3.4", pkt->getRemoteAddr().toText());
+    EXPECT_EQ("4.3.2.1", pkt->getLocalAddr().toText());
+
+}
+
 } // end of anonymous namespace