blob: 74177c3a8c3cff3a03582c9a08856917188247de [file] [log] [blame]
philoLbd28e132015-04-16 23:54:21 -07001# -*- 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
21import time
22from pyndn import Name
23from pyndn import Data
24from pyndn import Face
25from pyndn.security import KeyChain
philo5d4724e2014-11-10 19:34:05 +000026from base_node import BaseNode
27
philoLbd28e132015-04-16 23:54:21 -070028
29def dump(*list):
30 result = ""
31 for element in list:
32 result += (element if type(element) is str else repr(element)) + " "
33 print(result)
34
philo5d4724e2014-11-10 19:34:05 +000035class Controller(BaseNode):
Teng Lianga0b49372015-05-15 05:30:27 -070036 def __init__(self,configFileName):
37 super(Controller, self).__init__(configFileName=configFileName)
philoLbd28e132015-04-16 23:54:21 -070038 self._responseCount = 0
Teng Lianga0b49372015-05-15 05:30:27 -070039 self._symmetricKey = "symmetricKeyForBootStrapping"
40 self._prefix = "/home/controller"
philoLbd28e132015-04-16 23:54:21 -070041
42 def onInterest(self, prefix, interest, transport, registeredPrefixId):
43 self._responseCount += 1
Teng Lianga0b49372015-05-15 05:30:27 -070044
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()
philoLbd28e132015-04-16 23:54:21 -070071
72 #dump("Sent content", content)
Teng Lianga0b49372015-05-15 05:30:27 -070073 transport.send(encodedData.toBuffer())
philoLbd28e132015-04-16 23:54:21 -070074
75 def onRegisterFailed(self, prefix):
76 self._responseCount += 1
77 dump("Register failed for prefix", prefix.toUri())
78
Teng Lianga0b49372015-05-15 05:30:27 -070079 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
philo5d4724e2014-11-10 19:34:05 +0000101if __name__ == '__main__':
102
philoLbd28e132015-04-16 23:54:21 -0700103 # 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.
philo5d4724e2014-11-10 19:34:05 +0000107
Teng Lianga0b49372015-05-15 05:30:27 -0700108 controller = Controller("default.conf")
109 controller.beforeLoopStart()
110
philo5d4724e2014-11-10 19:34:05 +0000111 face.setCommandSigningInfo(controller.getKeyChain(), controller.getDefaultCertificateName())
philoLbd28e132015-04-16 23:54:21 -0700112
113 # Also use the default certificate name to sign data packets.
philo5d4724e2014-11-10 19:34:05 +0000114
philoLbd28e132015-04-16 23:54:21 -0700115 prefix = Name("/home/")
Teng Lianga0b49372015-05-15 05:30:27 -0700116 dump("Register prefix", prefix)
117
philoLbd28e132015-04-16 23:54:21 -0700118 face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed)
119
Teng Lianga0b49372015-05-15 05:30:27 -0700120 identityName = controller._identityManager.getDefaultIdentity()
121 keyName = controller._identityManager.getDefaultKeyNameForIdentity(identityName)
122
123 key = controller._identityManager.getPublicKey(keyName)
124 dump("key : ",key.getKeyDer().toHex())
125
philoLbd28e132015-04-16 23:54:21 -0700126 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