Пример #1
0
 def __init__(self, *args, **kwargs):
     '''
 Creates a socket. As of now, we try to get the ccnx key from the default location
 '''
     super(CcnxSocket, self).__init__()
     self.ccnx_key = CCN.getDefaultKey()
     self.ccnx_key_locator = pyccn.KeyLocator(self.ccnx_key)
     self.ccnx_handle = CCN()
     self.event_loop = CcnxLoop(self.ccnx_handle)
Пример #2
0
 def __init__(self, *args, **kwargs):
   '''
   Creates a socket. As of now, we try to get the ccnx key from the default location
   '''
   super(CcnxSocket, self).__init__()
   self.ccnx_key = CCN.getDefaultKey()
   self.ccnx_key_locator = pyccn.KeyLocator(self.ccnx_key)
   self.ccnx_handle = CCN() 
   self.event_loop = CcnxLoop(self.ccnx_handle)
Пример #3
0
print(os.getcwd())

root = os.path.join("tmp")
private_pem1 = os.path.join(root, 'private1.pem')
public_pem1 = os.path.join(root, 'public1.pem')
private_pem2 = os.path.join(root, 'private2.pem')
public_pem2 = os.path.join(root, 'public2.pem')

def rm_files(*list):
	for file in list:
		if os.path.exists(file):
			os.remove(file)

rm_files(private_pem1, public_pem1, private_pem2, public_pem2)

k = CCN.getDefaultKey()

k.privateToPEM(filename=private_pem1)
k.publicToPEM(filename=public_pem1)

k2 = Key()
k2.fromPEM(filename=private_pem1)

k2.privateToPEM(filename=private_pem2)
k2.publicToPEM(filename=public_pem2)

assert(filecmp.cmp(private_pem1, private_pem2))
assert(filecmp.cmp(public_pem1, public_pem2))
print(b64encode(k.publicKeyID))
print(b64encode(k2.publicKeyID))
assert(k.publicKeyID == k2.publicKeyID)
Пример #4
0
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)
print(key2.ccn_data_public)

assert(key1.publicToDER() == key2.publicToDER())

del key2
key2 = _pyccn.Key_obj_from_ccn(key1.ccn_data_private)
Пример #5
0
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)
Пример #6
0
class TyzxServer(Closure):
    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)

#        member_name = Name.Name(self.members_uri)
#        member_name.appendKeyID(fix_digest(self.key.publicKeyID))
#        self.member_message = self.publish(member_name, nick)



    def listen(self):
        #listen to requests in namespace
        self.handle.setInterestFilter(self.prefix, self)
        self.handle.run(-1)

    def publish(self, name, content):
        # Name
        #print name
        
        

        # SignedInfo
        si = pyccn.SignedInfo()
        #si.type = pyccn.CCN_CONTENT_DATA
        si.type = 0x0C04C0 # content type
        si.finalBlockID = b'\x00'
        si.publisherPublicKeyDigest = self.key.publicKeyID
        si.keyLocator = self.keylocator
        si.freshnessSeconds = FRESHNESS_SECONDS

        # ContentObject
        co = ContentObject()
        co.content = content
        co.name = name
        co.signedInfo = si        
        co.sign(self.key)
        return co

    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
Пример #7
0
from pyccn import CCN
c = CCN()
k = c.getDefaultKey()
Пример #8
0
class TyzxServer(Closure):
    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)


#        member_name = Name.Name(self.members_uri)
#        member_name.appendKeyID(fix_digest(self.key.publicKeyID))
#        self.member_message = self.publish(member_name, nick)

    def listen(self):
        #listen to requests in namespace
        self.handle.setInterestFilter(self.prefix, self)
        self.handle.run(-1)

    def publish(self, name, content):
        # Name
        #print name

        # SignedInfo
        si = pyccn.SignedInfo()
        #si.type = pyccn.CCN_CONTENT_DATA
        si.type = 0x0C04C0  # content type
        si.finalBlockID = b'\x00'
        si.publisherPublicKeyDigest = self.key.publicKeyID
        si.keyLocator = self.keylocator
        si.freshnessSeconds = FRESHNESS_SECONDS

        # ContentObject
        co = ContentObject()
        co.content = content
        co.name = name
        co.signedInfo = si
        co.sign(self.key)
        return co

    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