|
@@ -19,6 +19,7 @@
|
|
|
#include <netinet/in.h>
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
+#include <dhcp/dhcp4.h>
|
|
|
#include <dhcp/dhcp6.h>
|
|
|
#include <dhcp/iface_mgr.h>
|
|
|
#include <exceptions/exceptions.h>
|
|
@@ -83,14 +84,14 @@ IfaceMgr::Iface::getPlainMac() const {
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
+ for (AddressCollection::iterator a = addrs_.begin();
|
|
|
+ a!=addrs_.end(); ++a) {
|
|
|
+ if (*a==addr) {
|
|
|
+ addrs_.erase(a);
|
|
|
+ return (true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (false);
|
|
|
}
|
|
|
|
|
|
bool IfaceMgr::Iface::delSocket(uint16_t sockfd) {
|
|
@@ -148,10 +149,10 @@ void IfaceMgr::closeSockets() {
|
|
|
}
|
|
|
|
|
|
IfaceMgr::~IfaceMgr() {
|
|
|
- closeSockets();
|
|
|
-
|
|
|
// control_buf_ is deleted automatically (scoped_ptr)
|
|
|
control_buf_len_ = 0;
|
|
|
+
|
|
|
+ closeSockets();
|
|
|
}
|
|
|
|
|
|
void
|
|
@@ -200,42 +201,93 @@ void IfaceMgr::detectIfaces() {
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-void IfaceMgr::openSockets6(uint16_t port) {
|
|
|
- int sock1, sock2;
|
|
|
+bool IfaceMgr::openSockets4(uint16_t port) {
|
|
|
+ int sock;
|
|
|
+ int count = 0;
|
|
|
|
|
|
- for (IfaceCollection::iterator iface = ifaces_.begin();
|
|
|
- iface != ifaces_.end(); ++iface) {
|
|
|
+ for (IfaceCollection::iterator iface=ifaces_.begin();
|
|
|
+ iface!=ifaces_.end();
|
|
|
+ ++iface) {
|
|
|
+
|
|
|
+ cout << "Trying interface " << iface->getFullName() << endl;
|
|
|
+
|
|
|
+ if (iface->flag_loopback_ ||
|
|
|
+ !iface->flag_up_ ||
|
|
|
+ !iface->flag_running_) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
AddressCollection addrs = iface->getAddresses();
|
|
|
|
|
|
- for (AddressCollection::iterator addr = addrs.begin();
|
|
|
+ for (AddressCollection::iterator addr= addrs.begin();
|
|
|
addr != addrs.end();
|
|
|
++addr) {
|
|
|
|
|
|
- sock1 = openSocket(iface->getName(), *addr, port);
|
|
|
- if (sock1 < 0) {
|
|
|
- isc_throw(Unexpected, "Failed to open unicast socket on "
|
|
|
- << " interface " << iface->getFullName());
|
|
|
+ // skip IPv4 addresses
|
|
|
+ if (addr->getFamily() != AF_INET) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ sock = openSocket(iface->getName(), *addr, port);
|
|
|
+ if (sock<0) {
|
|
|
+ cout << "Failed to open unicast socket." << endl;
|
|
|
+ return (false);
|
|
|
+ }
|
|
|
+
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (count > 0);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+bool IfaceMgr::openSockets6(uint16_t port) {
|
|
|
+ int sock;
|
|
|
+ int count = 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) {
|
|
|
+
|
|
|
+ // skip IPv4 addresses
|
|
|
+ if (addr->getFamily() != AF_INET6) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ sock = openSocket(iface->getName(), *addr, port);
|
|
|
+ if (sock<0) {
|
|
|
+ cout << "Failed to open unicast socket." << endl;
|
|
|
+ return (false);
|
|
|
}
|
|
|
|
|
|
- if ( !joinMcast(sock1, iface->getName(),
|
|
|
+ if ( !joinMcast(sock, iface->getName(),
|
|
|
string(ALL_DHCP_RELAY_AGENTS_AND_SERVERS) ) ) {
|
|
|
- close(sock1);
|
|
|
+ close(sock);
|
|
|
isc_throw(Unexpected, "Failed to join " << ALL_DHCP_RELAY_AGENTS_AND_SERVERS
|
|
|
<< " multicast group.");
|
|
|
}
|
|
|
|
|
|
+ count++;
|
|
|
+#if 0
|
|
|
// this doesn't work too well on NetBSD
|
|
|
sock2 = openSocket(iface->getName(),
|
|
|
IOAddress(ALL_DHCP_RELAY_AGENTS_AND_SERVERS),
|
|
|
- port);
|
|
|
- if (sock2 < 0) {
|
|
|
+ DHCP6_SERVER_PORT);
|
|
|
+ if (sock2<0) {
|
|
|
isc_throw(Unexpected, "Failed to open multicast socket on "
|
|
|
<< " interface " << iface->getFullName());
|
|
|
iface->delSocket(sock1); // delete previously opened socket
|
|
|
}
|
|
|
+#endif
|
|
|
}
|
|
|
}
|
|
|
+ return (count > 0);
|
|
|
}
|
|
|
|
|
|
void
|
|
@@ -268,11 +320,11 @@ IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) {
|
|
|
|
|
|
IfaceMgr::Iface*
|
|
|
IfaceMgr::getIface(int ifindex) {
|
|
|
- for (IfaceCollection::iterator iface = ifaces_.begin();
|
|
|
- iface != ifaces_.end(); ++iface) {
|
|
|
- if (iface->getIndex() == ifindex) {
|
|
|
+ for (IfaceCollection::iterator iface=ifaces_.begin();
|
|
|
+ iface!=ifaces_.end();
|
|
|
+ ++iface) {
|
|
|
+ if (iface->getIndex() == ifindex)
|
|
|
return (&(*iface));
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
return (NULL); // not found
|
|
@@ -280,18 +332,18 @@ IfaceMgr::getIface(int ifindex) {
|
|
|
|
|
|
IfaceMgr::Iface*
|
|
|
IfaceMgr::getIface(const std::string& ifname) {
|
|
|
- for (IfaceCollection::iterator iface = ifaces_.begin();
|
|
|
- iface != ifaces_.end(); ++iface) {
|
|
|
- if (iface->getName() == 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,
|
|
|
+int IfaceMgr::openSocket(const std::string& ifname,
|
|
|
+ const IOAddress& addr,
|
|
|
int port) {
|
|
|
Iface* iface = getIface(ifname);
|
|
|
if (!iface) {
|
|
@@ -308,8 +360,7 @@ IfaceMgr::openSocket(const std::string& ifname, const IOAddress& addr,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-int
|
|
|
-IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
|
|
|
+int IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
|
|
|
|
|
|
cout << "Creating UDP4 socket on " << iface.getFullName()
|
|
|
<< " " << addr.toText() << "/port=" << port << endl;
|
|
@@ -318,8 +369,10 @@ IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
|
|
|
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));
|
|
|
+
|
|
|
+ addr4.sin_addr.s_addr = htonl(addr);
|
|
|
+ //addr4.sin_addr.s_addr = 0; // anyaddr: this will receive 0.0.0.0 => 255.255.255.255 traffic
|
|
|
+ // addr4.sin_addr.s_addr = 0xffffffffu; // broadcast address. This will receive 0.0.0.0 => 255.255.255.255 as well
|
|
|
|
|
|
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
if (sock < 0) {
|
|
@@ -332,8 +385,8 @@ IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
|
|
|
<< "/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 there is no support for IP_PKTINFO, we are really out of luck
|
|
|
+ // it will be difficult to undersand, where this packet came from
|
|
|
#if defined(IP_PKTINFO)
|
|
|
int flag = 1;
|
|
|
if (setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &flag, sizeof(flag)) != 0) {
|
|
@@ -345,13 +398,13 @@ IfaceMgr::openSocket4(Iface& iface, const IOAddress& addr, int port) {
|
|
|
cout << "Created socket " << sock << " on " << iface.getName() << "/" <<
|
|
|
addr.toText() << "/port=" << port << endl;
|
|
|
|
|
|
- iface.addSocket(SocketInfo(sock, addr, port));
|
|
|
+ SocketInfo info(sock, addr, port);
|
|
|
+ iface.addSocket(info);
|
|
|
|
|
|
return (sock);
|
|
|
}
|
|
|
|
|
|
-int
|
|
|
-IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
|
|
|
+int IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
|
|
|
|
|
|
cout << "Creating UDP6 socket on " << iface.getFullName()
|
|
|
<< " " << addr.toText() << "/port=" << port << endl;
|
|
@@ -378,8 +431,8 @@ IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
|
|
|
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) {
|
|
@@ -393,14 +446,14 @@ IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
|
|
|
<< "/port=" << port);
|
|
|
}
|
|
|
#ifdef IPV6_RECVPKTINFO
|
|
|
- // RFC3542 - a new way
|
|
|
+ /* RFC3542 - a new way */
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
|
|
&flag, sizeof(flag)) != 0) {
|
|
|
close(sock);
|
|
|
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) {
|
|
|
close(sock);
|
|
@@ -425,7 +478,8 @@ IfaceMgr::openSocket6(Iface& iface, const IOAddress& addr, int port) {
|
|
|
cout << "Created socket " << sock << " on " << iface.getName() << "/" <<
|
|
|
addr.toText() << "/port=" << port << endl;
|
|
|
|
|
|
- iface.addSocket(SocketInfo(sock, addr, port));
|
|
|
+ SocketInfo info(sock, addr, port);
|
|
|
+ iface.addSocket(info);
|
|
|
|
|
|
return (sock);
|
|
|
}
|
|
@@ -526,19 +580,232 @@ IfaceMgr::send(boost::shared_ptr<Pkt6>& pkt) {
|
|
|
}
|
|
|
|
|
|
bool
|
|
|
-IfaceMgr::send(boost::shared_ptr<Pkt4>& )
|
|
|
+IfaceMgr::send(boost::shared_ptr<Pkt4>& pkt)
|
|
|
{
|
|
|
- /// TODO: Implement this (ticket #1240)
|
|
|
- isc_throw(NotImplemented, "Pkt4 send not implemented yet.");
|
|
|
+ struct msghdr m;
|
|
|
+ struct iovec v;
|
|
|
+ int result;
|
|
|
+
|
|
|
+ Iface* iface = getIface(pkt->getIface());
|
|
|
+ if (!iface) {
|
|
|
+ isc_throw(BadValue, "Unable to send Pkt4. Invalid interface ("
|
|
|
+ << pkt->getIface() << ") specified.");
|
|
|
+ }
|
|
|
+
|
|
|
+ memset(&control_buf_[0], 0, control_buf_len_);
|
|
|
+
|
|
|
+ // Initialize our message header structure.
|
|
|
+ memset(&m, 0, sizeof(m));
|
|
|
+
|
|
|
+ // Set the target address we're sending to.
|
|
|
+ sockaddr_in to;
|
|
|
+ memset(&to, 0, sizeof(to));
|
|
|
+ to.sin_family = AF_INET;
|
|
|
+ to.sin_port = htons(pkt->getRemotePort());
|
|
|
+ to.sin_addr.s_addr = htonl(pkt->getRemoteAddr());
|
|
|
+
|
|
|
+ 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.)
|
|
|
+ v.iov_base = (char *) pkt->getBuffer().getData();
|
|
|
+ v.iov_len = pkt->getBuffer().getLength();
|
|
|
+ m.msg_iov = &v;
|
|
|
+ m.msg_iovlen = 1;
|
|
|
+
|
|
|
+// OS_LINUX defines are part of ticket #1237
|
|
|
+#if defined(OS_LINUX)
|
|
|
+ // Setting the interface is a bit more involved.
|
|
|
+ //
|
|
|
+ // We have to create a "control message", and set that to
|
|
|
+ // define the IPv4 packet information. We could set the
|
|
|
+ // source address if we wanted, but we can safely let the
|
|
|
+ // kernel decide what that should be.
|
|
|
+ struct in_pktinfo *pktinfo;
|
|
|
+ struct cmsghdr *cmsg;
|
|
|
+ m.msg_control = &control_buf_[0];
|
|
|
+ m.msg_controllen = control_buf_len_;
|
|
|
+ cmsg = CMSG_FIRSTHDR(&m);
|
|
|
+ cmsg->cmsg_level = IPPROTO_IP;
|
|
|
+ cmsg->cmsg_type = IP_PKTINFO;
|
|
|
+ cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
|
|
|
+ pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
|
|
|
+ memset(pktinfo, 0, sizeof(*pktinfo));
|
|
|
+ pktinfo->ipi_ifindex = pkt->getIndex();
|
|
|
+ m.msg_controllen = cmsg->cmsg_len;
|
|
|
+#endif
|
|
|
+
|
|
|
+ cout << "Trying to send " << pkt->getBuffer().getLength() << " bytes to "
|
|
|
+ << pkt->getRemoteAddr().toText() << ":" << pkt->getRemotePort()
|
|
|
+ << " over socket " << getSocket(*pkt) << " on interface "
|
|
|
+ << getIface(pkt->getIface())->getFullName() << endl;
|
|
|
+
|
|
|
+ result = sendmsg(getSocket(*pkt), &m, 0);
|
|
|
+ if (result < 0) {
|
|
|
+ isc_throw(Unexpected, "Pkt4 send failed.");
|
|
|
+ }
|
|
|
+
|
|
|
+ cout << "Sent " << pkt->getBuffer().getLength() << " bytes over socket " << getSocket(*pkt)
|
|
|
+ << " on " << iface->getFullName() << " interface: "
|
|
|
+ << " dst=" << pkt->getRemoteAddr().toText() << ":" << pkt->getRemotePort()
|
|
|
+ << ", src=" << pkt->getLocalAddr().toText() << ":" << pkt->getLocalPort()
|
|
|
+ << endl;
|
|
|
+
|
|
|
+ return (result);
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
+ const SocketInfo* candidate = 0;
|
|
|
+ IfaceCollection::const_iterator iface;
|
|
|
+ for (iface = ifaces_.begin(); iface != ifaces_.end(); ++iface) {
|
|
|
+
|
|
|
+#if 0
|
|
|
+ // uncomment this once #1237 is merged
|
|
|
+ // Let's skip loopback and downed interfaces.
|
|
|
+ if (iface->flag_loopback_ ||
|
|
|
+ !iface->flag_up_ ||
|
|
|
+ !iface->flag_running_) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ SocketCollection::const_iterator s = iface->sockets_.begin();
|
|
|
+ while (s != iface->sockets_.end()) {
|
|
|
+
|
|
|
+ // We don't want IPv6 addresses here.
|
|
|
+ if (s->addr_.getFamily() != AF_INET) {
|
|
|
+ ++s;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // This address looks good.
|
|
|
+ if (!candidate) {
|
|
|
+ candidate = &(*s);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ ++s;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (candidate) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!candidate) {
|
|
|
+ isc_throw(Unexpected, "Failed to find any suitable sockets on all interfaces.");
|
|
|
+ }
|
|
|
+
|
|
|
+ cout << "Trying to receive over UDP4 socket " << candidate->sockfd_ << " bound to "
|
|
|
+ << candidate->addr_.toText() << "/port=" << candidate->port_ << " on "
|
|
|
+ << iface->getFullName() << endl;
|
|
|
+
|
|
|
+ // Now we have a socket, let's get some data from it!
|
|
|
+
|
|
|
+ struct msghdr m;
|
|
|
+ struct iovec v;
|
|
|
+ int result;
|
|
|
+ struct sockaddr_in from_addr;
|
|
|
+ struct in_addr to_addr;
|
|
|
+ boost::shared_ptr<Pkt4> pkt;
|
|
|
+ const uint32_t RCVBUFSIZE = 1500;
|
|
|
+ uint8_t* buf = (uint8_t*) malloc(RCVBUFSIZE);
|
|
|
+
|
|
|
+ memset(&control_buf_[0], 0, control_buf_len_);
|
|
|
+ memset(&from_addr, 0, sizeof(from_addr));
|
|
|
+ memset(&to_addr, 0, sizeof(to_addr));
|
|
|
+
|
|
|
+ // Initialize our message header structure.
|
|
|
+ memset(&m, 0, sizeof(m));
|
|
|
+
|
|
|
+ // Point so we can get the from address.
|
|
|
+ m.msg_name = &from_addr;
|
|
|
+ m.msg_namelen = sizeof(from_addr);
|
|
|
+
|
|
|
+ v.iov_base = (void*)buf;
|
|
|
+ v.iov_len = RCVBUFSIZE;
|
|
|
+ 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.
|
|
|
+ m.msg_control = &control_buf_[0];
|
|
|
+ m.msg_controllen = control_buf_len_;
|
|
|
+
|
|
|
+ result = recvmsg(candidate->sockfd_, &m, 0);
|
|
|
+
|
|
|
+ if (result < 0) {
|
|
|
+ cout << "Failed to receive UDP4 data." << endl;
|
|
|
+ delete buf;
|
|
|
+ return (boost::shared_ptr<Pkt4>()); // NULL
|
|
|
+ }
|
|
|
+
|
|
|
+// OS_LINUX defines are part of ticket #1237
|
|
|
+#if defined(OS_LINUX)
|
|
|
+ struct cmsghdr* cmsg;
|
|
|
+ struct in_pktinfo* pktinfo;
|
|
|
+ unsigned int ifindex = 0;
|
|
|
+
|
|
|
+ int found_pktinfo = 0;
|
|
|
+ cmsg = CMSG_FIRSTHDR(&m);
|
|
|
+ while (cmsg != NULL) {
|
|
|
+ if ((cmsg->cmsg_level == IPPROTO_IP) &&
|
|
|
+ (cmsg->cmsg_type == IP_PKTINFO)) {
|
|
|
+ pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsg);
|
|
|
+
|
|
|
+ ifindex = pktinfo->ipi_ifindex;
|
|
|
+ to_addr = pktinfo->ipi_addr;
|
|
|
+
|
|
|
+ // This field is useful, when we are bound to unicast
|
|
|
+ // address e.g. 192.0.2.1 and the packet was sent to
|
|
|
+ // broadcast. This will return broadcast address, not
|
|
|
+ // the address we are bound to.
|
|
|
+
|
|
|
+ // IOAddress tmp(htonl(pktinfo->ipi_spec_dst.s_addr));
|
|
|
+ // cout << "The other addr is: " << tmp.toText() << endl;
|
|
|
+
|
|
|
+ // Perhaps we should uncomment this:
|
|
|
+ // to_addr = pktinfo->ipi_spec_dst;
|
|
|
+ found_pktinfo = 1;
|
|
|
+ }
|
|
|
+ cmsg = CMSG_NXTHDR(&m, cmsg);
|
|
|
+ }
|
|
|
+ if (!found_pktinfo) {
|
|
|
+ cout << "Unable to find pktinfo" << endl;
|
|
|
+ delete buf;
|
|
|
+ return (boost::shared_ptr<Pkt4>()); // NULL
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ IOAddress to(htonl(to_addr.s_addr));
|
|
|
+ IOAddress from(htonl(from_addr.sin_addr.s_addr));
|
|
|
+ uint16_t from_port = htons(from_addr.sin_port);
|
|
|
+
|
|
|
+ cout << "Received " << result << " bytes from " << from.toText()
|
|
|
+ << "/port=" << from_port
|
|
|
+ << " sent to " << to.toText() << " over interface "
|
|
|
+ << iface->getFullName() << endl;
|
|
|
+
|
|
|
+ // we have all data let's create Pkt4 object
|
|
|
+ pkt = boost::shared_ptr<Pkt4>(new Pkt4(buf, result));
|
|
|
+
|
|
|
+ pkt->setIface(iface->getName());
|
|
|
+ pkt->setIndex(iface->getIndex());
|
|
|
+ pkt->setLocalAddr(to);
|
|
|
+ pkt->setRemoteAddr(from);
|
|
|
+ pkt->setRemotePort(from_port);
|
|
|
+ pkt->setLocalPort(candidate->port_);
|
|
|
+
|
|
|
+ return (pkt);
|
|
|
}
|
|
|
|
|
|
boost::shared_ptr<Pkt6>
|
|
@@ -572,27 +839,35 @@ IfaceMgr::receive6() {
|
|
|
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_;
|
|
|
|
|
@@ -606,12 +881,16 @@ IfaceMgr::receive6() {
|
|
|
SocketCollection::const_iterator s = iface->sockets_.begin();
|
|
|
const SocketInfo* candidate = 0;
|
|
|
while (s != iface->sockets_.end()) {
|
|
|
+ if (s->addr_.getFamily() != AF_INET6) {
|
|
|
+ ++s;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
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
|
|
|
+ candidate = &(*s); // it's not multicast, but it's better than nothing
|
|
|
}
|
|
|
++s;
|
|
|
}
|
|
@@ -620,18 +899,20 @@ IfaceMgr::receive6() {
|
|
|
<< " does not have any sockets open.");
|
|
|
}
|
|
|
|
|
|
- cout << "Trying to receive over socket " << candidate->sockfd_ << " bound to "
|
|
|
+ cout << "Trying to receive over UDP6 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) {
|