Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/netutil.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2016 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Linux network utilities. | """Linux network utilities. | ||||
Roughly based on http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal | Roughly based on http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal | ||||
""" | """ | ||||
import array | import array | ||||
from binascii import unhexlify, hexlify | from binascii import unhexlify | ||||
import fcntl | import fcntl | ||||
import os | import os | ||||
import socket | import socket | ||||
import struct | import struct | ||||
import sys | import sys | ||||
# STATE_ESTABLISHED = '01' | # STATE_ESTABLISHED = '01' | ||||
# STATE_SYN_SENT = '02' | # STATE_SYN_SENT = '02' | ||||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | elif ':' in addr: # IPv6 | ||||
val = int(comp, 16) | val = int(comp, 16) | ||||
sub[x].append(val >> 8) | sub[x].append(val >> 8) | ||||
sub[x].append(val & 0xff) | sub[x].append(val & 0xff) | ||||
nullbytes = 16 - len(sub[0]) - len(sub[1]) | nullbytes = 16 - len(sub[0]) - len(sub[1]) | ||||
assert((x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0)) | assert((x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0)) | ||||
addr = sub[0] + ([0] * nullbytes) + sub[1] | addr = sub[0] + ([0] * nullbytes) + sub[1] | ||||
else: | else: | ||||
raise ValueError('Could not parse address {}'.format(addr)) | raise ValueError('Could not parse address {}'.format(addr)) | ||||
return hexlify(bytearray(addr)).decode('ascii') | return bytearray(addr).hex() | ||||
def test_ipv6_local(): | def test_ipv6_local(): | ||||
''' | ''' | ||||
Check for (local) IPv6 support. | Check for (local) IPv6 support. | ||||
''' | ''' | ||||
import socket | import socket | ||||
# By using SOCK_DGRAM this will not actually make a connection, but it will | # By using SOCK_DGRAM this will not actually make a connection, but it will | ||||
# fail if there is no route to IPv6 localhost. | # fail if there is no route to IPv6 localhost. | ||||
have_ipv6 = True | have_ipv6 = True | ||||
try: | try: | ||||
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) | s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) | ||||
s.connect(('::1', 0)) | s.connect(('::1', 0)) | ||||
except socket.error: | except socket.error: | ||||
have_ipv6 = False | have_ipv6 = False | ||||
return have_ipv6 | return have_ipv6 |