-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
switch off udp receiving of other hosts if network_mode=local #1231
Changes from 2 commits
90c5566
3bd45ad
2039a74
4b3036c
be9439f
287dfa0
79cce5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,40 @@ | |
#include "linux/ecal_socket_option_linux.h" | ||
#endif | ||
|
||
namespace | ||
{ | ||
bool isLocalAddress(const asio::ip::address& address) | ||
{ | ||
// Check if the address is a loopback address. | ||
if (address.is_loopback()) | ||
{ | ||
return true; | ||
} | ||
|
||
// Check if the address is in the private IP address ranges. | ||
// For IPv4, the private address ranges are 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. | ||
if (address.is_v4()) | ||
{ | ||
uint32_t ip = address.to_v4().to_ulong(); | ||
return ((ip & 0xFF000000) == 0x0A000000) || // 10.0.0.0/8 | ||
((ip & 0xFFF00000) == 0xAC100000) || // 172.16.0.0/12 | ||
((ip & 0xFFFF0000) == 0xC0A80000); // 192.168.0.0/16 | ||
} | ||
|
||
#if 0 // not used currently | ||
// Check if the address is in the link-local range for IPv6. | ||
// Link-local IPv6 addresses typically start with fe80::/10. | ||
if (address.is_v6()) | ||
{ | ||
const auto& bytes = address.to_v6().to_bytes(); | ||
return bytes[0] == 0xFE && (bytes[1] & 0xC0) == 0x80; | ||
} | ||
#endif | ||
|
||
return false; | ||
} | ||
} | ||
|
||
namespace eCAL | ||
{ | ||
//////////////////////////////////////////////////////// | ||
|
@@ -32,9 +66,9 @@ namespace eCAL | |
CUDPReceiverAsio::CUDPReceiverAsio(const SReceiverAttr& attr_) : | ||
CUDPReceiverBase(attr_), | ||
m_created(false), | ||
m_localhost(attr_.localhost), | ||
m_broadcast(attr_.broadcast), | ||
m_unicast(attr_.unicast), | ||
m_localhost(attr_.localhost), | ||
m_socket(m_iocontext) | ||
{ | ||
if (m_broadcast && m_unicast) | ||
|
@@ -102,14 +136,7 @@ namespace eCAL | |
} | ||
|
||
// join multicast group | ||
if (m_localhost) | ||
{ | ||
AddMultiCastGroup("127.0.0.1"); | ||
} | ||
else | ||
{ | ||
AddMultiCastGroup(attr_.ipaddr.c_str()); | ||
} | ||
AddMultiCastGroup(attr_.ipaddr.c_str()); | ||
|
||
// state successful creation | ||
m_created = true; | ||
|
@@ -196,23 +223,43 @@ namespace eCAL | |
// run for timeout ms | ||
RunIOContext(asio::chrono::milliseconds(timeout_)); | ||
|
||
// retrieve underlaying raw socket informations | ||
// did we receive anything ? | ||
if (reclen == 0) | ||
{ | ||
return (0); | ||
} | ||
|
||
// is the caller interested in the source address ? | ||
if (address_) | ||
{ | ||
// retrieve underlaying raw socket informations | ||
if (m_sender_endpoint.address().is_v4()) | ||
{ | ||
asio::detail::sockaddr_in4_type* in4 = reinterpret_cast<asio::detail::sockaddr_in4_type*>(m_sender_endpoint.data()); | ||
address_->sin_addr = in4->sin_addr; | ||
address_->sin_family = in4->sin_family; | ||
address_->sin_port = in4->sin_port; | ||
memset(&(address_->sin_zero), 0, 8); | ||
if (address_) | ||
{ | ||
address_->sin_addr = in4->sin_addr; | ||
address_->sin_family = in4->sin_family; | ||
address_->sin_port = in4->sin_port; | ||
memset(&(address_->sin_zero), 0, 8); | ||
} | ||
} | ||
else | ||
{ | ||
std::cout << "CUDPReceiverAsio: ipv4 address conversion failed." << std::endl; | ||
} | ||
} | ||
|
||
// are we running in 'local host only' receiving mode ? | ||
if (m_localhost) | ||
{ | ||
// if this is not a local address, return 0 | ||
if (!isLocalAddress(m_sender_endpoint.address())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your definition of a "Local address" has nothing to do with a loopback address (-> localhost) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I didn't name that unambiguously. What is meant is that the network packets come from your own machine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But your There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I got it .. yes that is true. So this approach will not work at all I think. |
||
{ | ||
return (0); | ||
} | ||
} | ||
|
||
return (reclen); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am puzzled why you would care about those. Why not just use a loopback address (127.0.0.0 - 127.255.255.255 https://de.wikipedia.org/wiki/Loopback)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my first approach. On my local system, however, I see packets that have my own IPv4 address as the sender 10.x.x.x instead of "127.x.x.x". I need to investigate this further - IT's A DRAFT :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good news is that with this draft fix all gtest (including local UDP Pub/Sub) and all samples are running correctly with and without local mode setting and with and without activated npcap mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, but then you are probably using DNS to resolve your hostname. Just skip DNS and just send to 127.0.0.1 directly. Why use DNS, if you already know the IP.
Btw, there is no guarantee that DNS will resolve your hostname to a local IP. It could also be a public IP, if you have one. I guess you rather want to check whether the resolved IP address is a multicast or a unicast address. But still I think you don't need DNS at all to send to a loopback adapter.