def sendSignedInterest(self,command): self.count = self.count +1 #time.sleep(self.cfg.refreshInterval) fullURI = self.cfg.appPrefix + command #print fullURI i = Interest() #self.state = NameCrypto.new_state() #build keyLocator to append to interest for NameCrypto on upcall keyLoc = pyccn.KeyLocator(self.key) keyLocStr = pyccn._pyccn.dump_charbuf(keyLoc.ccn_data) nameAndKeyLoc = Name(str(fullURI)) #print("there are "+str(len(nameAndKeyLoc))+" components") nameAndKeyLoc = nameAndKeyLoc.append(keyLocStr) #print("there are "+str(len(nameAndKeyLoc))+" components after adding keyLocStr") #symmetric #authName = NameCrypto.authenticate_command(self.state, nameAndKeyLoc, self.cfg.appName, self.cryptoKey) #asymmetric authName = NameCrypto.authenticate_command_sig(self.state, nameAndKeyLoc, self.cfg.appName, self.key) #print authName.components self.handle.setInterestFilter(Name(authName), self) #trace(str(time.time()),str(authName),"expressed") # time sent tS = time.time() self.log.info(str((tS-self.startTime))+",INTEREST_EXPRESSED, , "+ str(authName)) co = self.handle.expressInterest(authName,self) self.handle.run(0)
def get_pyccn_name(self, name): '''Get a valid name for PyCCN. This is useful when the name string is encoded as unicode, as is the usual case in Python. However, PyCCN has problem handling unicode names, raising TypeError as a result. Args: name (str): the name string Returns: An ascii encoded name string ''' if isinstance(name, unicode): return Name(name.encode('ascii', 'ignore')) else: return Name(name)
def build_interest(self, latest): if self.start_with_latest: latest=True self.start_with_latest = False excl = Interest.ExclusionFilter() excl.add_any() excl.add_name(Name.Name([self.latest_version])) # expected result should be between those two names excl.add_name(Name.Name([self.last_version_marker])) excl.add_any() interest = Interest.Interest(name=self.base_name, exclude=excl, \ minSuffixComponents=3, maxSuffixComponents=3) interest.childSelector = 1 if latest else 0 return interest
def getSegment(seg): global h, base name = Name.Name(base) name += segment(seg) co = h.get(name) return co.content
def __init__(self, prefixstr): self.handle = CCN() #XXX: temporary, until we allow fetching key from key storage self.key = self.handle.getDefaultKey() self.keylocator = KeyLocator(self.key) self.prefix = Name(prefixstr)
def __init__(self, prefix, handle): self.prefix = Name.Name(prefix) self.handle = handle self.content_objects = [] self.queue = Queue.Queue(500) self.cleanup_time = 15 * 60 # keep responses for 15 min handle.setInterestFilter(self.prefix, self)
def start(self): self.handle.setInterestFilter(self.name, self) interest = Interest.Interest(name=Name.Name(self.name)) interest.name += '\xC1.R.sw' interest.name.appendNonce() print("Expressing interest: ccnx:%s" % interest.name) self.handle.expressInterest(interest.name, self, interest) self.handle.run(-1)
def __init__(self, base_name, callback, handle=CCN.CCN(), version=None, latest=True): # some constants self.version_marker = '\xfd' self.first_version_marker = self.version_marker self.last_version_marker = '\xfe\x00\x00\x00\x00\x00\x00' self.base_name = Name.Name(base_name) self.callback = callback self.handle = handle self.latest_version = version if version else self.first_version_marker self.start_with_latest = latest
def sendSignedInterest(self,command): fullURI = self.cfg.appPrefix + command print fullURI i = Interest() self.state = NameCrypto.new_state() #build keyLocator to append to interest for NameCrypto on upcall # keyLoc = pyccn.KeyLocator(self.key) keyLocStr = pyccn._pyccn.dump_charbuf(keyLoc.ccn_data) nameAndKeyLoc = Name(str(fullURI)) #print("there are "+str(len(nameAndKeyLoc))+" components") nameAndKeyLoc = nameAndKeyLoc.append(keyLocStr) #print("there are "+str(len(nameAndKeyLoc))+" components after adding keyLocStr") authName = NameCrypto.authenticate_command(self.state, nameAndKeyLoc, self.cfg.appName, self.cryptoKey) #print authName.components # send interest # for just expressing co = self.handle.expressInterest(authName,self)
def preparePacket(seg, data): global basename, si, key co_name = Name.Name(basename) co_name += segment(seg) # print("preparing %s" % co_name) co = ContentObject.ContentObject() co.content = data co.name = co_name co.signedInfo = si co.sign(key) return co
def getSegment(seg): global h, base name = Name.Name(base) name += segment(seg) print "Requesting: %s" % name co = None tries = 3 while not co: if tries <= 0: return "" co = h.get(name, None, 150) if co: return co.content print "retrying" tries -= 1 return None
def publish(key, name, last_segment, content): print("Generating: %s" % name) # Name co_name = Name.Name(name) # SignedInfo si = ContentObject.SignedInfo() si.type = ContentObject.ContentType.CCN_CONTENT_DATA si.finalBlockID = last_segment si.publisherPublicKeyDigest = key.publicKeyID si.keyLocator = Key.KeyLocator(key) # ContentObject co = ContentObject.ContentObject() co.content = content co.name = co_name co.signedInfo = si co.sign(key) return co
def upcall(self, kind, upcallInfo): global sender_handle, n, k, kl print("Sender closure:") print(upcallInfo) co = ContentObject() co.name = Name(n) co.content = "Frou" si = SignedInfo() si.publisherPublicKeyDigest = k.publicKeyID si.type = pyccn.CONTENT_DATA si.freshnessSeconds = 5 si.keyLocator = kl co.signedInfo = si co.sign(k) r = sender_handle.put(co) print("put(co) = ", r) #sender_handle.setRunTimeout(0) return pyccn.RESULT_INTEREST_CONSUMED
def __init__(self, handle, name, content): self.handle = handle self.name = Name.Name(name) self.content_objects = content
def __init__(self, handle, name): self.queue = Queue.Queue(1000) self.counter = 0 self.handle = handle self.name = Name.Name(name)
def getNext(self): name = Name.Name(self.name) name += segment(self.counter) # print "Issuing interest %s" % name self.counter += 1 self.handle.expressInterest(name, self)
from pyccn import Key, Name, _pyccn, CCN n = Name("/this/is/a/name") ccn_name1 = n.ccn_data name1 = _pyccn.name_comps_from_ccn(ccn_name1) locator1 = _pyccn.KeyLocator_to_ccn(name=ccn_name1) print(locator1) locator1_obj = _pyccn.KeyLocator_obj_from_ccn(locator1) print(locator1_obj) print(locator1_obj.keyName) name2 = _pyccn.name_comps_from_ccn(locator1_obj.keyName.ccn_data) print(name2) for comp1, comp2 in zip(name1, name2): if comp1 != comp2: raise AssertionError("Got a different output: '%s' != '%s'" % (comp1, comp2)) key1 = CCN.getDefaultKey() locator2 = _pyccn.KeyLocator_to_ccn(key=key1.ccn_data_public) print(locator2) locator2_obj = _pyccn.KeyLocator_obj_from_ccn(locator2) key2 = locator2_obj.key print(key2) print(key1.ccn_data_public)
#fi = open(file, "r") #fi.seek(100*1024*1024) #def block(): # global fi, position, block_size # data = fi.read(block_size) # return data import struct, vlc_access from pyccn import * h = CCN.CCN() #base = Name.Name('/movie/%FD%04%E0%ACL%97%60') base = Name.Name('/testmovie') def segment(segment): return b'\x00' + struct.pack('!Q', segment).lstrip('\x00') def getSegment(seg): global h, base name = Name.Name(base) name += segment(seg) print "Requesting: %s" % name co = None tries = 3 while not co:
from pyccn import _pyccn, CCN, Name import sys comps = ['this', 'is', 'some', 'name'] print(comps) ccn_name = _pyccn.name_comps_to_ccn(comps) comps2 = _pyccn.name_comps_from_ccn(ccn_name) print(comps2) #for comp1, comp2 in zip(map(lambda x: bytearray(x), comps), comps2): for comp1, comp2 in zip([bytearray(x, "ascii") for x in comps], comps2): if comp1 != comp2: raise AssertionError("Got a different output: '%s' != '%s'" % (comp1, comp2)) n = Name(['hello', 'world']) print(str(n)) if str(n) != "/hello/world": raise AssertionError("expected /hello/world") n = Name("ccnx:///testing/1/2/3/") print(str(n)) if str(n) != "/testing/1/2/3": raise AssertionError("expected /testing/1/2/3 got: " + str(n)) if len(n) != 4: raise AssertionError("expected 4 components, got: " + len(n)) print(n.components) assert(n.components == [b'testing', b'1', b'2', b'3'])
from pyccn import CCN, Name c = CCN() print(c) n = Name() print(n) n = Name("ccnx:/ccnx/ping") print(n) co = c.get(n) print(co) #this shouldn't cause segfault print(n) n = Name("ccnx:/ccnx/some_nonexisting_name") co = c.get(n, None, 100) #this shouldn't cause segfault! print(co)
from pyccn import Interest, Name, Key, CCN, _pyccn import datetime k = CCN.getDefaultKey() i = Interest() i.name = Name('/hello/world') i.minSuffixComponents = 2 i.maxSuffixComponents = 4 i.publisherPublicKeyDigest = k.publicKeyID i.exclude = None i.childSelector = 1 i.answerOriginKind = 4 i.scope = 2 i.interestLifetime = 30.0 i.nonce = b'abababa' print(i) print(i.ccn_data) i2 = _pyccn.Interest_obj_from_ccn(i.ccn_data) print(i2) assert (i.name == i2.name) assert (i.minSuffixComponents == i2.minSuffixComponents) assert (i.maxSuffixComponents == i2.maxSuffixComponents) assert (i.publisherPublicKeyDigest == i2.publisherPublicKeyDigest) assert (i.exclude == i2.exclude) assert (i.childSelector == i2.childSelector) assert (i.scope == i2.scope) assert (i.interestLifetime == i2.interestLifetime)
import pyccn from pyccn import Name, NameCrypto, Key, KeyLocator, CCN window = -1 # Test symmetric authentication state = NameCrypto.new_state() secret = '1234567812345678' app_name = 'cuerda' app_key = NameCrypto.generate_application_key(secret, app_name) name = Name('/ndn/ucla.edu/apps/cuerda') auth_name = NameCrypto.authenticate_command(state, name, app_name, app_key) print auth_name state2 = NameCrypto.new_state() ret = NameCrypto.verify_command(state2, auth_name, window, fixture_key=secret) print ret assert(ret == True) name_from_js = Name( '/ndn/ucla.edu/apps/cuerda/%40%96%1CQ%00%06cuerdaQk%8A%13%00%07%B8%90%00%00%00%03%00%00%00%00%BB%14%FCHl%A5%F6%5D%18%1EIs%9E%91t%5El%C1y%3F%BAA%A7%02Y%EC%804%23%A4%D4t' ) state3 = NameCrypto.new_state()
from pyccn import CCN, Name, Interest, Key, ContentObject, Closure, KeyLocator, ExclusionFilter from Tyzx import CompositeObject, BaseObject prefix = "ccnx:/ndn/ucla.edu/apps/tv1/occupants" DISCOVER_INTEREST_PERIOD = 0.250 # How often UPDATE_INTEREST_PERIOD = 0.250 CCN_WAIT_TIME_MS = 1 tyzxObjects = {} oldObjects = set() # still may be in the content store ccn = CCN() interestDiscover = Interest() interestDiscover.name = Name(prefix) interestDiscover.minSuffixComponents = 2 # occupant id + the implicit digest at a minimum interestUpdate = Interest() interestUpdate.minSuffixComponents = 2 # time (version) + the implicit digest interestUpdate.childSelector = 1 # rightmost child #interestUpdate.interestLifetime = ???? lastdiscovertime = 0 def versionFromTime(t): bintime = struct.pack("!Q", int(t * 4096 + 0.5)) version = bintime.lstrip(b'\x00') return b'\xfd' + version
si = ContentObject.SignedInfo() si.type = ContentObject.ContentType.CCN_CONTENT_DATA si.finalBlockID = last_segment si.publisherPublicKeyDigest = key.publicKeyID si.keyLocator = Key.KeyLocator(key) # ContentObject co = ContentObject.ContentObject() co.content = content co.name = co_name co.signedInfo = si co.sign(key) return co name = Name.Name('/repo/upload/test') name_v = Name.Name(name) name_v.appendVersion() handle = CCN.CCN() key = handle.getDefaultKey() last_seg = segment(9) content = [] for i in range(10): name_s = Name.Name(name_v) name_s += segment(i) co = publish(key, name_s, last_seg, "Segment: %d\n" % i) content.append(co) upload = RepoUpload(handle, name_v, content)
from pyccn import CCN, Name, Interest, Closure from time import sleep worked = False class MyClosure(Closure): def upcall(self, kind, upcallInfo): global worked print("Got response") print(kind) print(upcallInfo) worked = True n = Name("ccnx:/ccnx/ping") i = Interest() closure = MyClosure() c = CCN() res = c.expressInterest(n, closure, i) print(res) #causes crashes c.run(10) print("Ha!") assert (worked)
import pyccn from pyccn import CCN, Name, Interest, ContentObject, SignedInfo, Key, KeyLocator, Closure #from threading import Timer k = CCN.getDefaultKey() kl = KeyLocator(k) n = Name("/forty/two") class SenderClosure(Closure): def upcall(self, kind, upcallInfo): global sender_handle, n, k, kl print("Sender closure:") print(upcallInfo) co = ContentObject() co.name = Name(n) co.content = "Frou" si = SignedInfo() si.publisherPublicKeyDigest = k.publicKeyID si.type = pyccn.CONTENT_DATA si.freshnessSeconds = 5 si.keyLocator = kl co.signedInfo = si co.sign(k) r = sender_handle.put(co) print("put(co) = ", r)
def upcall(self, kind, upcallInfo): global lasttime if lasttime is None: # can't answer yet return pyccn.RESULT_OK if len(tyzxObjs.objs) < 1: return pyccn.RESULT_OK interest = upcallInfo.Interest #print "Interest", interest.name, time.time() # CALL content matches interest to check exclusion on versions # #print interest.exclude name = interest.name #print name if name == self.prefix: # root #print "Request for root: %s" % str(name) if interest.exclude is None: freshids = tyzxObjs.objs.keys()[0:1] else: if len(interest.exclude.components) > 0: ids = set(tyzxObjs.objs.keys()) # new stuff we have suffixes = set( [int(str(s)[1:]) for s in interest.exclude.components]) freshids = list(ids.difference( suffixes)) # do we need this conversion? #print "ids", ids #print "suffixes", suffixes #print "freshids", freshids else: freshids = tyzxObjs.objs.keys()[0:1] if len(freshids) > 0: child = freshids[0] else: return pyccn.RESULT_OK # no new content else: # should check what we're receiving! take next component child = int( name.components[-1:][0]) # why not be able to do this on name? #print "Request for child: %s" % child, time.time() #print "child - ", child if tyzxObjs.objs.has_key(child): O = tyzxObjs.objs[child] else: # Don't want to respond with a nonexistent. NDN way is to not answer interest. # If we've just started, we may not even know what the content store knows. # But, we do want to answer "exits" that we know about. #print "child", child, "is not present" if tyzxObjs.objExits.has_key(child): #print "child", child, "is exited" O = tyzxObjs.objExits[child] else: return pyccn.RESULT_OK #O = CompositeObject(BaseObject()) #O.time = lasttime.time #O.id = int(child) #O.status = "nonexistent" msgname = Name(self.prefix) msgname += str(child) msgname.components.append(versionFromTime( O.time)) # should have msgname.append #msgname.ccn_data_dirty=True - now handled within PyCCN # need binary add component self.message = self.publish(msgname, O.toJSON()) #print "Publishing", msgname, O.toJSON() #print "Present:", tyzxObjs.objs.keys(), time.time() self.handle.put(self.message) return pyccn.RESULT_INTEREST_CONSUMED return pyccn.RESULT_OK