MCPcopy Create free account
hub / github.com/ElementsProject/elements / addr_to_hex

Function addr_to_hex

test/functional/test_framework/netutil.py:114–141  ·  view source on GitHub ↗

Convert string IPv4 or IPv6 address to binary address as returned by get_bind_addrs. Very naive implementation that certainly doesn't work for all IPv6 variants.

(addr)

Source from the content-addressed store, hash-verified

112 for i in range(0, outbytes, struct_size)]
113
114def addr_to_hex(addr):
115 '''
116 Convert string IPv4 or IPv6 address to binary address as returned by
117 get_bind_addrs.
118 Very naive implementation that certainly doesn't work for all IPv6 variants.
119 '''
120 if '.' in addr: # IPv4
121 addr = [int(x) for x in addr.split('.')]
122 elif ':' in addr: # IPv6
123 sub = [[], []] # prefix, suffix
124 x = 0
125 addr = addr.split(':')
126 for i,comp in enumerate(addr):
127 if comp == '':
128 if i == 0 or i == (len(addr)-1): # skip empty component at beginning or end
129 continue
130 x += 1 # :: skips to suffix
131 assert x < 2
132 else: # two bytes per component
133 val = int(comp, 16)
134 sub[x].append(val >> 8)
135 sub[x].append(val & 0xff)
136 nullbytes = 16 - len(sub[0]) - len(sub[1])
137 assert (x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0)
138 addr = sub[0] + ([0] * nullbytes) + sub[1]
139 else:
140 raise ValueError('Could not parse address %s' % addr)
141 return bytearray(addr).hex()
142
143def test_ipv6_local():
144 ''&#x27;

Callers 2

setup_networkMethod · 0.90
run_bind_testMethod · 0.90

Calls 2

hexMethod · 0.80
enumerateFunction · 0.50

Tested by

no test coverage detected