Skip to content
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

udp multicast ip #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tools/socktap/ethernet_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <linux/if_packet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>

static void initialize(ifreq& request, const char* interface_name)
{
Expand Down Expand Up @@ -56,3 +57,28 @@ vanetza::MacAddress EthernetDevice::address() const
std::copy_n(data.ifr_hwaddr.sa_data, addr.octets.size(), addr.octets.data());
return addr;
}

std::string EthernetDevice::ip() const
{
//@todo maybe there's a better way to obtain IP from device name...
std::string ip;
char host[NI_MAXHOST];
struct ifaddrs *interfaces = nullptr;
// get all network interfaces
if(getifaddrs(&interfaces) == 0){
// search for local interface and copy IP
for(struct ifaddrs *ifa = interfaces; ifa != NULL; ifa = ifa->ifa_next) {
if(ifa->ifa_addr == NULL)
continue;

if(ifa->ifa_addr->sa_family == AF_INET && std::string(ifa->ifa_name) == interface_name_){
if(getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) {
ip = std::string(host);
break;
}
}
}
freeifaddrs(interfaces);
}
return ip;
}
1 change: 1 addition & 0 deletions tools/socktap/ethernet_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class EthernetDevice

protocol::endpoint endpoint(int family) const;
vanetza::MacAddress address() const;
std::string ip() const;

private:
int index() const;
Expand Down
2 changes: 1 addition & 1 deletion tools/socktap/link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ create_link_layer(boost::asio::io_service& io_service, const EthernetDevice& dev
} else if (name == "udp") {
namespace ip = boost::asio::ip;
ip::udp::endpoint multicast(ip::address::from_string("239.118.122.97"), 8947);
link_layer.reset(new UdpLink { io_service, multicast });
link_layer.reset(new UdpLink { io_service, multicast, device.ip() });
} else if (name == "tcp") {
namespace ip = boost::asio::ip;

Expand Down
14 changes: 12 additions & 2 deletions tools/socktap/udp_link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@
namespace ip = boost::asio::ip;
using namespace vanetza;

UdpLink::UdpLink(boost::asio::io_service& io_service, const ip::udp::endpoint& endpoint) :
UdpLink::UdpLink(boost::asio::io_service& io_service, const ip::udp::endpoint& endpoint, const std::string& ip) :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing address as boost::asio::ip::address might be beneficial

multicast_endpoint_(endpoint),
tx_socket_(io_service), rx_socket_(io_service),
rx_buffer_(2560, 0x00)
{
tx_socket_.open(multicast_endpoint_.protocol());

if(ip.size() != 0){
boost::asio::ip::multicast::outbound_interface option(boost::asio::ip::address_v4::from_string(ip));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a look at the ip(7) man page: I assume we can even pass the interface index instead of an IP address to outbound_interface and join_group. This would save us the EthernetDevice::ip lookup call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface index instead of IP seems reasonable, I can try it later.

tx_socket_.set_option(option);
}

rx_socket_.open(multicast_endpoint_.protocol());
rx_socket_.set_option(ip::udp::socket::reuse_address(true));
rx_socket_.bind(multicast_endpoint_);
rx_socket_.set_option(ip::multicast::enable_loopback(false));
rx_socket_.set_option(ip::multicast::join_group(multicast_endpoint_.address()));

if(ip.size() != 0){
rx_socket_.set_option(ip::multicast::join_group(multicast_endpoint_.address().to_v4(), boost::asio::ip::address_v4::from_string(ip)));
}else{
rx_socket_.set_option(ip::multicast::join_group(multicast_endpoint_.address()));
}

do_receive();
}
Expand Down
2 changes: 1 addition & 1 deletion tools/socktap/udp_link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class UdpLink : public LinkLayer
{
public:
UdpLink(boost::asio::io_service&, const boost::asio::ip::udp::endpoint&);
UdpLink(boost::asio::io_service&, const boost::asio::ip::udp::endpoint&, const std::string&);

void indicate(IndicationCallback) override;
void request(const vanetza::access::DataRequest&, std::unique_ptr<vanetza::ChunkPacket>) override;
Expand Down