Commit 09f11926 authored by Dimitris Aragiorgis's avatar Dimitris Aragiorgis

Some pylint fixes

Signed-off-by: default avatarDimitris Aragiorgis <dimara@grnet.gr>
parent 9aa2fbe7
...@@ -30,7 +30,6 @@ import logging ...@@ -30,7 +30,6 @@ import logging
import logging.handlers import logging.handlers
import threading import threading
import traceback import traceback
import subprocess
import daemon import daemon
import daemon.runner import daemon.runner
...@@ -141,21 +140,6 @@ def get_indev(payload): ...@@ -141,21 +140,6 @@ def get_indev(payload):
return indev_ifindex return indev_ifindex
def get_binding(proxy, ifindex, mac):
try:
if proxy.mac_indexed_clients:
logging.debug(" - Getting binding for mac %s", mac)
b = proxy.clients[mac]
else:
logging.debug(" - Getting binding for ifindex %s", ifindex)
b = proxy.clients[ifindex]
return b
except KeyError:
logging.debug(" - No client found for mac / ifindex %s / %s",
mac, ifindex)
return None
def parse_binding_file(path): def parse_binding_file(path):
""" Read a client configuration from a tap file """ Read a client configuration from a tap file
...@@ -237,8 +221,10 @@ class ClientFileHandler(pyinotify.ProcessEvent): ...@@ -237,8 +221,10 @@ class ClientFileHandler(pyinotify.ProcessEvent):
class Client(object): class Client(object):
def __init__(self, tap=None, indev=None, mac=None, ip=None, hostname=None, def __init__(self, tap=None, indev=None,
subnet=None, gateway=None, subnet6=None, gateway6=None, eui64=None ): mac=None, ip=None, hostname=None,
subnet=None, gateway=None,
subnet6=None, gateway6=None, eui64=None):
self.mac = mac self.mac = mac
self.ip = ip self.ip = ip
self.hostname = hostname self.hostname = hostname
...@@ -420,6 +406,19 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -420,6 +406,19 @@ class VMNetProxy(object): # pylint: disable=R0902
self._setup_nfqueue(ns_queue_num, AF_INET6, self.ns_response, 10) self._setup_nfqueue(ns_queue_num, AF_INET6, self.ns_response, 10)
self.ipv6_enabled = True self.ipv6_enabled = True
def get_binding(self, ifindex, mac):
try:
if self.mac_indexed_clients:
logging.debug(" - Getting binding for mac %s", mac)
b = self.clients[mac]
else:
logging.debug(" - Getting binding for ifindex %s", ifindex)
b = self.clients[ifindex]
return b
except KeyError:
logging.debug(" - No client found for mac / ifindex %s / %s",
mac, ifindex)
return None
def _cleanup(self): def _cleanup(self):
""" Free all resources for a graceful exit """ Free all resources for a graceful exit
...@@ -428,7 +427,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -428,7 +427,7 @@ class VMNetProxy(object): # pylint: disable=R0902
logging.info("Cleaning up") logging.info("Cleaning up")
logging.debug(" - Closing netfilter queues") logging.debug(" - Closing netfilter queues")
for q, num in self.nfq.values(): for q, _ in self.nfq.values():
q.close() q.close()
logging.debug(" - Stopping inotify watches") logging.debug(" - Stopping inotify watches")
...@@ -448,15 +447,6 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -448,15 +447,6 @@ class VMNetProxy(object): # pylint: disable=R0902
self.nfq[q.get_fd()] = (q, pending) self.nfq[q.get_fd()] = (q, pending)
logging.debug(" - Successfully set up NFQUEUE %d", queue_num) logging.debug(" - Successfully set up NFQUEUE %d", queue_num)
def sendp(self, data, binding):
""" Send a raw packet using a layer-2 socket
"""
logging.info(" - Sending raw packet on %s (%s)",
binding.tap, binding.hostname)
binding.sendp(data)
def build_config(self): def build_config(self):
self.clients.clear() self.clients.clear()
...@@ -598,7 +588,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -598,7 +588,7 @@ class VMNetProxy(object): # pylint: disable=R0902
indev = get_indev(payload) indev = get_indev(payload)
binding = get_binding(self, indev, mac) binding = self.get_binding(indev, mac)
if binding is None: if binding is None:
# We don't know anything about this interface, so accept the packet # We don't know anything about this interface, so accept the packet
# and return # and return
...@@ -612,11 +602,13 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -612,11 +602,13 @@ class VMNetProxy(object): # pylint: disable=R0902
payload.set_verdict(nfqueue.NF_DROP) payload.set_verdict(nfqueue.NF_DROP)
if mac != binding.mac: if mac != binding.mac:
logging.warn(" - Recieved spoofed DHCP request for mac %s from tap %s", mac, indev) logging.warn(" - Recieved spoofed DHCP request: mac %s, indev %s",
mac, indev)
return return
logging.info(" - Generating DHCP response for host %s (mac %s) on tap %s", logging.info(" - Generating DHCP response:"
binding.hostname, mac, binding.tap) " host %s, mac %s, tap %s, indev %s",
binding.hostname, mac, binding.tap, indev)
resp = Ether(dst=mac, src=self.get_iface_hw_addr(binding.indev))/\ resp = Ether(dst=mac, src=self.get_iface_hw_addr(binding.indev))/\
...@@ -676,7 +668,8 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -676,7 +668,8 @@ class VMNetProxy(object): # pylint: disable=R0902
elif req_type == DHCPRELEASE: elif req_type == DHCPRELEASE:
# Log and ignore # Log and ignore
logging.info(" - DHCPRELEASE from %s on %s", binding.mac, binding.tap) logging.info(" - DHCPRELEASE from %s on %s",
binding.hostname, binding.tap)
return return
# Finally, always add the server identifier and end options # Finally, always add the server identifier and end options
...@@ -690,7 +683,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -690,7 +683,7 @@ class VMNetProxy(object): # pylint: disable=R0902
logging.info(" - %s to %s (%s) on %s", DHCP_TYPES[resp_type], mac, logging.info(" - %s to %s (%s) on %s", DHCP_TYPES[resp_type], mac,
binding.ip, binding.tap) binding.ip, binding.tap)
try: try:
self.sendp(resp, binding) binding.sendp(resp)
except socket.error, e: except socket.error, e:
logging.warn(" - DHCP response on %s (%s) failed: %s", logging.warn(" - DHCP response on %s (%s) failed: %s",
binding.tap, binding.hostname, str(e)) binding.tap, binding.hostname, str(e))
...@@ -721,7 +714,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -721,7 +714,7 @@ class VMNetProxy(object): # pylint: disable=R0902
indev = get_indev(payload) indev = get_indev(payload)
binding = get_binding(self, indev, mac) binding = self.get_binding(indev, mac)
if binding is None: if binding is None:
# We don't know anything about this interface, so accept the packet # We don't know anything about this interface, so accept the packet
# and return # and return
...@@ -735,7 +728,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -735,7 +728,7 @@ class VMNetProxy(object): # pylint: disable=R0902
payload.set_verdict(nfqueue.NF_DROP) payload.set_verdict(nfqueue.NF_DROP)
if mac != binding.mac: if mac != binding.mac:
logging.warn(" - Received spoofed RS request for mac %s from tap %s", logging.warn(" - Received spoofed RS request: mac %s, tap %s",
mac, binding.tap) mac, binding.tap)
return return
...@@ -763,7 +756,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -763,7 +756,7 @@ class VMNetProxy(object): # pylint: disable=R0902
lifetime=self.ra_period * 3) lifetime=self.ra_period * 3)
try: try:
self.sendp(resp, binding) binding.sendp(resp)
except socket.error, e: except socket.error, e:
logging.warn(" - RA on %s (%s) failed: %s", logging.warn(" - RA on %s (%s) failed: %s",
binding.tap, binding.hostname, str(e)) binding.tap, binding.hostname, str(e))
...@@ -797,7 +790,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -797,7 +790,7 @@ class VMNetProxy(object): # pylint: disable=R0902
indev = get_indev(payload) indev = get_indev(payload)
binding = get_binding(self, indev, mac) binding = self.get_binding(indev, mac)
if binding is None: if binding is None:
# We don't know anything about this interface, so accept the packet # We don't know anything about this interface, so accept the packet
# and return # and return
...@@ -839,10 +832,10 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -839,10 +832,10 @@ class VMNetProxy(object): # pylint: disable=R0902
ICMPv6NDOptDstLLAddr(lladdr=indevmac) ICMPv6NDOptDstLLAddr(lladdr=indevmac)
try: try:
self.sendp(resp, binding) binding.sendp(resp)
except socket.error, e: except socket.error, e:
logging.warn(" - NA on %s (%s) failed: %s", logging.warn(" - NA on %s (%s) failed: %s",
bindig.tap, binding.hostname, str(e)) binding.tap, binding.hostname, str(e))
except Exception, e: except Exception, e:
logging.warn(" - Unkown error during periodic NA to %s (%s): %s", logging.warn(" - Unkown error during periodic NA to %s (%s): %s",
binding.tap, binding.hostname, str(e)) binding.tap, binding.hostname, str(e))
...@@ -877,7 +870,7 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -877,7 +870,7 @@ class VMNetProxy(object): # pylint: disable=R0902
resp /= ICMPv6NDOptRDNSS(dns=self.ipv6_nameservers, resp /= ICMPv6NDOptRDNSS(dns=self.ipv6_nameservers,
lifetime=self.ra_period * 3) lifetime=self.ra_period * 3)
try: try:
self.sendp(resp, binding) binding.sendp(resp)
except socket.error, e: except socket.error, e:
logging.warn(" - Periodic RA on %s (%s) failed: %s", logging.warn(" - Periodic RA on %s (%s) failed: %s",
tap, binding.hostname, str(e)) tap, binding.hostname, str(e))
...@@ -915,7 +908,8 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -915,7 +908,8 @@ class VMNetProxy(object): # pylint: disable=R0902
while True: while True:
try: try:
rlist, _, xlist = select.select(self.nfq.keys() + [iwfd], [], [], timeout) rlist, _, xlist = select.select(self.nfq.keys() + [iwfd],
[], [], timeout)
except select.error, e: except select.error, e:
if e[0] == errno.EINTR: if e[0] == errno.EINTR:
logging.debug("select() got interrupted") logging.debug("select() got interrupted")
...@@ -957,9 +951,11 @@ class VMNetProxy(object): # pylint: disable=R0902 ...@@ -957,9 +951,11 @@ class VMNetProxy(object): # pylint: disable=R0902
timeout = self.ra_period - (time.time() - start) timeout = self.ra_period - (time.time() - start)
def print_clients(self): def print_clients(self):
logging.info("%10s %20s %20s %10s %20s",'Key', 'Client', 'MAC', 'TAP', 'IP') logging.info("%10s %20s %20s %10s %20s",
'Key', 'Client', 'MAC', 'TAP', 'IP')
for k, cl in self.clients.items(): for k, cl in self.clients.items():
logging.info("%10s | %20s %20s %10s %20s", k, cl.hostname, cl.mac, cl.tap, cl.ip) logging.info("%10s | %20s %20s %10s %20s",
k, cl.hostname, cl.mac, cl.tap, cl.ip)
...@@ -1049,7 +1045,8 @@ if __name__ == "__main__": ...@@ -1049,7 +1045,8 @@ if __name__ == "__main__":
capng.CAP_SETPCAP) capng.CAP_SETPCAP)
# change uid # change uid
capng.capng_change_id(uid.pw_uid, uid.pw_gid, capng.capng_change_id(uid.pw_uid, uid.pw_gid,
capng.CAPNG_DROP_SUPP_GRP | capng.CAPNG_CLEAR_BOUNDING) capng.CAPNG_DROP_SUPP_GRP | \
capng.CAPNG_CLEAR_BOUNDING)
logger = logging.getLogger() logger = logging.getLogger()
if opts.debug: if opts.debug:
...@@ -1123,12 +1120,12 @@ if __name__ == "__main__": ...@@ -1123,12 +1120,12 @@ if __name__ == "__main__":
logging.info("Ready to serve requests") logging.info("Ready to serve requests")
def handler(signum, frame): def debug_handler(signum, _):
logging.debug('Received SIGUSR1. Printing current proxy state...') logging.debug('Received signal %d. Printing proxy state...', signum)
proxy.print_clients() proxy.print_clients()
# Set the signal handler for debuging clients # Set the signal handler for debuging clients
signal.signal(signal.SIGUSR1, handler) signal.signal(signal.SIGUSR1, debug_handler)
signal.siginterrupt(signal.SIGUSR1, False) signal.siginterrupt(signal.SIGUSR1, False)
try: try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment