Skip to content
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
22 changes: 22 additions & 0 deletions mdns_cpp/include/mdns_cpp/mdns.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <cstdint>
#include <string>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -61,6 +62,15 @@ struct unsorted_query_t {

using QueryResList = std::vector<Query_result>;

// Describes one local IPv4 interface address together with the prefix length of its network.
struct InterfaceInfo {
static constexpr std::uint8_t kUnknownPrefixLength = 0xFF;

std::string name;
std::uint32_t ip{0}; // network byte order
std::uint8_t prefix_length{kUnknownPrefixLength}; // CIDR prefix, [kUnknownPrefixLength] when the OS did not report a netmask
};

class mDNS {
public:
~mDNS();
Expand All @@ -77,11 +87,18 @@ class mDNS {
void setServiceIP(std::uint32_t ip);
std::uint32_t getServiceIP();

// Restricts outgoing queries to the interface with this IPv4 address (network byte order).
// 0 (the default) queries on every non-loopback interface.
void setQueryInterface(std::uint32_t ip);
std::uint32_t getQueryInterface();

std::string getServiceIPPort(); // IP:Port

void setServiceName(const std::string &name);
void setServiceTxtRecord(const std::string &text_record);
std::vector<std::pair<std::string, uint32_t>> getInterfaces();
// Same interfaces as [getInterfaces], but including the prefix length of each network.
std::vector<InterfaceInfo> getInterfaceInfos();

QueryResList executeQuery2(const std::string &service);

Expand All @@ -91,6 +108,9 @@ class mDNS {
private:
void runMainLoop();
int openClientSockets(int *sockets, int max_sockets, int port);
// [filter_ipv4] == 0 opens sockets on every interface, otherwise only on the matching one.
// Interface enumeration into [fInterfaces] always covers every interface.
int openClientSocketsFiltered(int *sockets, int max_sockets, int port, std::uint32_t filter_ipv4);
int openServiceSockets(int *sockets, int max_sockets);

std::string hostname_{"dummy-host"};
Expand All @@ -104,7 +124,9 @@ class mDNS {
bool has_ipv6_{false};

uint32_t service_address_ipv4_{0};
uint32_t query_interface_ipv4_{0};
std::vector<std::pair<std::string, uint32_t>> fInterfaces;
std::vector<InterfaceInfo> fInterfaceInfos;
uint8_t service_address_ipv6_[16]{0};

std::thread worker_thread_;
Expand Down
73 changes: 65 additions & 8 deletions mdns_cpp/src/mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ int mDNS::openServiceSockets(int *sockets, int max_sockets) {
return num_sockets;
}

int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
int mDNS::openClientSocketsFiltered(int *sockets, int max_sockets, int port, std::uint32_t filter_ipv4) {
// When sending, each socket can only send to one network interface
// Thus we need to open one socket for each interface and address family
int num_sockets = 0;

// Every call re-enumerates all interfaces, so start from a clean list
fInterfaces.clear();
fInterfaceInfos.clear();

#ifdef _WIN32

IP_ADAPTER_ADDRESSES *adapter_address = nullptr;
Expand Down Expand Up @@ -109,11 +113,21 @@ int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
(saddr->sin_addr.S_un.S_un_b.s_b3 != 0) || (saddr->sin_addr.S_un.S_un_b.s_b4 != 1)) {
int log_addr = 1;
char buffer[128];
fInterfaces.push_back(std::make_pair(ipv4AddressToString(buffer, sizeof(buffer), saddr, sizeof(struct sockaddr_in)), saddr->sin_addr.s_addr));

const std::string interfaceName = ipv4AddressToString(buffer, sizeof(buffer), saddr, sizeof(struct sockaddr_in));
fInterfaces.push_back(std::make_pair(interfaceName, saddr->sin_addr.s_addr));

InterfaceInfo info;
info.name = interfaceName;
info.ip = saddr->sin_addr.s_addr;
if (unicast->OnLinkPrefixLength <= 32) {
info.prefix_length = (std::uint8_t)unicast->OnLinkPrefixLength;
}
fInterfaceInfos.push_back(info);


has_ipv4_ = 1;
if (num_sockets < max_sockets) {
const bool interface_selected = (filter_ipv4 == 0) || (saddr->sin_addr.s_addr == filter_ipv4);
if (interface_selected && num_sockets < max_sockets) {
saddr->sin_port = htons((unsigned short)port);
int sock = mdns_socket_open_ipv4(saddr);
if (sock >= 0) {
Expand Down Expand Up @@ -142,7 +156,7 @@ int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
log_addr = 1;
}
has_ipv6_ = 1;
if (num_sockets < max_sockets) {
if ((filter_ipv4 == 0) && num_sockets < max_sockets) {
saddr->sin6_port = htons((unsigned short)port);
int sock = mdns_socket_open_ipv6(saddr);
if (sock >= 0) {
Expand Down Expand Up @@ -185,8 +199,26 @@ int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
{
int log_addr = 0;
fInterfaces.push_back(std::make_pair(ifa->ifa_name, saddr->sin_addr.s_addr));

if (num_sockets < max_sockets) {

InterfaceInfo info;
info.name = ifa->ifa_name;
info.ip = saddr->sin_addr.s_addr;
if (ifa->ifa_netmask && ifa->ifa_netmask->sa_family == AF_INET) {
const std::uint32_t mask = ntohl(((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr);
std::uint8_t prefix = 0;
for (int bit = 31; bit >= 0 && (mask & (1u << bit)); --bit) {
++prefix;
}
// Only contiguous masks describe a valid prefix length
const std::uint32_t rebuilt = (prefix == 0) ? 0u : (0xFFFFFFFFu << (32 - prefix));
if (rebuilt == mask) {
info.prefix_length = prefix;
}
}
fInterfaceInfos.push_back(info);

const bool interface_selected = (filter_ipv4 == 0) || (saddr->sin_addr.s_addr == filter_ipv4);
if (interface_selected && num_sockets < max_sockets) {
saddr->sin_port = htons(port);
int sock = mdns_socket_open_ipv4(saddr);
if (sock >= 0) {
Expand All @@ -209,7 +241,7 @@ int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
log_addr = 1;
}
has_ipv6_ = 1;
if (num_sockets < max_sockets) {
if ((filter_ipv4 == 0) && num_sockets < max_sockets) {
saddr->sin6_port = htons(port);
int sock = mdns_socket_open_ipv6(saddr);
if (sock >= 0) {
Expand All @@ -235,6 +267,19 @@ int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
return num_sockets;
}

int mDNS::openClientSockets(int *sockets, int max_sockets, int port) {
int num_sockets = openClientSocketsFiltered(sockets, max_sockets, port, query_interface_ipv4_);

if ((query_interface_ipv4_ != 0) && (max_sockets > 0) && (num_sockets == 0)) {
// The selected interface is gone (unplugged, new DHCP lease, ...).
// Fall back to all interfaces instead of silently discovering nothing.
MDNS_LOG << "Selected network interface is not available, falling back to all interfaces\n";
num_sockets = openClientSocketsFiltered(sockets, max_sockets, port, 0);
}

return num_sockets;
}


static int query_callback(int sock, const struct sockaddr *from, size_t addrlen, mdns_entry_type_t entry,
uint16_t query_id, uint16_t rtype, uint16_t rclass, uint32_t ttl, const void *data,
Expand Down Expand Up @@ -386,6 +431,9 @@ std::uint16_t mDNS::getServicePort() { return port_; }
void mDNS::setServiceIP(std::uint32_t ip) { service_address_ipv4_ = ip; has_ipv4_ = true; }
std::uint32_t mDNS::getServiceIP() { return service_address_ipv4_; }

void mDNS::setQueryInterface(std::uint32_t ip) { query_interface_ipv4_ = ip; }
std::uint32_t mDNS::getQueryInterface() { return query_interface_ipv4_; }

std::string mDNS::getServiceIPPort()
{
char buffer[64];
Expand All @@ -410,6 +458,15 @@ std::vector<std::pair<std::string, uint32_t>> mDNS::getInterfaces()
return fInterfaces;
}

std::vector<InterfaceInfo> mDNS::getInterfaceInfos()
{
// Call the client socket function to enumerate and get local addresses,
// but not open the actual sockets
openClientSockets(0, 0, 0);

return fInterfaceInfos;
}

#define ITER_SEARCH_TIME 2
#define MAX_SEARCH_TIME ITER_SEARCH_TIME * 4

Expand Down
5 changes: 5 additions & 0 deletions src/Include/IMediaRessourceVectorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,11 @@ class DYNAMIC_ATTRIBUTE IGdtfMacro : public IVWUnknown
*/
virtual VCOMError VCOM_CALLTYPE QueryLocalServices(size_t& out_Count) = 0;
virtual VCOMError VCOM_CALLTYPE GetLocalServiceAt(size_t index, ConnectToLocalServiceArgs& outLocalService) = 0;

using NetworkInterface = std::pair<std::string, uint32_t>;
// Allows the user to query all available network interfaces on the system, and set one of them for the MVR-xchange service to use
virtual VCOMError VCOM_CALLTYPE QueryAllAvailableInterfaces( std::vector<NetworkInterface>& out ) = 0;
virtual VCOMError VCOM_CALLTYPE SetNetworkInterface( const NetworkInterface& inInterface ) = 0;

typedef IMVRxchangeMessage (*IMVRxchangeIncomingMessage)(const IMVRxchangeMessage& args, void* context);
typedef void (*IMVRxchangeReturningMessage)(const IMVRxchangeMessage& outgoingMsg, const IMVRxchangeMessage& returningMsg, void* context);
Expand Down
Loading
Loading