Skip to content

Commit

Permalink
fix sonarqube warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 22, 2018
1 parent 332d27b commit e88e8d3
Showing 1 changed file with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class RawSocket {
Expand All @@ -39,7 +44,7 @@ public class RawSocket {
private static final int SNAPLEN = 65536;
private static final int READ_TIMEOUT = 10;

private static final Map<InetAddress, MacAddress> arpCache = new HashMap<>();
private static final Map<InetAddress, MacAddress> arpCache = new HashMap<>();

// The id of the protocol we will be communicating in.
private final int protocolNumber;
Expand Down Expand Up @@ -79,11 +84,11 @@ public void connect(String remoteAddress) throws RawSocketException {

// Check if we can connect directly to the destination.
FirstHop firstHop = getFirstHop(remoteIpAddress);
if(firstHop == null) {
if (firstHop == null) {
// If this wouldn't work, try to figure out the default
// gateway and use that as next hop.
InetAddress defaultGatewayAddress = getDefaultGatewayAddress();
if(defaultGatewayAddress != null) {
if (defaultGatewayAddress != null) {
firstHop = getFirstHop(defaultGatewayAddress);
if (firstHop == null) {
// If this didn't work, we simply can't reach the
Expand Down Expand Up @@ -120,7 +125,7 @@ public void connect(String remoteAddress) throws RawSocketException {

receiveHandle.setFilter(filterString, BpfProgram.BpfCompileMode.OPTIMIZE);
PacketListener packetListener = packet -> {
for(RawSocketListener listener : listeners) {
for (RawSocketListener listener : listeners) {
listener.packetReceived(packet.getRawData());
}
};
Expand All @@ -143,8 +148,8 @@ public void disconnect() throws RawSocketException {
}

public void write(byte[] rawData) throws RawSocketException {
try(PcapHandle sendHandle =
nif.openLive(SNAPLEN, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, READ_TIMEOUT)) {
try (PcapHandle sendHandle =
nif.openLive(SNAPLEN, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, READ_TIMEOUT)) {
UnknownPacket.Builder packetBuilder = new UnknownPacket.Builder();
packetBuilder.rawData(rawData);

Expand Down Expand Up @@ -191,7 +196,7 @@ public void removeListener(RawSocketListener listener) {
}

private MacAddress getMacAddress(PcapNetworkInterface dev, InetAddress localIpAddress, InetAddress remoteIpAddress) throws RawSocketException {
if(!arpCache.containsKey(remoteIpAddress)) {
if (!arpCache.containsKey(remoteIpAddress)) {
MacAddress macAddress = lookupMacAddress(dev, localIpAddress, remoteIpAddress);
arpCache.put(remoteIpAddress, macAddress);
return macAddress;
Expand Down Expand Up @@ -267,7 +272,7 @@ private MacAddress lookupMacAddress(PcapNetworkInterface dev, InetAddress localI
if (sendHandle.isOpen()) {
sendHandle.close();
}
if(receiveHandle.isOpen()) {
if (receiveHandle.isOpen()) {
// Terminate the receive loop first.
receiveHandle.breakLoop();
receiveHandle.close();
Expand Down Expand Up @@ -331,6 +336,7 @@ private FirstHop getFirstHop(InetAddress remoteAddress) throws RawSocketExceptio
*
* @return InetAddress representing the address of the internet gateway.
*/
@SuppressWarnings("squid:S1313")
private InetAddress getDefaultGatewayAddress() {
try {
Runtime rt = Runtime.getRuntime();
Expand Down Expand Up @@ -362,6 +368,7 @@ private InetAddress getDefaultGatewayAddress() {
}
}
} catch (IOException e) {
logger.debug("error caught", e);
return null;
}

Expand Down

0 comments on commit e88e8d3

Please sign in to comment.