diff --git a/client/csocket.py b/client/csocket.py index 1f9915b7d..ae53a50b5 100644 --- a/client/csocket.py +++ b/client/csocket.py @@ -28,25 +28,27 @@ import socket, pickle class CSocket: + END_STRING = "" + SOCKET_FILE = "/tmp/fail2ban.sock" + def __init__(self): - self.socketFile = "/tmp/fail2ban.sock" # Create an INET, STREAMing socket #self.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.csock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #self.csock.connect(("localhost", 2222)) - self.csock.connect(self.socketFile) + self.csock.connect(CSocket.SOCKET_FILE) def send(self, msg): # Convert every list member to string obj = pickle.dumps(map(str, msg)) - self.csock.send(obj + "") + self.csock.send(obj + CSocket.END_STRING) ret = self.receive(self.csock) self.csock.close() return ret def receive(self, socket): msg = '' - while msg.rfind("") == -1: + while msg.rfind(CSocket.END_STRING) == -1: chunk = socket.recv(6) if chunk == '': raise RuntimeError, "socket connection broken" diff --git a/server/ssocket.py b/server/ssocket.py index b25754b20..57bf26c8f 100644 --- a/server/ssocket.py +++ b/server/ssocket.py @@ -32,20 +32,22 @@ logSys = logging.getLogger("fail2ban.comm") class SSocket(Thread): + END_STRING = "" + SOCKET_FILE = "/tmp/fail2ban.sock" + def __init__(self, transmitter): Thread.__init__(self) - self.socketFile = "/tmp/fail2ban.sock" self.transmit = transmitter self.isRunning = False logSys.debug("Created SSocket") def initialize(self, force = False): # Remove socket - if os.path.exists(self.socketFile): + if os.path.exists(SSocket.SOCKET_FILE): logSys.error("Fail2ban seems to be already running") if force: logSys.warn("Forcing execution of the server") - os.remove(self.socketFile) + os.remove(SSocket.SOCKET_FILE) else: raise SSocketErrorException("Server already running") # Create an INET, STREAMing socket @@ -55,7 +57,7 @@ class SSocket(Thread): self.ssock.setblocking(False) # Bind the socket to a public host and a well-known port #self.ssock.bind(("localhost", 2222)) - self.ssock.bind(self.socketFile) + self.ssock.bind(SSocket.SOCKET_FILE) # Become a server socket self.ssock.listen(1) @@ -79,9 +81,9 @@ class SSocket(Thread): stime = 1.0 self.ssock.close() # Remove socket - if os.path.exists(self.socketFile): - logSys.debug("Removed socket file " + self.socketFile) - os.remove(self.socketFile) + if os.path.exists(SSocket.SOCKET_FILE): + logSys.debug("Removed socket file " + SSocket.SOCKET_FILE) + os.remove(SSocket.SOCKET_FILE) logSys.debug("Socket shutdown") return True @@ -96,11 +98,11 @@ class SSocket(Thread): def send(self, socket, msg): obj = pickle.dumps(msg) - socket.send(obj + "") + socket.send(obj + SSocket.END_STRING) def receive(self, socket): msg = '' - while msg.rfind("") == -1: + while msg.rfind(SSocket.END_STRING) == -1: chunk = socket.recv(6) if chunk == '': raise RuntimeError, "socket connection broken"