From 65c44569d0d86d159e666fff00df4dc0481e8a1d Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Thu, 2 Apr 2020 00:55:38 +0200 Subject: [PATCH] Fix #54 (#100) Fix #54 and fix UDP packets max size --- src/protocolP2P/HashRequest.java | 2 +- src/protocolP2P/ProtocolP2PPacketTCP.java | 193 ++++++++++++---------- src/protocolP2P/ProtocolP2PPacketUDP.java | 13 +- 3 files changed, 112 insertions(+), 96 deletions(-) diff --git a/src/protocolP2P/HashRequest.java b/src/protocolP2P/HashRequest.java index cad74f7..bdf4609 100644 --- a/src/protocolP2P/HashRequest.java +++ b/src/protocolP2P/HashRequest.java @@ -64,7 +64,7 @@ public class HashRequest extends Payload { filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize); /* Read algo list */ - int size = getPayloadSize(packet); + int size = getPayloadSize(packet) - filenameSize - (FILENAME_POSITION - PAYLOAD_START_POSITION); String[] algoListStr = BytesArrayTools.readStringArray(packet, FILENAME_POSITION + filenameSize, size, "\n"); int i = 0; algoList = new HashAlgorithm[algoListStr.length]; diff --git a/src/protocolP2P/ProtocolP2PPacketTCP.java b/src/protocolP2P/ProtocolP2PPacketTCP.java index 69c8e6e..7f8f1d0 100644 --- a/src/protocolP2P/ProtocolP2PPacketTCP.java +++ b/src/protocolP2P/ProtocolP2PPacketTCP.java @@ -28,6 +28,7 @@ import protocolP2P.SizeResponse; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; +import java.util.Arrays; /** Representation of packet. * @author Louis Royer @@ -113,11 +114,63 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke throw new InternalError(); } Socket ss = (Socket)socket; - byte[] packet = new byte[1024]; + byte[] packetHeader = new byte[Payload.PAYLOAD_START_POSITION]; try { - if (-1 == ss.getInputStream().read(packet)) { + if (-1 == ss.getInputStream().read(packetHeader, 0, Payload.PAYLOAD_START_POSITION)) { throw new IOException(); } + int payloadSize = Payload.getPayloadSize(packetHeader); + byte[] packet = Arrays.copyOf(packetHeader, Payload.PAYLOAD_START_POSITION + payloadSize); + if (-1 == ss.getInputStream().read(packet, Payload.PAYLOAD_START_POSITION, payloadSize)) { + throw new IOException(); + } + // contruction + boolean protocolError = false; + try { + constructPacket(packet, ss); + Payload payload = getPayload(); + switch (payload.getRequestResponseCode()) { + case PROTOCOL_ERROR : + // we do not want to create an infinite loop of protocolError message exchange. + protocolError = true; + break; + case VERSION_ERROR : + case INTERNAL_ERROR : + case EMPTY_DIRECTORY : + case NOT_FOUND : + case EMPTY_FILE: + case LOAD_RESPONSE: + case LIST_RESPONSE: + case HASH_RESPONSE: + case DISCOVER_RESPONSE: + case NOT_A_TRACKER: + case RATIO_RESPONSE: + case DENIED: + case SIZE_RESPONSE: + // we were expecting a request, but we are receiving a response + throw new ProtocolError(); + default : + break; + } + } catch (TransmissionError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); + throw e; + } catch (ProtocolError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss); + throw e; + } catch (VersionError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss); + throw e; + } catch (InternalError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); + throw e; + } catch (SizeError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); + throw e; + } + if (protocolError) { + throw new ProtocolError(); + } } catch (IOException e) { // Error: cannot read request, closing socket try { @@ -128,53 +181,6 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke throw new SocketClosed(); } } - // contruction - boolean protocolError = false; - try { - constructPacket(packet, ss); - Payload payload = getPayload(); - switch (payload.getRequestResponseCode()) { - case PROTOCOL_ERROR : - // we do not want to create an infinite loop of protocolError message exchange. - protocolError = true; - break; - case VERSION_ERROR : - case INTERNAL_ERROR : - case EMPTY_DIRECTORY : - case NOT_FOUND : - case EMPTY_FILE: - case LOAD_RESPONSE: - case LIST_RESPONSE: - case HASH_RESPONSE: - case DISCOVER_RESPONSE: - case NOT_A_TRACKER: - case RATIO_RESPONSE: - case DENIED: - case SIZE_RESPONSE: - // we were expecting a request, but we are receiving a response - throw new ProtocolError(); - default : - break; - } - } catch (TransmissionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); - throw e; - } catch (ProtocolError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss); - throw e; - } catch (VersionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss); - throw e; - } catch (InternalError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); - throw e; - } catch (SizeError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); - throw e; - } - if (protocolError) { - throw new ProtocolError(); - } } /** Send a Response to a Request (typically used from server). @@ -221,11 +227,56 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke throw new InternalError(); } // reception - byte[] packet = new byte[8192]; + byte[] packetHeader = new byte[Payload.PAYLOAD_START_POSITION]; try { - if (-1== requestSocket.getInputStream().read(packet)) { + if (-1 == requestSocket.getInputStream().read(packetHeader, 0, Payload.PAYLOAD_START_POSITION)) { + throw new IOException(); + } + int payloadSize = Payload.getPayloadSize(packetHeader); + byte[] packet = Arrays.copyOf(packetHeader, Payload.PAYLOAD_START_POSITION + payloadSize); + if (-1 == requestSocket.getInputStream().read(packet, Payload.PAYLOAD_START_POSITION, payloadSize)) { throw new IOException(); } + // contruction + try { + ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP<>(packet); + Payload payload = p.getPayload(); + switch (payload.getRequestResponseCode()) { + case PROTOCOL_ERROR : + throw new ProtocolRemoteError(); + case VERSION_ERROR : + throw new VersionRemoteError(); + case INTERNAL_ERROR : + throw new InternalRemoteError(); + case EMPTY_DIRECTORY : + throw new EmptyDirectory(); + case NOT_FOUND : + throw new NotFound(); + case EMPTY_FILE: + throw new EmptyFile(); + case NOT_A_TRACKER: + throw new NotATracker(); + case UNKNOWN_HOST: + throw new UnknownHost(); + default : + return (ProtocolP2PPacket)p; + } + } catch (TransmissionError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + throw e; + } catch (ProtocolError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); + throw e; + } catch (VersionError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); + throw e; + } catch (InternalError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + throw e; + } catch (SizeError e) { + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + throw e; + } } catch (IOException e) { // Error: cannot read request, closing socket try { @@ -236,46 +287,6 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke throw new SocketClosed(); } } - // contruction - try { - ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP<>(packet); - Payload payload = p.getPayload(); - switch (payload.getRequestResponseCode()) { - case PROTOCOL_ERROR : - throw new ProtocolRemoteError(); - case VERSION_ERROR : - throw new VersionRemoteError(); - case INTERNAL_ERROR : - throw new InternalRemoteError(); - case EMPTY_DIRECTORY : - throw new EmptyDirectory(); - case NOT_FOUND : - throw new NotFound(); - case EMPTY_FILE: - throw new EmptyFile(); - case NOT_A_TRACKER: - throw new NotATracker(); - case UNKNOWN_HOST: - throw new UnknownHost(); - default : - return (ProtocolP2PPacket)p; - } - } catch (TransmissionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); - throw e; - } catch (ProtocolError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); - throw e; - } catch (VersionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); - throw e; - } catch (InternalError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); - throw e; - } catch (SizeError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); - throw e; - } } /** Private constructor with packet as byte[] parameter (typically used when receiving Packet response). diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index 0b639fe..afc1e64 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -30,6 +30,7 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketAddress; import java.io.IOException; +import java.util.Arrays; /** Representation of packet. * @author Louis Royer @@ -116,9 +117,11 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket throw new InternalError(); } DatagramSocket ss = (DatagramSocket)socket; - byte[] packet = new byte[1024]; - DatagramPacket reception = new DatagramPacket(packet, packet.length); + byte[] packetTmp = new byte[0xFFFF]; + DatagramPacket reception = new DatagramPacket(packetTmp, packetTmp.length); ss.receive(reception); + int payloadSize = Payload.getPayloadSize(packetTmp); + byte[] packet = Arrays.copyOf(packetTmp, Payload.PAYLOAD_START_POSITION + payloadSize); responseSocketAddress = reception.getSocketAddress(); remoteHost = new HostItem(reception.getAddress().getHostName(), reception.getPort()); // contruction @@ -216,9 +219,11 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket throw new InternalError(); } // reception - byte[] packet = new byte[8192]; - DatagramPacket reception = new DatagramPacket(packet, packet.length); + byte[] packetTmp = new byte[0xFFFF]; + DatagramPacket reception = new DatagramPacket(packetTmp, packetTmp.length); requestSocket.receive(reception); + int payloadSize = Payload.getPayloadSize(packetTmp); + byte[] packet = Arrays.copyOf(packetTmp, Payload.PAYLOAD_START_POSITION + payloadSize); // contruction try {