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)
def parse_blocks(self): while ddt2.ENCODED_HEADER in self.inbuf and \ ddt2.ENCODED_TRAILER in self.inbuf: s = self.inbuf.index(ddt2.ENCODED_HEADER) e = self.inbuf.index(ddt2.ENCODED_TRAILER) + \ len(ddt2.ENCODED_TRAILER) if e < s: # Excise the extraneous end _tmp = self.inbuf[:e-len(ddt2.ENCODED_TRAILER)] + \ self.inbuf[e:] self.inbuf = _tmp continue block = self.inbuf[s:e] self.inbuf = self.inbuf[e:] f = ddt2.DDT2EncodedFrame() try: if f.unpack(block): print "Got a block: %s" % f self._handle_frame(f) elif self.compat: self._send_text_block(block) else: print "Found a broken block (S:%i E:%i len(buf):%i" % (\ s, e, len(self.inbuf)) utils.hexprint(block) except Exception, e: print "Failed to process block:" utils.log_exception()
def recv_start_file(self): for i in range(1, self.limit_tries): if not self.enabled: raise TransferEnded("Cancelled by user") print "Getting start block" frames = self.recv_raw_frames(1, True) print "Got %i blocks" % len(frames) for i in frames: frame = DDTMultiXferStartFrame() try: if not frame.unpack(i): print "Unable to unpack start frame:" hexprint(i) continue except Exception, e: print "MC Start Exception: %s" % e continue print "Got file: %s (%i/%i bytes)" % (frame.get_filename(), frame.get_block_size(), frame.get_size()) self.total_size = frame.get_size() self.block_size = frame.get_block_size() self.filename = frame.get_filename() return True
def unpack(self, value): header = value[0:8] data = value[8:] (magic, length, seq, checksum, self.type) = struct.unpack(self.format, header) if magic not in [self.magic_com, self.magic_reg]: print "Failed magic: %0x != %0x" % (magic, self.magic) hexprint(header) hexprint(data) return False self.seq = seq if len(data) != length: print "Length %i != %i" % (len(data), length) hexprint(value) return False if magic == self.magic_com: self.data = zlib.decompress(data) else: self.data = data self.calc_checksum() if self.checksum != checksum: print "Checksum failed:" hexprint(self.checksum) hexprint(checksum) return False return True
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("Comm : [TNC] Sending:") utils.hexprint(buf) return buf
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
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, e: #print "Failed: %s" % e continue print "%s -> %s [%s]" % (f.get_from(), f.get_to(), chr(f.kind)) utils.hexprint(f.get_payload()) return
def _recv_block(self): data = "" to = Timeout(self.limit_timeout) while self.enabled and \ not data.endswith(ENCODED_TRAILER) and \ not to.expired(): _data = self.pipe.read(512) if len(_data) > 0: to = Timeout(self.limit_timeout) #print "Read.. data: \n%s\n" % _data data += _data if not data.endswith(ENCODED_TRAILER): print "Block doesn't have proper trailer" hexprint(data) return None print "Got frame" self.wire_size += len(data) data = strip_echo(data) type = detect_frame_type(data) if type is None: print "Unknown frame type" self.send_ack(0, False) return None frame = type() try: result = frame.unpack(data) except: print "Unpack failed" result = False if result: print "ACK on %i" % frame.get_seq() self.send_ack(frame.get_seq(), True) return frame else: print "NAK" #print "RAW FRAME: |%s|" % data self.send_ack(frame.get_seq(), False) return None
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("Comm : [TNC] Bad escape of 0x%x" % ord(char)) break elif inframe: _buf += char else: print("Comm : [TNC] Out-of-frame garbage: 0x%x" % ord(char)) _lst = char if TNC_DEBUG: print("Comm : [TNC] Data:") utils.hexprint(data) if not inframe and _buf: # There was not a partial frame started at the end of the data print("Comm : [TNC] Dumping non-frame data trailer") utils.hexprint(_buf) _buf = "" return data, _buf
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 "[TNC] Bad escape of 0x%x" % ord(char) break elif inframe: _buf += char else: print "[TNC] Out-of-frame garbage: 0x%x" % 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