def express_interest(self):
     if self.chunkinfo == None:#a new chunk requested
         self.chunkinfo = ChunkInfo(self.mydata.next_seg)
         self.chunkinfo.begin_byte = self.mydata.next_byte
         self.chunkinfo.expected_chunk_size = self.mydata.estimated_optimal_size or self.default_block_size
         self.chunkinfo.beginT = datetime.datetime.now()
         self.mydata.add_chunk_info(self.chunkinfo)
             
     self.chunkinfo.retxN += 1
     
     selector = pyndn.Interest()
     selector.answerOriginKind = True
     name = self.name
     chunkid = self.mydata.next_seg
     
     if self.mod == "adaptive": #byte index as chunkid
         name = self.name.append(ADAPTIVE_MOD_FLAG).append(str(self.chunkinfo.expected_chunk_size))
         chunkid = self.mydata.next_byte
         
     if self.interest_schema == "exclusion": #do not use this
         selector = pyndn.Interest(exclude = self.exclusions)
         selector.answerOriginKind = True
     elif self.interest_schema == "id":#the default one
         name = pyndn.Name(name.components)
         name = name.append(chunkid)
         
     elif self.interest_schema == "segment":
         name = pyndn.Name(name.components)
         name = name.appendSegment(chunkid)
         
     self.handle.expressInterest(name, self, selector)
     
     #log.info("express interest %s, exclusions: %s"  %(str(self.name), self.exclusions));
     log.debug("interest=%s " %name)
Beispiel #2
0
def get_certificate():
    client = request.args.get('client')
    name = request.args.get('name')
    isView = request.args.get('view')
    token = request.args.get('token')

    if client == nfd_android_client:
        cert = current_app.mongo.db.certs.find_one({'token': token})
        ndn_name = ndn.Name(str(cert['name']))
    else:
        ndn_name = ndn.Name(str(name))

        cert = current_app.mongo.db.certs.find_one({'name': str(name)})
    if cert == None:
        abort(404)

    if not isView:
        response = make_response(cert['cert'])
        response.headers['Content-Type'] = 'application/octet-stream'
        response.headers['Content-Disposition'] = 'attachment; filename=%s.ndncert' % str(ndn_name[-3])
        return response
    else:
        d = ndn.security.certificate.IdentityCertificate()
        d.wireDecode(bytearray(base64.b64decode(cert['cert'])))

        notBefore = datetime.utcfromtimestamp(d.getNotBefore() / 1000)
        notAfter = datetime.utcfromtimestamp(d.getNotAfter() / 1000)
        cert['from'] = notBefore
        cert['to'] = notAfter
        now = datetime.now()
        cert['isValid'] = (notBefore <= now and now <= notAfter)
        cert['info'] = d

        return render_template('cert-show.html',
                               cert=cert, title=cert['name'])
Beispiel #3
0
def get_candidates():
    commandInterestName = ndn.Name()
    commandInterestName.wireDecode(
        # ndn.Blob(buffer(base64.b64decode(request.form['commandInterest']))))
        ndn.Blob(base64.b64decode(request.form['commandInterest'])))

    site_prefix = ndn.Name()
    site_prefix.wireDecode(commandInterestName[-1].getValue().toBuffer())
    timestamp = commandInterestName[-2]

    # TODO: haitao remove this hack after test
    #signature = ndn.WireFormat.getDefaultWireFormat().decodeSignatureInfoAndValue(commandInterestName[-2].getValue().toBuffer(),
    #                                                                              commandInterestName[-1].getValue().toBuffer())
    #keyLocator = signature.getKeyLocator().getKeyName()

    #operator = mongo.db.operators.find_one({'site_prefix': site_prefix.toUri()})
    operator = {
        '_id': 1,
        'site_name': 'UCLA',
        'email': '*****@*****.**',
        'doNotSendOpRequests': False,
        'name': 'haitao'
    }
    if operator == None:
        abort(403)

    # @todo Command Interest verificateion

    requests = mongo.db.requests.find({'operator_id': str(operator['_id'])})
    output = []
    for req in requests:
        output.append(req)

    # return json.dumps (output)
    return json.dumps(output, default=json_util.default)
Beispiel #4
0
    def test_ndn_sign(self):
        data = pyndn.Data()
        data.setName(pyndn.Name("/foo/bar"))
        data.setContent("Hello, world!")

        self.signer.sign(data, [b'Monday', b'Tuesday'])
        self.assertEqual(data.getSignature().getTypeCode(), 42)
        self.assertEqual(data.getSignature().getKeyLocator().getKeyName().toUri(),
                         pyndn.Name("/test/authority/ABS/%FD%5C%CC%C8%C3/Monday%26Tuesday").toUri())
Beispiel #5
0
 def request(self,req,callback,content= None, mustbefresh= False):
     baseName = ndn.Name(req)
     interest = ndn.Interest(ndn.Name(baseName))
     interest.setMustBeFresh(mustbefresh)
     self.callback = callback
     if content != None:
      interest.setContent(content)
     self.face.expressInterest(interest, self._onData, self._onTimeout)
     self.loop.run_forever()
Beispiel #6
0
 def route(self, uri, methods):
     prefix = uri
     if bool(re.search('<(.*)>', prefix)):
         self.baseName = ndn.Name(
             re.sub('<(.*)>', methods[0] + '/<data>', prefix))
     else:
         self.baseName = ndn.Name(prefix + "/" + methods[0])
     self.methods[self.baseName.toUri()] = methods[0]
     return self.dec
Beispiel #7
0
    def __init__(self, context):
        self.context = context

        self.context.face.registerPrefix(
            pyndn.Name(self.context.broadcast_name).append('join'),
            self.on_join_interest, utils.on_registration_failed,
            utils.on_registration_success)

        self.context.face.registerPrefix(
            pyndn.Name(self.context.broadcast_name).append('leave'),
            self.on_leave_interest, utils.on_registration_failed,
            utils.on_registration_success)
Beispiel #8
0
def start(ctx):
    global context
    context = ctx

    context.face.registerPrefix(pyndn.Name(context.local_name), None,
                                utils.on_registration_failed,
                                utils.on_registration_success)

    context.face.registerPrefix(pyndn.Name(context.broadcast_name), None,
                                utils.on_registration_failed,
                                utils.on_registration_success)

    set_interest_filter()
    def upcall(self, kind, upcallInfo):
        if kind == pyndn.UPCALL_FINAL:#handler is about to be deregistered
            log.info("handler is about to be deregistered")
            if self.mydata.final_block_id == None:
                self.handle.setRunTimeout(DEFAULT_RUN_TIMEOUT*10)

            return pyndn.RESULT_OK
        
        if kind == pyndn.UPCALL_INTEREST_TIMED_OUT:
            
            self.do_meet_accident(kind, upcallInfo)
            return pyndn.RESULT_OK
        
        if kind in [pyndn.UPCALL_CONTENT_UNVERIFIED, pyndn.UPCALL_CONTENT_BAD]:
            
            self.do_meet_accident(kind, upcallInfo)
            return pyndn.RESULT_OK
        
        if kind in [pyndn.UPCALL_INTEREST, pyndn.UPCALL_CONSUMED_INTEREST]:
            log.warn("unexpected kind: %s" %kind)
            
            
        assert kind == pyndn.UPCALL_CONTENT, "kind: "+str(kind)
        if self.interest_schema == "exclusion":
            matched_comps = upcallInfo.matchedComps
            response_name = upcallInfo.ContentObject.name
            org_prefix = response_name[:matched_comps]
            
            assert org_prefix == self.name, "org_prefix: "+ str(org_prefix)+" , self.name: "+str(self.name)
            
            if matched_comps == len(response_name):
                comp = pyndn.Name([upcallInfo.ContentObject.digest()])
                disp_name = pyndn.Name(response_name)
            else:
                comp = response_name[matched_comps:matched_comps + 1]
                disp_name = response_name[:matched_comps + 1]
            
            log.info("disp_name: %s, comp: %s" %(disp_name, comp))
            
            self.exclusions.add_name(comp)
            
        self.do_receive_content(kind, upcallInfo)
        
        
        #if matched_comps + 1 < len(response_name):
        #    new = Consumer(response_name[:matched_comps+1])
        #    new.express_interest()
        
        
        return pyndn.RESULT_OK
Beispiel #10
0
    def sign(self, data, attributes, selfSign=False):
        if not isinstance(data, pyndn.Data):
            raise RuntimeError("ndnabs.Signer can only sign data packets")

        signatureInfo = Sha256WithAbsSignature()
        locator = pyndn.Name()
        if selfSign:
            locator.append(data.getName())
        else:
            if not isinstance(self.publicParams, pyndn.Data):
                raise RuntimeError(
                    "Public parameters must be installed before signing")
            locator.append(self.publicParams.getName())

        locator.append(b'&'.join(attributes))
        keyLocator = pyndn.KeyLocator()
        keyLocator.setType(pyndn.KeyLocatorType.KEYNAME)
        keyLocator.setKeyName(locator)
        data.setSignature(signatureInfo)
        data.getSignature().setKeyLocator(keyLocator)
        data.getSignature().wireEncode()

        toSign = data.wireEncode()

        signatureBytes = self._sign(toSign.toSignedBytes(), attributes)
        data.getSignature().setSignature(signatureBytes)

        data.wireEncode()
Beispiel #11
0
def send_enter_chunk_interest(entity):
    uid = entity.uid
    chunk_x, chunk_y = int(entity.x // 15), int(entity.y // 15)
    chunk_uid = entities.MapChunk.gen_uid(mngt.context.game_id, chunk_x,
                                          chunk_y)
    chunk_peer = mngt.context.peer_store.get_closest_peer(chunk_uid)
    if chunk_peer.uid == mngt.context.peer_id:
        # NOTE: Workaround to send update after coordinator transfert
        # TODO: Maybe need to be more robust
        mngt.load_chunk(chunk_x,
                        chunk_y,
                        callback=functools.partial(
                            mngt.emit_enter_chunk_update, chunk_x, chunk_y,
                            entity))
        return
    name = pyndn.Name(chunk_peer.prefix) \
        .append('chunk') \
        .append(str(chunk_x)) \
        .append(str(chunk_y)) \
        .append('enter') \
        .append(str(uid))
    logger.debug(
        'Sending EnterChunkInterest for entity %d '
        'and chunk %d (%d, %d), %s', uid, chunk_uid, chunk_x, chunk_y, entity)

    mngt.context.face.expressInterest(name, utils.on_dummy_data)
Beispiel #12
0
def send_chunk_update_interest(chunk_x, chunk_y, uid, interest=None):
    # Chunk not watched anymore, stop here
    if (chunk_x, chunk_y) not in mngt.watched_chunks:
        return

    # Neep to check peer actively to be aware of coordinator changes
    # TODO: See if possible to use leave event to be more lazy
    peer = mngt.context.peer_store.get_closest_peer(uid)

    # No need to subscribe for updates if the local peer is coordinator
    if peer.uid == mngt.context.peer_id:
        # mngt.watched_chunks.remove((chunk_x, chunk_y))
        return

    name = pyndn.Name(peer.prefix) \
        .append('chunk') \
        .append(str(chunk_x)) \
        .append(str(chunk_y)) \
        .append('updates')

    if (chunk_x, chunk_y) in mngt.player_watch:
        name.append('touch')

    logger.debug('Sending ChunkUpdateInterest for chunk %d (%d, %d) to %d',
                 uid, chunk_x, chunk_y, peer.uid)

    mngt.context.face.expressInterest(
        name, mngt.on_chunk_update_data,
        functools.partial(mngt.send_chunk_update_interest, chunk_x, chunk_y,
                          uid))
Beispiel #13
0
    def build_interest(self, latest):
        if self.start_with_latest:
            latest = True
            self.start_with_latest = False

        excl = pyndn.ExclusionFilter()
        excl.add_any()
        excl.add_name(pyndn.Name([self.latest_version]))
        # expected result should be between those two names
        excl.add_name(pyndn.Name([self.last_version_marker]))
        excl.add_any()

        interest = pyndn.Interest(name=self.base_name, exclude=excl, \
         minSuffixComponents=3, maxSuffixComponents=3)
        interest.childSelector = 1 if latest else 0
        return interest
 def __init__(self, face, prefix, interval):
     self.face = face
     self.prefix = ndn.Name(prefix)
     self.interval = interval
     self.lastTime = 0
     self.nSent = 0
     self.nRecv = 0
Beispiel #15
0
    def __init__(self, host, game_id, pseudo, peer_id, graphics=True):
        global context
        context = self

        self.game_id = game_id
        self.pseudo = pseudo
        self.peer_id = peer_id
        self.graphics = graphics

        self.keychain = pyndn.security.KeyChain()

        self.face = pyndn.Face(host)
        self.face.setCommandSigningInfo(
            self.keychain, self.keychain.getDefaultCertificateName())

        self.broadcast_name = pyndn.Name(utils.broadcast_name_uri) \
            .append(str(self.game_id))

        self.peers_mngt = peers.PeerManagement(self)
        # self.entities_mngt = entities.EntityManagement(self)

        self.peer_store = peers.PeerStore()
        self.object_store = entities.ObjectStore(self)

        self.clock = pygame.time.Clock()
        self.game = game.Game(self)

        self.prefix_discovered = False
        self.send_prefix_discovery_interest()

        while not self.prefix_discovered:
            self.face.processEvents()
            time.sleep(0.01)
Beispiel #16
0
    def __init__(self, face, filename):
        self.face = face
        self.baseName = ndn.Name("/my-local-prefix/simple-fetch/file").append(
            filename)
        self.currentSeqNo = 0

        self._requestNext()
Beispiel #17
0
    def __init__(self, prefix, handle):
        self.prefix = pyndn.Name(prefix)
        self.handle = handle
        self.content_objects = []

        self.cleanup_time = 15 * 60  # keep responses for 15 min
        handle.setInterestFilter(self.prefix, self)
Beispiel #18
0
def get_operator_for_email(email):
    # very basic pre-validation
    user, domain = email.split('@', 2)
    operator = mongo.db.operators.find_one({'site_emails': {'$in': [domain]}})
    if (operator == None):
        operator = mongo.db.operators.find_one(
            {'site_emails': {
                '$in': ['guest']
            }})

        # TODO: haitao remove this hack after testing
        operator = {
            '_id': 1,
            'site_name': 'UCLA',
            'email': '*****@*****.**',
            'doNotSendOpRequests': False,
            'name': 'haitao'
        }

        if (operator == None):
            raise Exception("Unknown site for domain [%s]" % domain)

        # Special handling for guests
        ndn_domain = ndn.Name("/ndn/guest")
        assigned_namespace = ndn.Name('/ndn/guest')
        assigned_namespace.append(str(email))
    else:
        if domain == "operators.named-data.net":
            ndn_domain = ndn.Name(str(user))
            assigned_namespace = ndn.Name(str(user))
        else:
            ndn_domain = ndnify(domain)
            assigned_namespace = ndn.Name('/ndn')
            assigned_namespace \
                .append(ndn_domain) \
                .append(str(user))

    # return various things
    return {
        'operator': operator,
        'user': user,
        'domain': domain,
        'requestDetails': True,
        'ndn_domain': ndn_domain,
        'assigned_namespace': assigned_namespace
    }
 def sendInterest(self):
     self.lastTime = time.time()
     name = ndn.Name(self.prefix)
     name.appendTimestamp(int(self.lastTime * 1000))
     self.lastName = name
     print('<I %s' % (name, ))
     self.face.expressInterest(name, self.processData)
     self.nSent += 1
Beispiel #20
0
 def test_setup(self):
     fd, tmpDbPath = tempfile.mkstemp()
     os.close(fd)
     os.remove(tmpDbPath)
     print(tmpDbPath)
     db = ndnabs.PickleDb(tmpDbPath)
     self.aa = ndnabs.AttributeAuthority(db)
     self.aa.setup(pyndn.Name("/test/name"))
Beispiel #21
0
    def send_join_interest(self):
        join_name = pyndn.Name(self.context.broadcast_name) \
            .append('join') \
            .append(self.context.local_name)

        self.context.face.expressInterest(join_name,
                                          self.on_peer_info_list_data,
                                          self.on_peer_info_list_timeout)
Beispiel #22
0
def send_find_coordinator_interest(uid):
    name = pyndn.Name(mngt.context.broadcast_name) \
        .append('coordinator') \
        .append(str(uid)) \
        .append(str(mngt.context.peer_id))
    logger.debug('Sending FindCoordinatorInterest to %s', name.toUri())
    mngt.context.face.expressInterest(name, mngt.on_find_coordinator_data,
                                      mngt.on_find_coordinator_timeout)
Beispiel #23
0
def send_find_entity_interest(uid):
    logger.debug('Sending FindEntityInterest for entity %d', uid)
    name = pyndn.Name(mngt.context.broadcast_name) \
        .append('entity') \
        .append(str(uid)) \
        .append(str(mngt.context.peer_id))
    mngt.context.face.expressInterest(name, utils.on_dummy_data,
                                      mngt.on_find_entity_timeout)
 def requestSegment(self, segmentNumber):
     # if self.pendingSegmentCount < self.pendingSegmentMax:
     self.pendingSegments.add(segmentNumber)
     segmentName = pyndn.Name(self.redirect.getName())
     segmentName.appendSegment(segmentNumber)
     interest = pyndn.Interest(segmentName)
     print("Sent segment interest '{}'".format(interestToString(interest)))
     self.face.expressInterest(interest, self.receiveSegment, self.timeoutSegment)
Beispiel #25
0
    def upcall(self, kind, upcallInfo):
        if kind == pyndn.UPCALL_FINAL:
            # any cleanup code here (so far I never had need for
            # this call type)
            return pyndn.RESULT_OK

        if kind == pyndn.UPCALL_INTEREST_TIMED_OUT:
            print("Got timeout!")
            return pyndn.RESULT_OK

        # make sure we're getting sane responses
        if not kind in [
                pyndn.UPCALL_CONTENT, pyndn.UPCALL_CONTENT_UNVERIFIED,
                pyndn.UPCALL_CONTENT_BAD
        ]:
            print("Received invalid kind type: %d" % kind)
            sys.exit(100)

        matched_comps = upcallInfo.matchedComps
        response_name = upcallInfo.ContentObject.name
        org_prefix = response_name[:matched_comps]

        assert (org_prefix == self.root)

        if matched_comps == len(response_name):
            comp = pyndn.Name([upcallInfo.ContentObject.digest()])
            disp_name = pyndn.Name(response_name)
        else:
            comp = response_name[matched_comps:matched_comps + 1]
            disp_name = response_name[:matched_comps + 1]

        if kind == pyndn.UPCALL_CONTENT_BAD:
            print("*** VERIFICATION FAILURE *** %s" % response_name)

        print("%s [%s]" % (disp_name, \
         "verified" if kind == pyndn.UPCALL_CONTENT else "unverified"))

        self.exclusions.add_name(comp)
        self.express_my_interest()

        # explore next level
        if matched_comps + 1 < len(response_name):
            new = Slurp(response_name[:matched_comps + 1], self.handle)
            new.express_my_interest()

        return pyndn.RESULT_OK
Beispiel #26
0
def get_candidates():
    commandInterestName = ndn.Name()
    commandInterestName.wireDecode(
        # ndn.Blob(buffer(base64.b64decode(request.form['commandInterest']))))
        ndn.Blob(base64.b64decode(request.form['commandInterest'])))

    site_prefix = ndn.Name()
    site_prefix.wireDecode(commandInterestName[-3].getValue().toBuffer())
    timestamp = commandInterestName[-4]

    signature = ndn.WireFormat.getDefaultWireFormat(
    ).decodeSignatureInfoAndValue(
        commandInterestName[-2].getValue().toBuffer(),
        commandInterestName[-1].getValue().toBuffer())
    keyLocator = signature.getKeyLocator().getKeyName()

    operator = mongo.db.operators.find_one(
        {'site_prefix': site_prefix.toUri()})
    if operator == None:
        abort(403)

    try:
        keyChain = KeyChain(
            policyManager=OperatorVerifyPolicyManager(operator))

        def onVerified(interest):
            pass

        def onVerifyFailed(interest):
            raise RuntimeError("Operator verification failed")

        keyChain.verifyInterest(ndn.Interest(commandInterestName),
                                onVerified,
                                onVerifyFailed,
                                stepCount=1)
    except Exception as e:
        print("ERROR: %s" % e)
        abort(403)

    # Will get here if verification succeeds
    requests = mongo.db.requests.find({'operator_id': str(operator['_id'])})
    output = []
    for req in requests:
        output.append(req)

    return json.dumps(output, default=json_util.default)
Beispiel #27
0
def send_entity_update_interest(uid, version=None, interest=None):
    entity = mngt.context.object_store.get(uid)
    pending_info = mngt.pending_update_interest.setdefault(
        entity.uid, [0, entity.version])

    if version is None:
        version = pending_info[1]

    # Called after timeout
    if interest is not None:
        pending_info[0] -= 1

    peer = mngt.context.peer_store.get_closest_peer(uid)
    chunk_coord = (entity.x // 15, entity.y // 15)

    # No need to subscribe for updates if the local peer is coordinator
    if peer.uid == mngt.context.peer_id:
        # TODO: active check when coordinator leave the game
        # (prevent entity freeze)
        mngt.context.object_store.set_local_coordinator(entity.uid, True)
        logger.debug(
            'Skipping send EntityUpdateInterest for entity %s: '
            'entity coordinated by local peer', uid)
        return

    # Entity not watch anymore
    if chunk_coord not in mngt.watched_chunks:
        logger.debug(
            'Skipping send EntityUpdateInterest for entity %s: '
            'chunk not watched, chunk=(%d, %d) watched_chunks=%s', uid,
            entity.x // 15, entity.y // 15, mngt.watched_chunks)

        entity.active = False
        return

    entity.active = True

    name = pyndn.Name(peer.prefix) \
        .append('entity') \
        .append(str(uid)) \
        .append('updates') \
        .append(str(version))

    if chunk_coord in mngt.player_watch:
        name.append('touch')

    logger.trace('Sending EntityUpdateInterest for entity %d to %d, %s', uid,
                 peer.uid, name)
    mngt.context.face.expressInterest(
        name, mngt.on_entity_update_data,
        functools.partial(mngt.send_entity_update_interest, uid, version))

    pending_info[0] += 1
    pending_info[1] = max(pending_info[1], version + 1)

    if pending_info[0] <= const.MAX_PENDING_UPDATE_INTEREST:
        mngt.send_entity_update_interest(uid)
Beispiel #28
0
def send_entity_found_interest(peer, entity):
    logger.debug('Sending EntityFoundInterest for entity %d to peer %d',
                 entity.uid, peer.uid)
    name = pyndn.Name(peer.prefix) \
        .append('entity_found') \
        .append(str(entity.uid)) \
        .append(str(mngt.context.peer_id)) \
        .append(str(getattr(entity, 'timestamp', 0)))

    mngt.context.face.expressInterest(name, utils.on_dummy_data)
Beispiel #29
0
 def registerPrefixes(self, prefixes):
     self.rem = len(prefixes)
     for prefix in prefixes:
         self.face.registerPrefix(ndn.Name(prefix), None,
                                  self.onRegisterFailed,
                                  self.onRegisterSuccess)
     while self.rem > 0:
         self.face.processEvents()
         time.sleep(0.1)
     return self.rem == 0
Beispiel #30
0
def extract_cert_name(name):
    # remove two (or 3 in case of rejection) last components and remove "KEY" keyword at any position
    newname = ndn.Name()
    last = -2
    if name[-1] == 'REVOKED':
        last = -3
    for component in name[:last]:
        if str(component) != 'KEY':
            newname.append(component)
    return newname