diff --git a/Documentation_codeChange_task2.docx b/Documentation_codeChange_task2.docx index cad8b3faeeb09dff0a4a593e8369f2b5a32ef2c4..03eb232228c3d1a5769d23fb4163cb9962d7933b 100644 Binary files a/Documentation_codeChange_task2.docx and b/Documentation_codeChange_task2.docx differ diff --git a/Documentation_codeChange_task2.pdf b/Documentation_codeChange_task2.pdf new file mode 100644 index 0000000000000000000000000000000000000000..846df6f545a37cb26f3f758ff9d2a8c1685b2188 Binary files /dev/null and b/Documentation_codeChange_task2.pdf differ diff --git a/Internetworking.zip b/Internetworking.zip deleted file mode 100644 index 65c5ff488dc246cf0597bc8de544bcfd470f7375..0000000000000000000000000000000000000000 Binary files a/Internetworking.zip and /dev/null differ diff --git a/internetworking_ws23.zip b/internetworking_ws23.zip new file mode 100644 index 0000000000000000000000000000000000000000..d493d9093c39214694934dc9d5377a8b888536fa Binary files /dev/null and b/internetworking_ws23.zip differ diff --git a/src/main/java/cp/CPCommandResponseMessage.java b/src/main/java/cp/CPCommandResponseMessage.java index 9c8fb47128c14f8fee4e72f01c3d5122f7f0556b..f4806303f6aab1ec47c827d4655a7b56cafa56f1 100644 --- a/src/main/java/cp/CPCommandResponseMessage.java +++ b/src/main/java/cp/CPCommandResponseMessage.java @@ -7,6 +7,7 @@ import exceptions.IllegalMsgException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.zip.CRC32; public class CPCommandResponseMessage extends CPMsg{ @@ -77,7 +78,20 @@ public class CPCommandResponseMessage extends CPMsg{ } - //comparing the CRC checksum + // Compare the CRC checksum + String receivedChecksumStr = fields[fields.length - 1]; + long receivedChecksum; + try { + receivedChecksum = Long.parseLong(receivedChecksumStr, 16); + } catch (NumberFormatException e) { + throw new IllegalMsgException(); + } + + long calculatedChecksum = this.calcCRC(reconstructedMessage.toString()); + + if (calculatedChecksum != receivedChecksum) { + throw new IllegalMsgException(); + } //sets the received message as the data, so the client can print it on the console diff --git a/src/main/java/cp/CPCookie.java b/src/main/java/cp/CPCookie.java index cd8ed74a6bb59d907fab4ab00146f52d96d028a1..258a3bd738383be296e8c7107f98db9ac058ffca 100644 --- a/src/main/java/cp/CPCookie.java +++ b/src/main/java/cp/CPCookie.java @@ -2,18 +2,24 @@ package cp; public class CPCookie { - private int cookie; - private long createdAt; - CPCookie(long createdAt, int cookie){ - this.createdAt = createdAt; - this.cookie = cookie; + //error Messages for Cookie Response Message + public static final String cookieErrorLimit = "COOKIE_ERR_LIMIT_REACHED"; + public static final String cookieErrorOldCookie = "COOKIE_ERR_OLD_COOKIE_VALID"; + public static final String cookieSuccess = "COOKIE_ERR_LIMIT_REACHED"; + + private int cookieValue; //"random" Number + private long createdAt; //time of Creation as "Unix time" + private static final long MAX_LIFETIME_SECONDS = 600; //600 Seconds, according to protocol specification + CPCookie(){ + this.createdAt = System.currentTimeMillis(); + this.cookieValue = 1; } - public int getCookie() { - return cookie; + public int getCookieValue() { + return cookieValue; } - public long getCreatedAt() { - return createdAt; + public boolean isStillValid(){ + return System.currentTimeMillis() - createdAt <= 600; //returns true, if the cookie has lived 600 Seconds or less } } diff --git a/src/main/java/cp/CPProtocol.java b/src/main/java/cp/CPProtocol.java index 44cfc8c02c240201b4daa8e373011ffbc93b7d59..88d1055cd9d9b6d52cc9c8927b8ba9838e59056c 100644 --- a/src/main/java/cp/CPProtocol.java +++ b/src/main/java/cp/CPProtocol.java @@ -21,7 +21,7 @@ public class CPProtocol extends Protocol { private PhyConfiguration PhyConfig; private PhyProtocol PhyProto; boolean isClient; - HashMap<PhyConfiguration, Integer> cookieMap; + HashMap<Integer, CPCookie> cookieMap; Random rnd; // Constructor for clients @@ -39,6 +39,10 @@ public class CPProtocol extends Protocol { } @Override public void send(String s, Configuration config) throws IOException, IWProtocolException { + this.send(s, "", config); + } + + public void send(String s, String message, Configuration config) throws IOException, IWProtocolException { if (cookie == null) { // Request a new cookie from server @@ -59,7 +63,7 @@ public class CPProtocol extends Protocol { //1.2: 1 b CPCommandMessage cmdMsg = new CPCommandMessage(); - cmdMsg.create(cookie, s, "", messageIdCount); + cmdMsg.create(cookie, s, message, messageIdCount); //remembers the id of the current message, so when it's time to receive wel can check if the id's are the same this.id = cmdMsg.getMessageId(); @@ -109,14 +113,31 @@ public class CPProtocol extends Protocol { return in; } - public void cookieProcesing(PhyConfiguration config, String cookie){ + + public void cookieProcesing(PhyConfiguration config, CPCookie cookie) throws IWProtocolException, IOException { + if(cookieMap.size() >= CP_HASHMAP_SIZE){ - System.out.println("Too many cookies."); + new CPServerCookieResponseMsg(false,"Cookie limit reached", cookie).sendResponse(this.PhyProto, config); + return; + } + + CPCookie recievedCookie = cookieMap.get(config.hashCode()); //is null when there is no cookie currently saved + + if(recievedCookie != null){ //if the cookie still exists + + if(recievedCookie.isStillValid()){ + + //tells the client that an old cookie has not expired + new CPServerCookieResponseMsg(false,"Old Cookie is still valid", cookie).sendResponse(this.PhyProto, config); + }else{ + cookieMap.remove(config.hashCode()); //removes the Cookie, because it has expired + } return; } - cookieMap.put(config, config.hashCode(cookie)); + cookieMap.put(config.hashCode(), cookie); + new CPServerCookieResponseMsg(true,"Cookie is valid", cookie).sendResponse(this.PhyProto, config); } diff --git a/src/main/java/cp/CPServerCookieResponseMsg.java b/src/main/java/cp/CPServerCookieResponseMsg.java new file mode 100644 index 0000000000000000000000000000000000000000..5c0a02e73cb965708fa20c59abb9f62ef65317ed --- /dev/null +++ b/src/main/java/cp/CPServerCookieResponseMsg.java @@ -0,0 +1,24 @@ +package cp; + +import exceptions.IWProtocolException; +import phy.PhyConfiguration; +import phy.PhyProtocol; + +import java.io.IOException; + +public class CPServerCookieResponseMsg extends CPCookieResponseMsg{ + + CPServerCookieResponseMsg(boolean success, String responseMessage, CPCookie cookie){ + super(success); + + if(success){ + super.create(String.valueOf(cookie.getCookieValue())); + }else { + super.create(responseMessage); + } + } + + public void sendResponse(PhyProtocol phy, PhyConfiguration config) throws IWProtocolException, IOException { + phy.send(super.data, config); + } +} diff --git a/src/main/java/phy/PhyConfiguration.java b/src/main/java/phy/PhyConfiguration.java index e434d09123e693f05f51590acf6eebf1e0027a7f..984ec135904ff9fa931e597836a4557a9e0e6c02 100644 --- a/src/main/java/phy/PhyConfiguration.java +++ b/src/main/java/phy/PhyConfiguration.java @@ -31,23 +31,17 @@ public class PhyConfiguration extends Configuration{ public Protocol.proto_id getPid() {return this.pid;} - public boolean equals(PhyConfiguration otherConfig ){ - return false; - } - public int hashCode(String cookie){ - return System.identityHashCode(cookie); - } - - - //generated using Alt+Insert @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof PhyConfiguration that)) return false; - return remotePort == that.remotePort && isClient == that.isClient && Objects.equals(remoteIPAddress, that.remoteIPAddress) && pid == that.pid; + + if(o instanceof PhyConfiguration phy1){ //no "null check" needed, because instanceof already covers that + + return phy1.hashCode() == this.hashCode(); //if the hash code is the same, the objects are the same + } + + return false; } - //generated using Alt+Insert @Override public int hashCode() { return Objects.hash(remotePort, remoteIPAddress, pid, isClient);