From cc06ccc3349ff82f6c50c9e272ab0aedc39853ed Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 20 Mar 2020 14:50:01 +0100 Subject: [PATCH] Bind sur localhost --- src/serverP2P/ServerManagementTCP.java | 2 +- src/serverP2P/ServerManagementUDP.java | 6 +++++- src/tools/HostItem.java | 22 ++++++++++++++++++++++ src/tools/Logger.java | 1 + src/tracker/TrackerManagementTCP.java | 23 ++++++++++++----------- src/tracker/TrackerManagementUDP.java | 8 +++++--- 6 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index 98ce3cd..69ce5d6 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -64,7 +64,7 @@ public class ServerManagementTCP implements Runnable { initFileList(); initSha512(); try { - socket = new ServerSocket(TCPPort); + socket = new ServerSocket(TCPPort, 10, InetAddress.getLocalHost()); } catch (SocketException e) { logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error); System.exit(-1); diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index 7573bda..2050916 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -33,6 +33,7 @@ import protocolP2P.HashResponse; import tools.HostItem; import protocolP2P.Register; import protocolP2P.Unregister; +import java.net.UnknownHostException; /** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. * @author Louis Royer @@ -62,10 +63,13 @@ public class ServerManagementUDP implements Runnable { initFileList(); initSha512(); try { - socket = new DatagramSocket(UDPPort); + socket = new DatagramSocket(UDPPort, InetAddress.getLocalHost()); } catch (SocketException e) { logger.writeUDP("Error: cannot listen on port " + UDPPort, LogLevel.Error); System.exit(-1); + } catch (UnknownHostException e) { + logger.writeUDP(e, LogLevel.Error); + System.exit(-2); } } diff --git a/src/tools/HostItem.java b/src/tools/HostItem.java index fe15007..76dcb2d 100644 --- a/src/tools/HostItem.java +++ b/src/tools/HostItem.java @@ -17,6 +17,7 @@ public class HostItem { private int port; private Socket tcpSocket; private DatagramSocket udpSocket; + private InetAddress inetAddress; /** Constructor with hostname and port * @param hostname Hostname @@ -125,4 +126,25 @@ public class HostItem { public int hashCode() { return hostname.hashCode() ^ port; } + + public InetAddress getInetAddress() { + if (inetAddress == null) { + try { + inetAddress = InetAddress.getByName(getHostname()); + } catch (UnknownHostException e) { + System.err.println("Error: Unknown host."); + System.exit(-1); + } + } + return inetAddress; + } + + public HostItem(Socket s) { + tcpSocket = s; + inetAddress = s.getInetAddress(); + hostname = inetAddress.getCanonicalHostName(); + port = s.getPort(); + } + + } diff --git a/src/tools/Logger.java b/src/tools/Logger.java index 63c2107..4da4848 100644 --- a/src/tools/Logger.java +++ b/src/tools/Logger.java @@ -120,4 +120,5 @@ public class Logger { e.printStackTrace(); } + } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 32f5394..8ab4724 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -22,6 +22,8 @@ import protocolP2P.DiscoverResponse; import protocolP2P.FileList; import protocolP2P.HashRequest; import localException.InternalError; +import java.net.UnknownHostException; +import java.net.InetAddress; /** Tracker management implementation with tcp @@ -32,7 +34,7 @@ import localException.InternalError; */ public class TrackerManagementTCP implements Runnable { - private int port; + private HostItem tracker; private Logger logger; private ServerSocket socket; private List hostList = new ArrayList<>(); @@ -43,15 +45,15 @@ public class TrackerManagementTCP implements Runnable { * @param logger Logger object */ public TrackerManagementTCP(int port, Logger logger) { - this.port = port; + tracker = new HostItem("localhost", port); this.logger = logger; try { - socket = new ServerSocket(port); + socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress()); } catch (SocketException e) { - logger.writeTCP("Error: cannot listen on port " + port, LogLevel.Error); + logger.writeTCP("Error: cannot listen on" + tracker, LogLevel.Error); System.exit(-1); } catch (IOException e) { - logger.writeTCP("Error: cannot openning socket", LogLevel.Error); + logger.writeTCP("Error: cannot open socket", LogLevel.Error); System.exit(-2); } } @@ -76,13 +78,12 @@ public class TrackerManagementTCP implements Runnable { */ private class ClientHandler implements Runnable { private Socket s; - private String addr; + private HostItem addr; /** Constructor with a socket. * @param s Socket of this client */ public ClientHandler(Socket s) { - this.s = s; - this.addr = "[" +s.getInetAddress().getHostAddress() + "]:" + s.getPort() + " "; + this.addr = new HostItem(s); } /** Implementation of runnable. This method allow to serve one client. @@ -90,11 +91,11 @@ public class TrackerManagementTCP implements Runnable { public void run() { boolean end = false; - logger.writeTCP(addr + "New connection", LogLevel.Action); + logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action); do { end = handleRequest(); } while(!end); - logger.writeTCP(addr + "End of connection", LogLevel.Action); + logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action); } /** Respond to next request incomming on socket s. @@ -103,7 +104,7 @@ public class TrackerManagementTCP implements Runnable { */ private boolean handleRequest() { try { - ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s); + ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket()); Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: diff --git a/src/tracker/TrackerManagementUDP.java b/src/tracker/TrackerManagementUDP.java index ee604c4..41ff8d8 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -22,6 +22,8 @@ import protocolP2P.FileList; import protocolP2P.HashRequest; import localException.InternalError; import remoteException.EmptyDirectory; +import java.net.InetAddress; +import java.net.UnknownHostException; /** Tracker management implementation with udp * @author Louis Royer @@ -31,7 +33,7 @@ import remoteException.EmptyDirectory; */ public class TrackerManagementUDP implements Runnable { - private int port; + private HostItem tracker; private Logger logger; private DatagramSocket socket; private List hostList = new ArrayList<>(); @@ -42,10 +44,10 @@ public class TrackerManagementUDP implements Runnable { * @param logger Logger object */ public TrackerManagementUDP(int port, Logger logger) { - this.port = port; + tracker = new HostItem("localhost", port); this.logger = logger; try { - socket = new DatagramSocket(port); + socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress()); } catch (SocketException e) { logger.writeUDP("Error: cannot listen on port " + port, LogLevel.Error); System.exit(-1);