Support ipv6 address packing/unpacking
This commit is contained in:
parent
b66676de85
commit
0c56a7a23b
1 changed files with 18 additions and 10 deletions
|
@ -9,6 +9,9 @@ import logging
|
||||||
import base64
|
import base64
|
||||||
import gevent
|
import gevent
|
||||||
|
|
||||||
|
if "inet_pton" not in dir(socket):
|
||||||
|
import win_inet_pton
|
||||||
|
|
||||||
from Config import config
|
from Config import config
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,25 +79,29 @@ def shellquote(*args):
|
||||||
|
|
||||||
|
|
||||||
def packPeers(peers):
|
def packPeers(peers):
|
||||||
packed_peers = {"ip4": [], "onion": []}
|
packed_peers = {"ipv4": [], "ipv6": [], "onion": []}
|
||||||
for peer in peers:
|
for peer in peers:
|
||||||
try:
|
try:
|
||||||
if peer.ip.endswith(".onion"):
|
ip_type = getIpType(peer.ip)
|
||||||
packed_peers["onion"].append(peer.packMyAddress())
|
packed_peers[ip_type].append(peer.packMyAddress())
|
||||||
else:
|
|
||||||
packed_peers["ip4"].append(peer.packMyAddress())
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.error("Error packing peer address: %s" % peer)
|
logging.error("Error packing peer address: %s" % peer)
|
||||||
return packed_peers
|
return packed_peers
|
||||||
|
|
||||||
|
|
||||||
# ip, port to packed 6byte format
|
# ip, port to packed 6byte or 18byte format
|
||||||
def packAddress(ip, port):
|
def packAddress(ip, port):
|
||||||
|
if ":" in ip:
|
||||||
|
return socket.inet_pton(socket.AF_INET6, ip) + struct.pack("H", port)
|
||||||
|
else:
|
||||||
return socket.inet_aton(ip) + struct.pack("H", port)
|
return socket.inet_aton(ip) + struct.pack("H", port)
|
||||||
|
|
||||||
|
|
||||||
# From 6byte format to ip, port
|
# From 6byte or 18byte format to ip, port
|
||||||
def unpackAddress(packed):
|
def unpackAddress(packed):
|
||||||
|
if len(packed) == 18:
|
||||||
|
return socket.inet_ntop(socket.AF_INET6, packed[0:16]), struct.unpack_from("H", packed, 16)[0]
|
||||||
|
else:
|
||||||
assert len(packed) == 6, "Invalid length ip4 packed address: %s" % len(packed)
|
assert len(packed) == 6, "Invalid length ip4 packed address: %s" % len(packed)
|
||||||
return socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0]
|
return socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0]
|
||||||
|
|
||||||
|
@ -124,6 +131,7 @@ def getDirname(path):
|
||||||
def getFilename(path):
|
def getFilename(path):
|
||||||
return path[path.rfind("/") + 1:]
|
return path[path.rfind("/") + 1:]
|
||||||
|
|
||||||
|
|
||||||
def getFilesize(path):
|
def getFilesize(path):
|
||||||
try:
|
try:
|
||||||
s = os.stat(path)
|
s = os.stat(path)
|
||||||
|
|
Loading…
Reference in a new issue