2 import java.util.Vector;
3 import java.nio.ByteBuffer;
4 import javax.crypto.Mac;
5 import java.util.Arrays;
8 * Data structuring for holding Slot information.
13 class Slot implements Liveness {
14 /** Sets the slot size. */
15 static final int SLOT_SIZE=2048;
16 /** Sets how many bytes we reserve. */
17 static final int RESERVED_SPACE=64;
18 /** Sets the size for the HMAC. */
19 static final int HMAC_SIZE=32;
21 /** Sequence number of the slot. */
22 private long seqnum;//
23 /** HMAC of previous slot. */
24 private byte[] prevhmac;//
25 /** HMAC of this slot. */
26 private byte[] hmac;//
27 /** Machine that sent this slot. */
28 private long machineid;//
29 /** Vector of entries in this slot. */
30 private Vector<Entry> entries;
31 /** Pieces of information that are live. */
32 private int livecount;//
33 /** Flag that indicates whether this slot is still live for
34 * recording the machine that sent it. */
35 private boolean seqnumlive;//
36 /** Number of bytes of free space. */
37 private int freespace;
39 Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
44 entries=new Vector<Entry>();
47 freespace = SLOT_SIZE - getBaseSize();
50 Slot(long _seqnum, long _machineid, byte[] _prevhmac) {
51 this(_seqnum, _machineid, _prevhmac, null);
54 Slot(long _seqnum, long _machineid) {
55 this(_seqnum, _machineid, new byte[ HMAC_SIZE], null);
62 byte[] getPrevHMAC() {
66 void addEntry(Entry e) {
70 freespace -= e.getSize();
73 private void addShallowEntry(Entry e) {
76 freespace -= e.getSize();
80 * Returns true if the slot has free space to hold the entry without
81 * using its reserved space. */
83 boolean hasSpace(Entry e) {
84 int newfreespace = freespace - e.getSize();
85 return newfreespace > RESERVED_SPACE;
89 * Returns true if the slot can fit the entry potentially using the
92 boolean canFit(Entry e) {
93 int newfreespace = freespace - e.getSize();
94 return newfreespace >= 0;
97 Vector<Entry> getEntries() {
101 static Slot decode(byte[] array, Mac mac) {
102 mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
103 byte[] realmac=mac.doFinal();
105 ByteBuffer bb=ByteBuffer.wrap(array);
106 byte[] hmac=new byte[HMAC_SIZE];
107 byte[] prevhmac=new byte[HMAC_SIZE];
110 if (!Arrays.equals(realmac, hmac))
111 throw new Error("Server Error: Invalid HMAC! Potential Attack!");
113 long seqnum=bb.getLong();
114 long machineid=bb.getLong();
115 int numentries=bb.getInt();
116 Slot slot=new Slot(seqnum, machineid, prevhmac, hmac);
118 for(int i=0; i<numentries; i++) {
119 slot.addShallowEntry(Entry.decode(slot, bb));
125 byte[] encode(Mac mac) {
126 byte[] array=new byte[SLOT_SIZE];
127 ByteBuffer bb=ByteBuffer.wrap(array);
128 /* Leave space for the slot HMAC. */
129 bb.position(HMAC_SIZE);
132 bb.putLong(machineid);
133 bb.putInt(entries.size());
134 for(Entry entry:entries) {
137 /* Compute our HMAC */
138 mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
139 byte[] realmac=mac.doFinal();
147 * Returns the empty size of a Slot. Includes 2 HMACs, the machine
148 * identifier, the sequence number, and the number of entries.
151 return 2*HMAC_SIZE+2*Long.BYTES+Integer.BYTES;
155 * Returns the live set of entries for this Slot. Generates a fake
156 * LastMessage entry to represent the information stored by the slot
160 Vector<Entry> getLiveEntries() {
161 Vector<Entry> liveEntries=new Vector<Entry>();
162 for(Entry entry: entries) {
164 liveEntries.add(entry);
168 liveEntries.add(new LastMessage(this, machineid, seqnum));
174 * Returns the sequence number of the slot.
177 long getSequenceNumber() {
182 * Returns the machine that sent this slot.
185 long getMachineID() {
190 * Records that a newer slot records the fact that this slot was
191 * sent by the relevant machine.
196 decrementLiveCount();
200 * Update the count of live entries.
203 void decrementLiveCount() {
205 Vector<Entry> e=getLiveEntries();
209 * Returns whether the slot stores any live information.
213 return livecount > 0;
216 public String toString() {
217 return "<"+getSequenceNumber()+">";