Browse Source

[1957] Added new open socket methods.

Marcin Siodelski 13 years ago
parent
commit
c4901d0f8f
2 changed files with 82 additions and 0 deletions
  1. 73 0
      src/lib/dhcp/iface_mgr.cc
  2. 9 0
      src/lib/dhcp/iface_mgr.h

+ 73 - 0
src/lib/dhcp/iface_mgr.cc

@@ -23,6 +23,7 @@
 #include <dhcp/dhcp6.h>
 #include <dhcp/iface_mgr.h>
 #include <exceptions/exceptions.h>
+#include <asiolink/io_error.h>
 #include <util/io/pktinfo_utilities.h>
 
 using namespace std;
@@ -393,6 +394,78 @@ int IfaceMgr::openSocket(const std::string& ifname, const IOAddress& addr,
     }
 }
 
+int IfaceMgr::openSocketFromIface(const std::string& ifname, const uint16_t port, const uint8_t family) {
+    int sock = 0;
+    for (IfaceCollection::iterator iface=ifaces_.begin();
+         iface!=ifaces_.end();
+         ++iface) {
+
+        if ((iface->getFullName() != ifname) &&
+            (iface->getName() != ifname)) {
+            continue;
+        }
+
+        AddressCollection addrs = iface->getAddresses();
+
+        for (AddressCollection::iterator addr= addrs.begin();
+             addr != addrs.end();
+             ++addr) {
+
+            // skip IPv4 addresses
+            if (addr->getFamily() != family) {
+                continue;
+            }
+
+            sock = openSocket(iface->getName(), *addr, port);
+            if (sock<0) {
+                cout << "Failed to open unicast socket." << endl;
+            }
+            return (sock);
+        }
+    }
+    return (sock);
+}
+
+int IfaceMgr::openSocketFromAddr(const std::string& addr_name, const uint16_t port) {
+    int sock = 0;
+    for (IfaceCollection::iterator iface=ifaces_.begin();
+         iface!=ifaces_.end();
+         ++iface) {
+
+        AddressCollection addrs = iface->getAddresses();
+
+        for (AddressCollection::iterator addr = addrs.begin();
+             addr != addrs.end();
+             ++addr) {
+
+            try {
+                IOAddress searched_addr(addr_name);
+                if (addr->getAddress() != searched_addr.getAddress()) {
+                    continue;
+                }
+            } catch (const isc::asiolink::IOError& e) {
+                cout << "Failed to open socket from address: " << e.what() << endl;
+                return (sock);
+            }
+
+            sock = openSocket(iface->getName(), *addr, port);
+            if (sock<0) {
+                cout << "Failed to open unicast socket." << endl;
+            }
+            return (sock);
+        }
+    }
+    return (sock);
+    
+}
+
+    /*int IfaceMgr::openSocketFromRemoteAddr(const std::string& remote_addr_name,
+                                       const uint16_t port,
+                                       const uint8_t family) {
+    return 0;
+    }*/
+
+
 int IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, uint16_t port) {
 
     cout << "Creating UDP4 socket on " << iface.getFullName()

+ 9 - 0
src/lib/dhcp/iface_mgr.h

@@ -375,6 +375,15 @@ public:
     int openSocket(const std::string& ifname,
                    const isc::asiolink::IOAddress& addr, const uint16_t port);
 
+    int openSocketFromIface(const std::string& ifname, const uint16_t port,
+                            const uint8_t family);
+
+    int openSocketFromAddr(const std::string& addr_name, const uint16_t port);
+
+    int openSocketFromRemoteAddr(const std::string& remote_addr_name,
+                                 const uint16_t port,
+                                 const uint8_t family);
+
     /// Opens IPv6 sockets on detected interfaces.
     ///
     /// Will throw exception if socket creation fails.