Skip to content

Commit 0a2bd52

Browse files
ADD IPV6 SUPPORT (#22)
* ADD IPV6 SUPPORT Added 'tcp6' to socket types. IPv6 must be set in the `socket_host` WITHOUT braces.
1 parent 1b26bc7 commit 0a2bd52

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

darwin/darwinapi.py

100755100644
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ class DarwinApi:
9292

9393
DEFAULT_TIMEOUT = 10
9494

95+
_SOCKET_PROTOCOL = {
96+
'unix': (socket.AF_UNIX, socket.SOCK_STREAM),
97+
'tcp': (socket.AF_INET, socket.SOCK_STREAM),
98+
'tcp6': (socket.AF_INET6, socket.SOCK_STREAM),
99+
}
100+
95101
@classmethod
96102
def get_filter_code(cls, filter_name):
97103
"""
@@ -136,59 +142,45 @@ def __init__(self, **kwargs):
136142

137143
socket_type = kwargs.get("socket_type", None)
138144

139-
if not socket_type or (socket_type.lower() != "tcp" and socket_type.lower() != "unix"):
140-
raise DarwinInvalidArgumentError("DarwinApi:: __init__:: You must give a socket type (tcp/unix)")
141-
142145
try:
143146
darwin_timeout = kwargs["timeout"]
144147

145148
except KeyError:
146149
darwin_timeout = self.DEFAULT_TIMEOUT
147150

148151
self.socket = None
152+
connection_info = None
149153

150154
if socket_type == "unix":
151-
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
152-
darwin_socket_path = kwargs.get("socket_path", None)
153-
154-
if darwin_socket_path is None:
155+
connection_info = kwargs.get("socket_path", None)
156+
if connection_info is None:
155157
raise DarwinInvalidArgumentError("DarwinApi:: __init__:: No socket path has been given")
156-
157-
self.socket.setblocking(False)
158-
self.socket.settimeout(darwin_timeout)
159-
160158
if self.verbose:
161-
print("DarwinApi:: __init__:: Connecting to " + str(darwin_socket_path) + "...")
159+
print("DarwinApi:: __init__:: Connecting to " + str(connection_info) + "...")
162160

163-
try:
164-
self.socket.connect(darwin_socket_path)
165-
except socket.error as error:
166-
raise DarwinConnectionError(str(error))
167-
168-
else:
169-
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
161+
elif socket_type in self._SOCKET_PROTOCOL:
170162
darwin_socket_host = kwargs.get("socket_host", None)
171163
darwin_socket_port = kwargs.get("socket_port", None)
172-
173164
if darwin_socket_host is None:
174165
raise DarwinInvalidArgumentError("DarwinApi:: __init__:: No socket host has been given")
175-
176166
if darwin_socket_port is None:
177167
raise DarwinInvalidArgumentError("DarwinApi:: __init__:: No socket port has been given")
178-
179-
self.socket.setblocking(False)
180-
self.socket.settimeout(darwin_timeout)
181-
168+
connection_info = (darwin_socket_host, darwin_socket_port)
182169
if self.verbose:
183170
print("DarwinApi:: __init__:: Connecting to {darwin_socket_host}: {darwin_socket_port}...".format(
184171
darwin_socket_host=darwin_socket_host,
185172
darwin_socket_port=darwin_socket_port,
186173
))
174+
else :
175+
raise DarwinInvalidArgumentError("DarwinApi:: __init__:: Unknown socket_type provided")
187176

188-
try:
189-
self.socket.connect((darwin_socket_host, darwin_socket_port))
190-
except socket.error as error:
191-
raise DarwinConnectionError(str(error))
177+
try:
178+
self.socket = socket.socket(*self._SOCKET_PROTOCOL[socket_type])
179+
self.socket.setblocking(False)
180+
self.socket.settimeout(darwin_timeout)
181+
self.socket.connect(connection_info)
182+
except socket.error as error:
183+
raise DarwinConnectionError(str(error))
192184

193185
def low_level_call(self, **kwargs):
194186
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="darwin",
5-
version="1.2.1",
5+
version="1.2.2",
66
description="Call Darwin with your Python code!",
77
url="https://github.com/VultureProject/darwin-client-python",
88
author="Guillaume Catto",

0 commit comments

Comments
 (0)