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

fix(socket): IPv6 support #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions nfspy/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# XXX There is no provision for call timeout on TCP connections

import ipaddress
import xdrlib
import socket
import os
Expand Down Expand Up @@ -343,7 +344,7 @@ def bindresvport(sock, host):
class RawTCPClient(Client):

def makesocket(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock = get_socket(self.host, socket.SOCK_STREAM)

def do_call(self):
call = self.packer.get_buf()
Expand All @@ -366,7 +367,7 @@ def __init__(self, *args, **kwargs):
self.BUFSIZE = 8192

def makesocket(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock = get_socket(self.host, socket.SOCK_DGRAM)

def do_call(self):
call = self.packer.get_buf()
Expand Down Expand Up @@ -753,7 +754,7 @@ def addpackers(self):
class TCPServer(Server):

def makesocket(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock = get_socket(self.host, socket.SOCK_STREAM)
self.prot = IPPROTO_TCP

def loop(self):
Expand Down Expand Up @@ -807,7 +808,7 @@ def forksession(self, connection):
class UDPServer(Server):

def makesocket(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock = get_socket(self.host, socket.SOCK_DGRAM)
self.prot = IPPROTO_UDP

def loop(self):
Expand Down Expand Up @@ -894,3 +895,25 @@ def call_1(self, arg):
print 'making call...'
reply = c.call_1('hello, world, ')
print 'call returned', repr(reply)


def get_socket(host, socktype=socket.SOCK_STREAM):
sock = None
if is_ipv6(host):
sock = socket.socket(socket.AF_INET6, socktype)
else:
sock = socket.socket(socket.AF_INET, socktype)

return sock

def is_ipv6(host):
try:
host = host.decode()
except:
pass

try:
ipaddress.IPv6Address(host)
return True
except:
return False