chrberger / libcluon

libcluon is a small and efficient, single-file and header-only library written in modern C++ to power microservices.
Mozilla Public License 2.0
99 stars 13 forks source link

Is there a way to query local interfaces IP? #7

Closed piotrek-szczygiel closed 4 years ago

piotrek-szczygiel commented 4 years ago

I found something in UDPReceiver class but it is behind a private variable.

piotrek-szczygiel commented 4 years ago

I would like to send broadcast on all interfaces. I've already tried using 255.255.255.255 IP but that doesn't seem to work.

piotrek-szczygiel commented 4 years ago

Also seems like even doing something like 192.168.188.255 doesn't work. So is there no way of sending broadcasts?

chrberger commented 4 years ago

Hi, just for me to understand: do you want to send data via broadcast using UDPSender or do you want to receive data sent from somewhere via broadcast using UDPReceiver?

piotrek-szczygiel commented 4 years ago

I'd like to send broadcast on all connected interfaces. For example when I'm connected to WiFi and Ethernet, I'd like to send broadcast on both of these interfaces.

Creating broadcasting sockets on every interface looks something like this in boost::asio:

    std::vector<ba::ip::udp::socket> sockets {};

    // Open one socket for every network interface
    ba::io_service io_service;
    ba::ip::tcp::resolver resolver(io_service);
    ba::ip::tcp::resolver::query query(ba::ip::host_name(), "");
    auto it = resolver.resolve(query);
    while (it != ba::ip::tcp::resolver::iterator()) {
        auto addr = (it++)->endpoint().address();
        if (addr.is_v4()) {
            ba::ip::udp::socket socket { m_ctx };
            boost::system::error_code ec;
            socket.open(ba::ip::udp::v4(), ec);
            if (ec) {
                BOOST_LOG_TRIVIAL(error) << "Unable to create broadcast socket on "
                                         << addr.to_string() << ": " << ec.message();
            } else {
                socket.set_option(ba::ip::udp::socket::reuse_address(true));
                socket.set_option(ba::socket_base::broadcast(true));
                socket.bind(ba::ip::udp::endpoint(addr, 0));

                sockets.push_back(std::move(socket));
            }
        }
    }

Also it seems like at the moment there is actually no way to send broadcast at all even when knowing the exact network address. I've tried creating UDPSender with address 192.168.188.255 (my ip was 192.168.188.21 with mask 255.255.255.0), but wireshark didn't intercept anything. I was only able to see any udp traffic when i chose unicast address as an endpoint.

chrberger commented 4 years ago

Added as of https://github.com/chrberger/libcluon/releases/tag/v0.0.128