示例#1
0
def transmit_data(conn, dcall, spath, data):
    c, s = ssid(dcall)

    # Encode the call by grabbing each character and shifting
    # left one bit
    dst = "".join([chr(ord(x) << 1) for x in c])
    dst += encode_ssid(s)

    src = ""
    for scall in spath:
        c, s = ssid(scall)
        src += "".join([chr(ord(x) << 1) for x in c])
        src += encode_ssid(s, spath[-1] == scall)

    d = struct.pack(
        "B7s%isBB" % len(src),
        0x00,  # Space for flag (?)
        dst,  # Dest Call
        src,  # Source Path
        0x3E,  # Info
        0xF0)  # PID: No layer 3
    d += data

    utils.hexprint(d)

    f = AGWFrame_K()
    f.set_payload(d)
    conn.send_frame(f)
示例#2
0
def kiss_send_frame(frame, port=0):
    cmd = (port & 0x0F) << 4

    frame = kiss_escape_frame(frame)
    buf = struct.pack("BB", FEND, cmd) + frame + struct.pack("B", FEND)

    if TNC_DEBUG:
        print("[TNC] Sending:")
        utils.hexprint(buf)

    return buf
示例#3
0
def agw_recv_frame(s):
    data = ""
    while True:
        data += s.recv(1)
        if len(data) >= 36:
            f = AGWFrame_K()
            try:
                f.unpack(data)
                data = ""
            except Exception as err:
                #print "Failed: %s" % e
                continue
            print(f"{f.get_from()} -> {f.get_to()} [{chr(f.kind)}]")
            utils.hexprint(f.get_payload())
            return
示例#4
0
def kiss_recv_frame(buf):
    if not buf:
        return "", ""

    data = ""
    inframe = False

    _buf = ""
    _lst = "0"  # Make sure we don't choke trying to ord() this
    for char in buf:
        if ord(char) == FEND:
            if not inframe:
                inframe = True
            else:
                data += _buf[1:]
                _buf = ""
                inframe = False
        elif ord(char) == FESC:
            pass  # Ignore this and wait for the next character
        elif ord(_lst) == FESC:
            if ord(char) == TFEND:
                _buf += chr(FEND)
            elif ord(char) == TFESC:
                _buf += chr(FESC)
            else:
                print(f"[TNC] Bad escape of 0x{ord(char):x}")
                break
        elif inframe:
            _buf += char
        else:
            print(f"[TNC] Out-of-frame garbage: 0x{ord(char)}")
        _lst = char

    if TNC_DEBUG:
        print("[TNC] Data:")
        utils.hexprint(data)

    if not inframe and _buf:
        # There was not a partial frame started at the end of the data
        print("[TNC] Dumping non-frame data trailer")
        utils.hexprint(_buf)
        _buf = ""

    return data, _buf
示例#5
0
            self._store_bit(0)
        self._store_bit(bit)

    def get_output(self):
        if self.bits:
            for i in range(self.bits, 8):
                self.store_bit(0)
        return self.outbound


def bitstuff(data):
    ctx = BitStuffContext()

    for byte in data:
        for bit in range(0, 8):
            ctx.store_bit(ord(byte) & (1 << bit))

    return ctx.get_output()


if __name__ == "__main__":
    from d_rats.utils import hexprint

    data = "\xFF\xFF\xFF"

    print("Start:")
    hexprint(data)

    print("\nStuffed:")
    hexprint(bitstuff(data))