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, |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 14 | * host: null, // If null, use getHostAndPort when connecting. |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 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); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 28 | this.host = (settings.host !== undefined ? settings.host : null); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 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'); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 168 | else { |
| 169 | var thisNDN = this; |
| 170 | this.connectAndExecute |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 171 | (function() { thisNDN.reconnectAndExpressInterest(interest, closure); }); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 172 | } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 173 | } |
| 174 | else |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 175 | this.reconnectAndExpressInterest(interest, closure); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 176 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 177 | |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 178 | /* |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 179 | * If the host and port are different than the ones in this.transport, then call |
| 180 | * this.transport.connect to change the connection (or connect for the first time). |
| 181 | * Then call expressInterestHelper. |
| 182 | */ |
| 183 | NDN.prototype.reconnectAndExpressInterest = function(interest, closure) { |
| 184 | if (this.transport.connectedHost != this.host || this.transport.connectedPort != this.port) { |
| 185 | var thisNDN = this; |
| 186 | this.transport.connect(thisNDN, function() { thisNDN.expressInterestHelper(interest, closure); }); |
| 187 | } |
| 188 | else |
| 189 | this.expressInterestHelper(interest, closure); |
| 190 | }; |
| 191 | |
| 192 | /* |
| 193 | * Do the work of reconnectAndExpressInterest once we know we are connected. Set the PITTable and call |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 194 | * this.transport.send to send the interest. |
| 195 | */ |
| 196 | NDN.prototype.expressInterestHelper = function(interest, closure) { |
| 197 | //TODO: check local content store first |
| 198 | if (closure != null) { |
| 199 | var pitEntry = new PITEntry(interest, closure); |
| 200 | // TODO: This needs to be a single thread-safe transaction on a global object. |
| 201 | NDN.PITTable.push(pitEntry); |
| 202 | closure.pitEntry = pitEntry; |
| 203 | } |
| 204 | |
| 205 | // Set interest timer |
| 206 | var thisNDN = this; |
| 207 | if (closure != null) { |
Jeff Thompson | a53e65a | 2013-02-10 10:52:52 -0800 | [diff] [blame^] | 208 | var timeoutMilliseconds = (interest.interestLifetime || 4000); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 209 | pitEntry.timerID = setTimeout(function() { |
| 210 | if (LOG > 3) console.log("Interest time out."); |
| 211 | |
| 212 | // Remove PIT entry from NDN.PITTable. |
| 213 | // TODO: Make this a thread-safe operation on the global PITTable. |
| 214 | var index = NDN.PITTable.indexOf(pitEntry); |
| 215 | //console.log(NDN.PITTable); |
| 216 | if (index >= 0) |
| 217 | NDN.PITTable.splice(index, 1); |
| 218 | //console.log(NDN.PITTable); |
| 219 | //console.log(pitEntry.interest.name.getName()); |
| 220 | |
| 221 | // Raise closure callback |
| 222 | closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, new UpcallInfo(thisNDN, interest, 0, null)); |
Jeff Thompson | a53e65a | 2013-02-10 10:52:52 -0800 | [diff] [blame^] | 223 | }, timeoutMilliseconds); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 224 | //console.log(closure.timerID); |
| 225 | } |
| 226 | |
| 227 | this.transport.send(encodeToBinaryInterest(interest)); |
| 228 | }; |
| 229 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 230 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 231 | var thisNDN = this; |
| 232 | var onConnected = function() { |
| 233 | if (thisNDN.ccndid == null) { |
| 234 | // Fetch ccndid first, then register. |
| 235 | var interest = new Interest(NDN.ccndIdFetcher); |
| 236 | interest.interestLifetime = 4000; // milliseconds |
| 237 | if (LOG>3) console.log('Expressing interest for ccndid from ccnd.'); |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 238 | thisNDN.reconnectAndExpressInterest |
| 239 | (interest, new NDN.FetchCcndidClosure(thisNDN, name, closure, flag)); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | thisNDN.registerPrefixHelper(name, closure, flag); |
| 243 | }; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 244 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 245 | if (this.host == null || this.port == null) { |
| 246 | if (this.getHostAndPort == null) |
| 247 | console.log('ERROR: host OR port NOT SET'); |
| 248 | else |
| 249 | this.connectAndExecute(onConnected); |
| 250 | } |
| 251 | else |
| 252 | onConnected(); |
| 253 | }; |
| 254 | |
| 255 | /* |
| 256 | * This is a closure to receive the ContentObject for NDN.ccndIdFetcher and call |
| 257 | * registerPrefixHelper(name, callerClosure, flag). |
| 258 | */ |
| 259 | NDN.FetchCcndidClosure = function FetchCcndidClosure(ndn, name, callerClosure, flag) { |
| 260 | // Inherit from Closure. |
| 261 | Closure.call(this); |
| 262 | |
| 263 | this.ndn = ndn; |
| 264 | this.name = name; |
| 265 | this.callerClosure = callerClosure; |
| 266 | this.flag = flag; |
| 267 | }; |
| 268 | |
| 269 | NDN.FetchCcndidClosure.prototype.upcall = function(kind, upcallInfo) { |
| 270 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 271 | console.log("Timeout while requesting the ccndid. Cannot registerPrefix for " + |
| 272 | this.name.to_uri() + " ."); |
| 273 | return Closure.RESULT_OK; |
| 274 | } |
| 275 | if (!(kind == Closure.UPCALL_CONTENT || |
| 276 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
| 277 | // The upcall is not for us. |
| 278 | return Closure.RESULT_ERR; |
| 279 | |
| 280 | var co = upcallInfo.contentObject; |
| 281 | if (!co.signedInfo || !co.signedInfo.publisher |
| 282 | || !co.signedInfo.publisher.publisherPublicKeyDigest) |
| 283 | console.log |
| 284 | ("ContentObject doesn't have a publisherPublicKeyDigest. Cannot set ccndid and registerPrefix for " |
| 285 | + this.name.to_uri() + " ."); |
| 286 | else { |
| 287 | if (LOG>3) console.log('Got ccndid from ccnd.'); |
| 288 | this.ndn.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 289 | if (LOG>3) console.log(this.ndn.ccndid); |
| 290 | |
| 291 | this.ndn.registerPrefixHelper(this.name, this.callerClosure, this.flag); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 292 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 293 | |
| 294 | return Closure.RESULT_OK; |
| 295 | }; |
| 296 | |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 297 | /* |
| 298 | * Do the work of registerPrefix once we know we are connected with a ccndid. |
| 299 | */ |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 300 | NDN.prototype.registerPrefixHelper = function(name, closure, flag) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 301 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
| 302 | var bytes = encodeForwardingEntry(fe); |
| 303 | |
| 304 | var si = new SignedInfo(); |
| 305 | si.setFields(); |
| 306 | |
| 307 | var co = new ContentObject(new Name(), si, bytes, new Signature()); |
| 308 | co.sign(); |
| 309 | var coBinary = encodeToBinaryContentObject(co); |
| 310 | |
| 311 | //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'); |
| 312 | var nodename = this.ccndid; |
| 313 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
| 314 | |
| 315 | var interest = new Interest(interestName); |
| 316 | interest.scope = 1; |
| 317 | if (LOG > 3) console.log('Send Interest registration packet.'); |
| 318 | |
| 319 | var csEntry = new CSEntry(name.getName(), closure); |
| 320 | NDN.CSTable.push(csEntry); |
| 321 | |
| 322 | this.transport.send(encodeToBinaryInterest(interest)); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | /* |
| 326 | * This is called when an entire binary XML element is received, such as a ContentObject or Interest. |
| 327 | * Look up in the PITTable and call the closure callback. |
| 328 | */ |
| 329 | NDN.prototype.onReceivedElement = function(element) { |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 330 | 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] | 331 | var decoder = new BinaryXMLDecoder(element); |
| 332 | // Dispatch according to packet type |
| 333 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet |
| 334 | if (LOG > 3) console.log('Interest packet received.'); |
| 335 | |
| 336 | var interest = new Interest(); |
| 337 | interest.from_ccnb(decoder); |
| 338 | if (LOG > 3) console.log(interest); |
| 339 | var nameStr = escape(interest.name.getName()); |
| 340 | if (LOG > 3) console.log(nameStr); |
| 341 | |
| 342 | var entry = getEntryForRegisteredPrefix(nameStr); |
| 343 | if (entry != null) { |
| 344 | //console.log(entry); |
| 345 | var info = new UpcallInfo(this, interest, 0, null); |
| 346 | var ret = entry.closure.upcall(Closure.UPCALL_INTEREST, info); |
| 347 | if (ret == Closure.RESULT_INTEREST_CONSUMED && info.contentObject != null) |
| 348 | this.transport.send(encodeToBinaryContentObject(info.contentObject)); |
| 349 | } |
| 350 | } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet |
| 351 | if (LOG > 3) console.log('ContentObject packet received.'); |
| 352 | |
| 353 | var co = new ContentObject(); |
| 354 | co.from_ccnb(decoder); |
| 355 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 356 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
| 357 | if (pitEntry != null) { |
| 358 | //console.log(pitEntry); |
| 359 | // Remove PIT entry from NDN.PITTable |
| 360 | var index = NDN.PITTable.indexOf(pitEntry); |
| 361 | if (index >= 0) |
| 362 | NDN.PITTable.splice(index, 1); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 363 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 364 | var currentClosure = pitEntry.closure; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 365 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 366 | // Cancel interest timer |
| 367 | clearTimeout(pitEntry.timerID); |
| 368 | //console.log("Clear interest timer"); |
| 369 | //console.log(currentClosure.timerID); |
| 370 | |
| 371 | if (this.verify == false) { |
| 372 | // Pass content up without verifying the signature |
| 373 | currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, new UpcallInfo(this, null, 0, co)); |
| 374 | return; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 375 | } |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 376 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 377 | // Key verification |
| 378 | |
| 379 | // Recursive key fetching & verification closure |
| 380 | var KeyFetchClosure = function KeyFetchClosure(content, closure, key, sig, wit) { |
| 381 | this.contentObject = content; // unverified content object |
| 382 | this.closure = closure; // closure corresponding to the contentObject |
| 383 | this.keyName = key; // name of current key to be fetched |
| 384 | this.sigHex = sig; // hex signature string to be verified |
| 385 | this.witness = wit; |
| 386 | |
| 387 | Closure.call(this); |
| 388 | }; |
| 389 | |
| 390 | var thisNDN = this; |
| 391 | KeyFetchClosure.prototype.upcall = function(kind, upcallInfo) { |
| 392 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 393 | console.log("In KeyFetchClosure.upcall: interest time out."); |
| 394 | console.log(this.keyName.contentName.getName()); |
| 395 | } else if (kind == Closure.UPCALL_CONTENT) { |
| 396 | //console.log("In KeyFetchClosure.upcall: signature verification passed"); |
| 397 | |
| 398 | var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content); |
| 399 | var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.witness, this.sigHex); |
| 400 | |
| 401 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 402 | //console.log("raise encapsulated closure"); |
| 403 | this.closure.upcall(flag, new UpcallInfo(thisNDN, null, 0, this.contentObject)); |
| 404 | |
| 405 | // Store key in cache |
| 406 | var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 407 | NDN.addKeyEntry(keyEntry); |
| 408 | //console.log(NDN.KeyStore); |
| 409 | } else if (kind == Closure.UPCALL_CONTENT_BAD) { |
| 410 | console.log("In KeyFetchClosure.upcall: signature verification failed"); |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 411 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 412 | }; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 413 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 414 | if (co.signedInfo && co.signedInfo.locator && co.signature) { |
| 415 | if (LOG > 3) console.log("Key verification..."); |
| 416 | var sigHex = DataUtils.toHex(co.signature.signature).toLowerCase(); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 417 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 418 | var wit = null; |
| 419 | if (co.signature.Witness != null) { |
| 420 | wit = new Witness(); |
| 421 | wit.decode(co.signature.Witness); |
| 422 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 423 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 424 | var keylocator = co.signedInfo.locator; |
| 425 | if (keylocator.type == KeyLocatorType.KEYNAME) { |
| 426 | if (LOG > 3) console.log("KeyLocator contains KEYNAME"); |
| 427 | //var keyname = keylocator.keyName.contentName.getName(); |
| 428 | //console.log(nameStr); |
| 429 | //console.log(keyname); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 430 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 431 | if (keylocator.keyName.contentName.match(co.name)) { |
| 432 | if (LOG > 3) console.log("Content is key itself"); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 433 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 434 | var rsakey = decodeSubjectPublicKeyInfo(co.content); |
| 435 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 436 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 437 | |
| 438 | currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co)); |
| 439 | |
| 440 | // SWT: We don't need to store key here since the same key will be |
| 441 | // stored again in the closure. |
| 442 | //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 443 | //NDN.addKeyEntry(keyEntry); |
| 444 | //console.log(NDN.KeyStore); |
| 445 | } else { |
| 446 | // Check local key store |
| 447 | var keyEntry = NDN.getKeyByName(keylocator.keyName); |
| 448 | if (keyEntry) { |
| 449 | // Key found, verify now |
| 450 | if (LOG > 3) console.log("Local key cache hit"); |
| 451 | var rsakey = keyEntry.rsaKey; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 452 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 453 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 454 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 455 | // Raise callback |
| 456 | currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co)); |
| 457 | } else { |
| 458 | // Not found, fetch now |
| 459 | if (LOG > 3) console.log("Fetch key according to keylocator"); |
| 460 | var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit); |
| 461 | this.expressInterest(keylocator.keyName.contentName.getPrefix(4), nextClosure); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 462 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 463 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 464 | } else if (keylocator.type == KeyLocatorType.KEY) { |
| 465 | if (LOG > 3) console.log("Keylocator contains KEY"); |
| 466 | |
| 467 | var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey); |
| 468 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 469 | |
| 470 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 471 | // Raise callback |
| 472 | currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(this, null, 0, co)); |
| 473 | |
| 474 | // Since KeyLocator does not contain key name for this key, |
| 475 | // we have no way to store it as a key entry in KeyStore. |
| 476 | } else { |
| 477 | var cert = keylocator.certificate; |
| 478 | console.log("KeyLocator contains CERT"); |
| 479 | console.log(cert); |
| 480 | |
| 481 | // TODO: verify certificate |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } else |
| 486 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 487 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * Assume this.getHostAndPort is not null. This is called when this.host is null or its host |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 491 | * is not alive. Get a host and port, connect, then execute onConnected(). |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 492 | */ |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 493 | NDN.prototype.connectAndExecute = function(onConnected) { |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 494 | var hostAndPort = this.getHostAndPort(); |
| 495 | if (hostAndPort == null) { |
| 496 | console.log('ERROR: No more hosts from getHostAndPort'); |
| 497 | this.host = null; |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | if (hostAndPort.host == this.host && hostAndPort.port == this.port) { |
| 502 | console.log('ERROR: The host returned by getHostAndPort is not alive: ' + |
| 503 | this.host + ":" + this.port); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | this.host = hostAndPort.host; |
| 508 | this.port = hostAndPort.port; |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 509 | if (LOG>3) console.log("Connect: trying host from getHostAndPort: " + this.host); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 510 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 511 | // Fetch any content. |
| 512 | var interest = new Interest(new Name("/")); |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 513 | interest.interestLifetime = 4000; // milliseconds |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 514 | |
| 515 | var thisNDN = this; |
| 516 | var timerID = setTimeout(function() { |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 517 | if (LOG>3) console.log("Connect: timeout waiting for host " + thisNDN.host); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 518 | // Try again. |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 519 | thisNDN.connectAndExecute(onConnected); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 520 | }, 3000); |
| 521 | |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 522 | this.reconnectAndExpressInterest |
| 523 | (interest, new NDN.ConnectClosure(this, onConnected, timerID)); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 524 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 525 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 526 | NDN.ConnectClosure = function ConnectClosure(ndn, onConnected, timerID) { |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 527 | // Inherit from Closure. |
| 528 | Closure.call(this); |
| 529 | |
| 530 | this.ndn = ndn; |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 531 | this.onConnected = onConnected; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 532 | this.timerID = timerID; |
| 533 | }; |
| 534 | |
| 535 | NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) { |
| 536 | if (!(kind == Closure.UPCALL_CONTENT || |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 537 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 538 | // The upcall is not for us. |
| 539 | return Closure.RESULT_ERR; |
| 540 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 541 | // The host is alive, so cancel the timeout and continue with onConnected(). |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 542 | clearTimeout(this.timerID); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 543 | |
| 544 | // Call NDN.onopen after success |
| 545 | this.ndn.readyStatus = NDN.OPENED; |
| 546 | this.ndn.onopen(); |
| 547 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 548 | this.onConnected(); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 549 | |
| 550 | return Closure.RESULT_OK; |
| 551 | }; |
| 552 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 553 | /* |
| 554 | * A BinaryXmlElementReader lets you call onReceivedData multiple times which uses a |
| 555 | * BinaryXMLStructureDecoder to detect the end of a binary XML element and calls |
| 556 | * elementListener.onReceivedElement(element) with the element. |
| 557 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
| 558 | */ |
| 559 | var BinaryXmlElementReader = function BinaryXmlElementReader(elementListener) { |
| 560 | this.elementListener = elementListener; |
| 561 | this.dataParts = []; |
| 562 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 563 | }; |
| 564 | |
| 565 | BinaryXmlElementReader.prototype.onReceivedData = function(/* Uint8Array */ rawData) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 566 | // Process multiple objects in the data. |
| 567 | while(true) { |
| 568 | // Scan the input to check if a whole ccnb object has been read. |
| 569 | this.structureDecoder.seek(0); |
| 570 | if (this.structureDecoder.findElementEnd(rawData)) { |
| 571 | // Got the remainder of an object. Report to the caller. |
| 572 | this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset)); |
Jeff Thompson | 0790af8 | 2013-01-26 19:54:27 -0800 | [diff] [blame] | 573 | var element = DataUtils.concatArrays(this.dataParts); |
| 574 | this.dataParts = []; |
| 575 | try { |
| 576 | this.elementListener.onReceivedElement(element); |
| 577 | } catch (ex) { |
| 578 | console.log("BinaryXmlElementReader: ignoring exception from onReceivedElement: " + ex); |
| 579 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 580 | |
| 581 | // Need to read a new object. |
| 582 | rawData = rawData.subarray(this.structureDecoder.offset, rawData.length); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 583 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 584 | if (rawData.length == 0) |
| 585 | // No more data in the packet. |
| 586 | return; |
| 587 | |
| 588 | // else loop back to decode. |
| 589 | } |
| 590 | else { |
| 591 | // Save for a later call to concatArrays so that we only copy data once. |
| 592 | this.dataParts.push(rawData); |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 593 | 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] | 594 | return; |
| 595 | } |
| 596 | } |
| 597 | } |