From: Brian Demsky Date: Sat, 23 Jul 2016 20:31:33 +0000 (-0700) Subject: more code X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=fab8d658ba7fadb167e72884a3cab2920b718178;ds=sidebyside more code --- diff --git a/src/java/iotcloud/Entry.java b/src/java/iotcloud/Entry.java index 9f85bac..076b4fc 100644 --- a/src/java/iotcloud/Entry.java +++ b/src/java/iotcloud/Entry.java @@ -6,7 +6,8 @@ abstract class Entry { static final byte TypeLastMessage = 2; static final byte TypeRejectedMessage = 3; static final byte TypeTableStatus = 4; - + boolean islive = true; + static Entry decode(ByteBuffer bb) { byte type=bb.get(); switch(type) { @@ -23,7 +24,17 @@ abstract class Entry { } } + boolean isLive() { + return islive; + } + + void setDead() { + islive = false; + } + abstract void encode(ByteBuffer bb); abstract int getSize(); + + abstract byte getType(); } diff --git a/src/java/iotcloud/KeyValue.java b/src/java/iotcloud/KeyValue.java index 39125e2..36a56dc 100644 --- a/src/java/iotcloud/KeyValue.java +++ b/src/java/iotcloud/KeyValue.java @@ -30,4 +30,8 @@ class KeyValue extends Entry { int getSize() { return 2*Integer.BYTES+key.length+value.length+Byte.BYTES; } + + byte getType() { + return Entry.TypeKeyValue; + } } diff --git a/src/java/iotcloud/LastMessage.java b/src/java/iotcloud/LastMessage.java index 023608b..23e6bcd 100644 --- a/src/java/iotcloud/LastMessage.java +++ b/src/java/iotcloud/LastMessage.java @@ -26,4 +26,10 @@ class LastMessage extends Entry { int getSize() { return 2*Long.BYTES+Byte.BYTES; } + + byte getType() { + return Entry.TypeLastMessage; + } } + + diff --git a/src/java/iotcloud/RejectedMessage.java b/src/java/iotcloud/RejectedMessage.java index c0dfaa6..f8af5ec 100644 --- a/src/java/iotcloud/RejectedMessage.java +++ b/src/java/iotcloud/RejectedMessage.java @@ -33,4 +33,8 @@ class RejectedMessage extends Entry { int getSize() { return 3*Long.BYTES + 2*Byte.BYTES; } + + byte getType() { + return Entry.TypeRejectedMessage; + } } diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index 8b6f619..8815a5b 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -8,11 +8,11 @@ class Slot { public static final int SLOT_SIZE=2048; public static final int HMAC_SIZE=32; - long seqnum; - byte[] prevhmac; - byte[] hmac; - long machineid; - Vector entries; + private long seqnum; + private byte[] prevhmac; + private byte[] hmac; + private long machineid; + private Vector entries; Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac, Vector _entries) { seqnum=_seqnum; @@ -33,6 +33,10 @@ class Slot { byte[] getPrevHMAC() { return prevhmac; } + + Vector getEntries() { + return entries; + } static Slot decode(byte[] array, Mac mac) { mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE); @@ -44,7 +48,7 @@ class Slot { bb.get(hmac); bb.get(prevhmac); if (!Arrays.equals(realmac, hmac)) - throw new Error("Invalid HMAC! Potential Attack!"); + throw new Error("Server Error: Invalid HMAC! Potential Attack!"); long seqnum=bb.getLong(); long machineid=bb.getLong(); diff --git a/src/java/iotcloud/SlotIndexer.java b/src/java/iotcloud/SlotIndexer.java new file mode 100644 index 0000000..4e344b7 --- /dev/null +++ b/src/java/iotcloud/SlotIndexer.java @@ -0,0 +1,24 @@ +package iotcloud; + +class SlotIndexer { + private Slot[] updates; + private SlotBuffer buffer; + private long firstslotseqnum; + + SlotIndexer(Slot[] _updates, SlotBuffer _buffer) { + buffer = _buffer; + updates = _updates; + firstslotseqnum = updates[0].getSequenceNumber(); + } + + Slot getSlot(long seqnum) { + if (seqnum >= firstslotseqnum) { + int offset = (int) (seqnum - firstslotseqnum); + if (offset >= updates.length) + throw new Error("Invalid Slot Sequence Number Reference"); + else + return updates[offset]; + } else + return buffer.getSlot(seqnum); + } +} diff --git a/src/java/iotcloud/Table.java b/src/java/iotcloud/Table.java index 5c6ec6b..679421e 100644 --- a/src/java/iotcloud/Table.java +++ b/src/java/iotcloud/Table.java @@ -7,15 +7,18 @@ import javax.crypto.*; public class Table { int numslots; HashMap table=new HashMap(); + HashMap lastmessage=new HashMap(); SlotBuffer buffer; CloudComm cloud; private Mac hmac; long sequencenumber; + long machineid; - public Table(String baseurl, String password) { - initCloud(baseurl, password); + public Table(String baseurl, String password, long _machineid) { + machineid=_machineid; buffer = new SlotBuffer(); sequencenumber = 1; + initCloud(baseurl, password); } private void initCloud(String baseurl, String password) { @@ -52,39 +55,65 @@ public class Table { void validateandupdate(Slot[] newslots) { //The cloud communication layer has checked slot HMACs already //before decoding - if (newslots.length==0) return; long firstseqnum=newslots[0].getSequenceNumber(); if (firstseqnum < sequencenumber) - throw new Error("Server sent older slots!"); + throw new Error("Server Error: Sent older slots!"); - checkHMACChain(newslots); - - if (firstseqnum == (buffer.getNewestSeqNum()+1)) { - //contiguous update - } else { - //non-contiguous update + SlotIndexer indexer = new SlotIndexer(newslots, buffer); + checkHMACChain(indexer, newslots); + for(Slot slot: newslots) { + processSlot(indexer, slot); } - } - void checkHMACChain(Slot[] newslots) { - if (newslots[0].getSequenceNumber() == (buffer.getNewestSeqNum()+1)) { - Slot prevslot=buffer.getSlot(buffer.getNewestSeqNum()); - Slot currslot=newslots[0]; - if (!Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC())) - throw new Error("Error in HMAC Chain"); - } + void processEntry(KeyValue entry, SlotIndexer indexer, Slot slot) { - for(int i=1; i < newslots.length; i++) { - Slot prevslot=newslots[i-1]; - Slot currslot=newslots[i]; - if (!Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC())) - throw new Error("Error in HMAC Chain"); + } + + void processEntry(LastMessage entry, SlotIndexer indexer, Slot slot) { + + } + + void processEntry(RejectedMessage entry, SlotIndexer indexer, Slot slot) { + + } + + void processEntry(TableStatus entry, SlotIndexer indexer, Slot slot) { + + } + + void processSlot(SlotIndexer indexer, Slot slot) { + for(Entry entry : slot.getEntries()) { + switch(entry.getType()) { + case Entry.TypeKeyValue: + processEntry((KeyValue)entry, indexer, slot); + break; + case Entry.TypeLastMessage: + processEntry((LastMessage)entry, indexer, slot); + break; + case Entry.TypeRejectedMessage: + processEntry((RejectedMessage)entry, indexer, slot); + break; + case Entry.TypeTableStatus: + processEntry((TableStatus)entry, indexer, slot); + break; + default: + throw new Error("Unrecognized type: "+entry.getType()); + } } } + void checkHMACChain(SlotIndexer indexer, Slot[] newslots) { + for(int i=0; i < newslots.length; i++) { + Slot currslot=newslots[i]; + Slot prevslot=indexer.getSlot(currslot.getSequenceNumber()-1); + if (prevslot != null && + !Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC())) + throw new Error("Server Error: Invalid HMAC Chain"); + } + } } diff --git a/src/java/iotcloud/TableStatus.java b/src/java/iotcloud/TableStatus.java index 27f9017..970ce18 100644 --- a/src/java/iotcloud/TableStatus.java +++ b/src/java/iotcloud/TableStatus.java @@ -21,4 +21,8 @@ class TableStatus extends Entry { int getSize() { return Integer.BYTES+Byte.BYTES; } + + byte getType() { + return Entry.TypeTableStatus; + } }