blob: 90186a545b8be9eb2075b8b37ddbb11fcd990596 [file] [log] [blame]
Wentao Shangc0311e52012-12-03 10:38:23 -08001/**
Jeff Thompson5b265a72012-11-12 01:13:08 -08002 * @author: Meki Cherkaoui, Jeff Thompson, Wentao Shang
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
4 * This class represents the top-level object for communicating with an NDN host.
5 */
Meki Cherkaoui8f173612012-06-06 01:05:40 -07006
Jeff Thompson3c263812012-12-01 17:20:28 -08007var LOG = 0;
Jeff Thompson5b265a72012-11-12 01:13:08 -08008
Jeff Thompsone06b31e2012-09-30 17:19:19 -07009/**
Jeff Thompson5b265a72012-11-12 01:13:08 -080010 * settings is an associative array with the following defaults:
11 * {
Jeff Thompson5b265a72012-11-12 01:13:08 -080012 * getTransport: function() { return new WebSocketTransport(); }
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080013 * getHostAndPort: transport.defaultGetHostAndPort,
Jeff Thompsond771b122013-01-26 19:04:41 -080014 * host: null, // If null, use getHostAndPort when connecting.
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080015 * port: 9696,
Wentao Shangc0311e52012-12-03 10:38:23 -080016 * onopen: function() { if (LOG > 3) console.log("NDN connection established."); }
17 * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); }
Jeff Thompson5b265a72012-11-12 01:13:08 -080018 * }
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080019 *
20 * getHostAndPort is a function, on each call it returns a new { host: host, port: port } or
21 * null if there are no more hosts.
Jeff Thompsone06b31e2012-09-30 17:19:19 -070022 */
Jeff Thompson5b265a72012-11-12 01:13:08 -080023var NDN = function NDN(settings) {
24 settings = (settings || {});
Jeff Thompson5b265a72012-11-12 01:13:08 -080025 var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); });
Wentao Shang0e291c82012-12-02 23:36:29 -080026 this.transport = getTransport();
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080027 this.getHostAndPort = (settings.getHostAndPort || this.transport.defaultGetHostAndPort);
Jeff Thompsond771b122013-01-26 19:04:41 -080028 this.host = (settings.host !== undefined ? settings.host : null);
Jeff Thompsond3a80dc2012-12-16 17:52:43 -080029 this.port = (settings.port || 9696);
Wentao Shang0e291c82012-12-02 23:36:29 -080030 this.readyStatus = NDN.UNOPEN;
Wentao Shangd4607392013-01-24 23:08:49 -080031 this.verify = (settings.verify !== undefined ? settings.verify : true);
Wentao Shang0e291c82012-12-02 23:36:29 -080032 // Event handler
Wentao Shangc0311e52012-12-03 10:38:23 -080033 this.onopen = (settings.onopen || function() { if (LOG > 3) console.log("NDN connection established."); });
34 this.onclose = (settings.onclose || function() { if (LOG > 3) console.log("NDN connection closed."); });
Jeff Thompson75771cb2013-01-20 23:27:38 -080035 this.ccndid = null;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070036};
37
Wentao Shang0e291c82012-12-02 23:36:29 -080038NDN.UNOPEN = 0; // created but not opened yet
39NDN.OPENED = 1; // connection to ccnd opened
40NDN.CLOSED = 2; // connection to ccnd closed
Jeff Thompson5b265a72012-11-12 01:13:08 -080041
Wentao Shangb42483a2013-01-03 15:32:32 -080042NDN.ccndIdFetcher = new Name('/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY');
Jeff Thompson5b265a72012-11-12 01:13:08 -080043
Wentao Shangb42483a2013-01-03 15:32:32 -080044NDN.prototype.createRoute = function(host, port) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -070045 this.host=host;
46 this.port=port;
Wentao Shangb42483a2013-01-03 15:32:32 -080047};
Meki Cherkaoui8f173612012-06-06 01:05:40 -070048
Wentao Shang82854bd2012-12-27 14:14:41 -080049
50NDN.KeyStore = new Array();
51
Wentao Shangb5d0c3e2012-12-30 11:12:03 -080052var KeyStoreEntry = function KeyStoreEntry(name, rsa, time) {
Wentao Shang82854bd2012-12-27 14:14:41 -080053 this.keyName = name; // KeyName
Wentao Shang82854bd2012-12-27 14:14:41 -080054 this.rsaKey = rsa; // RSA key
Wentao Shangb5d0c3e2012-12-30 11:12:03 -080055 this.timeStamp = time; // Time Stamp
56};
57
58NDN.addKeyEntry = function(/* KeyStoreEntry */ keyEntry) {
59 var result = NDN.getKeyByName(keyEntry.keyName);
60 if (result == null)
61 NDN.KeyStore.push(keyEntry);
62 else
63 result = keyEntry;
Wentao Shang82854bd2012-12-27 14:14:41 -080064};
65
66NDN.getKeyByName = function(/* KeyName */ name) {
67 var result = null;
68
69 for (var i = 0; i < NDN.KeyStore.length; i++) {
Wentao Shangb42483a2013-01-03 15:32:32 -080070 if (NDN.KeyStore[i].keyName.contentName.match(name.contentName)) {
Wentao Shang82854bd2012-12-27 14:14:41 -080071 if (result == null ||
72 NDN.KeyStore[i].keyName.contentName.components.length > result.keyName.contentName.components.length)
73 result = NDN.KeyStore[i];
74 }
75 }
76
77 return result;
78};
79
Jeff Thompsonbe85be62012-12-13 22:32:01 -080080// For fetching data
81NDN.PITTable = new Array();
82
83var PITEntry = function PITEntry(interest, closure) {
84 this.interest = interest; // Interest
85 this.closure = closure; // Closure
Wentao Shangfcb16262013-01-20 14:42:46 -080086 this.timerID = -1; // Timer ID
Jeff Thompsonbe85be62012-12-13 22:32:01 -080087};
88
Jeff Thompson202728a2013-02-10 22:20:08 -080089/*
90 * Return the entry from NDN.PITTable where the name conforms to the interest selectors, and
91 * the interest name is the longest that matches name.
92 */
Jeff Thompsonbe85be62012-12-13 22:32:01 -080093NDN.getEntryForExpressedInterest = function(/*Name*/ name) {
Jeff Thompsonbe85be62012-12-13 22:32:01 -080094 var result = null;
95
96 for (var i = 0; i < NDN.PITTable.length; i++) {
97 if (NDN.PITTable[i].interest.matches_name(name)) {
98 if (result == null ||
99 NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length)
100 result = NDN.PITTable[i];
101 }
102 }
103
104 return result;
105};
106
Jeff Thompson75771cb2013-01-20 23:27:38 -0800107// For publishing data
108NDN.CSTable = new Array();
109
110var CSEntry = function CSEntry(name, closure) {
111 this.name = name; // String
112 this.closure = closure; // Closure
113};
114
115function getEntryForRegisteredPrefix(name) {
116 for (var i = 0; i < NDN.CSTable.length; i++) {
117 if (NDN.CSTable[i].name.match(name) != null)
118 return NDN.CSTable[i];
119 }
120 return null;
121}
122
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800123/*
124 * Return a function that selects a host at random from hostList and returns { host: host, port: port }.
125 * If no more hosts remain, return null.
126 */
127NDN.makeShuffledGetHostAndPort = function(hostList, port) {
128 // Make a copy.
129 hostList = hostList.slice(0, hostList.length);
130 DataUtils.shuffle(hostList);
131
132 return function() {
133 if (hostList.length == 0)
134 return null;
135
136 return { host: hostList.splice(0, 1)[0], port: port };
137 };
138};
139
Jeff Thompson34419762012-10-15 22:24:12 -0700140/** Encode name as an Interest. If template is not null, use its attributes.
141 * Send the interest to host:port, read the entire response and call
142 * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED),
Jeff Thompson97f27432012-10-16 00:28:03 -0700143 * new UpcallInfo(this, interest, 0, contentObject)).
Jeff Thompson34419762012-10-15 22:24:12 -0700144 */
145NDN.prototype.expressInterest = function(
146 // Name
147 name,
148 // Closure
149 closure,
150 // Interest
151 template) {
Jeff Thompson5b265a72012-11-12 01:13:08 -0800152 var interest = new Interest(name);
Jeff Thompson34419762012-10-15 22:24:12 -0700153 if (template != null) {
Jeff Thompson4404ab52012-10-21 10:29:48 -0700154 interest.minSuffixComponents = template.minSuffixComponents;
155 interest.maxSuffixComponents = template.maxSuffixComponents;
156 interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
157 interest.exclude = template.exclude;
158 interest.childSelector = template.childSelector;
159 interest.answerOriginKind = template.answerOriginKind;
160 interest.scope = template.scope;
161 interest.interestLifetime = template.interestLifetime;
Jeff Thompson34419762012-10-15 22:24:12 -0700162 }
163 else
Jeff Thompson42806a12012-12-29 18:19:39 -0800164 interest.interestLifetime = 4000; // default interest timeout value in milliseconds.
Jeff Thompson34419762012-10-15 22:24:12 -0700165
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800166 if (this.host == null || this.port == null) {
167 if (this.getHostAndPort == null)
168 console.log('ERROR: host OR port NOT SET');
Jeff Thompsona5668d52013-01-26 16:23:27 -0800169 else {
170 var thisNDN = this;
171 this.connectAndExecute
Jeff Thompsonf668acb2013-01-26 20:29:46 -0800172 (function() { thisNDN.reconnectAndExpressInterest(interest, closure); });
Jeff Thompsona5668d52013-01-26 16:23:27 -0800173 }
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800174 }
175 else
Jeff Thompsonf668acb2013-01-26 20:29:46 -0800176 this.reconnectAndExpressInterest(interest, closure);
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800177};
Jeff Thompson5b265a72012-11-12 01:13:08 -0800178
Jeff Thompson537ce142013-01-26 20:02:48 -0800179/*
Jeff Thompsonf668acb2013-01-26 20:29:46 -0800180 * If the host and port are different than the ones in this.transport, then call
181 * this.transport.connect to change the connection (or connect for the first time).
182 * Then call expressInterestHelper.
183 */
184NDN.prototype.reconnectAndExpressInterest = function(interest, closure) {
185 if (this.transport.connectedHost != this.host || this.transport.connectedPort != this.port) {
186 var thisNDN = this;
187 this.transport.connect(thisNDN, function() { thisNDN.expressInterestHelper(interest, closure); });
188 }
189 else
190 this.expressInterestHelper(interest, closure);
191};
192
193/*
194 * Do the work of reconnectAndExpressInterest once we know we are connected. Set the PITTable and call
Jeff Thompson537ce142013-01-26 20:02:48 -0800195 * this.transport.send to send the interest.
196 */
197NDN.prototype.expressInterestHelper = function(interest, closure) {
Jeff Thompsone8de5822013-02-17 21:18:49 -0800198 var binaryInterest = encodeToBinaryInterest(interest);
199 var thisNDN = this;
Jeff Thompson537ce142013-01-26 20:02:48 -0800200 //TODO: check local content store first
201 if (closure != null) {
202 var pitEntry = new PITEntry(interest, closure);
203 // TODO: This needs to be a single thread-safe transaction on a global object.
204 NDN.PITTable.push(pitEntry);
205 closure.pitEntry = pitEntry;
Jeff Thompson537ce142013-01-26 20:02:48 -0800206
Jeff Thompsone8de5822013-02-17 21:18:49 -0800207 // Set interest timer.
Jeff Thompsona53e65a2013-02-10 10:52:52 -0800208 var timeoutMilliseconds = (interest.interestLifetime || 4000);
Jeff Thompsone8de5822013-02-17 21:18:49 -0800209 var timeoutCallback = function() {
210 if (LOG > 3) console.log("Interest time out: " + interest.name.to_uri());
Jeff Thompson537ce142013-01-26 20:02:48 -0800211
Jeff Thompsone8de5822013-02-17 21:18:49 -0800212 // Remove PIT entry from NDN.PITTable, even if we add it again later to re-express
213 // the interest because we don't want to match it in the mean time.
Jeff Thompson537ce142013-01-26 20:02:48 -0800214 // TODO: Make this a thread-safe operation on the global PITTable.
215 var index = NDN.PITTable.indexOf(pitEntry);
Jeff Thompson537ce142013-01-26 20:02:48 -0800216 if (index >= 0)
217 NDN.PITTable.splice(index, 1);
Jeff Thompson537ce142013-01-26 20:02:48 -0800218
219 // Raise closure callback
Jeff Thompsone8de5822013-02-17 21:18:49 -0800220 if (closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT,
221 new UpcallInfo(thisNDN, interest, 0, null)) == Closure.RESULT_REEXPRESS) {
222 if (LOG > 3) console.log("Re-express interest: " + interest.name.to_uri());
223 pitEntry.timerID = setTimeout(timeoutCallback, timeoutMilliseconds);
224 NDN.PITTable.push(pitEntry);
225 thisNDN.transport.send(binaryInterest);
226 }
227 };
228 pitEntry.timerID = setTimeout(timeoutCallback, timeoutMilliseconds);
Jeff Thompson537ce142013-01-26 20:02:48 -0800229 }
230
Jeff Thompsone8de5822013-02-17 21:18:49 -0800231 this.transport.send(binaryInterest);
Jeff Thompson537ce142013-01-26 20:02:48 -0800232};
233
Jeff Thompson5b265a72012-11-12 01:13:08 -0800234NDN.prototype.registerPrefix = function(name, closure, flag) {
Jeff Thompsond771b122013-01-26 19:04:41 -0800235 var thisNDN = this;
236 var onConnected = function() {
237 if (thisNDN.ccndid == null) {
238 // Fetch ccndid first, then register.
239 var interest = new Interest(NDN.ccndIdFetcher);
240 interest.interestLifetime = 4000; // milliseconds
241 if (LOG>3) console.log('Expressing interest for ccndid from ccnd.');
Jeff Thompsonf668acb2013-01-26 20:29:46 -0800242 thisNDN.reconnectAndExpressInterest
243 (interest, new NDN.FetchCcndidClosure(thisNDN, name, closure, flag));
Jeff Thompsond771b122013-01-26 19:04:41 -0800244 }
245 else
246 thisNDN.registerPrefixHelper(name, closure, flag);
247 };
Jeff Thompson75771cb2013-01-20 23:27:38 -0800248
Jeff Thompsond771b122013-01-26 19:04:41 -0800249 if (this.host == null || this.port == null) {
250 if (this.getHostAndPort == null)
251 console.log('ERROR: host OR port NOT SET');
252 else
253 this.connectAndExecute(onConnected);
254 }
255 else
256 onConnected();
257};
258
259/*
260 * This is a closure to receive the ContentObject for NDN.ccndIdFetcher and call
261 * registerPrefixHelper(name, callerClosure, flag).
262 */
263NDN.FetchCcndidClosure = function FetchCcndidClosure(ndn, name, callerClosure, flag) {
264 // Inherit from Closure.
265 Closure.call(this);
266
267 this.ndn = ndn;
268 this.name = name;
269 this.callerClosure = callerClosure;
270 this.flag = flag;
271};
272
273NDN.FetchCcndidClosure.prototype.upcall = function(kind, upcallInfo) {
274 if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
275 console.log("Timeout while requesting the ccndid. Cannot registerPrefix for " +
276 this.name.to_uri() + " .");
277 return Closure.RESULT_OK;
278 }
279 if (!(kind == Closure.UPCALL_CONTENT ||
280 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
281 // The upcall is not for us.
282 return Closure.RESULT_ERR;
283
284 var co = upcallInfo.contentObject;
285 if (!co.signedInfo || !co.signedInfo.publisher
286 || !co.signedInfo.publisher.publisherPublicKeyDigest)
287 console.log
288 ("ContentObject doesn't have a publisherPublicKeyDigest. Cannot set ccndid and registerPrefix for "
289 + this.name.to_uri() + " .");
290 else {
291 if (LOG>3) console.log('Got ccndid from ccnd.');
292 this.ndn.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest;
293 if (LOG>3) console.log(this.ndn.ccndid);
294
295 this.ndn.registerPrefixHelper(this.name, this.callerClosure, this.flag);
Jeff Thompson75771cb2013-01-20 23:27:38 -0800296 }
Jeff Thompsond771b122013-01-26 19:04:41 -0800297
298 return Closure.RESULT_OK;
299};
300
Jeff Thompson537ce142013-01-26 20:02:48 -0800301/*
302 * Do the work of registerPrefix once we know we are connected with a ccndid.
303 */
Jeff Thompsond771b122013-01-26 19:04:41 -0800304NDN.prototype.registerPrefixHelper = function(name, closure, flag) {
Jeff Thompson75771cb2013-01-20 23:27:38 -0800305 var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647);
306 var bytes = encodeForwardingEntry(fe);
307
308 var si = new SignedInfo();
309 si.setFields();
310
311 var co = new ContentObject(new Name(), si, bytes, new Signature());
312 co.sign();
313 var coBinary = encodeToBinaryContentObject(co);
314
315 //var nodename = unescape('%00%88%E2%F4%9C%91%16%16%D6%21%8E%A0c%95%A5%A6r%11%E0%A0%82%89%A6%A9%85%AB%D6%E2%065%DB%AF');
316 var nodename = this.ccndid;
317 var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]);
318
319 var interest = new Interest(interestName);
320 interest.scope = 1;
321 if (LOG > 3) console.log('Send Interest registration packet.');
322
323 var csEntry = new CSEntry(name.getName(), closure);
324 NDN.CSTable.push(csEntry);
325
326 this.transport.send(encodeToBinaryInterest(interest));
Jeff Thompson75771cb2013-01-20 23:27:38 -0800327};
328
329/*
330 * This is called when an entire binary XML element is received, such as a ContentObject or Interest.
331 * Look up in the PITTable and call the closure callback.
332 */
333NDN.prototype.onReceivedElement = function(element) {
Jeff Thompsona46083c2013-01-20 23:55:21 -0800334 if (LOG>3) console.log('Complete element received. Length ' + element.length + '. Start decoding.');
Jeff Thompson75771cb2013-01-20 23:27:38 -0800335 var decoder = new BinaryXMLDecoder(element);
336 // Dispatch according to packet type
337 if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet
338 if (LOG > 3) console.log('Interest packet received.');
339
340 var interest = new Interest();
341 interest.from_ccnb(decoder);
342 if (LOG > 3) console.log(interest);
343 var nameStr = escape(interest.name.getName());
344 if (LOG > 3) console.log(nameStr);
345
346 var entry = getEntryForRegisteredPrefix(nameStr);
347 if (entry != null) {
348 //console.log(entry);
349 var info = new UpcallInfo(this, interest, 0, null);
350 var ret = entry.closure.upcall(Closure.UPCALL_INTEREST, info);
351 if (ret == Closure.RESULT_INTEREST_CONSUMED && info.contentObject != null)
352 this.transport.send(encodeToBinaryContentObject(info.contentObject));
353 }
354 } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet
355 if (LOG > 3) console.log('ContentObject packet received.');
356
357 var co = new ContentObject();
358 co.from_ccnb(decoder);
359
Jeff Thompsond771b122013-01-26 19:04:41 -0800360 var pitEntry = NDN.getEntryForExpressedInterest(co.name);
361 if (pitEntry != null) {
362 //console.log(pitEntry);
363 // Remove PIT entry from NDN.PITTable
364 var index = NDN.PITTable.indexOf(pitEntry);
365 if (index >= 0)
366 NDN.PITTable.splice(index, 1);
Jeff Thompson75771cb2013-01-20 23:27:38 -0800367
Jeff Thompsond771b122013-01-26 19:04:41 -0800368 var currentClosure = pitEntry.closure;
Jeff Thompson75771cb2013-01-20 23:27:38 -0800369
Jeff Thompsond771b122013-01-26 19:04:41 -0800370 // Cancel interest timer
371 clearTimeout(pitEntry.timerID);
372 //console.log("Clear interest timer");
373 //console.log(currentClosure.timerID);
374
375 if (this.verify == false) {
376 // Pass content up without verifying the signature
377 currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, new UpcallInfo(this, null, 0, co));
378 return;
Jeff Thompson75771cb2013-01-20 23:27:38 -0800379 }
Wentao Shangd4607392013-01-24 23:08:49 -0800380
Jeff Thompsond771b122013-01-26 19:04:41 -0800381 // Key verification
382
383 // Recursive key fetching & verification closure
384 var KeyFetchClosure = function KeyFetchClosure(content, closure, key, sig, wit) {
385 this.contentObject = content; // unverified content object
386 this.closure = closure; // closure corresponding to the contentObject
387 this.keyName = key; // name of current key to be fetched
388 this.sigHex = sig; // hex signature string to be verified
389 this.witness = wit;
390
391 Closure.call(this);
392 };
393
394 var thisNDN = this;
395 KeyFetchClosure.prototype.upcall = function(kind, upcallInfo) {
396 if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
397 console.log("In KeyFetchClosure.upcall: interest time out.");
398 console.log(this.keyName.contentName.getName());
399 } else if (kind == Closure.UPCALL_CONTENT) {
400 //console.log("In KeyFetchClosure.upcall: signature verification passed");
401
402 var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content);
403 var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.witness, this.sigHex);
404
405 var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
406 //console.log("raise encapsulated closure");
407 this.closure.upcall(flag, new UpcallInfo(thisNDN, null, 0, this.contentObject));
408
409 // Store key in cache
410 var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime());
411 NDN.addKeyEntry(keyEntry);
412 //console.log(NDN.KeyStore);
413 } else if (kind == Closure.UPCALL_CONTENT_BAD) {
414 console.log("In KeyFetchClosure.upcall: signature verification failed");
Wentao Shangd4607392013-01-24 23:08:49 -0800415 }
Jeff Thompsond771b122013-01-26 19:04:41 -0800416 };
Jeff Thompson75771cb2013-01-20 23:27:38 -0800417
Jeff Thompsond771b122013-01-26 19:04:41 -0800418 if (co.signedInfo && co.signedInfo.locator && co.signature) {
419 if (LOG > 3) console.log("Key verification...");
420 var sigHex = DataUtils.toHex(co.signature.signature).toLowerCase();
Jeff Thompson75771cb2013-01-20 23:27:38 -0800421
Jeff Thompsond771b122013-01-26 19:04:41 -0800422 var wit = null;
423 if (co.signature.Witness != null) {
424 wit = new Witness();
425 wit.decode(co.signature.Witness);
426 }
Jeff Thompson75771cb2013-01-20 23:27:38 -0800427
Jeff Thompsond771b122013-01-26 19:04:41 -0800428 var keylocator = co.signedInfo.locator;
429 if (keylocator.type == KeyLocatorType.KEYNAME) {
430 if (LOG > 3) console.log("KeyLocator contains KEYNAME");
431 //var keyname = keylocator.keyName.contentName.getName();
432 //console.log(nameStr);
433 //console.log(keyname);
Jeff Thompson75771cb2013-01-20 23:27:38 -0800434
Jeff Thompsond771b122013-01-26 19:04:41 -0800435 if (keylocator.keyName.contentName.match(co.name)) {
436 if (LOG > 3) console.log("Content is key itself");
Jeff Thompson75771cb2013-01-20 23:27:38 -0800437
Jeff Thompsond771b122013-01-26 19:04:41 -0800438 var rsakey = decodeSubjectPublicKeyInfo(co.content);
439 var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex);
440 var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
441
442 currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co));
443
444 // SWT: We don't need to store key here since the same key will be
445 // stored again in the closure.
446 //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime());
447 //NDN.addKeyEntry(keyEntry);
448 //console.log(NDN.KeyStore);
449 } else {
450 // Check local key store
451 var keyEntry = NDN.getKeyByName(keylocator.keyName);
452 if (keyEntry) {
453 // Key found, verify now
454 if (LOG > 3) console.log("Local key cache hit");
455 var rsakey = keyEntry.rsaKey;
Jeff Thompson75771cb2013-01-20 23:27:38 -0800456 var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex);
457 var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
Jeff Thompson75771cb2013-01-20 23:27:38 -0800458
Jeff Thompsond771b122013-01-26 19:04:41 -0800459 // Raise callback
460 currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co));
461 } else {
462 // Not found, fetch now
463 if (LOG > 3) console.log("Fetch key according to keylocator");
464 var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit);
465 this.expressInterest(keylocator.keyName.contentName.getPrefix(4), nextClosure);
Jeff Thompson75771cb2013-01-20 23:27:38 -0800466 }
Jeff Thompson75771cb2013-01-20 23:27:38 -0800467 }
Jeff Thompsond771b122013-01-26 19:04:41 -0800468 } else if (keylocator.type == KeyLocatorType.KEY) {
469 if (LOG > 3) console.log("Keylocator contains KEY");
470
471 var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey);
472 var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex);
473
474 var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
475 // Raise callback
476 currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(this, null, 0, co));
477
478 // Since KeyLocator does not contain key name for this key,
479 // we have no way to store it as a key entry in KeyStore.
480 } else {
481 var cert = keylocator.certificate;
482 console.log("KeyLocator contains CERT");
483 console.log(cert);
484
485 // TODO: verify certificate
Jeff Thompson75771cb2013-01-20 23:27:38 -0800486 }
487 }
488 }
489 } else
490 console.log('Incoming packet is not Interest or ContentObject. Discard now.');
Wentao Shangb42483a2013-01-03 15:32:32 -0800491};
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800492
493/*
494 * Assume this.getHostAndPort is not null. This is called when this.host is null or its host
Jeff Thompsona5668d52013-01-26 16:23:27 -0800495 * is not alive. Get a host and port, connect, then execute onConnected().
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800496 */
Jeff Thompsona5668d52013-01-26 16:23:27 -0800497NDN.prototype.connectAndExecute = function(onConnected) {
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800498 var hostAndPort = this.getHostAndPort();
499 if (hostAndPort == null) {
500 console.log('ERROR: No more hosts from getHostAndPort');
501 this.host = null;
502 return;
503 }
504
505 if (hostAndPort.host == this.host && hostAndPort.port == this.port) {
506 console.log('ERROR: The host returned by getHostAndPort is not alive: ' +
507 this.host + ":" + this.port);
508 return;
509 }
510
511 this.host = hostAndPort.host;
512 this.port = hostAndPort.port;
Jeff Thompsona5668d52013-01-26 16:23:27 -0800513 if (LOG>3) console.log("Connect: trying host from getHostAndPort: " + this.host);
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800514
Jeff Thompson75771cb2013-01-20 23:27:38 -0800515 // Fetch any content.
516 var interest = new Interest(new Name("/"));
Jeff Thompson42806a12012-12-29 18:19:39 -0800517 interest.interestLifetime = 4000; // milliseconds
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800518
519 var thisNDN = this;
520 var timerID = setTimeout(function() {
Jeff Thompsona5668d52013-01-26 16:23:27 -0800521 if (LOG>3) console.log("Connect: timeout waiting for host " + thisNDN.host);
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800522 // Try again.
Jeff Thompsona5668d52013-01-26 16:23:27 -0800523 thisNDN.connectAndExecute(onConnected);
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800524 }, 3000);
525
Jeff Thompsonf668acb2013-01-26 20:29:46 -0800526 this.reconnectAndExpressInterest
527 (interest, new NDN.ConnectClosure(this, onConnected, timerID));
Wentao Shangb42483a2013-01-03 15:32:32 -0800528};
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800529
Jeff Thompsona5668d52013-01-26 16:23:27 -0800530NDN.ConnectClosure = function ConnectClosure(ndn, onConnected, timerID) {
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800531 // Inherit from Closure.
532 Closure.call(this);
533
534 this.ndn = ndn;
Jeff Thompsona5668d52013-01-26 16:23:27 -0800535 this.onConnected = onConnected;
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800536 this.timerID = timerID;
537};
538
539NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) {
540 if (!(kind == Closure.UPCALL_CONTENT ||
Jeff Thompson75771cb2013-01-20 23:27:38 -0800541 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800542 // The upcall is not for us.
543 return Closure.RESULT_ERR;
544
Jeff Thompsona5668d52013-01-26 16:23:27 -0800545 // The host is alive, so cancel the timeout and continue with onConnected().
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800546 clearTimeout(this.timerID);
Jeff Thompsond771b122013-01-26 19:04:41 -0800547
548 // Call NDN.onopen after success
549 this.ndn.readyStatus = NDN.OPENED;
550 this.ndn.onopen();
551
Jeff Thompsona5668d52013-01-26 16:23:27 -0800552 this.onConnected();
Jeff Thompsond3a80dc2012-12-16 17:52:43 -0800553
554 return Closure.RESULT_OK;
555};
556
Jeff Thompson75771cb2013-01-20 23:27:38 -0800557/*
558 * A BinaryXmlElementReader lets you call onReceivedData multiple times which uses a
559 * BinaryXMLStructureDecoder to detect the end of a binary XML element and calls
560 * elementListener.onReceivedElement(element) with the element.
561 * This handles the case where a single call to onReceivedData may contain multiple elements.
562 */
563var BinaryXmlElementReader = function BinaryXmlElementReader(elementListener) {
564 this.elementListener = elementListener;
565 this.dataParts = [];
566 this.structureDecoder = new BinaryXMLStructureDecoder();
567};
568
569BinaryXmlElementReader.prototype.onReceivedData = function(/* Uint8Array */ rawData) {
Jeff Thompson75771cb2013-01-20 23:27:38 -0800570 // Process multiple objects in the data.
571 while(true) {
572 // Scan the input to check if a whole ccnb object has been read.
573 this.structureDecoder.seek(0);
574 if (this.structureDecoder.findElementEnd(rawData)) {
575 // Got the remainder of an object. Report to the caller.
576 this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset));
Jeff Thompson0790af82013-01-26 19:54:27 -0800577 var element = DataUtils.concatArrays(this.dataParts);
578 this.dataParts = [];
579 try {
580 this.elementListener.onReceivedElement(element);
581 } catch (ex) {
582 console.log("BinaryXmlElementReader: ignoring exception from onReceivedElement: " + ex);
583 }
Jeff Thompson75771cb2013-01-20 23:27:38 -0800584
585 // Need to read a new object.
586 rawData = rawData.subarray(this.structureDecoder.offset, rawData.length);
Jeff Thompson75771cb2013-01-20 23:27:38 -0800587 this.structureDecoder = new BinaryXMLStructureDecoder();
588 if (rawData.length == 0)
589 // No more data in the packet.
590 return;
591
592 // else loop back to decode.
593 }
594 else {
595 // Save for a later call to concatArrays so that we only copy data once.
596 this.dataParts.push(rawData);
Jeff Thompsona46083c2013-01-20 23:55:21 -0800597 if (LOG>3) console.log('Incomplete packet received. Length ' + rawData.length + '. Wait for more input.');
Jeff Thompson75771cb2013-01-20 23:27:38 -0800598 return;
599 }
600 }
601}