class CloudComm {
private static final int SALT_SIZE = 8;
- private static final int TIMEOUT_MILLIS = 100;
+ private static final int TIMEOUT_MILLIS = 25; // 100
+
+ /** Sets the size for the HMAC. */
+ static final int HMAC_SIZE = 32;
private String baseurl;
private Cipher encryptCipher;
return null;
}
try {
+
+ System.out.println("Passing Locally");
+
+ mac.update(sendData);
+ byte[] genmac = mac.doFinal();
+ byte[] totalData = new byte[sendData.length + genmac.length];
+ System.arraycopy(sendData, 0, totalData, 0, sendData.length);
+ System.arraycopy(genmac, 0, totalData, sendData.length, genmac.length);
+
// Encrypt the data for sending
- byte[] encryptedData = encryptCipher.doFinal(sendData);
+ // byte[] encryptedData = encryptCipher.doFinal(totalData);
+ byte[] encryptedData = encryptCipher.doFinal(totalData);
// Open a TCP socket connection to a local device
Socket socket = new Socket(host, port);
input.readFully(returnData);
returnData = decryptCipher.doFinal(returnData);
- // We are dont with this socket
+ // We are done with this socket
socket.close();
- return returnData;
+ mac.update(returnData, 0, returnData.length - HMAC_SIZE);
+ byte[] realmac = mac.doFinal();
+ byte[] recmac = new byte[HMAC_SIZE];
+ System.arraycopy(returnData, returnData.length - realmac.length, recmac, 0, realmac.length);
+
+ if (!Arrays.equals(recmac, realmac))
+ throw new Error("Local Error: Invalid HMAC! Potential Attack!");
+
+ byte[] returnData2 = new byte[lengthOfReturnData - recmac.length];
+ System.arraycopy(returnData, 0, returnData2, 0, returnData2.length);
+
+ return returnData2;
} catch (SocketTimeoutException e) {
} catch (BadPaddingException e) {
// Decrypt the data
readData = decryptCipher.doFinal(readData);
+ mac.update(readData, 0, readData.length - HMAC_SIZE);
+ byte[] genmac = mac.doFinal();
+ byte[] recmac = new byte[HMAC_SIZE];
+ System.arraycopy(readData, readData.length - recmac.length, recmac, 0, recmac.length);
+
+ if (!Arrays.equals(recmac, genmac))
+ throw new Error("Local Error: Invalid HMAC! Potential Attack!");
+
+ byte[] returnData = new byte[readData.length - recmac.length];
+ System.arraycopy(readData, 0, returnData, 0, returnData.length);
+
// Process the data
- byte[] sendData = table.acceptDataFromLocal(readData);
+ // byte[] sendData = table.acceptDataFromLocal(readData);
+ byte[] sendData = table.acceptDataFromLocal(returnData);
+
+ mac.update(sendData);
+ byte[] realmac = mac.doFinal();
+ byte[] totalData = new byte[sendData.length + realmac.length];
+ System.arraycopy(sendData, 0, totalData, 0, sendData.length);
+ System.arraycopy(realmac, 0, totalData, sendData.length, realmac.length);
// Encrypt the data for sending
- sendData = encryptCipher.doFinal(sendData);
+ byte[] encryptedData = encryptCipher.doFinal(totalData);
// Send data to output (length of data, the data)
- output.writeInt(sendData.length);
- output.write(sendData, 0, sendData.length);
+ output.writeInt(encryptedData.length);
+ output.write(encryptedData, 0, encryptedData.length);
output.flush();
// close the socket
private Map<Long, Long> lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = null;
-
-
-
public Table(String baseurl, String password, long _localMachineId, int listeningPort) {
localMachineId = _localMachineId;
cloud = new CloudComm(this, baseurl, password, listeningPort);
System.out.println("Old: " + o);
System.out.println("New: " + n);
System.out.println("Size: " + buffer.size());
+ System.out.println("Commits: " + liveCommitsTable.size());
+
+ for (Long a : liveCommitsTable.keySet()) {
+ for (Long b : liveCommitsTable.get(a).keySet()) {
+ for (KeyValue kv : liveCommitsTable.get(a).get(b).getKeyValueUpdateSet()) {
+ System.out.print(kv + " ");
+ }
+ System.out.print("|| ");
+ }
+ System.out.println();
+ }
- // List<IoTString> strList = new ArrayList<IoTString>();
- // for (int i = 0; i < 100; i++) {
- // String keyA = "a" + i;
- // String keyB = "b" + i;
- // String keyC = "c" + i;
- // String keyD = "d" + i;
-
- // IoTString iKeyA = new IoTString(keyA);
- // IoTString iKeyB = new IoTString(keyB);
- // IoTString iKeyC = new IoTString(keyC);
- // IoTString iKeyD = new IoTString(keyD);
-
- // strList.add(iKeyA);
- // strList.add(iKeyB);
- // strList.add(iKeyC);
- // strList.add(iKeyD);
- // }
-
-
- // for (Long l : commitMap.keySet()) {
- // for (Long l2 : commitMap.get(l).keySet()) {
- // for (KeyValue kv : commitMap.get(l).get(l2).getkeyValueUpdateSet()) {
- // strList.remove(kv.getKey());
- // System.out.print(kv.getKey() + " ");
- // }
- // }
- // }
-
- // System.out.println();
- // System.out.println();
-
- // for (IoTString s : strList) {
- // System.out.print(s + " ");
- // }
- // System.out.println();
- // System.out.println(strList.size());
}
/**
}
for (Transaction transaction : transactionPartsSent.keySet()) {
-
-
transaction.resetServerFailure();
-
// Update which transactions parts still need to be sent
transaction.removeSentParts(transactionPartsSent.get(transaction));
}
} catch (ServerException e) {
- System.out.println("Server Failure: " + e.getType());
-
+ // System.out.println("Server Failure: " + e.getType());
if (e.getType() != ServerException.TypeInputTimeout) {
// e.printStackTrace();
part.encode(bbEncode);
}
+
// Send by local
byte[] returnData = cloud.sendLocalData(sendData, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
}
public synchronized byte[] acceptDataFromLocal(byte[] data) {
+
// Decode the data
ByteBuffer bbDecode = ByteBuffer.wrap(data);
long lastArbitratedSequenceNumberSeen = bbDecode.getLong();
status.setStatus(TransactionStatus.StatusAborted);
}
} else {
-
Set addAbortSet = new HashSet<Abort>();
lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
}
-
-
// Update the last transaction that was updated if we can
if (commit.getTransactionSequenceNumber() != -1) {
Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
// We processed a new commit that we havent seen before
didProcessANewCommit = true;
-
-
// Update the committed table of keys and which commit is using which key
for (KeyValue kv : commit.getKeyValueUpdateSet()) {
committedKeyValueTable.put(kv.getKey(), kv);
while (t1.update() == false) {}
while (t2.update() == false) {}
while (t1.update() == false) {}
+ while (t2.update() == false) {}
System.out.println("Checking Key-Values...");
for (int i = 0; i < NUMBER_OF_TESTS; i++) {
private Set<Integer> missingParts = null;
private List<Integer> partsPendingSend = null;
private boolean isComplete = false;
+ private boolean hasLastPart = false;
private Set<KeyValue> keyValueGuardSet = null;
private Set<KeyValue> keyValueUpdateSet = null;
private boolean isDead = false;
previoslySeenPart.setDead();
} else if (newPart.isLastPart()) {
missingParts = new HashSet<Integer>();
+ hasLastPart = true;
for (int i = 0; i < newPart.getPartNumber(); i++) {
if (parts.get(i) == null) {
}
}
- if (!isComplete) {
+ if (!isComplete && hasLastPart) {
// We have seen this part so remove it from the set of missing parts
missingParts.remove(newPart.getPartNumber());
}
} else {
if (kv != null) {
- System.out.println("kvGuard was nulled: " + kv);
return false;
}
}
long clientLocalSequenceNumber = bb.getLong();
int partNumber = bb.getInt();
int dataSize = bb.getInt();
- Boolean isLastPart = bb.get() == 1;
-
+ Boolean isLastPart = (bb.get() == 1);
// Get the data
byte[] data = new byte[dataSize];
bb.get(data);