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 | ac0ed60 | 2013-02-18 21:52:42 -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, |
Jeff Thompson | ac0ed60 | 2013-02-18 21:52:42 -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."); }, |
| 18 | * verify: true // If false, don't verify and call upcall with Closure.UPCALL_CONTENT_UNVERIFIED. |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 19 | * } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 20 | * |
| 21 | * getHostAndPort is a function, on each call it returns a new { host: host, port: port } or |
| 22 | * null if there are no more hosts. |
Jeff Thompson | e2f7b6d | 2013-02-24 20:37:27 -0800 | [diff] [blame] | 23 | * |
| 24 | * This throws an exception if NDN.supported is false. |
Jeff Thompson | e06b31e | 2012-09-30 17:19:19 -0700 | [diff] [blame] | 25 | */ |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 26 | var NDN = function NDN(settings) { |
Jeff Thompson | e2f7b6d | 2013-02-24 20:37:27 -0800 | [diff] [blame] | 27 | if (!NDN.supported) |
| 28 | throw new Error("The necessary JavaScript support is not available on this platform."); |
| 29 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 30 | settings = (settings || {}); |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 31 | var getTransport = (settings.getTransport || function() { return new WebSocketTransport(); }); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 32 | this.transport = getTransport(); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 33 | this.getHostAndPort = (settings.getHostAndPort || this.transport.defaultGetHostAndPort); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 34 | this.host = (settings.host !== undefined ? settings.host : null); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 35 | this.port = (settings.port || 9696); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 36 | this.readyStatus = NDN.UNOPEN; |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 37 | this.verify = (settings.verify !== undefined ? settings.verify : true); |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 38 | // Event handler |
Wentao Shang | c0311e5 | 2012-12-03 10:38:23 -0800 | [diff] [blame] | 39 | this.onopen = (settings.onopen || function() { if (LOG > 3) console.log("NDN connection established."); }); |
| 40 | 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] | 41 | this.ccndid = null; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Wentao Shang | 0e291c8 | 2012-12-02 23:36:29 -0800 | [diff] [blame] | 44 | NDN.UNOPEN = 0; // created but not opened yet |
| 45 | NDN.OPENED = 1; // connection to ccnd opened |
| 46 | NDN.CLOSED = 2; // connection to ccnd closed |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 47 | |
Jeff Thompson | e2f7b6d | 2013-02-24 20:37:27 -0800 | [diff] [blame] | 48 | /* |
| 49 | * Return true if necessary JavaScript support is available, else log an error and return false. |
| 50 | */ |
| 51 | NDN.getSupported = function() { |
| 52 | try { |
| 53 | var dummy = new Uint8Array(1).subarray(0, 1); |
| 54 | } catch (ex) { |
| 55 | console.log("NDN not available: Uint8Array not supported. " + ex); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return true; |
| 60 | }; |
| 61 | |
| 62 | NDN.supported = NDN.getSupported(); |
| 63 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 64 | 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] | 65 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 66 | NDN.prototype.createRoute = function(host, port) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 67 | this.host=host; |
| 68 | this.port=port; |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 69 | }; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 70 | |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 71 | |
| 72 | NDN.KeyStore = new Array(); |
| 73 | |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 74 | var KeyStoreEntry = function KeyStoreEntry(name, rsa, time) { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 75 | this.keyName = name; // KeyName |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 76 | this.rsaKey = rsa; // RSA key |
Wentao Shang | b5d0c3e | 2012-12-30 11:12:03 -0800 | [diff] [blame] | 77 | this.timeStamp = time; // Time Stamp |
| 78 | }; |
| 79 | |
| 80 | NDN.addKeyEntry = function(/* KeyStoreEntry */ keyEntry) { |
| 81 | var result = NDN.getKeyByName(keyEntry.keyName); |
| 82 | if (result == null) |
| 83 | NDN.KeyStore.push(keyEntry); |
| 84 | else |
| 85 | result = keyEntry; |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | NDN.getKeyByName = function(/* KeyName */ name) { |
| 89 | var result = null; |
| 90 | |
| 91 | for (var i = 0; i < NDN.KeyStore.length; i++) { |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 92 | if (NDN.KeyStore[i].keyName.contentName.match(name.contentName)) { |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 93 | if (result == null || |
| 94 | NDN.KeyStore[i].keyName.contentName.components.length > result.keyName.contentName.components.length) |
| 95 | result = NDN.KeyStore[i]; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return result; |
| 100 | }; |
| 101 | |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 102 | // For fetching data |
| 103 | NDN.PITTable = new Array(); |
| 104 | |
| 105 | var PITEntry = function PITEntry(interest, closure) { |
| 106 | this.interest = interest; // Interest |
| 107 | this.closure = closure; // Closure |
Wentao Shang | fcb1626 | 2013-01-20 14:42:46 -0800 | [diff] [blame] | 108 | this.timerID = -1; // Timer ID |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
Jeff Thompson | 202728a | 2013-02-10 22:20:08 -0800 | [diff] [blame] | 111 | /* |
| 112 | * Return the entry from NDN.PITTable where the name conforms to the interest selectors, and |
| 113 | * the interest name is the longest that matches name. |
| 114 | */ |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 115 | NDN.getEntryForExpressedInterest = function(/*Name*/ name) { |
Jeff Thompson | be85be6 | 2012-12-13 22:32:01 -0800 | [diff] [blame] | 116 | var result = null; |
| 117 | |
| 118 | for (var i = 0; i < NDN.PITTable.length; i++) { |
| 119 | if (NDN.PITTable[i].interest.matches_name(name)) { |
| 120 | if (result == null || |
| 121 | NDN.PITTable[i].interest.name.components.length > result.interest.name.components.length) |
| 122 | result = NDN.PITTable[i]; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return result; |
| 127 | }; |
| 128 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 129 | // For publishing data |
| 130 | NDN.CSTable = new Array(); |
| 131 | |
| 132 | var CSEntry = function CSEntry(name, closure) { |
| 133 | this.name = name; // String |
| 134 | this.closure = closure; // Closure |
| 135 | }; |
| 136 | |
| 137 | function getEntryForRegisteredPrefix(name) { |
| 138 | for (var i = 0; i < NDN.CSTable.length; i++) { |
| 139 | if (NDN.CSTable[i].name.match(name) != null) |
| 140 | return NDN.CSTable[i]; |
| 141 | } |
| 142 | return null; |
| 143 | } |
| 144 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 145 | /* |
| 146 | * Return a function that selects a host at random from hostList and returns { host: host, port: port }. |
| 147 | * If no more hosts remain, return null. |
| 148 | */ |
| 149 | NDN.makeShuffledGetHostAndPort = function(hostList, port) { |
| 150 | // Make a copy. |
| 151 | hostList = hostList.slice(0, hostList.length); |
| 152 | DataUtils.shuffle(hostList); |
| 153 | |
| 154 | return function() { |
| 155 | if (hostList.length == 0) |
| 156 | return null; |
| 157 | |
| 158 | return { host: hostList.splice(0, 1)[0], port: port }; |
| 159 | }; |
| 160 | }; |
| 161 | |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 162 | /** Encode name as an Interest. If template is not null, use its attributes. |
| 163 | * Send the interest to host:port, read the entire response and call |
| 164 | * closure.upcall(Closure.UPCALL_CONTENT (or Closure.UPCALL_CONTENT_UNVERIFIED), |
Jeff Thompson | 97f2743 | 2012-10-16 00:28:03 -0700 | [diff] [blame] | 165 | * new UpcallInfo(this, interest, 0, contentObject)). |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 166 | */ |
| 167 | NDN.prototype.expressInterest = function( |
| 168 | // Name |
| 169 | name, |
| 170 | // Closure |
| 171 | closure, |
| 172 | // Interest |
| 173 | template) { |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 174 | var interest = new Interest(name); |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 175 | if (template != null) { |
Jeff Thompson | 4404ab5 | 2012-10-21 10:29:48 -0700 | [diff] [blame] | 176 | interest.minSuffixComponents = template.minSuffixComponents; |
| 177 | interest.maxSuffixComponents = template.maxSuffixComponents; |
| 178 | interest.publisherPublicKeyDigest = template.publisherPublicKeyDigest; |
| 179 | interest.exclude = template.exclude; |
| 180 | interest.childSelector = template.childSelector; |
| 181 | interest.answerOriginKind = template.answerOriginKind; |
| 182 | interest.scope = template.scope; |
| 183 | interest.interestLifetime = template.interestLifetime; |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 184 | } |
| 185 | else |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 186 | interest.interestLifetime = 4000; // default interest timeout value in milliseconds. |
Jeff Thompson | 3441976 | 2012-10-15 22:24:12 -0700 | [diff] [blame] | 187 | |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 188 | if (this.host == null || this.port == null) { |
| 189 | if (this.getHostAndPort == null) |
| 190 | console.log('ERROR: host OR port NOT SET'); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 191 | else { |
| 192 | var thisNDN = this; |
| 193 | this.connectAndExecute |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 194 | (function() { thisNDN.reconnectAndExpressInterest(interest, closure); }); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 195 | } |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 196 | } |
| 197 | else |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 198 | this.reconnectAndExpressInterest(interest, closure); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 199 | }; |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 200 | |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 201 | /* |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 202 | * If the host and port are different than the ones in this.transport, then call |
| 203 | * this.transport.connect to change the connection (or connect for the first time). |
| 204 | * Then call expressInterestHelper. |
| 205 | */ |
| 206 | NDN.prototype.reconnectAndExpressInterest = function(interest, closure) { |
| 207 | if (this.transport.connectedHost != this.host || this.transport.connectedPort != this.port) { |
| 208 | var thisNDN = this; |
| 209 | this.transport.connect(thisNDN, function() { thisNDN.expressInterestHelper(interest, closure); }); |
| 210 | } |
| 211 | else |
| 212 | this.expressInterestHelper(interest, closure); |
| 213 | }; |
| 214 | |
| 215 | /* |
| 216 | * 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] | 217 | * this.transport.send to send the interest. |
| 218 | */ |
| 219 | NDN.prototype.expressInterestHelper = function(interest, closure) { |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 220 | var binaryInterest = encodeToBinaryInterest(interest); |
| 221 | var thisNDN = this; |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 222 | //TODO: check local content store first |
| 223 | if (closure != null) { |
| 224 | var pitEntry = new PITEntry(interest, closure); |
| 225 | // TODO: This needs to be a single thread-safe transaction on a global object. |
| 226 | NDN.PITTable.push(pitEntry); |
| 227 | closure.pitEntry = pitEntry; |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 228 | |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 229 | // Set interest timer. |
Jeff Thompson | a53e65a | 2013-02-10 10:52:52 -0800 | [diff] [blame] | 230 | var timeoutMilliseconds = (interest.interestLifetime || 4000); |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 231 | var timeoutCallback = function() { |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame^] | 232 | if (LOG > 1) console.log("Interest time out: " + interest.name.to_uri()); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 233 | |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 234 | // Remove PIT entry from NDN.PITTable, even if we add it again later to re-express |
| 235 | // the interest because we don't want to match it in the mean time. |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 236 | // TODO: Make this a thread-safe operation on the global PITTable. |
| 237 | var index = NDN.PITTable.indexOf(pitEntry); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 238 | if (index >= 0) |
| 239 | NDN.PITTable.splice(index, 1); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 240 | |
| 241 | // Raise closure callback |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 242 | if (closure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, |
| 243 | new UpcallInfo(thisNDN, interest, 0, null)) == Closure.RESULT_REEXPRESS) { |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame^] | 244 | if (LOG > 1) console.log("Re-express interest: " + interest.name.to_uri()); |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 245 | pitEntry.timerID = setTimeout(timeoutCallback, timeoutMilliseconds); |
| 246 | NDN.PITTable.push(pitEntry); |
| 247 | thisNDN.transport.send(binaryInterest); |
| 248 | } |
| 249 | }; |
| 250 | pitEntry.timerID = setTimeout(timeoutCallback, timeoutMilliseconds); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Jeff Thompson | e8de582 | 2013-02-17 21:18:49 -0800 | [diff] [blame] | 253 | this.transport.send(binaryInterest); |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 254 | }; |
| 255 | |
Jeff Thompson | 5b265a7 | 2012-11-12 01:13:08 -0800 | [diff] [blame] | 256 | NDN.prototype.registerPrefix = function(name, closure, flag) { |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 257 | var thisNDN = this; |
| 258 | var onConnected = function() { |
| 259 | if (thisNDN.ccndid == null) { |
| 260 | // Fetch ccndid first, then register. |
| 261 | var interest = new Interest(NDN.ccndIdFetcher); |
| 262 | interest.interestLifetime = 4000; // milliseconds |
| 263 | if (LOG>3) console.log('Expressing interest for ccndid from ccnd.'); |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 264 | thisNDN.reconnectAndExpressInterest |
| 265 | (interest, new NDN.FetchCcndidClosure(thisNDN, name, closure, flag)); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 266 | } |
| 267 | else |
| 268 | thisNDN.registerPrefixHelper(name, closure, flag); |
| 269 | }; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 270 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 271 | if (this.host == null || this.port == null) { |
| 272 | if (this.getHostAndPort == null) |
| 273 | console.log('ERROR: host OR port NOT SET'); |
| 274 | else |
| 275 | this.connectAndExecute(onConnected); |
| 276 | } |
| 277 | else |
| 278 | onConnected(); |
| 279 | }; |
| 280 | |
| 281 | /* |
| 282 | * This is a closure to receive the ContentObject for NDN.ccndIdFetcher and call |
| 283 | * registerPrefixHelper(name, callerClosure, flag). |
| 284 | */ |
| 285 | NDN.FetchCcndidClosure = function FetchCcndidClosure(ndn, name, callerClosure, flag) { |
| 286 | // Inherit from Closure. |
| 287 | Closure.call(this); |
| 288 | |
| 289 | this.ndn = ndn; |
| 290 | this.name = name; |
| 291 | this.callerClosure = callerClosure; |
| 292 | this.flag = flag; |
| 293 | }; |
| 294 | |
| 295 | NDN.FetchCcndidClosure.prototype.upcall = function(kind, upcallInfo) { |
| 296 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 297 | console.log("Timeout while requesting the ccndid. Cannot registerPrefix for " + |
| 298 | this.name.to_uri() + " ."); |
| 299 | return Closure.RESULT_OK; |
| 300 | } |
| 301 | if (!(kind == Closure.UPCALL_CONTENT || |
| 302 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
| 303 | // The upcall is not for us. |
| 304 | return Closure.RESULT_ERR; |
| 305 | |
| 306 | var co = upcallInfo.contentObject; |
| 307 | if (!co.signedInfo || !co.signedInfo.publisher |
| 308 | || !co.signedInfo.publisher.publisherPublicKeyDigest) |
| 309 | console.log |
| 310 | ("ContentObject doesn't have a publisherPublicKeyDigest. Cannot set ccndid and registerPrefix for " |
| 311 | + this.name.to_uri() + " ."); |
| 312 | else { |
| 313 | if (LOG>3) console.log('Got ccndid from ccnd.'); |
| 314 | this.ndn.ccndid = co.signedInfo.publisher.publisherPublicKeyDigest; |
| 315 | if (LOG>3) console.log(this.ndn.ccndid); |
| 316 | |
| 317 | this.ndn.registerPrefixHelper(this.name, this.callerClosure, this.flag); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 318 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 319 | |
| 320 | return Closure.RESULT_OK; |
| 321 | }; |
| 322 | |
Jeff Thompson | 537ce14 | 2013-01-26 20:02:48 -0800 | [diff] [blame] | 323 | /* |
| 324 | * Do the work of registerPrefix once we know we are connected with a ccndid. |
| 325 | */ |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 326 | NDN.prototype.registerPrefixHelper = function(name, closure, flag) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 327 | var fe = new ForwardingEntry('selfreg', name, null, null, 3, 2147483647); |
| 328 | var bytes = encodeForwardingEntry(fe); |
| 329 | |
| 330 | var si = new SignedInfo(); |
| 331 | si.setFields(); |
| 332 | |
| 333 | var co = new ContentObject(new Name(), si, bytes, new Signature()); |
| 334 | co.sign(); |
| 335 | var coBinary = encodeToBinaryContentObject(co); |
| 336 | |
| 337 | //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'); |
| 338 | var nodename = this.ccndid; |
| 339 | var interestName = new Name(['ccnx', nodename, 'selfreg', coBinary]); |
| 340 | |
| 341 | var interest = new Interest(interestName); |
| 342 | interest.scope = 1; |
| 343 | if (LOG > 3) console.log('Send Interest registration packet.'); |
| 344 | |
| 345 | var csEntry = new CSEntry(name.getName(), closure); |
| 346 | NDN.CSTable.push(csEntry); |
| 347 | |
| 348 | this.transport.send(encodeToBinaryInterest(interest)); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | /* |
| 352 | * This is called when an entire binary XML element is received, such as a ContentObject or Interest. |
| 353 | * Look up in the PITTable and call the closure callback. |
| 354 | */ |
| 355 | NDN.prototype.onReceivedElement = function(element) { |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 356 | 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] | 357 | var decoder = new BinaryXMLDecoder(element); |
| 358 | // Dispatch according to packet type |
| 359 | if (decoder.peekStartElement(CCNProtocolDTags.Interest)) { // Interest packet |
| 360 | if (LOG > 3) console.log('Interest packet received.'); |
| 361 | |
| 362 | var interest = new Interest(); |
| 363 | interest.from_ccnb(decoder); |
| 364 | if (LOG > 3) console.log(interest); |
| 365 | var nameStr = escape(interest.name.getName()); |
| 366 | if (LOG > 3) console.log(nameStr); |
| 367 | |
| 368 | var entry = getEntryForRegisteredPrefix(nameStr); |
| 369 | if (entry != null) { |
| 370 | //console.log(entry); |
| 371 | var info = new UpcallInfo(this, interest, 0, null); |
| 372 | var ret = entry.closure.upcall(Closure.UPCALL_INTEREST, info); |
| 373 | if (ret == Closure.RESULT_INTEREST_CONSUMED && info.contentObject != null) |
| 374 | this.transport.send(encodeToBinaryContentObject(info.contentObject)); |
| 375 | } |
| 376 | } else if (decoder.peekStartElement(CCNProtocolDTags.ContentObject)) { // Content packet |
| 377 | if (LOG > 3) console.log('ContentObject packet received.'); |
| 378 | |
| 379 | var co = new ContentObject(); |
| 380 | co.from_ccnb(decoder); |
| 381 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 382 | var pitEntry = NDN.getEntryForExpressedInterest(co.name); |
| 383 | if (pitEntry != null) { |
Jeff Thompson | 0ee721e | 2013-02-18 17:46:55 -0800 | [diff] [blame] | 384 | // Cancel interest timer |
| 385 | clearTimeout(pitEntry.timerID); |
| 386 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 387 | // Remove PIT entry from NDN.PITTable |
| 388 | var index = NDN.PITTable.indexOf(pitEntry); |
| 389 | if (index >= 0) |
| 390 | NDN.PITTable.splice(index, 1); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 391 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 392 | var currentClosure = pitEntry.closure; |
Jeff Thompson | 0ee721e | 2013-02-18 17:46:55 -0800 | [diff] [blame] | 393 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 394 | if (this.verify == false) { |
| 395 | // Pass content up without verifying the signature |
| 396 | currentClosure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED, new UpcallInfo(this, null, 0, co)); |
| 397 | return; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 398 | } |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 399 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 400 | // Key verification |
| 401 | |
| 402 | // Recursive key fetching & verification closure |
| 403 | var KeyFetchClosure = function KeyFetchClosure(content, closure, key, sig, wit) { |
| 404 | this.contentObject = content; // unverified content object |
| 405 | this.closure = closure; // closure corresponding to the contentObject |
| 406 | this.keyName = key; // name of current key to be fetched |
| 407 | this.sigHex = sig; // hex signature string to be verified |
| 408 | this.witness = wit; |
| 409 | |
| 410 | Closure.call(this); |
| 411 | }; |
| 412 | |
| 413 | var thisNDN = this; |
| 414 | KeyFetchClosure.prototype.upcall = function(kind, upcallInfo) { |
| 415 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 416 | console.log("In KeyFetchClosure.upcall: interest time out."); |
| 417 | console.log(this.keyName.contentName.getName()); |
| 418 | } else if (kind == Closure.UPCALL_CONTENT) { |
| 419 | //console.log("In KeyFetchClosure.upcall: signature verification passed"); |
| 420 | |
| 421 | var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content); |
| 422 | var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.witness, this.sigHex); |
| 423 | |
| 424 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 425 | //console.log("raise encapsulated closure"); |
| 426 | this.closure.upcall(flag, new UpcallInfo(thisNDN, null, 0, this.contentObject)); |
| 427 | |
| 428 | // Store key in cache |
| 429 | var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 430 | NDN.addKeyEntry(keyEntry); |
| 431 | //console.log(NDN.KeyStore); |
| 432 | } else if (kind == Closure.UPCALL_CONTENT_BAD) { |
| 433 | console.log("In KeyFetchClosure.upcall: signature verification failed"); |
Wentao Shang | d460739 | 2013-01-24 23:08:49 -0800 | [diff] [blame] | 434 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 435 | }; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 436 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 437 | if (co.signedInfo && co.signedInfo.locator && co.signature) { |
| 438 | if (LOG > 3) console.log("Key verification..."); |
| 439 | var sigHex = DataUtils.toHex(co.signature.signature).toLowerCase(); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 440 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 441 | var wit = null; |
| 442 | if (co.signature.Witness != null) { |
| 443 | wit = new Witness(); |
| 444 | wit.decode(co.signature.Witness); |
| 445 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 446 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 447 | var keylocator = co.signedInfo.locator; |
| 448 | if (keylocator.type == KeyLocatorType.KEYNAME) { |
| 449 | if (LOG > 3) console.log("KeyLocator contains KEYNAME"); |
| 450 | //var keyname = keylocator.keyName.contentName.getName(); |
| 451 | //console.log(nameStr); |
| 452 | //console.log(keyname); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 453 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 454 | if (keylocator.keyName.contentName.match(co.name)) { |
| 455 | if (LOG > 3) console.log("Content is key itself"); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 456 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 457 | var rsakey = decodeSubjectPublicKeyInfo(co.content); |
| 458 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 459 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 460 | |
| 461 | currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co)); |
| 462 | |
| 463 | // SWT: We don't need to store key here since the same key will be |
| 464 | // stored again in the closure. |
| 465 | //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime()); |
| 466 | //NDN.addKeyEntry(keyEntry); |
| 467 | //console.log(NDN.KeyStore); |
| 468 | } else { |
| 469 | // Check local key store |
| 470 | var keyEntry = NDN.getKeyByName(keylocator.keyName); |
| 471 | if (keyEntry) { |
| 472 | // Key found, verify now |
| 473 | if (LOG > 3) console.log("Local key cache hit"); |
| 474 | var rsakey = keyEntry.rsaKey; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 475 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 476 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 477 | |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 478 | // Raise callback |
| 479 | currentClosure.upcall(flag, new UpcallInfo(this, null, 0, co)); |
| 480 | } else { |
| 481 | // Not found, fetch now |
| 482 | if (LOG > 3) console.log("Fetch key according to keylocator"); |
| 483 | var nextClosure = new KeyFetchClosure(co, currentClosure, keylocator.keyName, sigHex, wit); |
| 484 | this.expressInterest(keylocator.keyName.contentName.getPrefix(4), nextClosure); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 485 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 486 | } |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 487 | } else if (keylocator.type == KeyLocatorType.KEY) { |
| 488 | if (LOG > 3) console.log("Keylocator contains KEY"); |
| 489 | |
| 490 | var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey); |
| 491 | var verified = rsakey.verifyByteArray(co.rawSignatureData, wit, sigHex); |
| 492 | |
| 493 | var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD; |
| 494 | // Raise callback |
| 495 | currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(this, null, 0, co)); |
| 496 | |
| 497 | // Since KeyLocator does not contain key name for this key, |
| 498 | // we have no way to store it as a key entry in KeyStore. |
| 499 | } else { |
| 500 | var cert = keylocator.certificate; |
| 501 | console.log("KeyLocator contains CERT"); |
| 502 | console.log(cert); |
| 503 | |
| 504 | // TODO: verify certificate |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | } else |
| 509 | console.log('Incoming packet is not Interest or ContentObject. Discard now.'); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 510 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | * 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] | 514 | * is not alive. Get a host and port, connect, then execute onConnected(). |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 515 | */ |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 516 | NDN.prototype.connectAndExecute = function(onConnected) { |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 517 | var hostAndPort = this.getHostAndPort(); |
| 518 | if (hostAndPort == null) { |
| 519 | console.log('ERROR: No more hosts from getHostAndPort'); |
| 520 | this.host = null; |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (hostAndPort.host == this.host && hostAndPort.port == this.port) { |
| 525 | console.log('ERROR: The host returned by getHostAndPort is not alive: ' + |
| 526 | this.host + ":" + this.port); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | this.host = hostAndPort.host; |
| 531 | this.port = hostAndPort.port; |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame^] | 532 | if (LOG>0) console.log("connectAndExecute: trying host from getHostAndPort: " + this.host); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 533 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 534 | // Fetch any content. |
| 535 | var interest = new Interest(new Name("/")); |
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 536 | interest.interestLifetime = 4000; // milliseconds |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 537 | |
| 538 | var thisNDN = this; |
| 539 | var timerID = setTimeout(function() { |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame^] | 540 | if (LOG>0) console.log("connectAndExecute: timeout waiting for host " + thisNDN.host); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 541 | // Try again. |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 542 | thisNDN.connectAndExecute(onConnected); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 543 | }, 3000); |
| 544 | |
Jeff Thompson | f668acb | 2013-01-26 20:29:46 -0800 | [diff] [blame] | 545 | this.reconnectAndExpressInterest |
| 546 | (interest, new NDN.ConnectClosure(this, onConnected, timerID)); |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 547 | }; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 548 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 549 | NDN.ConnectClosure = function ConnectClosure(ndn, onConnected, timerID) { |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 550 | // Inherit from Closure. |
| 551 | Closure.call(this); |
| 552 | |
| 553 | this.ndn = ndn; |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 554 | this.onConnected = onConnected; |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 555 | this.timerID = timerID; |
| 556 | }; |
| 557 | |
| 558 | NDN.ConnectClosure.prototype.upcall = function(kind, upcallInfo) { |
| 559 | if (!(kind == Closure.UPCALL_CONTENT || |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 560 | kind == Closure.UPCALL_CONTENT_UNVERIFIED)) |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 561 | // The upcall is not for us. |
| 562 | return Closure.RESULT_ERR; |
| 563 | |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 564 | // The host is alive, so cancel the timeout and continue with onConnected(). |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 565 | clearTimeout(this.timerID); |
Jeff Thompson | d771b12 | 2013-01-26 19:04:41 -0800 | [diff] [blame] | 566 | |
| 567 | // Call NDN.onopen after success |
| 568 | this.ndn.readyStatus = NDN.OPENED; |
| 569 | this.ndn.onopen(); |
| 570 | |
Jeff Thompson | 3d9ad14 | 2013-03-10 16:55:28 -0700 | [diff] [blame^] | 571 | if (LOG>0) console.log("connectAndExecute: connected to host " + this.ndn.host); |
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 572 | this.onConnected(); |
Jeff Thompson | d3a80dc | 2012-12-16 17:52:43 -0800 | [diff] [blame] | 573 | |
| 574 | return Closure.RESULT_OK; |
| 575 | }; |
| 576 | |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 577 | /* |
| 578 | * A BinaryXmlElementReader lets you call onReceivedData multiple times which uses a |
| 579 | * BinaryXMLStructureDecoder to detect the end of a binary XML element and calls |
| 580 | * elementListener.onReceivedElement(element) with the element. |
| 581 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
| 582 | */ |
| 583 | var BinaryXmlElementReader = function BinaryXmlElementReader(elementListener) { |
| 584 | this.elementListener = elementListener; |
| 585 | this.dataParts = []; |
| 586 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 587 | }; |
| 588 | |
| 589 | BinaryXmlElementReader.prototype.onReceivedData = function(/* Uint8Array */ rawData) { |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 590 | // Process multiple objects in the data. |
| 591 | while(true) { |
| 592 | // Scan the input to check if a whole ccnb object has been read. |
| 593 | this.structureDecoder.seek(0); |
| 594 | if (this.structureDecoder.findElementEnd(rawData)) { |
| 595 | // Got the remainder of an object. Report to the caller. |
| 596 | this.dataParts.push(rawData.subarray(0, this.structureDecoder.offset)); |
Jeff Thompson | 0790af8 | 2013-01-26 19:54:27 -0800 | [diff] [blame] | 597 | var element = DataUtils.concatArrays(this.dataParts); |
| 598 | this.dataParts = []; |
| 599 | try { |
| 600 | this.elementListener.onReceivedElement(element); |
| 601 | } catch (ex) { |
| 602 | console.log("BinaryXmlElementReader: ignoring exception from onReceivedElement: " + ex); |
| 603 | } |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 604 | |
| 605 | // Need to read a new object. |
| 606 | rawData = rawData.subarray(this.structureDecoder.offset, rawData.length); |
Jeff Thompson | 75771cb | 2013-01-20 23:27:38 -0800 | [diff] [blame] | 607 | this.structureDecoder = new BinaryXMLStructureDecoder(); |
| 608 | if (rawData.length == 0) |
| 609 | // No more data in the packet. |
| 610 | return; |
| 611 | |
| 612 | // else loop back to decode. |
| 613 | } |
| 614 | else { |
| 615 | // Save for a later call to concatArrays so that we only copy data once. |
| 616 | this.dataParts.push(rawData); |
Jeff Thompson | a46083c | 2013-01-20 23:55:21 -0800 | [diff] [blame] | 617 | 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] | 618 | return; |
| 619 | } |
| 620 | } |
| 621 | } |