[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPv6 strings with an inet6_ntoa #454

Merged
merged 2 commits into from
Mar 30, 2016
Merged

Conversation

brendangregg
Copy link
Member

This has a simple implementation of an inet6_ntoa() in tcpconnect. If this proves suitable, we can move it to bcc init..py.

Known shortcomings: requires "re" package (on my systems that's usually there, whereas netaddr usually isn't). Also doesn't shorten maximum run, so is RFC4291 but not RFC5952 (missing 4.2.1), however, most of the time it is producing RFC5952 addresses. I think it works fine for now, but someone later might drop in a better Python implementation.

@4ast
Copy link
Member
4ast commented Mar 28, 2016

lgtm

@@ -228,6 +234,35 @@ def inet_ntoa(addr):
addr = addr >> 8
return dq

def inet6_ntoa(addr):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is overly complicated. An approach with struct.unpack and an array of bytes along with your compacting regex would be a lot less code and still eliminate netaddr dependency. The struct module is also a python stdlib. Something like:

s = ":".join(["%x"%x for x in unpack(">HHHHHHHH", rawaddr)])
s = re.sub(r'(^|:)0:(0:)+', r'::', s, 1)

@brendangregg
Copy link
Member Author

ok; I haven't gotten unpack to work yet but I'll keep trying.

@brendangregg
Copy link
Member Author

Forgot to note it earlier: this PR will be to fix #380 .

@brendangregg
Copy link
Member Author

@drzaeus77 something like this? (using a pack to make the bytearray, then an unpack):

def inet6_ntoa(addr):
    # see RFC4291 summary in RFC5952 section 2
    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)
    return s

@drzaeus77
Copy link
Collaborator

That looks better. If addr were a byte[] then the pack("QQ" could be avoided, but this version works.

@drzaeus77 drzaeus77 merged commit a6200fb into iovisor:master Mar 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants