2 constructor(table, seqnum, machineid, prevhmac, hmac) {
4 this.RESERVED_SPACE = 64;
6 if(typeof seqnum === "number"){
9 throw new Error("seqnum should be a number");
11 if(typeof machineid === "number"){
12 this.machineid = machineid;
14 throw new Error("machine should be a number");
17 this.seqnumlive = true;
18 if(prevhmac && prevhmac instanceof Uint8Array){
19 this.prevhmac = prevhmac;
21 throw new Error("prevhmac input not valid");
23 if(hmac && hmac instanceof Uint8Array){
26 throw new Error("Hmac input not valid");
29 this.freespace = this.SLOT_SIZE - getBaseSize(); //???????
39 if (entry && entry instanceof Entry) {
41 this.entries.push(_.extend(obj, entry));
43 this.freespace -= entry.getSize();
46 addShallowEntry(entry){
47 if(entry && entry instanceof Entry){
48 this.entries.push(entry);
50 this.freespace -= entry.getSize();
54 var newfreespace = this.freespace - entry.getSize();
55 return newfreespace > this.RESERVED_SPACE;
60 static decode(table, array, key){
61 var cond1 = (array && array instanceof Uint8Array);
63 var mac = crypto.HmacMD5(array.slice(this.HMAC_SIZE).join([separator='']),key);
65 var realmac = new Uint8Array(mac.length);
66 for(var i=0; i< mac.length ; i++){
67 realmac[i] = mac.charCodeAt(i);
70 var bb = new ByteBuffer().wrap(array.join([separator=''])).flip();
71 var hmac= new Uint8Array(this.HMAC_SIZE);
72 var prevhmac = new Uint8Array(this.HMAC_SIZE);
73 for(var i = 0; i<this.HMAC_SIZE; i++){
74 hmac[i] = bb.readByte();
76 for(var j = 0; j<this.HMAC_SIZE; j++){
77 prevhmac[j] = bb.readByte();
80 for(var k =0; k<this.HMAC_SIZE; k++){
81 if(!(hmac[k] === realmac[k])){
82 throw new Error("Server Error: Invalid HMAC! Potential Attack!");
85 var _seqnum = bb.readLong();
86 var _machineid = bb.readLong();
87 var numentries = bb.readInt8();
89 var _slot = new Slot(table,_seqnum,_machineid,prevhmac,hmac);
91 for(var l= 0 ;l<numentries;l++){
92 _slot.addShallowEntry(Entry.decode(_slot,bb));
98 var array = new Uint8Array(this.SLOT_SIZE);
99 var bb = new ByteBuffer().wrap(array.join([separator='']))
100 //leave space for slot HMAC
101 bb.skip(this.HMAC_SIZE);
102 bb.writeIString(this.prevhmac.join([separator='']));
103 bb.writeInt64(this.seqnum);
104 bb.writeInt64(this.machineid);
105 bb.writeByte(this.entries.length);
106 this.entries.forEach((entry) => entry.encode(bb));
108 var mac = crypto.HmacMD5(array.slice(this.HMAC_SIZE).join([separator='']),key);
109 var realmac = new Uint8Array(mac.length);
110 for(var i=0; i< mac.length ; i++){
111 realmac[i] = mac.charCodeAt(i);
115 bb.wrap(realmac.join([separator='']));
116 return new Uint8Array(bb.toArrayBuffer());
119 * Returns the empty size of a Slot. Includes 2 HMACs, the machine
120 * identifier, the sequence number, and the number of entries.
123 return 2*this.HMAC_SIZE+2*8+2*4; //Problematic area
126 getLiveEntries(resize){
127 var liveEntries = [];
128 this.entries.forEach(function(entry){
129 if(entry.isLive === true){
130 if(!resize || entry.getType() !== entry.TypeTableStatus){
131 liveEntries.push(entry);
136 if(this.seqnumlive && !resize){
137 liveEntries.push(new LastMessage(this,this.machineid,this.seqnumlive))
143 getSequenceNumber() {
148 return this.machineid;
152 this.seqnumlive=false;
153 this.decrementLiveCount();
156 decrementLiveCount() {
158 if (this.livecount === 0)
159 this.table.decrementLiveCount();
163 return this.livecount > 0;
167 return '<'+this.getSequenceNumber()+'>';