philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2014-2015 Regents of the University of California. |
| 4 | # Author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU Lesser General Public License as published by |
| 8 | # the Free Software Foundation, either version 3 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU Lesser General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU Lesser General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | # A copy of the GNU Lesser General Public License is in the file COPYING. |
| 19 | |
| 20 | |
| 21 | import time |
| 22 | from pyndn import Name |
| 23 | from pyndn import Data |
| 24 | from pyndn import Face |
| 25 | from pyndn.security import KeyChain |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 26 | from base_node import BaseNode |
| 27 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 28 | |
| 29 | def dump(*list): |
| 30 | result = "" |
| 31 | for element in list: |
| 32 | result += (element if type(element) is str else repr(element)) + " " |
| 33 | print(result) |
| 34 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 35 | class Controller(BaseNode): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 36 | def __init__(self,configFileName): |
| 37 | super(Controller, self).__init__(configFileName=configFileName) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 38 | self._responseCount = 0 |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 39 | self._symmetricKey = "symmetricKeyForBootStrapping" |
| 40 | self._prefix = "/home/controller" |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 41 | |
| 42 | def onInterest(self, prefix, interest, transport, registeredPrefixId): |
| 43 | self._responseCount += 1 |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 44 | |
| 45 | interestName = interest.getName() |
| 46 | dump("Received interest ", interestName) |
| 47 | |
| 48 | componentsString = [] |
| 49 | for eachComponent in interestName._components: |
| 50 | componentsString.append(eachComponent.toEscapedString()) |
| 51 | if (len(componentsString) >= 6 and componentsString[0] == "home" and componentsString[1] == "controller" and componentsString[2] == "bootstrap"): |
| 52 | |
| 53 | newDeviceCategory = componentsString[3]; |
| 54 | newDeviceId = componentsString[4]; |
| 55 | signature = componentsString[5]; |
| 56 | |
| 57 | if (signature == self._symmetricKey): |
| 58 | #newDeviceIdentityName = Name("/home"+newDeviceCategory+newDeviceId) |
| 59 | content = "/home/"+newDeviceCategory+"/"+newDeviceId+"/" |
| 60 | #content = content + "/" |
| 61 | identityName = self._identityManager.getDefaultIdentity() |
| 62 | keyName = self._identityManager.getDefaultKeyNameForIdentity(identityName) |
| 63 | key = self._identityManager.getPublicKey(keyName) |
| 64 | content = content+key.getKeyDer().toHex() |
| 65 | |
| 66 | dump("Send data : ",content) |
| 67 | data = Data(interest.getName()) |
| 68 | data.setContent(content) |
| 69 | #self._keyChain.sign(data, self._certificateName) |
| 70 | encodedData = data.wireEncode() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 71 | |
| 72 | #dump("Sent content", content) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 73 | transport.send(encodedData.toBuffer()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 74 | |
| 75 | def onRegisterFailed(self, prefix): |
| 76 | self._responseCount += 1 |
| 77 | dump("Register failed for prefix", prefix.toUri()) |
| 78 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 79 | def beforeLoopStart(self): |
| 80 | identityName = Name(self._prefix) |
| 81 | dump(identityName) |
| 82 | defaultIdentityExist = True |
| 83 | try: |
| 84 | defaultIdentityName = self._identityManager.getDefaultIdentity() |
| 85 | dump(self._identityManager.getDefaultIdentity()) |
| 86 | dump(self._identityManager.getDefaultKeyNameForIdentity(defaultIdentityName)) |
| 87 | except: |
| 88 | defaultIdentityExist = False |
| 89 | |
| 90 | |
| 91 | #dump(self._identityManager.getDefaultKeyNameForIdentity(self._prefix)) |
| 92 | if not defaultIdentityExist or self._identityManager.getDefaultIdentity() != identityName: |
| 93 | #make one |
| 94 | self._identityManager.setDefaultIdentity(identityName) |
| 95 | self.log.warn("Generating controller key pair(this would take a while)......") |
| 96 | newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._prefix), isKsk=True) |
| 97 | newCert = self._identityManager.selfSign(newKey) |
| 98 | self._identityManager.addCertificateAsDefault(newCert) |
| 99 | |
| 100 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 101 | if __name__ == '__main__': |
| 102 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 103 | # The default Face will connect using a Unix socket, or to "localhost". |
| 104 | face = Face() |
| 105 | |
| 106 | # Use the system default key chain and certificate name to sign commands. |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 107 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 108 | controller = Controller("default.conf") |
| 109 | controller.beforeLoopStart() |
| 110 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 111 | face.setCommandSigningInfo(controller.getKeyChain(), controller.getDefaultCertificateName()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 112 | |
| 113 | # Also use the default certificate name to sign data packets. |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 114 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 115 | prefix = Name("/home/") |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 116 | dump("Register prefix", prefix) |
| 117 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 118 | face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed) |
| 119 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 120 | identityName = controller._identityManager.getDefaultIdentity() |
| 121 | keyName = controller._identityManager.getDefaultKeyNameForIdentity(identityName) |
| 122 | |
| 123 | key = controller._identityManager.getPublicKey(keyName) |
| 124 | dump("key : ",key.getKeyDer().toHex()) |
| 125 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 126 | while controller._responseCount < 100: |
| 127 | face.processEvents() |
| 128 | # We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 129 | time.sleep(0.01) |
| 130 | |
| 131 | face.shutdown() |
| 132 | |