|
@@ -18,9 +18,9 @@
|
|
|
#include <netinet/in.h>
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
-#include "dhcp/dhcp6.h"
|
|
|
-#include "dhcp6/iface_mgr.h"
|
|
|
-#include "exceptions/exceptions.h"
|
|
|
+#include <dhcp/dhcp6.h>
|
|
|
+#include <dhcp6/iface_mgr.h>
|
|
|
+#include <exceptions/exceptions.h>
|
|
|
|
|
|
using namespace std;
|
|
|
using namespace isc;
|
|
@@ -79,6 +79,30 @@ IfaceMgr::Iface::getPlainMac() const {
|
|
|
return (tmp.str());
|
|
|
}
|
|
|
|
|
|
+bool IfaceMgr::Iface::delAddress(const isc::asiolink::IOAddress& addr) {
|
|
|
+
|
|
|
+ // Let's delete all addresses that match. It really shouldn't matter
|
|
|
+ // if we delete first or all, as the OS should allow to add a single
|
|
|
+ // address to an interface only once. If OS allows multiple instances
|
|
|
+ // of the same address added, we are in deep problems anyway.
|
|
|
+ size_t size = addrs_.size();
|
|
|
+ addrs_.erase(remove(addrs_.begin(), addrs_.end(), addr), addrs_.end());
|
|
|
+ return (addrs_.size() < size);
|
|
|
+}
|
|
|
+
|
|
|
+bool IfaceMgr::Iface::delSocket(uint16_t sockfd) {
|
|
|
+ list<SocketInfo>::iterator sock = sockets_.begin();
|
|
|
+ while (sock!=sockets_.end()) {
|
|
|
+ if (sock->sockfd_ == sockfd) {
|
|
|
+ close(sockfd);
|
|
|
+ sockets_.erase(sock);
|
|
|
+ return (true); //socket found
|
|
|
+ }
|
|
|
+ ++sock;
|
|
|
+ }
|
|
|
+ return (false); // socket not found
|
|
|
+}
|
|
|
+
|
|
|
IfaceMgr::IfaceMgr()
|
|
|
:control_buf_len_(CMSG_SPACE(sizeof(struct in6_pktinfo))),
|
|
|
control_buf_(new char[control_buf_len_])
|
|
@@ -95,9 +119,6 @@ IfaceMgr::IfaceMgr()
|
|
|
|
|
|
detectIfaces();
|
|
|
|
|
|
- if (!openSockets()) {
|
|
|
- isc_throw(Unexpected, "Failed to open/bind sockets.");
|
|
|
- }
|
|
|
} catch (const std::exception& ex) {
|
|
|
cout << "IfaceMgr creation failed:" << ex.what() << endl;
|
|
|
|
|
@@ -109,7 +130,23 @@ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
IfaceMgr::~IfaceMgr() {
|
|
|
+ closeSockets();
|
|
|
+
|
|
|
// control_buf_ is deleted automatically (scoped_ptr)
|
|
|
control_buf_len_ = 0;
|
|
|
}
|
|
@@ -139,8 +176,8 @@ IfaceMgr::detectIfaces() {
|
|
|
|
|
|
Iface iface(ifaceName, if_nametoindex( ifaceName.c_str() ) );
|
|
|
IOAddress addr(linkLocal);
|
|
|
- iface.addrs_.push_back(addr);
|
|
|
- ifaces_.push_back(iface);
|
|
|
+ iface.addAddress(addr);
|
|
|
+ addInterface(iface);
|
|
|
interfaces.close();
|
|
|
} catch (const std::exception& ex) {
|
|
|
// TODO: deallocate whatever memory we used
|
|
@@ -154,51 +191,55 @@ IfaceMgr::detectIfaces() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-bool
|
|
|
-IfaceMgr::openSockets() {
|
|
|
- int sock;
|
|
|
+void
|
|
|
+IfaceMgr::openSockets(uint16_t port) {
|
|
|
+ int sock1, sock2;
|
|
|
+
|
|
|
+ for (IfaceCollection::iterator iface = ifaces_.begin();
|
|
|
+ iface != ifaces_.end(); ++iface) {
|
|
|
|
|
|
- for (IfaceLst::iterator iface=ifaces_.begin();
|
|
|
- iface!=ifaces_.end();
|
|
|
- ++iface) {
|
|
|
+ AddressCollection addrs = iface->getAddresses();
|
|
|
|
|
|
- for (Addr6Lst::iterator addr=iface->addrs_.begin();
|
|
|
- addr!=iface->addrs_.end();
|
|
|
+ for (AddressCollection::iterator addr = addrs.begin();
|
|
|
+ addr != addrs.end();
|
|
|
++addr) {
|
|
|
|
|
|
- sock = openSocket(iface->name_, *addr,
|
|
|
- DHCP6_SERVER_PORT);
|
|
|
- if (sock<0) {
|
|
|
- cout << "Failed to open unicast socket." << endl;
|
|
|
- return (false);
|
|
|
+ sock1 = openSocket(iface->getName(), *addr, port);
|
|
|
+ if (sock1 < 0) {
|
|
|
+ isc_throw(Unexpected, "Failed to open unicast socket on "
|
|
|
+ << " interface " << iface->getFullName());
|
|
|
}
|
|
|
- sendsock_ = sock;
|
|
|
-
|
|
|
- sock = openSocket(iface->name_,
|
|
|
- IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
|
|
|
- DHCP6_SERVER_PORT);
|
|
|
- if (sock<0) {
|
|
|
- cout << "Failed to open multicast socket." << endl;
|
|
|
- close(sendsock_);
|
|
|
- return (false);
|
|
|
+
|
|
|
+ if ( !joinMcast(sock1, iface->getName(),
|
|
|
+ string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {
|
|
|
+ close(sock1);
|
|
|
+ isc_throw(Unexpected, "Failed to join " << ALL_DHCP_RELAY_AGENTS_AND_SERVERS
|
|
|
+ << " multicast group.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // this doesn't work too well on NetBSD
|
|
|
+ sock2 = openSocket(iface->getName(),
|
|
|
+ IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
|
|
|
+ port);
|
|
|
+ if (sock2 < 0) {
|
|
|
+ isc_throw(Unexpected, "Failed to open multicast socket on "
|
|
|
+ << " interface " << iface->getFullName());
|
|
|
+ iface->delSocket(sock1); // delete previously opened socket
|
|
|
}
|
|
|
- recvsock_ = sock;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- return (true);
|
|
|
}
|
|
|
|
|
|
void
|
|
|
IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) {
|
|
|
- for (IfaceLst::const_iterator iface=ifaces_.begin();
|
|
|
- iface!=ifaces_.end();
|
|
|
- ++iface) {
|
|
|
+ for (IfaceCollection::const_iterator iface = ifaces_.begin();
|
|
|
+ iface != ifaces_.end(); ++iface) {
|
|
|
out << "Detected interface " << iface->getFullName() << endl;
|
|
|
- out << " " << iface->addrs_.size() << " addr(s):" << endl;
|
|
|
- for (Addr6Lst::const_iterator addr=iface->addrs_.begin();
|
|
|
- addr != iface->addrs_.end();
|
|
|
- ++addr) {
|
|
|
+ out << " " << iface->getAddresses().size() << " addr(s):" << endl;
|
|
|
+ const AddressCollection addrs = iface->getAddresses();
|
|
|
+
|
|
|
+ for (AddressCollection::const_iterator addr = addrs.begin();
|
|
|
+ addr != addrs.end(); ++addr) {
|
|
|
out << " " << addr->toText() << endl;
|
|
|
}
|
|
|
out << " mac: " << iface->getPlainMac() << endl;
|
|
@@ -207,11 +248,11 @@ IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) {
|
|
|
|
|
|
IfaceMgr::Iface*
|
|
|
IfaceMgr::getIface(int ifindex) {
|
|
|
- for (IfaceLst::iterator iface=ifaces_.begin();
|
|
|
- iface!=ifaces_.end();
|
|
|
- ++iface) {
|
|
|
- if (iface->ifindex_ == ifindex)
|
|
|
+ for (IfaceCollection::iterator iface = ifaces_.begin();
|
|
|
+ iface != ifaces_.end(); ++iface) {
|
|
|
+ if (iface->getIndex() == ifindex) {
|
|
|
return (&(*iface));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return (NULL); // not found
|
|
@@ -219,29 +260,87 @@ IfaceMgr::getIface(int ifindex) {
|
|
|
|
|
|
IfaceMgr::Iface*
|
|
|
IfaceMgr::getIface(const std::string& ifname) {
|
|
|
- for (IfaceLst::iterator iface=ifaces_.begin();
|
|
|
- iface!=ifaces_.end();
|
|
|
- ++iface) {
|
|
|
- if (iface->name_ == ifname)
|
|
|
+ for (IfaceCollection::iterator iface = ifaces_.begin();
|
|
|
+ iface != ifaces_.end(); ++iface) {
|
|
|
+ if (iface->getName() == ifname) {
|
|
|
return (&(*iface));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return (NULL); // not found
|
|
|
}
|
|
|
|
|
|
int
|
|
|
-IfaceMgr::openSocket(const std::string& ifname,
|
|
|
- const IOAddress& addr,
|
|
|
+IfaceMgr::openSocket(const std::string& ifname, const IOAddress& addr,
|
|
|
int port) {
|
|
|
- struct sockaddr_in6 addr6;
|
|
|
+ Iface* iface = getIface(ifname);
|
|
|
+ if (!iface) {
|
|
|
+ isc_throw(BadValue, "There is no " << ifname << " interface present.");
|
|
|
+ }
|
|
|
+ switch (addr.getFamily()) {
|
|
|
+ case AF_INET:
|
|
|
+ return openSocket4(*iface, addr, port);
|
|
|
+ case AF_INET6:
|
|
|
+ return openSocket6(*iface, addr, port);
|
|
|
+ default:
|
|
|
+ isc_throw(BadValue, "Failed to detect family of address: "
|
|
|
+ << addr.toText());
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int
|
|
|
+IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
|
|
|
+
|
|
|
+ cout << "Creating UDP4 socket on " << iface.getFullName()
|
|
|
+ << " " << addr.toText() << "/port=" << port << endl;
|
|
|
+
|
|
|
+ struct sockaddr_in addr4;
|
|
|
+ memset(&addr4, 0, sizeof(sockaddr));
|
|
|
+ addr4.sin_family = AF_INET;
|
|
|
+ addr4.sin_port = htons(port);
|
|
|
+ memcpy(&addr4.sin_addr, addr.getAddress().to_v4().to_bytes().data(),
|
|
|
+ sizeof(addr4.sin_addr));
|
|
|
+
|
|
|
+ int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
+ if (sock < 0) {
|
|
|
+ isc_throw(Unexpected, "Failed to create UDP6 socket.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bind(sock, (struct sockaddr *)&addr4, sizeof(addr4)) < 0) {
|
|
|
+ close(sock);
|
|
|
+ isc_throw(Unexpected, "Failed to bind socket " << sock << " to " << addr.toText()
|
|
|
+ << "/port=" << port);
|
|
|
+ }
|
|
|
+
|
|
|
+ // If there is no support for IP_PKTINFO, we are really out of luck.
|
|
|
+ // It will be difficult to understand, where this packet came from.
|
|
|
+#if defined(IP_PKTINFO)
|
|
|
+ int flag = 1;
|
|
|
+ if (setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &flag, sizeof(flag)) != 0) {
|
|
|
+ close(sock);
|
|
|
+ isc_throw(Unexpected, "setsockopt: IP_PKTINFO: failed.");
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ cout << "Created socket " << sock << " on " << iface.getName() << "/" <<
|
|
|
+ addr.toText() << "/port=" << port << endl;
|
|
|
|
|
|
- cout << "Creating socket on " << ifname << "/" << addr.toText()
|
|
|
- << "/port=" << port << endl;
|
|
|
+ iface.addSocket(SocketInfo(sock, addr, port));
|
|
|
|
|
|
+ return (sock);
|
|
|
+}
|
|
|
+
|
|
|
+int
|
|
|
+IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
|
|
|
+
|
|
|
+ cout << "Creating UDP6 socket on " << iface.getFullName()
|
|
|
+ << " " << addr.toText() << "/port=" << port << endl;
|
|
|
+
|
|
|
+ struct sockaddr_in6 addr6;
|
|
|
memset(&addr6, 0, sizeof(addr6));
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
addr6.sin6_port = htons(port);
|
|
|
- addr6.sin6_scope_id = if_nametoindex(ifname.c_str());
|
|
|
+ addr6.sin6_scope_id = if_nametoindex(iface.getName().c_str());
|
|
|
|
|
|
memcpy(&addr6.sin6_addr,
|
|
|
addr.getAddress().to_v6().to_bytes().data(),
|
|
@@ -255,61 +354,58 @@ IfaceMgr::openSocket(const std::string& ifname,
|
|
|
// make a socket
|
|
|
int sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
|
if (sock < 0) {
|
|
|
- cout << "Failed to create UDP6 socket." << endl;
|
|
|
- return (-1);
|
|
|
+ isc_throw(Unexpected, "Failed to create UDP6 socket.");
|
|
|
}
|
|
|
|
|
|
- /* Set the REUSEADDR option so that we don't fail to start if
|
|
|
- we're being restarted. */
|
|
|
+ // Set the REUSEADDR option so that we don't fail to start if
|
|
|
+ // we're being restarted.
|
|
|
int flag = 1;
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
|
|
(char *)&flag, sizeof(flag)) < 0) {
|
|
|
- cout << "Can't set SO_REUSEADDR option on dhcpv6 socket." << endl;
|
|
|
close(sock);
|
|
|
- return (-1);
|
|
|
+ isc_throw(Unexpected, "Can't set SO_REUSEADDR option on dhcpv6 socket.");
|
|
|
}
|
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&addr6, sizeof(addr6)) < 0) {
|
|
|
- cout << "Failed to bind socket " << sock << " to " << addr.toText()
|
|
|
- << "/port=" << port << endl;
|
|
|
close(sock);
|
|
|
- return (-1);
|
|
|
+ isc_throw(Unexpected, "Failed to bind socket " << sock << " to " << addr.toText()
|
|
|
+ << "/port=" << port);
|
|
|
}
|
|
|
#ifdef IPV6_RECVPKTINFO
|
|
|
- /* RFC3542 - a new way */
|
|
|
+ // RFC3542 - a new way
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
|
|
&flag, sizeof(flag)) != 0) {
|
|
|
- cout << "setsockopt: IPV6_RECVPKTINFO failed." << endl;
|
|
|
close(sock);
|
|
|
- return (-1);
|
|
|
+ isc_throw(Unexpected, "setsockopt: IPV6_RECVPKTINFO failed.");
|
|
|
}
|
|
|
#else
|
|
|
- /* RFC2292 - an old way */
|
|
|
+ // RFC2292 - an old way
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO,
|
|
|
&flag, sizeof(flag)) != 0) {
|
|
|
- cout << "setsockopt: IPV6_PKTINFO: failed." << endl;
|
|
|
close(sock);
|
|
|
- return (-1);
|
|
|
+ isc_throw(Unexpected, "setsockopt: IPV6_PKTINFO: failed.");
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
// multicast stuff
|
|
|
-
|
|
|
if (addr.getAddress().to_v6().is_multicast()) {
|
|
|
// both mcast (ALL_DHCP_RELAY_AGENTS_AND_SERVERS and ALL_DHCP_SERVERS)
|
|
|
// are link and site-scoped, so there is no sense to join those groups
|
|
|
// with global addresses.
|
|
|
|
|
|
- if ( !joinMcast( sock, ifname,
|
|
|
+ if ( !joinMcast( sock, iface.getName(),
|
|
|
string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {
|
|
|
close(sock);
|
|
|
- return (-1);
|
|
|
+ isc_throw(Unexpected, "Failed to join " << ALL_DHCP_RELAY_AGENTS_AND_SERVERS
|
|
|
+ << " multicast group.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- cout << "Created socket " << sock << " on " << ifname << "/" <<
|
|
|
+ cout << "Created socket " << sock << " on " << iface.getName() << "/" <<
|
|
|
addr.toText() << "/port=" << port << endl;
|
|
|
|
|
|
+ iface.addSocket(SocketInfo(sock, addr, port));
|
|
|
+
|
|
|
return (sock);
|
|
|
}
|
|
|
|
|
@@ -345,16 +441,19 @@ IfaceMgr::send(boost::shared_ptr<Pkt6>& pkt) {
|
|
|
int result;
|
|
|
struct in6_pktinfo *pktinfo;
|
|
|
struct cmsghdr *cmsg;
|
|
|
+
|
|
|
+ Iface* iface = getIface(pkt->iface_);
|
|
|
+ if (!iface) {
|
|
|
+ isc_throw(BadValue, "Unable to send Pkt6. Invalid interface ("
|
|
|
+ << pkt->iface_ << ") specified.");
|
|
|
+ }
|
|
|
+
|
|
|
memset(&control_buf_[0], 0, control_buf_len_);
|
|
|
|
|
|
- /*
|
|
|
- * Initialize our message header structure.
|
|
|
- */
|
|
|
+ // Initialize our message header structure.
|
|
|
memset(&m, 0, sizeof(m));
|
|
|
|
|
|
- /*
|
|
|
- * Set the target address we're sending to.
|
|
|
- */
|
|
|
+ // Set the target address we're sending to.
|
|
|
sockaddr_in6 to;
|
|
|
memset(&to, 0, sizeof(to));
|
|
|
to.sin6_family = AF_INET6;
|
|
@@ -367,24 +466,20 @@ IfaceMgr::send(boost::shared_ptr<Pkt6>& pkt) {
|
|
|
m.msg_name = &to;
|
|
|
m.msg_namelen = sizeof(to);
|
|
|
|
|
|
- /*
|
|
|
- * Set the data buffer we're sending. (Using this wacky
|
|
|
- * "scatter-gather" stuff... we only have a single chunk
|
|
|
- * of data to send, so we declare a single vector entry.)
|
|
|
- */
|
|
|
+ // Set the data buffer we're sending. (Using this wacky
|
|
|
+ // "scatter-gather" stuff... we only have a single chunk
|
|
|
+ // of data to send, so we declare a single vector entry.)
|
|
|
v.iov_base = (char *) &pkt->data_[0];
|
|
|
v.iov_len = pkt->data_len_;
|
|
|
m.msg_iov = &v;
|
|
|
m.msg_iovlen = 1;
|
|
|
|
|
|
- /*
|
|
|
- * Setting the interface is a bit more involved.
|
|
|
- *
|
|
|
- * We have to create a "control message", and set that to
|
|
|
- * define the IPv6 packet information. We could set the
|
|
|
- * source address if we wanted, but we can safely let the
|
|
|
- * kernel decide what that should be.
|
|
|
- */
|
|
|
+ // Setting the interface is a bit more involved.
|
|
|
+ //
|
|
|
+ // We have to create a "control message", and set that to
|
|
|
+ // define the IPv6 packet information. We could set the
|
|
|
+ // source address if we wanted, but we can safely let the
|
|
|
+ // kernel decide what that should be.
|
|
|
m.msg_control = &control_buf_[0];
|
|
|
m.msg_controllen = control_buf_len_;
|
|
|
cmsg = CMSG_FIRSTHDR(&m);
|
|
@@ -396,14 +491,12 @@ IfaceMgr::send(boost::shared_ptr<Pkt6>& pkt) {
|
|
|
pktinfo->ipi6_ifindex = pkt->ifindex_;
|
|
|
m.msg_controllen = cmsg->cmsg_len;
|
|
|
|
|
|
- result = sendmsg(sendsock_, &m, 0);
|
|
|
+ result = sendmsg(getSocket(*pkt), &m, 0);
|
|
|
if (result < 0) {
|
|
|
cout << "Send packet failed." << endl;
|
|
|
}
|
|
|
- cout << "Sent " << result << " bytes." << endl;
|
|
|
-
|
|
|
- cout << "Sent " << pkt->data_len_ << " bytes over "
|
|
|
- << pkt->iface_ << "/" << pkt->ifindex_ << " interface: "
|
|
|
+ cout << "Sent " << pkt->data_len_ << " bytes over socket " << getSocket(*pkt)
|
|
|
+ << " on " << iface->getFullName() << " interface: "
|
|
|
<< " dst=" << pkt->remote_addr_.toText()
|
|
|
<< ", src=" << pkt->local_addr_.toText()
|
|
|
<< endl;
|
|
@@ -411,8 +504,24 @@ IfaceMgr::send(boost::shared_ptr<Pkt6>& pkt) {
|
|
|
return (result);
|
|
|
}
|
|
|
|
|
|
+bool
|
|
|
+IfaceMgr::send(boost::shared_ptr<Pkt4>& )
|
|
|
+{
|
|
|
+ /// TODO: Implement this (ticket #1240)
|
|
|
+ isc_throw(NotImplemented, "Pkt4 send not implemented yet.");
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+boost::shared_ptr<Pkt4>
|
|
|
+IfaceMgr::receive4() {
|
|
|
+ isc_throw(NotImplemented, "Pkt4 reception not implemented yet.");
|
|
|
+
|
|
|
+ // TODO: To be implemented (ticket #1239)
|
|
|
+ return (boost::shared_ptr<Pkt4>()); // NULL
|
|
|
+}
|
|
|
+
|
|
|
boost::shared_ptr<Pkt6>
|
|
|
-IfaceMgr::receive() {
|
|
|
+IfaceMgr::receive6() {
|
|
|
struct msghdr m;
|
|
|
struct iovec v;
|
|
|
int result;
|
|
@@ -442,49 +551,66 @@ IfaceMgr::receive() {
|
|
|
memset(&from, 0, sizeof(from));
|
|
|
memset(&to_addr, 0, sizeof(to_addr));
|
|
|
|
|
|
- /*
|
|
|
- * Initialize our message header structure.
|
|
|
- */
|
|
|
+ // Initialize our message header structure.
|
|
|
memset(&m, 0, sizeof(m));
|
|
|
|
|
|
- /*
|
|
|
- * Point so we can get the from address.
|
|
|
- */
|
|
|
+ // Point so we can get the from address.
|
|
|
m.msg_name = &from;
|
|
|
m.msg_namelen = sizeof(from);
|
|
|
|
|
|
- /*
|
|
|
- * Set the data buffer we're receiving. (Using this wacky
|
|
|
- * "scatter-gather" stuff... but we that doesn't really make
|
|
|
- * sense for us, so we use a single vector entry.)
|
|
|
- */
|
|
|
+ // Set the data buffer we're receiving. (Using this wacky
|
|
|
+ // "scatter-gather" stuff... but we that doesn't really make
|
|
|
+ // sense for us, so we use a single vector entry.)
|
|
|
v.iov_base = (void*)&pkt->data_[0];
|
|
|
v.iov_len = pkt->data_len_;
|
|
|
m.msg_iov = &v;
|
|
|
m.msg_iovlen = 1;
|
|
|
|
|
|
- /*
|
|
|
- * Getting the interface is a bit more involved.
|
|
|
- *
|
|
|
- * We set up some space for a "control message". We have
|
|
|
- * previously asked the kernel to give us packet
|
|
|
- * information (when we initialized the interface), so we
|
|
|
- * should get the destination address from that.
|
|
|
- */
|
|
|
+ // Getting the interface is a bit more involved.
|
|
|
+ //
|
|
|
+ // We set up some space for a "control message". We have
|
|
|
+ // previously asked the kernel to give us packet
|
|
|
+ // information (when we initialized the interface), so we
|
|
|
+ // should get the destination address from that.
|
|
|
m.msg_control = &control_buf_[0];
|
|
|
m.msg_controllen = control_buf_len_;
|
|
|
|
|
|
- result = recvmsg(recvsock_, &m, 0);
|
|
|
+ /// TODO: Need to move to select() and pool over
|
|
|
+ /// all available sockets. For now, we just take the
|
|
|
+ /// first interface and use first socket from it.
|
|
|
+ IfaceCollection::const_iterator iface = ifaces_.begin();
|
|
|
+ if (iface == ifaces_.end()) {
|
|
|
+ isc_throw(Unexpected, "No interfaces detected. Can't receive anything.");
|
|
|
+ }
|
|
|
+ SocketCollection::const_iterator s = iface->sockets_.begin();
|
|
|
+ const SocketInfo* candidate = 0;
|
|
|
+ while (s != iface->sockets_.end()) {
|
|
|
+ if (s->addr_.getAddress().to_v6().is_multicast()) {
|
|
|
+ candidate = &(*s);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!candidate) {
|
|
|
+ candidate = &(*s); // it's not multicast, but it's better than none
|
|
|
+ }
|
|
|
+ ++s;
|
|
|
+ }
|
|
|
+ if (!candidate) {
|
|
|
+ isc_throw(Unexpected, "Interface " << iface->getFullName()
|
|
|
+ << " does not have any sockets open.");
|
|
|
+ }
|
|
|
+
|
|
|
+ cout << "Trying to receive over socket " << candidate->sockfd_ << " bound to "
|
|
|
+ << candidate->addr_.toText() << "/port=" << candidate->port_ << " on "
|
|
|
+ << iface->getFullName() << endl;
|
|
|
+ result = recvmsg(candidate->sockfd_, &m, 0);
|
|
|
|
|
|
if (result >= 0) {
|
|
|
- /*
|
|
|
- * If we did read successfully, then we need to loop
|
|
|
- * through the control messages we received and
|
|
|
- * find the one with our destination address.
|
|
|
- *
|
|
|
- * We also keep a flag to see if we found it. If we
|
|
|
- * didn't, then we consider this to be an error.
|
|
|
- */
|
|
|
+ // If we did read successfully, then we need to loop
|
|
|
+ // through the control messages we received and
|
|
|
+ // find the one with our destination address.
|
|
|
+ //
|
|
|
+ // We also keep a flag to see if we found it. If we
|
|
|
+ // didn't, then we consider this to be an error.
|
|
|
int found_pktinfo = 0;
|
|
|
cmsg = CMSG_FIRSTHDR(&m);
|
|
|
while (cmsg != NULL) {
|
|
@@ -520,7 +646,7 @@ IfaceMgr::receive() {
|
|
|
|
|
|
Iface* received = getIface(pkt->ifindex_);
|
|
|
if (received) {
|
|
|
- pkt->iface_ = received->name_;
|
|
|
+ pkt->iface_ = received->getName();
|
|
|
} else {
|
|
|
cout << "Received packet over unknown interface (ifindex="
|
|
|
<< pkt->ifindex_ << ")." << endl;
|
|
@@ -539,4 +665,60 @@ IfaceMgr::receive() {
|
|
|
return (pkt);
|
|
|
}
|
|
|
|
|
|
+uint16_t
|
|
|
+IfaceMgr::getSocket(isc::dhcp::Pkt6 const& pkt) {
|
|
|
+ Iface* iface = getIface(pkt.iface_);
|
|
|
+ if (!iface) {
|
|
|
+ isc_throw(BadValue, "Tried to find socket for non-existent interface "
|
|
|
+ << pkt.iface_);
|
|
|
+ }
|
|
|
+
|
|
|
+ SocketCollection::const_iterator s;
|
|
|
+ for (s = iface->sockets_.begin(); s != iface->sockets_.end(); ++s) {
|
|
|
+ if (s->family_ != AF_INET6) {
|
|
|
+ // don't use IPv4 sockets
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (s->addr_.getAddress().to_v6().is_multicast()) {
|
|
|
+ // don't use IPv6 sockets bound to multicast address
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /// TODO: Add more checks here later. If remote address is
|
|
|
+ /// not link-local, we can't use link local bound socket
|
|
|
+ /// to send data.
|
|
|
+
|
|
|
+ return (s->sockfd_);
|
|
|
+ }
|
|
|
+
|
|
|
+ isc_throw(Unexpected, "Interface " << iface->getFullName()
|
|
|
+ << " does not have any suitable IPv6 sockets open.");
|
|
|
+}
|
|
|
+
|
|
|
+uint16_t
|
|
|
+IfaceMgr::getSocket(isc::dhcp::Pkt4 const& pkt) {
|
|
|
+ Iface* iface = getIface(pkt.getIface());
|
|
|
+ if (!iface) {
|
|
|
+ isc_throw(BadValue, "Tried to find socket for non-existent interface "
|
|
|
+ << pkt.getIface());
|
|
|
+ }
|
|
|
+
|
|
|
+ SocketCollection::const_iterator s;
|
|
|
+ for (s = iface->sockets_.begin(); s != iface->sockets_.end(); ++s) {
|
|
|
+ if (s->family_ != AF_INET) {
|
|
|
+ // don't use IPv4 sockets
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /// TODO: Add more checks here later. If remote address is
|
|
|
+ /// not link-local, we can't use link local bound socket
|
|
|
+ /// to send data.
|
|
|
+
|
|
|
+ return (s->sockfd_);
|
|
|
+ }
|
|
|
+
|
|
|
+ isc_throw(Unexpected, "Interface " << iface->getFullName()
|
|
|
+ << " does not have any suitable IPv4 sockets open.");
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|