summaryrefslogtreecommitdiff
path: root/src/leap/base/checks.py
diff options
context:
space:
mode:
authorantialias <antialias@leap.se>2012-09-25 16:05:02 -0400
committerantialias <antialias@leap.se>2012-09-25 16:05:02 -0400
commit15b017656e6865b7b85ae389ab3b462c562a1e42 (patch)
tree7e51d663651bf16bc28e7837c41bb8c6bbc4504a /src/leap/base/checks.py
parent021e7ea900c64af9577412f11349f69e01634c0c (diff)
moved LeapNetworkChecker and test in base.
Diffstat (limited to 'src/leap/base/checks.py')
-rw-r--r--src/leap/base/checks.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py
new file mode 100644
index 00000000..c5438b09
--- /dev/null
+++ b/src/leap/base/checks.py
@@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+
+import platform
+
+import ping
+import requests
+
+from leap.base import constants
+from leap.base import exceptions
+
+
+class LeapNetworkChecker(object):
+ """
+ all network related checks
+ """
+ # TODO eventually, use a more portable solution
+ # like psutil
+
+ def run_all(self, checker=None):
+ if not checker:
+ checker = self
+ self.error = None # ?
+
+ # for MVS
+ checker.check_internet_connection()
+ checker.is_internet_up()
+ checker.ping_gateway()
+
+ def check_internet_connection(self):
+ try:
+ # XXX remove this hardcoded random ip
+ requests.get('http://216.172.161.165')
+ except (requests.HTTPError, requests.RequestException) as e:
+ raise exceptions.NoInternetConnection(e.message)
+ except requests.ConnectionError as e:
+ error = "Unidentified Connection Error"
+ if e.message == "[Errno 113] No route to host":
+ if not self.is_internet_up():
+ error = "No valid internet connection found."
+ else:
+ error = "Provider server appears to be down."
+ raise exceptions.NoInternetConnection(error)
+
+ def is_internet_up(self):
+ iface, gateway = self.get_default_interface_gateway()
+ self.ping_gateway(self)
+
+ def get_default_interface_gateway(self):
+ """only impletemented for linux so far."""
+ if not platform.system() == "Linux":
+ raise NotImplementedError
+
+ f = open("/proc/net/route")
+ route_table = f.readlines()
+ f.close()
+ #toss out header
+ route_table.pop(0)
+
+ default_iface = None
+ gateway = None
+ while route_table:
+ line = route_table.pop(0)
+ iface, destination, gateway = line.split('\t')[0:3]
+ if destination == '00000000':
+ default_iface = iface
+ break
+
+ if not default_iface:
+ raise exceptions.NoDefaultInterfaceFoundError
+
+ if default_iface not in netifaces.interfaces():
+ raise exceptions.InterfaceNotFoundError
+
+ return default_iface, gateway
+
+ def ping_gateway(self, gateway):
+ #TODO: Discuss how much packet loss (%) is acceptable.
+ packet_loss = ping.quiet_ping(gateway)[0]
+ if packet_loss > constants.MAX_ICMP_PACKET_LOSS:
+ raise exceptions.NoConnectionToGateway