Fix self discovery
Check address on broadcast reception and use only ipv4
This commit is contained in:
parent
3b9f4f0a41
commit
c4b2b55887
2 changed files with 38 additions and 8 deletions
|
@ -7,6 +7,8 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
|
import static fr.insa.clavardator.network.NetUtil.isLocalAddress;
|
||||||
|
|
||||||
public class NetDiscoverer {
|
public class NetDiscoverer {
|
||||||
private static final short DISCOVERY_PORT = 31593;
|
private static final short DISCOVERY_PORT = 31593;
|
||||||
private static final short RESPONSE_PORT = 31594;
|
private static final short RESPONSE_PORT = 31594;
|
||||||
|
@ -51,10 +53,20 @@ public class NetDiscoverer {
|
||||||
broadcastListener.stopListening();
|
broadcastListener.stopListening();
|
||||||
|
|
||||||
broadcastListener = new BroadcastListener((ipAddr, data) -> {
|
broadcastListener = new BroadcastListener((ipAddr, data) -> {
|
||||||
if (onBroadcastReceived != null)
|
try {
|
||||||
onBroadcastReceived.onBroadcastReceived(ipAddr, data);
|
if (!isLocalAddress(ipAddr)) {
|
||||||
responseSender = new ResponseSender(ipAddr, responseMessage, errorCallback);
|
if (onBroadcastReceived != null)
|
||||||
responseSender.start();
|
onBroadcastReceived.onBroadcastReceived(ipAddr, data);
|
||||||
|
responseSender = new ResponseSender(ipAddr, responseMessage, errorCallback);
|
||||||
|
responseSender.start();
|
||||||
|
} else {
|
||||||
|
Log.w(this.getClass().getSimpleName(), "Received broadcast from self");
|
||||||
|
}
|
||||||
|
} catch (SocketException e) {
|
||||||
|
Log.e(this.getClass().getSimpleName(), "Error getting local adresses", e);
|
||||||
|
if (errorCallback != null)
|
||||||
|
errorCallback.onError(e);
|
||||||
|
}
|
||||||
}, e -> {
|
}, e -> {
|
||||||
if (errorCallback != null)
|
if (errorCallback != null)
|
||||||
errorCallback.onError(e);
|
errorCallback.onError(e);
|
||||||
|
|
|
@ -1,15 +1,20 @@
|
||||||
package fr.insa.clavardator.network;
|
package fr.insa.clavardator.network;
|
||||||
|
|
||||||
|
import fr.insa.clavardator.util.Log;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InterfaceAddress;
|
import java.net.InterfaceAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class NetUtil {
|
public class NetUtil {
|
||||||
|
/**
|
||||||
|
* Lists all ipv4 broadcast addresses
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws SocketException
|
||||||
|
*/
|
||||||
public static List<InetAddress> listAllBroadcastAddresses() throws SocketException {
|
public static List<InetAddress> listAllBroadcastAddresses() throws SocketException {
|
||||||
List<InetAddress> broadcastList = new ArrayList<>();
|
List<InetAddress> broadcastList = new ArrayList<>();
|
||||||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
|
@ -26,6 +31,12 @@ public class NetUtil {
|
||||||
return broadcastList;
|
return broadcastList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists all local Ipv4 addresses
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws SocketException
|
||||||
|
*/
|
||||||
public static List<InetAddress> listAllLocalAddresses() throws SocketException {
|
public static List<InetAddress> listAllLocalAddresses() throws SocketException {
|
||||||
List<InetAddress> localList = new ArrayList<>();
|
List<InetAddress> localList = new ArrayList<>();
|
||||||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
|
@ -34,14 +45,21 @@ public class NetUtil {
|
||||||
NetworkInterface networkInterface = interfaces.nextElement();
|
NetworkInterface networkInterface = interfaces.nextElement();
|
||||||
if (!networkInterface.isLoopback() && networkInterface.isUp()) {
|
if (!networkInterface.isLoopback() && networkInterface.isUp()) {
|
||||||
networkInterface.getInterfaceAddresses().stream()
|
networkInterface.getInterfaceAddresses().stream()
|
||||||
|
.filter((i) -> Objects.nonNull(i.getBroadcast()))
|
||||||
.map(InterfaceAddress::getAddress)
|
.map(InterfaceAddress::getAddress)
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.forEach(localList::add);
|
.forEach(localList::add);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Log.v("NetUtil", "Local addresses: " + Arrays.toString(new List[]{localList}));
|
||||||
return localList;
|
return localList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isLocalAddress(InetAddress address) throws SocketException {
|
||||||
|
final List<InetAddress> list = listAllLocalAddresses();
|
||||||
|
return list.stream().anyMatch((a) -> Arrays.equals(address.getAddress(), a.getAddress()));
|
||||||
|
}
|
||||||
|
|
||||||
public static int getIdFromIp(InetAddress ipAddr) {
|
public static int getIdFromIp(InetAddress ipAddr) {
|
||||||
byte[] addr = ipAddr.getAddress();
|
byte[] addr = ipAddr.getAddress();
|
||||||
int id = 0;
|
int id = 0;
|
||||||
|
|
Loading…
Reference in a new issue