Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 2 | * @author: Meki Cherkaoui, Jeff Thompson, Wentao Shang |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | * This class represents the top-level object for communicating with an NDN host. |
| 5 | */ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 6 | |
Jeff Thompson | 3c26381 | 2012-12-01 17:20:28 -0800 | [diff] [blame] | 7 | var LOG = 0; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 8 | |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 9 | /** |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 10 | * settings is an associative array with the following defaults: |
| 11 | * { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 12 | * getTransport: function() { return new WebSocketTransport(); } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 13 | * getHostAndPort: transport.defaultGetHostAndPort, |
| 14 | * host: 'localhost', // If null, use getHostAndPort when connecting. |
| 15 | * port: 9696, |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 16 | * onopen: function() { if (LOG > 3) console.log("NDN connection established."); } |
| 17 | * onclose: function() { if (LOG > 3) console.log("NDN connection closed."); } |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 18 | * } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 19 | * |
| 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 Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 22 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 23 | var NDN = function NDN(settings) { |
| 24 | settings = (settings || {}); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 25 | var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); }); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 26 | this.transport = getTransport(); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 27 | this.getHostAndPort = (settings.getHostAndPort || this.transport.defaultGetHostAndPort); |
| 28 | this.host = (settings.host !== undefined ? settings.host : 'localhost'); |
| 29 | this.port = (settings.port || 9696); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 30 | this.readyStatus = NDN.UNOPEN; |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 31 | this.verify = (settings.verify !== undefined ? settings.verify : true); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 32 | // Event handler |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 33 | 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 Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 35 | this.ccndid = null; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 38 | NDN.UNOPEN = 0; // created but not opened yet |
| 39 | NDN.OPENED = 1; // connection to ccnd opened |
| 40 | NDN.CLOSED = 2; // connection to ccnd closed |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 41 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 42 | NDN.ccndIdFetcher = new Name('/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY'); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 43 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 44 | NDN.prototype.createRoute = function(host, port) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 45 | this.host=host; |
| 46 | this.port=port; |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 47 | }; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 48 | |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 49 | |
| 50 | NDN.KeyStore = new Array(); |
| 51 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 52 | var KeyStoreEntry = function KeyStoreEntry(name, rsa, time) { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 53 | this.keyName = name; // KeyName |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 54 | this.rsaKey = rsa; // RSA key |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 55 | this.timeStamp = time; // Time Stamp |
| 56 | }; |
| 57 | |
| 58 | NDN.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 Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | NDN.getKeyByName = function(/* KeyName */ name) { |
| 67 | var result = null; |
| 68 | |
| 69 | for (var i = 0; i < NDN.KeyStore.length; i++) { |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 70 | if (NDN.KeyStore[i].keyName.contentName.match(name.contentName)) { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 71 | 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 Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 80 | // For fetching data |
| 81 | NDN.PITTable = new Array(); |
| 82 | |
| 83 | var PITEntry = function PITEntry(interest, closure) { |
| 84 | this.interest = interest; // Interest |
| 85 | this.closure = closure; // Closure |
Wentao Shang | fcb1626 | 2013-01-20 14:42:46 -0800 | [diff] [blame] | 86 | this.timerID = -1; // Timer ID |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // Return the longest entry from NDN.PITTable that matches name. |
| 90 | NDN.getEntryForExpressedInterest = function(/*Name*/ name) { |
| 91 | // TODO: handle multiple matches? Maybe not from registerPrefix because multiple ContentObject |
| 92 | // could be sent for one Interest? |
| 93 | var result = null; |
| 94 | |
| 95 | for (var i = 0; i < NDN.PITTable.length; i++) { |
| 96 | if (NDN.PITTable[i].interest.matches_name(name)) { |
| 97 | if (result == null || |
| 98 | NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length) |
| 99 | result = NDN.PITTable[i]; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return result; |
| 104 | }; |
| 105 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 106 | // For publishing data |
| 107 | NDN.CSTable = new Array(); |
| 108 | |
| 109 | var CSEntry = function CSEntry(name, closure) { |
| 110 | this.name = name; // String |
| 111 | this.closure = closure; // Closure |
| 112 | }; |
| 113 | |
| 114 | function getEntryForRegisteredPrefix(name) { |
| 115 | for (var i = 0; i < NDN.CSTable.length; i++) { |
| 116 | if (NDN.CSTable[i].name.match(name) != null) |
| 117 | return NDN.CSTable[i]; |
| 118 | } |
| 119 | return null; |
| 120 | } |
| 121 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 122 | /* |
| 123 | * Return a function that selects a host at random from hostList and returns { host: host, port: port }. |
| 124 | * If no more hosts remain, return null. |
| 125 | */ |
| 126 | NDN.makeShuffledGetHostAndPort = function(hostList, port) { |
| 127 | // Make a copy. |
| 128 | hostList = hostList.slice(0, hostList.length); |
| 129 | DataUtils.shuffle(hostList); |
| 130 | |
| 131 | return function() { |
| 132 | if (hostList.length == 0) |
| 133 | return null; |
| 134 | |
| 135 | return { host: hostList.splice(0, 1)[0], port: port }; |
| 136 | }; |
| 137 | }; |
| 138 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 139 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 140 | * Send the interest to host:port, read the entire response and call |
| 141 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 142 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 143 | */ |
| 144 | NDN.prototype.expressInterest = function( |
| 145 | // Name |
| 146 | name, |
| 147 | // Closure |
| 148 | closure, |
| 149 | // Interest |
| 150 | template) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 151 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 152 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 153 | interest.minSuffixComponents = template.minSuffixComponents; |
| 154 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 155 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 156 | interest.exclude = template.exclude; |
| 157 | interest.childSelector = template.childSelector; |
| 158 | interest.answerOriginKind = template.answerOriginKind; |
| 159 | interest.scope = template.scope; |
| 160 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 161 | } |
| 162 | else |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 163 | interest.interestLifetime = 4000; // default interest timeout value in milliseconds. |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 164 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 165 | if (this.host == null || this.port == null) { |
| 166 | if (this.getHostAndPort == null) |
| 167 | console.log('ERROR: host OR port NOT SET'); |
| 168 | else |
| 169 | this.connectAndExpressInterest(interest, closure); |
| 170 | } |
| 171 | else |
| 172 | this.transport.expressInterest(this, interest, closure); |
| 173 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 174 | |
| 175 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 176 | if (this.readyStatus != NDN.OPENED) { |
| 177 | console.log('Connection is not established.'); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | if (this.ccndid == null) { |
| 182 | console.log('ccnd node ID unkonwn. Cannot register prefix.'); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
| 187 | var bytes = encodeForwardingEntry(fe); |
| 188 | |
| 189 | var si = new SignedInfo(); |
| 190 | si.setFields(); |
| 191 | |
| 192 | var co = new ContentObject(new Name(), si, bytes, new Signature()); |
| 193 | co.sign(); |
| 194 | var coBinary = encodeToBinaryContentObject(co); |
| 195 | |
| 196 | //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'); |
| 197 | var nodename = this.ccndid; |
| 198 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
| 199 | |
| 200 | var interest = new Interest(interestName); |
| 201 | interest.scope = 1; |
| 202 | if (LOG > 3) console.log('Send Interest registration packet.'); |
| 203 | |
| 204 | var csEntry = new CSEntry(name.getName(), closure); |
| 205 | NDN.CSTable.push(csEntry); |
| 206 | |
| 207 | this.transport.send(encodeToBinaryInterest(interest)); |
| 208 | |
| 209 | return 0; |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * This is called when an entire binary XML element is received, such as a ContentObject or Interest. |
| 214 | * Look up in the PITTable and call the closure callback. |
| 215 | */ |
| 216 | NDN.prototype.onReceivedElement = function(element) { |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 217 | if (LOG>3) console.log('Complete element received. Length ' + element.length + '. Start decoding.'); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 218 | var decoder = new BinaryXMLDecoder(element); |
| 219 | // Dispatch according to packet type |
| 220 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet |
| 221 | if (LOG > 3) console.log('Interest packet received.'); |
| 222 | |
| 223 | var interest = new Interest(); |
| 224 | interest.from_ccnb(decoder); |
| 225 | if (LOG > 3) console.log(interest); |
| 226 | var nameStr = escape(interest.name.getName()); |
| 227 | if (LOG > 3) console.log(nameStr); |
| 228 | |
| 229 | var entry = getEntryForRegisteredPrefix(nameStr); |
| 230 | if (entry != null) { |
| 231 | //console.log(entry); |
| 232 | var info = new UpcallInfo(this, interest, 0, null); |
| 233 | var ret = entry.closure.upcall(Closure.UPCALL_INTEREST, info); |
| 234 | if (ret == Closure.RESULT_INTEREST_CONSUMED && info.contentObject != null) |
| 235 | this.transport.send(encodeToBinaryContentObject(info.contentObject)); |
| 236 | } |
| 237 | } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet |
| 238 | if (LOG > 3) console.log('ContentObject packet received.'); |
| 239 | |
| 240 | var co = new ContentObject(); |
| 241 | co.from_ccnb(decoder); |
| 242 | |
| 243 | if (this.ccndid == null && NDN.ccndIdFetcher.match(co.name)) { |
| 244 | // We are in starting phase, record publisherPublicKeyDigest in ccndid |
| 245 | if(!co.signedInfo || !co.signedInfo.publisher |
| 246 | || !co.signedInfo.publisher.publisherPublicKeyDigest) { |
| 247 | console.log("Cannot contact router, close NDN now."); |
| 248 | |
| 249 | // Close NDN if we fail to connect to a ccn router |
| 250 | this.readyStatus = NDN.CLOSED; |
| 251 | this.onclose(); |
| 252 | //console.log("NDN.onclose event fired."); |
| 253 | } else { |
Wentao Shang | 261b4be | 2013-01-26 09:22:37 -0800 | [diff] [blame^] | 254 | if (LOG>3) console.log('Connected to ccnd.'); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 255 | this.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 256 | if (LOG>3) console.log(ndn.ccndid); |
| 257 | |
| 258 | // Call NDN.onopen after success |
| 259 | this.readyStatus = NDN.OPENED; |
| 260 | this.onopen(); |
| 261 | //console.log("NDN.onopen event fired."); |
| 262 | } |
| 263 | } else { |
| 264 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
| 265 | if (pitEntry != null) { |
| 266 | //console.log(pitEntry); |
| 267 | // Remove PIT entry from NDN.PITTable |
| 268 | var index = NDN.PITTable.indexOf(pitEntry); |
| 269 | if (index >= 0) |
| 270 | NDN.PITTable.splice(index, 1); |
| 271 | |
| 272 | var currentClosure = pitEntry.closure; |
| 273 | |
| 274 | // Cancel interest timer |
| 275 | clearTimeout(pitEntry.timerID); |
| 276 | //console.log("Clear interest timer"); |
| 277 | //console.log(currentClosure.timerID); |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 278 | |
| 279 | if (this.verify == false) { |
| 280 | // Pass content up without verifying the signature |
| 281 | currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, new UpcallInfo(this, null, 0, co)); |
| 282 | return; |
| 283 | } |
| 284 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 285 | // Key verification |
| 286 | |
| 287 | // Recursive key fetching & verification closure |
| 288 | var KeyFetchClosure = function KeyFetchClosure(content, closure, key, sig, wit) { |
| 289 | this.contentObject = content; // unverified content object |
| 290 | this.closure = closure; // closure corresponding to the contentObject |
| 291 | this.keyName = key; // name of current key to be fetched |
| 292 | this.sigHex = sig; // hex signature string to be verified |
| 293 | this.witness = wit; |
| 294 | |
| 295 | Closure.call(this); |
| 296 | }; |
| 297 | |
| 298 | var thisNdn = this; |
| 299 | KeyFetchClosure.prototype.upcall = function(kind, upcallInfo) { |
| 300 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 301 | console.log("In KeyFetchClosure.upcall: interest time out."); |
| 302 | console.log(this.keyName.contentName.getName()); |
| 303 | } else if (kind == Closure.UPCALL_CONTENT) { |
| 304 | //console.log("In KeyFetchClosure.upcall: signature verification passed"); |
| 305 | |
| 306 | var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content); |
| 307 | var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.witness, this.sigHex); |
| 308 | |
| 309 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 310 | //console.log("raise encapsulated closure"); |
| 311 | this.closure.upcall(flag, new UpcallInfo(thisNdn, null, 0, this.contentObject)); |
| 312 | |
| 313 | // Store key in cache |
| 314 | var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 315 | NDN.addKeyEntry(keyEntry); |
| 316 | //console.log(NDN.KeyStore); |
| 317 | } else if (kind == Closure.UPCALL_CONTENT_BAD) { |
| 318 | console.log("In KeyFetchClosure.upcall: signature verification failed"); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | if (co.signedInfo && co.signedInfo.locator && co.signature) { |
| 323 | if (LOG > 3) console.log("Key verification..."); |
| 324 | var sigHex = DataUtils.toHex(co.signature.signature).toLowerCase(); |
| 325 | |
| 326 | var wit = null; |
| 327 | if (co.signature.Witness != null) { |
| 328 | wit = new Witness(); |
| 329 | wit.decode(co.signature.Witness); |
| 330 | } |
| 331 | |
| 332 | var keylocator = co.signedInfo.locator; |
| 333 | if (keylocator.type == KeyLocatorType.KEYNAME) { |
| 334 | if (LOG > 3) console.log("KeyLocator contains KEYNAME"); |
| 335 | //var keyname = keylocator.keyName.contentName.getName(); |
| 336 | //console.log(nameStr); |
| 337 | //console.log(keyname); |
| 338 | |
| 339 | if (keylocator.keyName.contentName.match(co.name)) { |
| 340 | if (LOG > 3) console.log("Content is key itself"); |
| 341 | |
| 342 | var rsakey = decodeSubjectPublicKeyInfo(co.content); |
| 343 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 344 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 345 | |
| 346 | currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co)); |
| 347 | |
| 348 | // SWT: We don't need to store key here since the same key will be |
| 349 | // stored again in the closure. |
| 350 | //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 351 | //NDN.addKeyEntry(keyEntry); |
| 352 | //console.log(NDN.KeyStore); |
| 353 | } else { |
| 354 | // Check local key store |
| 355 | var keyEntry = NDN.getKeyByName(keylocator.keyName); |
| 356 | if (keyEntry) { |
| 357 | // Key found, verify now |
| 358 | if (LOG > 3) console.log("Local key cache hit"); |
| 359 | var rsakey = keyEntry.rsaKey; |
| 360 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 361 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 362 | |
| 363 | // Raise callback |
| 364 | currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co)); |
| 365 | } else { |
| 366 | // Not found, fetch now |
| 367 | if (LOG > 3) console.log("Fetch key according to keylocator"); |
| 368 | var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit); |
| 369 | this.expressInterest(keylocator.keyName.contentName.getPrefix(4), nextClosure); |
| 370 | } |
| 371 | } |
| 372 | } else if (keylocator.type == KeyLocatorType.KEY) { |
| 373 | if (LOG > 3) console.log("Keylocator contains KEY"); |
| 374 | |
| 375 | var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey); |
| 376 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 377 | |
| 378 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 379 | // Raise callback |
| 380 | currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(this, null, 0, co)); |
| 381 | |
| 382 | // Since KeyLocator does not contain key name for this key, |
| 383 | // we have no way to store it as a key entry in KeyStore. |
| 384 | } else { |
| 385 | var cert = keylocator.certificate; |
| 386 | console.log("KeyLocator contains CERT"); |
| 387 | console.log(cert); |
| 388 | |
| 389 | // TODO: verify certificate |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } else |
| 395 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 396 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | * Assume this.getHostAndPort is not null. This is called when this.host is null or its host |
| 400 | * is not alive. Get a host and port, connect, then express callerInterest with callerClosure. |
| 401 | */ |
| 402 | NDN.prototype.connectAndExpressInterest = function(callerInterest, callerClosure) { |
| 403 | var hostAndPort = this.getHostAndPort(); |
| 404 | if (hostAndPort == null) { |
| 405 | console.log('ERROR: No more hosts from getHostAndPort'); |
| 406 | this.host = null; |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | if (hostAndPort.host == this.host && hostAndPort.port == this.port) { |
| 411 | console.log('ERROR: The host returned by getHostAndPort is not alive: ' + |
| 412 | this.host + ":" + this.port); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | this.host = hostAndPort.host; |
| 417 | this.port = hostAndPort.port; |
| 418 | console.log("Trying host from getHostAndPort: " + this.host); |
| 419 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 420 | // Fetch any content. |
| 421 | var interest = new Interest(new Name("/")); |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 422 | interest.interestLifetime = 4000; // milliseconds |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 423 | |
| 424 | var thisNDN = this; |
| 425 | var timerID = setTimeout(function() { |
| 426 | console.log("Timeout waiting for host " + thisNDN.host); |
| 427 | // Try again. |
| 428 | thisNDN.connectAndExpressInterest(callerInterest, callerClosure); |
| 429 | }, 3000); |
| 430 | |
| 431 | this.transport.expressInterest |
| 432 | (this, interest, new NDN.ConnectClosure(this, callerInterest, callerClosure, timerID)); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 433 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 434 | |
| 435 | NDN.ConnectClosure = function ConnectClosure(ndn, callerInterest, callerClosure, timerID) { |
| 436 | // Inherit from Closure. |
| 437 | Closure.call(this); |
| 438 | |
| 439 | this.ndn = ndn; |
| 440 | this.callerInterest = callerInterest; |
| 441 | this.callerClosure = callerClosure; |
| 442 | this.timerID = timerID; |
| 443 | }; |
| 444 | |
| 445 | NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) { |
| 446 | if (!(kind == Closure.UPCALL_CONTENT || |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 447 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 448 | // The upcall is not for us. |
| 449 | return Closure.RESULT_ERR; |
| 450 | |
| 451 | // The host is alive, so cancel the timeout and issue the caller's interest. |
| 452 | clearTimeout(this.timerID); |
| 453 | console.log(this.ndn.host + ": Host is alive. Fetching callerInterest."); |
| 454 | this.ndn.transport.expressInterest(this.ndn, this.callerInterest, this.callerClosure); |
| 455 | |
| 456 | return Closure.RESULT_OK; |
| 457 | }; |
| 458 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 459 | /* |
| 460 | * A BinaryXmlElementReader lets you call onReceivedData multiple times which uses a |
| 461 | * BinaryXMLStructureDecoder to detect the end of a binary XML element and calls |
| 462 | * elementListener.onReceivedElement(element) with the element. |
| 463 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
| 464 | */ |
| 465 | var BinaryXmlElementReader = function BinaryXmlElementReader(elementListener) { |
| 466 | this.elementListener = elementListener; |
| 467 | this.dataParts = []; |
| 468 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 469 | }; |
| 470 | |
| 471 | BinaryXmlElementReader.prototype.onReceivedData = function(/* Uint8Array */ rawData) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 472 | // Process multiple objects in the data. |
| 473 | while(true) { |
| 474 | // Scan the input to check if a whole ccnb object has been read. |
| 475 | this.structureDecoder.seek(0); |
| 476 | if (this.structureDecoder.findElementEnd(rawData)) { |
| 477 | // Got the remainder of an object. Report to the caller. |
| 478 | this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset)); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 479 | this.elementListener.onReceivedElement(DataUtils.concatArrays(this.dataParts)); |
| 480 | |
| 481 | // Need to read a new object. |
| 482 | rawData = rawData.subarray(this.structureDecoder.offset, rawData.length); |
| 483 | this.dataParts = []; |
| 484 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 485 | if (rawData.length == 0) |
| 486 | // No more data in the packet. |
| 487 | return; |
| 488 | |
| 489 | // else loop back to decode. |
| 490 | } |
| 491 | else { |
| 492 | // Save for a later call to concatArrays so that we only copy data once. |
| 493 | this.dataParts.push(rawData); |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 494 | if (LOG>3) console.log('Incomplete packet received. Length ' + rawData.length + '. Wait for more input.'); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | } |
| 498 | } |