def create_tcp_obj(cls, utg_tcp): flags_list = [ int(utg_tcp.flag_urgent_pointer_valid), int(utg_tcp.flag_acknowledge_valid), int(utg_tcp.flag_push_function), int(utg_tcp.flag_reset_connection), int(utg_tcp.flag_synchronize_sequence), int(utg_tcp.flag_no_more_data_from_sender) ] flags = functools.reduce(lambda out, x: (out << 1) + int(x), flags_list, 0) header_obj = TCP( sport=int(utg_tcp.source_port.value), dport=int(utg_tcp.destination_port.value), seq=Converter.hexaString2int(utg_tcp.sequence_number), ack=Converter.hexaString2int(utg_tcp.acknowledgement_number), #off_x2 TODO flags=flags, win=Converter.hexaString2int(utg_tcp.window), #sum TODO urp=Converter.hexaString2int(utg_tcp.urgent_pointer) #opts TODO ) return header_obj
def create_gre_obj(cls, utg_gre): # type: (Packet.GRE) -> object obj = GRE() #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8') obj.version_number = utg_gre.version #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8') if utg_gre.use_checksum is True: obj.opts.append(GREOptionChecksum(checksum=0)) obj.opts.append(GREOptionReserve_1()) obj.checksum_bit = 1 #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8') if utg_gre.key_field is not None: obj.opts.append( GREOptionKey( key=Converter.convertstring2int(utg_gre.key_field))) obj.key_bit = 1 #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8') if utg_gre.sequence_number is not None: obj.opts.append( GREOptionSequence(sequence=Converter.convertstring2int( utg_gre.sequence_number))) obj.sequence_bit = 1 #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8') if utg_gre.use_checksum is True: innerPacketData = obj.bin() if utg_gre.l3_proto is TGEnums.L3_PROTO.IPV4: obj.protocol_type = ETH_TYPE_IP packer_v4 = utg_gre.ipv4.to_packer() innerPacketData += packer_v4.bin() elif utg_gre.l3_proto is TGEnums.L3_PROTO.IPV6: obj.protocol_type = ETH_TYPE_IP6 v6 = utg_gre.ipv6.to_packer() innerPacketData += v6.bin() else: proto = Converter.remove_non_hexa_sumbols(utg_gre.l3_proto) proto = Converter.hexaString2int(proto) obj.protocol_type = proto if utg_gre.l4_proto is TGEnums.L4_PROTO.UDP: packer_udp = utg_gre.udp.to_packer() innerPacketData += packer_udp.bin() elif utg_gre.l4_proto is TGEnums.L4_PROTO.TCP: packer_tcp = utg_gre.tcp.to_packer() innerPacketData += packer_tcp.bin() s = checksum.in_cksum(innerPacketData) obj.opts[0].checksum = s #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8') return obj