Skip to content

Commit

Permalink
use pack/unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
brendangregg committed Mar 30, 2016
1 parent 9e0b087 commit 917cb85
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions tools/tcpconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from bcc import BPF
import argparse
import re
from struct import pack, unpack_from
import ctypes as ct

# arguments
Expand Down Expand Up @@ -236,28 +237,8 @@ def inet_ntoa(addr):

def inet6_ntoa(addr):
# see RFC4291 summary in RFC5952 section 2
s = ''
for i in range(0, 16):
if ((i % 2) == 0):
zerorun = 1
if (i != 0):
s = s + ':'

v = addr & 0xff
if ((i % 2) == 0):
# if first byte in field is zero, skip it:
if v != 0:
# don't zero pad first byte in field:
s = s + "%x" % v
zerorun = 0
else:
if zerorun:
# previous byte was zero, don't zero pad this one:
s = s + "%x" % v
else:
# previous byte was non-zero, need a zero pad:
s = s + "%02x" % v
addr = addr >> 8
s = ":".join(["%x" % x for x in unpack_from(">HHHHHHHH",
pack("QQ", addr & 0xffffffff, addr >> 64))])

# compress left-most zero run only (change to most for RFC5952):
s = re.sub(r'(^|:)0:(0:)+', r'::', s, 1)
Expand Down

0 comments on commit 917cb85

Please sign in to comment.