-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Use socket.pton & socket.ntop is available #363
Conversation
@@ -17,6 +17,10 @@ def inet_pton(af, addr): | |||
if af == socket.AF_INET: | |||
return socket.inet_aton(addr) | |||
elif af == socket.AF_INET6: | |||
# Use inet_pton if available | |||
if hasattr(socket, "inet_pton"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since socket
will, except in very rare occasions, have inet_pton
, you might want to write:
try:
return socket.inet_pton(af, addr)
except AttributeError:
pass
@@ -65,6 +69,10 @@ def inet_ntop(af, addr): | |||
if af == socket.AF_INET: | |||
return socket.inet_ntoa(addr) | |||
elif af == socket.AF_INET6: | |||
# Use inet_ntop if available | |||
if hasattr(socket, "inet_ntop"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same remark here.
parts.append(hexstr.lower()) | ||
|
||
# Note: the returned address is never compact | ||
return ":".join(parts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to work with regex. I think I found a way to compact the IP again.
import re
ips = [
'0:0:1234:0:0:abcd:1000:0123:0:0:0:0:5678:0:0:0:0',
'0:0:1234:0:0:0:abcd:1000:0123:0:0:0:0:5678:0:0:0',
'0:0:123:45:1:7894:1234',
'0:0:123:45:1:7894:1234:0:0',
'123:45:1:7894:1234:0:0',
'123:45:1:7894:1234:0:123',
'0:0:0:0:0:0:0:0:0:1',
'1:0:0:0:0:0:0:0:0:0',
'0:0:0:0:0:0:0:0:0:0',
'0:0:0:0:0:0:0:1:0:1',
'0:1:0:1:0:1:0:1:0:1',
'1:0:1:0:1:0:1:0:1:0',
]
for ip in ips:
matches = re.findall('(?::|^)(0(?::0)*)(?::|$)', ip)
match = max(matches)
leftidx = ip.rfind(match)
left = ip[:leftidx]
rightidx = leftidx + len(match)
if len(ip) == rightidx:
result = left + ":"
elif leftidx == 0:
result = ":" + ip[rightidx:]
else:
result = left + ip[rightidx:]
print("{} -> {}".format(ip,result))
The only think not working correctly is the undefined IP (0:0:...:0), which should not occur anyways?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your proposal. As converting packet IPv6 to strings is full of corner cases, it is better to use socket.ntop and socket.ntop when possible.
If the current patch does not break tests on Windows, I will rather fall back to uncompressed IPv6 addresses at the last resort. I think that this will only happen when there is no IPv6 support ons the operating system. The only use case for these Scapy specific functions will be to send/receive IPv6 packets on an OS that does not support IPv6. I believe that this users can leave with uncompressed IPv6 addresses.
result = "0" + result | ||
return result | ||
else: | ||
raise Exception("Address family not supported yet") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the else-case raising the exception should stay?
@p-l- done. Could you check that this PR does not break unit tests on Windows ? |
This is a fix to issue #359