Skip to content

Commit

Permalink
replace toHex() with binascii.hexlify()
Browse files Browse the repository at this point in the history
  • Loading branch information
wgblikeW committed May 2, 2022
1 parent c9635f8 commit 84a2f7e
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions examples/networking/http_filter/http-parse-complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@
MAX_AGE_SECONDS = 30 # max age entry in bpf_sessions map


# convert a bin string into a string of hex char
# helper function to print raw packet in hex
def toHex(s):
lst = ""
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0' + hv
lst = lst + hv
return lst


# print str until CR+LF
def printUntilCRLF(s):
print(s.split(b'\r\n')[0].decode())
Expand Down Expand Up @@ -151,7 +139,7 @@ def help():
packet_count += 1

# DEBUG - print raw packet in hex format
# packet_hex = toHex(packet_str)
# packet_hex = binascii.hexlify(packet_str)
# print ("%s" % packet_hex)

# convert packet into bytearray
Expand Down Expand Up @@ -188,8 +176,8 @@ def help():
ip_src_str = packet_str[ETH_HLEN + 12: ETH_HLEN + 16] # ip source offset 12..15
ip_dst_str = packet_str[ETH_HLEN + 16:ETH_HLEN + 20] # ip dest offset 16..19

ip_src = int(toHex(ip_src_str), 16)
ip_dst = int(toHex(ip_dst_str), 16)
ip_src = int(binascii.hexlify(ip_src_str), 16)
ip_dst = int(binascii.hexlify(ip_dst_str), 16)

# TCP HEADER
# https://www.rfc-editor.org/rfc/rfc793.txt
Expand All @@ -215,8 +203,8 @@ def help():
port_src_str = packet_str[ETH_HLEN + ip_header_length:ETH_HLEN + ip_header_length + 2]
port_dst_str = packet_str[ETH_HLEN + ip_header_length + 2:ETH_HLEN + ip_header_length + 4]

port_src = int(toHex(port_src_str), 16)
port_dst = int(toHex(port_dst_str), 16)
port_src = int(binascii.hexlify(port_src_str), 16)
port_dst = int(binascii.hexlify(port_dst_str), 16)

# calculate payload offset
payload_offset = ETH_HLEN + ip_header_length + tcp_header_length
Expand Down

0 comments on commit 84a2f7e

Please sign in to comment.