mirror of
https://github.com/brunoherbelin/vimix.git
synced 2026-06-16 04:27:23 +02:00
BugFix P2P for OSX; Add broadcast IP retrieval to NetworkToolkit and update Connection for subnet broadcasting
This commit is contained in:
+8
-3
@@ -30,6 +30,7 @@
|
||||
#include "Settings.h"
|
||||
#include "Streamer.h"
|
||||
#include "Log.h"
|
||||
#include "Toolkit/NetworkToolkit.h"
|
||||
|
||||
#include "Connection.h"
|
||||
|
||||
@@ -219,9 +220,13 @@ void Connection::ask()
|
||||
// loop infinitely
|
||||
while(Connection::manager().asking_)
|
||||
{
|
||||
// broadcast on several ports
|
||||
for(int i=HANDSHAKE_PORT; i<HANDSHAKE_PORT+MAX_HANDSHAKE; i++)
|
||||
socket.SendTo( IpEndpointName( i ), p.Data(), p.Size() );
|
||||
// broadcast on several ports using directed subnet broadcast addresses
|
||||
// (required on macOS; 0.0.0.0 only reaches loopback there)
|
||||
std::vector<std::string> bcasts = NetworkToolkit::broadcast_ips();
|
||||
for(int i=HANDSHAKE_PORT; i<HANDSHAKE_PORT+MAX_HANDSHAKE; i++) {
|
||||
for (const std::string &bcast : bcasts)
|
||||
socket.SendTo( IpEndpointName( bcast.c_str(), i ), p.Data(), p.Size() );
|
||||
}
|
||||
|
||||
// wait a bit
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
|
||||
@@ -183,6 +183,52 @@ std::vector<std::string> NetworkToolkit::host_ips()
|
||||
return ipstrings_;
|
||||
}
|
||||
|
||||
std::vector<std::string> NetworkToolkit::broadcast_ips()
|
||||
{
|
||||
std::vector<std::string> broadcasts;
|
||||
|
||||
char buf[16384];
|
||||
int fd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0)
|
||||
return broadcasts;
|
||||
|
||||
struct ifconf ifconf;
|
||||
ifconf.ifc_len = sizeof buf;
|
||||
ifconf.ifc_buf = buf;
|
||||
if (ioctl(fd, SIOCGIFCONF, &ifconf) == 0) {
|
||||
struct ifreq *ifreq = ifconf.ifc_req;
|
||||
for (int i = 0; i < ifconf.ifc_len; ) {
|
||||
size_t len;
|
||||
#ifndef linux
|
||||
len = IFNAMSIZ + ifreq->ifr_addr.sa_len;
|
||||
#else
|
||||
len = sizeof *ifreq;
|
||||
#endif
|
||||
// only consider IPv4 interfaces
|
||||
struct ifreq req;
|
||||
memset(&req, 0, sizeof req);
|
||||
strncpy(req.ifr_name, ifreq->ifr_name, IFNAMSIZ);
|
||||
if (ioctl(fd, SIOCGIFADDR, &req) == 0 &&
|
||||
req.ifr_addr.sa_family == AF_INET) {
|
||||
// get broadcast address for this interface
|
||||
if (ioctl(fd, SIOCGIFBRDADDR, &req) == 0) {
|
||||
char host[128];
|
||||
getnameinfo(&req.ifr_broadaddr, sizeof req.ifr_broadaddr,
|
||||
host, sizeof host, 0, 0, NI_NUMERICHOST);
|
||||
std::string bcast(host);
|
||||
// exclude loopback broadcast and duplicates
|
||||
if (bcast != "0.0.0.0" && bcast != "255.255.255.255" &&
|
||||
std::find(broadcasts.begin(), broadcasts.end(), bcast) == broadcasts.end())
|
||||
broadcasts.push_back(bcast);
|
||||
}
|
||||
}
|
||||
ifreq = (struct ifreq *)((char *)ifreq + len);
|
||||
i += len;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return broadcasts;
|
||||
}
|
||||
|
||||
bool NetworkToolkit::is_host_ip(const std::string &ip)
|
||||
{
|
||||
|
||||
@@ -61,6 +61,7 @@ struct StreamConfig {
|
||||
|
||||
std::string hostname();
|
||||
std::vector<std::string> host_ips();
|
||||
std::vector<std::string> broadcast_ips();
|
||||
bool is_host_ip(const std::string &ip);
|
||||
std::string closest_host_ip(const std::string &ip);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user