vici: Support non-Unix sockets for vici connections using Python
authorMartin Willi <martin@revosec.ch>
Fri, 27 Feb 2015 12:59:23 +0000 (13:59 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 18 Mar 2015 12:59:14 +0000 (13:59 +0100)
src/libcharon/plugins/vici/python/vici/protocol.py
src/libcharon/plugins/vici/python/vici/session.py

index fe4e5d7..60b94ed 100644 (file)
@@ -11,10 +11,8 @@ class Transport(object):
     HEADER_LENGTH = 4
     MAX_SEGMENT = 512 * 1024
 
-    def __init__(self, address="/var/run/charon.vici"):
-        self.address = address
-        self.socket = socket.socket(socket.AF_UNIX)
-        self.socket.connect(address)
+    def __init__(self, sock):
+        self.socket = sock
 
     def send(self, packet):
         self.socket.sendall(struct.pack("!I", len(packet)) + packet)
@@ -191,4 +189,4 @@ class FiniteStream(io.BytesIO):
         return len(self.getvalue())
 
     def has_more(self):
-        return self.tell() < len(self)
\ No newline at end of file
+        return self.tell() < len(self)
index dd02493..cffac6a 100644 (file)
@@ -1,4 +1,5 @@
 import collections
+import socket
 
 from .exception import SessionException
 from .protocol import Transport, Packet, Message
@@ -11,8 +12,11 @@ CommandResult = collections.namedtuple(
 
 
 class Session(object):
-    def __init__(self, address="/var/run/charon.vici"):
-        self.handler = SessionHandler(Transport(address))
+    def __init__(self, sock=None):
+        if sock is None:
+            sock = socket.socket(socket.AF_UNIX)
+            sock.connect("/var/run/charon.vici")
+        self.handler = SessionHandler(Transport(sock))
 
     def version(self):
         """Retrieve daemon and system specific version information.