Beispiel #1
0
def handle(newpacket, info):
    from transport import packet_out
    handled = False
    # check that signed by a contact of ours
    if not newpacket.Valid():
        lg.warn('new packet from %s://%s is NOT VALID: %r' % (
            info.proto, info.host, newpacket))
        return None
    for p in packet_out.search_by_response_packet(newpacket, info.proto, info.host):
        p.automat('inbox-packet', (newpacket, info))
        handled = True
        if _Debug:
            lg.out(_DebugLevel, '    processed by %s as response packet' % p)
    handled = callback.run_inbox_callbacks(newpacket, info, info.status, info.error_message) or handled
    if not handled and newpacket.Command not in [commands.Ack(), commands.Fail(), commands.Identity(), ]:
        lg.warn('incoming %s from [%s://%s] was NOT HANDLED' % (newpacket, info.proto, info.host))
    if _Debug:
        history().append({
            'time': newpacket.Date,
            'command': newpacket.Command,
            'packet_id': newpacket.PacketID,
            'creator_id': newpacket.CreatorID,
            'owner_id': newpacket.OwnerID,
            'remote_id': newpacket.RemoteID,
            'payload': len(newpacket.Payload),
            'address': '%s://%s' % (info.proto, info.host),
        })
        if len(history()) > 100:
            history().pop(0)
    return handled
Beispiel #2
0
def handle(newpacket, info):
    """
    Actually process incoming packet. Here we can be sure that owner/creator of the packet is identified.
    """
    from transport import packet_out
    handled = False
    # check that signed by a contact of ours
    try:
        is_signature_valid = newpacket.Valid(raise_signature_invalid=False)
    except:
        is_signature_valid = False
        # lg.exc('new packet from %s://%s is NOT VALID:\n\n%r\n' % (
        #     info.proto, info.host, newpacket.Serialize()))
    if not is_signature_valid:
        if _Debug:
            lg.args(
                _DebugLevel,
                PacketID=newpacket.PacketID,
                OwnerID=newpacket.OwnerID,
                CreatorID=newpacket.CreatorID,
                RemoteID=newpacket.RemoteID,
            )
        lg.warn('signature is not valid for %r from %r|%r to %r' %
                (newpacket, newpacket.OwnerID, newpacket.CreatorID,
                 newpacket.RemoteID))
        return None
    try:
        for p in packet_out.search_by_response_packet(newpacket, info.proto,
                                                      info.host):
            p.automat('inbox-packet', (newpacket, info))
            handled = True
            if _Debug:
                lg.out(_DebugLevel,
                       '    processed by %s as response packet' % p)
    except:
        lg.exc()
    try:
        handled = callback.run_inbox_callbacks(newpacket, info, info.status,
                                               info.error_message) or handled
    except:
        lg.exc()
    if not handled and newpacket.Command not in [
            commands.Ack(),
            commands.Fail(),
            commands.Identity(),
    ]:
        lg.warn('incoming %s from [%s://%s] was NOT HANDLED' %
                (newpacket, info.proto, info.host))
        if _PacketLogFileEnabled:
            lg.out(
                0,
                '                \033[1;49;91mIN NOT HANDLED %s(%s) with %d bytes from %s to %s TID:%s\033[0m'
                % (newpacket.Command, newpacket.PacketID, info.bytes_received,
                   global_id.UrlToGlobalID(info.sender_idurl),
                   global_id.UrlToGlobalID(
                       newpacket.RemoteID), info.transfer_id),
                log_name='packet',
                showtime=True)
    else:
        if _PacketLogFileEnabled:
            lg.out(
                0,
                '                \033[0;49;32mIN OK %s(%s) with %d bytes from %s to %s TID:%s\033[0m'
                % (newpacket.Command, newpacket.PacketID, info.bytes_received,
                   global_id.UrlToGlobalID(info.sender_idurl),
                   global_id.UrlToGlobalID(
                       newpacket.RemoteID), info.transfer_id),
                log_name='packet',
                showtime=True)
    if _Debug and False:
        history().append({
            'time': newpacket.Date,
            'command': newpacket.Command,
            'packet_id': newpacket.PacketID,
            'creator_id': newpacket.CreatorID,
            'owner_id': newpacket.OwnerID,
            'remote_id': newpacket.RemoteID,
            'payload': len(newpacket.Payload),
            'address': '%s://%s' % (info.proto, info.host),
        })
        if len(history()) > 100:
            history().pop(0)
    return handled