def __repr__(self): """Represents the payload of the message""" message = '' for header_name, header_value in self.headers.iteritems(): message += '\t%s: %s\\r\\n\n' % (header_name, header_value) message += '\t\\r\\n\n' if self.headers['Content-Type'] != "application/x-msnmsgrp2p": message += '\t' + debug.escape_string(self.body).\ replace("\r\n", "\\r\\n\n\t") else: tlp_header = self.body[:48] tlp_footer = self.body[-4:] tlp_flags = struct.unpack("<L", self.body[28:32])[0] body = self.body[48:-4] message += "\t" + debug.hexify_string(tlp_header).replace("\r\n", "\n\t") if tlp_flags == 0: message += "\n\t" + debug.escape_string(body).\ replace("\r\n", "\\r\\n\n\t") elif len(body) > 0: message += "\n\t" + "[%d bytes of data]" % len(body) message += "\n\t" + debug.hexify_string(tlp_footer) return message.rstrip("\n\t")
def __repr__(self): """Represents the payload of the message""" message = '' for header_name, header_value in self.headers.iteritems(): message += '\t%s: %s\\r\\n\n' % (header_name, header_value) message += '\t\\r\\n\n' if self.headers['Content-Type'] != "application/x-msnmsgrp2p": message += '\t' + debug.escape_string(self.body).\ replace("\r\n", "\\r\\n\n\t") else: tlp_header = self.body[:48] tlp_footer = self.body[-4:] tlp_session_id = struct.unpack("<L", self.body[0:4])[0] tlp_flags = struct.unpack("<L", self.body[28:32])[0] body = self.body[48:-4] message += "\t" + debug.hexify_string(tlp_header).replace( "\r\n", "\n\t") if tlp_flags == 0 or tlp_session_id == 0: message += "\n\t" + debug.escape_string(body).\ replace("\r\n", "\\r\\n\n\t") elif len(body) > 0: message += "\n\t" + "[%d bytes of data]" % len(body) message += "\n\t" + debug.hexify_string(tlp_footer) return message.rstrip("\n\t")
def parse_attribute(msg): if len(msg) < 4: raise TURNParseError("attribute must be at least 4 bytes long", hexify_string(msg)) try: type, size = struct.unpack("!HH", msg[:4]) except: raise TURNParseError("invalid attribute", hexify_string(msg[:4])) value = msg[4:size+4] return TURNAttribute(type, value)
def parse(self, data, chunk_size): header = debug.hexify_string(data[:48]) try: fields = struct.unpack("<LLQQLLLLQ", data[:48]) except: raise TLPParseError(1, "invalid header", header) self.session_id = fields[0] self.blob_id = fields[1] self.blob_offset = fields[2] self.blob_size = fields[3] self.chunk_size = fields[4] self.flags = fields[5] self.dw1 = fields[6] self.dw2 = fields[7] self.qw1 = fields[8] # if chunk is containing all blob data, chunk_size field might be 0 if self.blob_size == chunk_size and self.chunk_size == 0: self.chunk_size = chunk_size if self.blob_offset + self.chunk_size > self.blob_size: logger.warning(str(TLPParseError(1, "chunk end exceeds blob size", header))) self.chunk_size = chunk_size if self.blob_size >= 0 and self.chunk_size == 0: logger.warning(str(TLPParseError(1, "empty chunk for non-empty blob", header))) self.blob_size = 0
def parse_error(value): try: values = struct.unpack("!HBB", value[0:4]) except: raise TURNParseError("invalid error attribute", hexify_string(value)) code = values[1] * 100 + values[2] reason = value[4:] return TURNError(code, reason)
def parse_message(msg): if len(msg) < 20: raise TURNParseError("message must be a least 20 bytes long", hexify_string(msg)) try: hdr = struct.unpack("!HH16s", msg[:20]) except: raise TURNParseError("invalid message header", hexify_string(msg[:20])) type = hdr[0] id = uuid.UUID(bytes=hdr[2]) attributes = [] msg = msg[20:] while msg: attr = parse_attribute(msg) attributes.append(attr) msg = msg[len(attr):] return TURNMessage(id, type, attributes)
def parse_server(value): try: server = struct.unpack("!HH4s", value) except: raise TURNParseError("invalid server attribute", hexify_string(value)) ip = socket.inet_ntoa(server[2]) port = server[1] if port == 0: port = 1863 return ip, port
def parse_data_header(self, data): try: fields = struct.unpack(">BBHI", data[:8]) except: header = debug.hexify_string(data[:8]) raise TLPParseError(2, "invalid data header", header) size = fields[0] self.tf_combination = fields[1] self.package_number = fields[2] self.session_id = fields[3] self.data_tlv.parse(data[8:], size - 8) return size
def parse(self, data): try: fields = struct.unpack(">BBHL", data[:8]) except: header = debug.hexify_string(data[:8]) raise TLPParseError(2, "invalid header", header) size = fields[0] self.op_code = fields[1] self.chunk_size = fields[2] self.chunk_id = fields[3] self.tlv.parse(data[8:], size - 8) if self.chunk_size > 0: dph_size = self.parse_data_header(data[size:]) self.chunk_size -= dph_size size += dph_size return size
def parse(self, data, size): """Parse the given TLV data and add values to the internal dict.""" offset = 0 while offset < size: if ord(data[offset]) is 0: break # ignore padding bytes t = ord(data[offset]) l = ord(data[offset + 1]) f = self.size_to_packed_format(l) end = offset + 2 + l if end > size: raise TLVParseError("Overflow (%i > %i)" % (end, size)) try: self._data[t] = struct.unpack(">%s" % f, data[offset + 2:end])[0] except: infos = hexify_string(data[offset + 2:end]) raise TLVParseError("Couldn't unpack format %s" % f, infos) offset = end
def parse(self, data): header = debug.hexify_string(data[:48]) try: fields = struct.unpack("<LLQQLLLLQ", data[:48]) except: raise TLPParseError(1, "invalid header", header) self.session_id = fields[0] self.blob_id = fields[1] self.blob_offset = fields[2] self.blob_size = fields[3] self.chunk_size = fields[4] self.flags = fields[5] self.dw1 = fields[6] self.dw2 = fields[7] self.qw1 = fields[8] if self.blob_offset + self.chunk_size > self.blob_size: raise TLPParseError(1, "chunk end exceeds blob size", header) if self.blob_size >= 0 and self.chunk_size == 0: raise TLPParseError(1, "empty chunk for non-empty blob", header)