20
from gi.repository import GObject
23
23
from blueman.Lib import get_net_address
26
class DhcpClient(gobject.GObject):
26
class DhcpClient(GObject.GObject):
28
28
#arg: interface name eg. ppp0
29
'connected' : (gobject.SIGNAL_NO_HOOKS, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
30
'error-occurred' : (gobject.SIGNAL_NO_HOOKS, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
29
'connected' : (GObject.SignalFlags.NO_HOOKS, None, (GObject.TYPE_PYOBJECT,)),
30
'error-occurred' : (GObject.SignalFlags.NO_HOOKS, None, (GObject.TYPE_PYOBJECT,)),
35
35
def __init__(self, interface, timeout=30):
36
gobject.GObject.__init__(self)
36
GObject.GObject.__init__(self)
38
38
self.interface = interface
39
39
self.timeout = timeout
41
self.DHCP_CLIENT = False
43
self.dhclient = subprocess.Popen(["dhclient", "--version"])
44
self.dhclient.terminate()
45
self.DHCP_CLIENT = "DHCLIENT"
48
self.dhcpcd = subprocess.Popen(["dhcpcd", "--version"])
49
self.dhcpcd.terminate()
50
self.DHCP_CLIENT = "DHCPCD"
52
self.DHCP_CLIENT = "NONE"
55
if self.DHCP_CLIENT == "NONE":
56
raise Exception("no DHCP client found, please install dhcpcd or dhclient")
42
58
if self.interface in DhcpClient.quering:
43
raise Exception("dhclient already running on this interface")
59
raise Exception("DHCP already running on this interface")
45
61
DhcpClient.quering.append(self.interface)
48
self.dhclient = subprocess.Popen(["dhclient", "-e", "IF_METRIC=100", "-1", self.interface])
50
raise Exception("dhclient binary not found")
52
gobject.timeout_add(1000, self.check_dhclient)
53
gobject.timeout_add(self.timeout*1000, self.on_timeout)
63
if self.DHCP_CLIENT == "DHCLIENT":
65
self.dhclient = subprocess.Popen(["dhclient", "-e", "IF_METRIC=100", "-1", self.interface])
67
#Should never happen now...
68
raise Exception("dhclient binary not found")
69
GObject.timeout_add(1000, self.check_dhclient)
70
elif self.DHCP_CLIENT == "DHCPCD":
72
self.dhcpcd = subprocess.Popen(["dhcpcd", "-m", "100", self.interface])
74
#Should never happen now...
75
raise Exception("dhcpcd binary not found")
76
GObject.timeout_add(1000, self.check_dhcpcd)
78
GObject.timeout_add(self.timeout*1000, self.on_timeout)
56
81
def on_timeout(self):
57
if self.dhclient.poll() == None:
58
dprint("Timeout reached, terminating dhclient")
59
self.dhclient.terminate()
82
if self.DHCP_CLIENT == "DHCLIENT":
83
if self.dhclient.poll() == None:
84
dprint("Timeout reached, terminating dhclient")
85
self.dhclient.terminate()
86
elif self.DHCP_CLIENT == "DHCPCD":
87
if self.dhcpcd.poll() == None:
88
dprint("Timeout reached, terminating dhcpcd")
89
self.dhcpcd.terminate()
61
91
def check_dhclient(self):
62
92
status = self.dhclient.poll()
67
97
dprint("bound to", ip)
68
98
self.emit("connected", ip)
70
gobject.timeout_add(1000, complete)
71
DhcpClient.quering.remove(self.interface)
75
dprint("dhclient failed with status code", status)
100
GObject.timeout_add(1000, complete)
101
DhcpClient.quering.remove(self.interface)
105
dprint("dhcp client failed with status code", status)
106
self.emit("error-occurred", status)
107
DhcpClient.quering.remove(self.interface)
112
def check_dhcpcd(self):
113
status = self.dhcpcd.poll()
117
ip = get_net_address(self.interface)
118
dprint("bound to", ip)
119
self.emit("connected", ip)
121
GObject.timeout_add(1000, complete)
122
DhcpClient.quering.remove(self.interface)
126
dprint("dhcpcd failed with status code", status)
76
127
self.emit("error-occurred", status)
77
128
DhcpClient.quering.remove(self.interface)