Browse Source

[1959] Moved sockets_ member in IfaceMgr to protected scope.

Marcin Siodelski 12 years ago
parent
commit
4ae9b5ea74
2 changed files with 43 additions and 22 deletions
  1. 26 20
      src/lib/dhcp/iface_mgr.cc
  2. 17 2
      src/lib/dhcp/iface_mgr.h

+ 26 - 20
src/lib/dhcp/iface_mgr.cc

@@ -66,6 +66,16 @@ IfaceMgr::Iface::Iface(const std::string& name, int ifindex)
     memset(mac_, 0, sizeof(mac_));
 }
 
+void
+IfaceMgr::Iface::closeSockets() {
+    for (SocketCollection::iterator sock = sockets_.begin();
+         sock != sockets_.end(); ++sock) {
+        cout << "Closing socket " << sock->sockfd_ << endl;
+        close(sock->sockfd_);
+    }
+    sockets_.clear();
+}
+
 std::string
 IfaceMgr::Iface::getFullName() const {
     ostringstream tmp;
@@ -154,15 +164,8 @@ IfaceMgr::IfaceMgr()
 void IfaceMgr::closeSockets() {
     for (IfaceCollection::iterator iface = ifaces_.begin();
          iface != ifaces_.end(); ++iface) {
-
-        for (SocketCollection::iterator sock = iface->sockets_.begin();
-             sock != iface->sockets_.end(); ++sock) {
-            cout << "Closing socket " << sock->sockfd_ << endl;
-            close(sock->sockfd_);
-        }
-        iface->sockets_.clear();
+        iface->closeSockets();
     }
-
 }
 
 IfaceMgr::~IfaceMgr() {
@@ -561,8 +564,7 @@ int IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, uint16_t port) {
     struct sockaddr_in6 addr6;
     memset(&addr6, 0, sizeof(addr6));
     addr6.sin6_family = AF_INET6;
-    addr6.sin6_port = htons(port);
-    if (addr.toText() != "::1")
+    addr6.sin6_port = htons(port);    if (addr.toText() != "::1")
       addr6.sin6_scope_id = if_nametoindex(iface.getName().c_str());
 
     memcpy(&addr6.sin6_addr,
@@ -740,7 +742,6 @@ IfaceMgr::send(const Pkt6Ptr& pkt) {
 bool
 IfaceMgr::send(const Pkt4Ptr& pkt)
 {
-
     Iface* iface = getIface(pkt->getIface());
     if (!iface) {
         isc_throw(BadValue, "Unable to send Pkt4. Invalid interface ("
@@ -817,8 +818,9 @@ IfaceMgr::receive4(uint32_t timeout) {
     /// provided set to indicated which sockets have something to read.
     for (iface = ifaces_.begin(); iface != ifaces_.end(); ++iface) {
 
-        for (SocketCollection::const_iterator s = iface->sockets_.begin();
-             s != iface->sockets_.end(); ++s) {
+        const SocketCollection& socket_collection = iface->getSockets();
+        for (SocketCollection::const_iterator s = socket_collection.begin();
+             s != socket_collection.end(); ++s) {
 
             // Only deal with IPv4 addresses.
             if (s->addr_.getFamily() == AF_INET) {
@@ -881,8 +883,9 @@ IfaceMgr::receive4(uint32_t timeout) {
 
     // Let's find out which interface/socket has the data
     for (iface = ifaces_.begin(); iface != ifaces_.end(); ++iface) {
-        for (SocketCollection::const_iterator s = iface->sockets_.begin();
-             s != iface->sockets_.end(); ++s) {
+        const SocketCollection& socket_collection = iface->getSockets();
+        for (SocketCollection::const_iterator s = socket_collection.begin();
+             s != socket_collection.end(); ++s) {
             if (FD_ISSET(s->sockfd_, &sockets)) {
                 candidate = &(*s);
                 break;
@@ -1010,8 +1013,9 @@ Pkt6Ptr IfaceMgr::receive6() {
     IfaceCollection::const_iterator iface = ifaces_.begin();
     const SocketInfo* candidate = 0;
     while (iface != ifaces_.end()) {
-        for (SocketCollection::const_iterator s = iface->sockets_.begin();
-             s != iface->sockets_.end(); ++s) {
+        const SocketCollection& socket_collection = iface->getSockets();
+        for (SocketCollection::const_iterator s = socket_collection.begin();
+             s != socket_collection.end(); ++s) {
             if (s->addr_.getFamily() != AF_INET6) {
                 continue;
             }
@@ -1122,11 +1126,12 @@ uint16_t IfaceMgr::getSocket(const isc::dhcp::Pkt6& pkt) {
                   << pkt.getIface());
     }
 
+    const SocketCollection& socket_collection = iface->getSockets();
     SocketCollection::const_iterator s;
-    for (s = iface->sockets_.begin(); s != iface->sockets_.end(); ++s) {
+    for (s = socket_collection.begin(); s != socket_collection.end(); ++s) {
         if ((s->family_ == AF_INET6) &&
             (!s->addr_.getAddress().to_v6().is_multicast())) {
-            return (s->sockfd_);
+            return (s->sockfd_);t
         }
         /// @todo: Add more checks here later. If remote address is
         /// not link-local, we can't use link local bound socket
@@ -1144,8 +1149,9 @@ uint16_t IfaceMgr::getSocket(isc::dhcp::Pkt4 const& pkt) {
                   << pkt.getIface());
     }
 
+    const SocketCollection& socket_collection = iface->getSockets();
     SocketCollection::const_iterator s;
-    for (s = iface->sockets_.begin(); s != iface->sockets_.end(); ++s) {
+    for (s = socket_collection.begin(); s != socket_collection.end(); ++s) {
         if (s->family_ == AF_INET) {
             return (s->sockfd_);
         }

+ 17 - 2
src/lib/dhcp/iface_mgr.h

@@ -89,6 +89,9 @@ public:
         /// @param ifindex interface index (unique integer identifier)
         Iface(const std::string& name, int ifindex);
 
+        /// @brief Closes all open sockets on interface
+        void closeSockets();
+
         /// @brief Returns full interface name as "ifname/ifindex" string.
         ///
         /// @return string with interface name
@@ -192,11 +195,23 @@ public:
         /// @return true if there was such socket, false otherwise
         bool delSocket(uint16_t sockfd);
 
+        /// @brief Returns collection of all sockets added to interface.
+        ///
+        /// When new socket is created with @ref IfaceMgr::openSocket
+        /// it is added to sockets collection on particular interface.
+        /// If socket is opened by other means (e.g. function that does
+        /// not use @ref IfaceMgr::openSocket) it will not be available
+        /// in this collection. Note that functions like
+        /// @ref IfaceMgr::openSocketFromIface use
+        /// @ref IfaceMgr::openSocket internally.
+        ///
+        /// @return collection of sockets added to interface
+        const SocketCollection& getSockets() const { return sockets_; }
+
+    protected:
         /// socket used to sending data
-        /// TODO: this should be protected
         SocketCollection sockets_;
 
-    protected:
         /// network interface name
         std::string name_;