|
@@ -23,190 +23,201 @@
|
|
|
|
|
|
namespace isc {
|
|
|
|
|
|
- namespace dhcp {
|
|
|
- /// @brief handles network interfaces, transmission and reception
|
|
|
+namespace dhcp {
|
|
|
+/// @brief handles network interfaces, transmission and reception
|
|
|
+///
|
|
|
+/// IfaceMgr is an interface manager class that detects available network
|
|
|
+/// interfaces, configured addresses, link-local addresses, and provides
|
|
|
+/// API for using sockets.
|
|
|
+///
|
|
|
+class IfaceMgr : public boost::noncopyable {
|
|
|
+public:
|
|
|
+ /// type that defines list of addresses
|
|
|
+ typedef std::list<isc::asiolink::IOAddress> Addr6Lst;
|
|
|
+
|
|
|
+ /// maximum MAC address length (Infiniband uses 20 bytes)
|
|
|
+ static const unsigned int MAX_MAC_LEN = 20;
|
|
|
+
|
|
|
+ /// @brief represents a single network interface
|
|
|
///
|
|
|
- /// IfaceMgr is an interface manager class that detects available network
|
|
|
- /// interfaces, configured addresses, link-local addresses, and provides
|
|
|
- /// API for using sockets.
|
|
|
- ///
|
|
|
- class IfaceMgr : public boost::noncopyable {
|
|
|
- public:
|
|
|
- /// type that defines list of addresses
|
|
|
- typedef std::list<isc::asiolink::IOAddress> Addr6Lst;
|
|
|
+ /// Iface structure represents network interface with all useful
|
|
|
+ /// information, like name, interface index, MAC address and
|
|
|
+ /// list of assigned addresses
|
|
|
+ struct Iface {
|
|
|
+ /// constructor
|
|
|
+ Iface(const std::string& name, int ifindex);
|
|
|
+
|
|
|
+ /// returns full interface name in format ifname/ifindex
|
|
|
+ std::string getFullName() const;
|
|
|
+
|
|
|
+ /// returns link-layer address a plain text
|
|
|
+ std::string getPlainMac() const;
|
|
|
|
|
|
- /// maximum MAC address length (Infiniband uses 20 bytes)
|
|
|
- static const unsigned int MAX_MAC_LEN = 20;
|
|
|
+ /// network interface name
|
|
|
+ std::string name_;
|
|
|
+
|
|
|
+ /// interface index (a value that uniquely indentifies an interface)
|
|
|
+ int ifindex_;
|
|
|
|
|
|
- /// @brief represents a single network interface
|
|
|
- ///
|
|
|
- /// Iface structure represents network interface with all useful
|
|
|
- /// information, like name, interface index, MAC address and
|
|
|
/// list of assigned addresses
|
|
|
- struct Iface {
|
|
|
- /// constructor
|
|
|
- Iface(const std::string& name, int ifindex);
|
|
|
-
|
|
|
- /// returns full interface name in format ifname/ifindex
|
|
|
- std::string getFullName() const;
|
|
|
-
|
|
|
- /// returns link-layer address a plain text
|
|
|
- std::string getPlainMac() const;
|
|
|
-
|
|
|
- std::string name_; /// network interface name
|
|
|
- int ifindex_; /// interface index (a value that uniquely
|
|
|
- /// indentifies an interface
|
|
|
- Addr6Lst addrs_; /// list of assigned addresses
|
|
|
- uint8_t mac_[MAX_MAC_LEN]; /// link-layer address
|
|
|
- int mac_len_; /// length of link-layer address (usually 6)
|
|
|
-
|
|
|
- int sendsock_; /// socket used to sending data
|
|
|
- int recvsock_; /// socket used for receiving data
|
|
|
-
|
|
|
- // next field is not needed, let's keep it in cointainers
|
|
|
- };
|
|
|
-
|
|
|
- // TODO performance improvement: we may change this into
|
|
|
- // 2 maps (ifindex-indexed and name-indexed) and
|
|
|
- // also hide it (make it public make tests easier for now)
|
|
|
- typedef std::list<Iface> IfaceLst;
|
|
|
-
|
|
|
- /// IfaceMgr is a singleton class. This method returns reference
|
|
|
- /// to its sole instance.
|
|
|
- ///
|
|
|
- /// @return the only existing instance of interface manager
|
|
|
- static IfaceMgr& instance();
|
|
|
-
|
|
|
- /// @brief Returns interface with specified interface index
|
|
|
- ///
|
|
|
- /// @param ifindex index of searched interface
|
|
|
- ///
|
|
|
- /// @return interface with requested index (or NULL if no such
|
|
|
- /// interface is present)
|
|
|
- ///
|
|
|
- Iface*
|
|
|
- getIface(int ifindex);
|
|
|
-
|
|
|
- /// @brief Returns interface with specified interface name
|
|
|
- ///
|
|
|
- /// @param ifname name of searched interface
|
|
|
- ///
|
|
|
- /// @return interface with requested name (or NULL if no such
|
|
|
- /// interface is present)
|
|
|
- ///
|
|
|
- Iface*
|
|
|
- getIface(const std::string& ifname);
|
|
|
-
|
|
|
- /// debugging method that prints out all available interfaces
|
|
|
- ///
|
|
|
- /// @param out specifies stream to print list of interfaces to
|
|
|
- void
|
|
|
- printIfaces(std::ostream& out = std::cout);
|
|
|
-
|
|
|
- /// @brief Sends a packet.
|
|
|
- ///
|
|
|
- /// Sends a packet. All parameters regarding interface, destination
|
|
|
- /// address are set in pkt object.
|
|
|
- ///
|
|
|
- /// @param pkt packet to be sent
|
|
|
- ///
|
|
|
- /// @return true if sending was successful
|
|
|
- ///
|
|
|
- bool
|
|
|
- send(boost::shared_ptr<Pkt6> pkt);
|
|
|
-
|
|
|
- /// @brief Tries to receive packet over open sockets.
|
|
|
- ///
|
|
|
- /// Attempts to receive a single packet of any of the open sockets.
|
|
|
- /// If reception is successful and all information about its sender
|
|
|
- /// are obtained, Pkt6 object is created and returned.
|
|
|
- ///
|
|
|
- /// @return Pkt6 object representing received packet (or NULL)
|
|
|
- ///
|
|
|
- boost::shared_ptr<Pkt6> receive();
|
|
|
-
|
|
|
- // don't use private, we need derived classes in tests
|
|
|
- protected:
|
|
|
-
|
|
|
- /// @brief Protected constructor.
|
|
|
- ///
|
|
|
- /// Protected constructor. This is a singleton class. We don't want
|
|
|
- /// anyone to create instances of IfaceMgr. Use instance() method
|
|
|
- IfaceMgr();
|
|
|
-
|
|
|
- ~IfaceMgr();
|
|
|
-
|
|
|
- /// @brief Detects network interfaces.
|
|
|
- ///
|
|
|
- /// This method will eventually detect available interfaces. For now
|
|
|
- /// it offers stub implementation. First interface name and link-local
|
|
|
- /// IPv6 address is read from intefaces.txt file.
|
|
|
- void
|
|
|
- detectIfaces();
|
|
|
-
|
|
|
- ///
|
|
|
- /// Opens UDP/IPv6 socket and binds it to address, interface nad port.
|
|
|
- ///
|
|
|
- /// @param ifname name of the interface
|
|
|
- /// @param addr address to be bound.
|
|
|
- /// @param port UDP port.
|
|
|
- ///
|
|
|
- /// @return socket descriptor, if socket creation, binding and multicast
|
|
|
- /// group join were all successful. -1 otherwise.
|
|
|
- int openSocket(const std::string& ifname,
|
|
|
- const isc::asiolink::IOAddress& addr,
|
|
|
- int port);
|
|
|
-
|
|
|
- // TODO: having 2 maps (ifindex->iface and ifname->iface would)
|
|
|
- // probably be better for performance reasons
|
|
|
-
|
|
|
- /// List of available interfaces
|
|
|
- IfaceLst ifaces_;
|
|
|
-
|
|
|
- /// a pointer to a sole instance of this class (a singleton)
|
|
|
- static IfaceMgr * instance_;
|
|
|
-
|
|
|
- // TODO: Also keep this interface on Iface once interface detection
|
|
|
- // is implemented. We may need it e.g. to close all sockets on
|
|
|
- // specific interface
|
|
|
- int recvsock_; // TODO: should be fd_set eventually, but we have only
|
|
|
- int sendsock_; // 2 sockets for now. Will do for until next release
|
|
|
- // we can't use the same socket, as receiving socket
|
|
|
- // is bound to multicast address. And we all know what happens
|
|
|
- // to people who try to use multicast as source address.
|
|
|
-
|
|
|
- /// control-buffer, used in transmission and reception
|
|
|
- char * control_buf_;
|
|
|
- int control_buf_len_;
|
|
|
-
|
|
|
- private:
|
|
|
- /// Opens sockets on detected interfaces.
|
|
|
- bool
|
|
|
- openSockets();
|
|
|
-
|
|
|
- /// creates a single instance of this class (a singleton implementation)
|
|
|
- static void
|
|
|
- instanceCreate();
|
|
|
-
|
|
|
- /// @brief Joins IPv6 multicast group on a socket.
|
|
|
- ///
|
|
|
- /// Socket must be created and bound to an address. Note that this
|
|
|
- /// address is different than the multicast address. For example DHCPv6
|
|
|
- /// server should bind its socket to link-local address (fe80::1234...)
|
|
|
- /// and later join ff02::1:2 multicast group.
|
|
|
- ///
|
|
|
- /// @param sock socket fd (socket must be bound)
|
|
|
- /// @param ifname interface name (for link-scoped multicast groups)
|
|
|
- /// @param mcast multicast address to join (e.g. "ff02::1:2")
|
|
|
- ///
|
|
|
- /// @return true if multicast join was successful
|
|
|
- ///
|
|
|
- bool
|
|
|
- joinMcast(int sock, const std::string& ifname,
|
|
|
- const std::string& mcast);
|
|
|
+ Addr6Lst addrs_;
|
|
|
+
|
|
|
+ /// link-layer address
|
|
|
+ uint8_t mac_[MAX_MAC_LEN];
|
|
|
+
|
|
|
+ /// length of link-layer address (usually 6)
|
|
|
+ int mac_len_;
|
|
|
+
|
|
|
+ /// socket used to sending data
|
|
|
+ int sendsock_;
|
|
|
+
|
|
|
+ /// socket used for receiving data
|
|
|
+ int recvsock_;
|
|
|
};
|
|
|
|
|
|
- }; // namespace isc::dhcp
|
|
|
+ // TODO performance improvement: we may change this into
|
|
|
+ // 2 maps (ifindex-indexed and name-indexed) and
|
|
|
+ // also hide it (make it public make tests easier for now)
|
|
|
+
|
|
|
+ /// type that holds a list of interfaces
|
|
|
+ typedef std::list<Iface> IfaceLst;
|
|
|
+
|
|
|
+ /// IfaceMgr is a singleton class. This method returns reference
|
|
|
+ /// to its sole instance.
|
|
|
+ ///
|
|
|
+ /// @return the only existing instance of interface manager
|
|
|
+ static IfaceMgr& instance();
|
|
|
+
|
|
|
+ /// @brief Returns interface with specified interface index
|
|
|
+ ///
|
|
|
+ /// @param ifindex index of searched interface
|
|
|
+ ///
|
|
|
+ /// @return interface with requested index (or NULL if no such
|
|
|
+ /// interface is present)
|
|
|
+ ///
|
|
|
+ Iface*
|
|
|
+ getIface(int ifindex);
|
|
|
+
|
|
|
+ /// @brief Returns interface with specified interface name
|
|
|
+ ///
|
|
|
+ /// @param ifname name of searched interface
|
|
|
+ ///
|
|
|
+ /// @return interface with requested name (or NULL if no such
|
|
|
+ /// interface is present)
|
|
|
+ ///
|
|
|
+ Iface*
|
|
|
+ getIface(const std::string& ifname);
|
|
|
+
|
|
|
+ /// debugging method that prints out all available interfaces
|
|
|
+ ///
|
|
|
+ /// @param out specifies stream to print list of interfaces to
|
|
|
+ void
|
|
|
+ printIfaces(std::ostream& out = std::cout);
|
|
|
+
|
|
|
+ /// @brief Sends a packet.
|
|
|
+ ///
|
|
|
+ /// Sends a packet. All parameters regarding interface, destination
|
|
|
+ /// address are set in pkt object.
|
|
|
+ ///
|
|
|
+ /// @param pkt packet to be sent
|
|
|
+ ///
|
|
|
+ /// @return true if sending was successful
|
|
|
+ ///
|
|
|
+ bool
|
|
|
+ send(boost::shared_ptr<Pkt6> pkt);
|
|
|
+
|
|
|
+ /// @brief Tries to receive packet over open sockets.
|
|
|
+ ///
|
|
|
+ /// Attempts to receive a single packet of any of the open sockets.
|
|
|
+ /// If reception is successful and all information about its sender
|
|
|
+ /// are obtained, Pkt6 object is created and returned.
|
|
|
+ ///
|
|
|
+ /// @return Pkt6 object representing received packet (or NULL)
|
|
|
+ ///
|
|
|
+ boost::shared_ptr<Pkt6> receive();
|
|
|
+
|
|
|
+ // don't use private, we need derived classes in tests
|
|
|
+protected:
|
|
|
+
|
|
|
+ /// @brief Protected constructor.
|
|
|
+ ///
|
|
|
+ /// Protected constructor. This is a singleton class. We don't want
|
|
|
+ /// anyone to create instances of IfaceMgr. Use instance() method
|
|
|
+ IfaceMgr();
|
|
|
+
|
|
|
+ ~IfaceMgr();
|
|
|
+
|
|
|
+ /// @brief Detects network interfaces.
|
|
|
+ ///
|
|
|
+ /// This method will eventually detect available interfaces. For now
|
|
|
+ /// it offers stub implementation. First interface name and link-local
|
|
|
+ /// IPv6 address is read from intefaces.txt file.
|
|
|
+ void
|
|
|
+ detectIfaces();
|
|
|
+
|
|
|
+ ///
|
|
|
+ /// Opens UDP/IPv6 socket and binds it to address, interface and port.
|
|
|
+ ///
|
|
|
+ /// @param ifname name of the interface
|
|
|
+ /// @param addr address to be bound.
|
|
|
+ /// @param port UDP port.
|
|
|
+ ///
|
|
|
+ /// @return socket descriptor, if socket creation, binding and multicast
|
|
|
+ /// group join were all successful. -1 otherwise.
|
|
|
+ int openSocket(const std::string& ifname,
|
|
|
+ const isc::asiolink::IOAddress& addr,
|
|
|
+ int port);
|
|
|
+
|
|
|
+ // TODO: having 2 maps (ifindex->iface and ifname->iface would)
|
|
|
+ // probably be better for performance reasons
|
|
|
+
|
|
|
+ /// List of available interfaces
|
|
|
+ IfaceLst ifaces_;
|
|
|
+
|
|
|
+ /// a pointer to a sole instance of this class (a singleton)
|
|
|
+ static IfaceMgr * instance_;
|
|
|
+
|
|
|
+ // TODO: Also keep this interface on Iface once interface detection
|
|
|
+ // is implemented. We may need it e.g. to close all sockets on
|
|
|
+ // specific interface
|
|
|
+ int recvsock_; // TODO: should be fd_set eventually, but we have only
|
|
|
+ int sendsock_; // 2 sockets for now. Will do for until next release
|
|
|
+ // we can't use the same socket, as receiving socket
|
|
|
+ // is bound to multicast address. And we all know what happens
|
|
|
+ // to people who try to use multicast as source address.
|
|
|
+
|
|
|
+ /// control-buffer, used in transmission and reception
|
|
|
+ char * control_buf_;
|
|
|
+ int control_buf_len_;
|
|
|
+
|
|
|
+private:
|
|
|
+ /// Opens sockets on detected interfaces.
|
|
|
+ bool
|
|
|
+ openSockets();
|
|
|
+
|
|
|
+ /// creates a single instance of this class (a singleton implementation)
|
|
|
+ static void
|
|
|
+ instanceCreate();
|
|
|
+
|
|
|
+ /// @brief Joins IPv6 multicast group on a socket.
|
|
|
+ ///
|
|
|
+ /// Socket must be created and bound to an address. Note that this
|
|
|
+ /// address is different than the multicast address. For example DHCPv6
|
|
|
+ /// server should bind its socket to link-local address (fe80::1234...)
|
|
|
+ /// and later join ff02::1:2 multicast group.
|
|
|
+ ///
|
|
|
+ /// @param sock socket fd (socket must be bound)
|
|
|
+ /// @param ifname interface name (for link-scoped multicast groups)
|
|
|
+ /// @param mcast multicast address to join (e.g. "ff02::1:2")
|
|
|
+ ///
|
|
|
+ /// @return true if multicast join was successful
|
|
|
+ ///
|
|
|
+ bool
|
|
|
+ joinMcast(int sock, const std::string& ifname,
|
|
|
+ const std::string& mcast);
|
|
|
+};
|
|
|
+
|
|
|
+}; // namespace isc::dhcp
|
|
|
}; // namespace isc
|
|
|
|
|
|
#endif
|