~ubuntu-branches/ubuntu/vivid/blueman/vivid-proposed

« back to all changes in this revision

Viewing changes to blueman/main/DhcpClient.py

  • Committer: Package Import Robot
  • Author(s): Artur Rona
  • Date: 2014-12-24 18:33:36 UTC
  • mfrom: (2.3.8 sid)
  • Revision ID: package-import@ubuntu.com-20141224183336-cyb82ot0y8tz8flq
Tags: 1.99~alpha1-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/patches/01_dont_autostart_lxde.patch:
    + Don't autostart the applet in LXDE.
  - debian/patches/03_filemanager_fix.patch:
    + Add support for more filemanagers.
* debian/patches/02_dont_crash_on_non-bluetooth_card.patch:
  - Dropped, no longer applicable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
18
18
import socket
19
19
import fcntl
20
 
import gobject
 
20
from gi.repository import GObject
21
21
import struct
22
22
import subprocess
23
23
from blueman.Lib import get_net_address
24
24
 
25
25
 
26
 
class DhcpClient(gobject.GObject):
 
26
class DhcpClient(GObject.GObject):
27
27
        __gsignals__ = {
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,)),
31
31
        }       
32
32
        
33
33
        quering = []
34
34
        
35
35
        def __init__(self, interface, timeout=30):
36
 
                gobject.GObject.__init__(self)
 
36
                GObject.GObject.__init__(self)
37
37
        
38
38
                self.interface = interface
39
39
                self.timeout = timeout
 
40
                
 
41
                self.DHCP_CLIENT = False
 
42
                try:
 
43
                        self.dhclient = subprocess.Popen(["dhclient", "--version"])
 
44
                        self.dhclient.terminate()
 
45
                        self.DHCP_CLIENT = "DHCLIENT"
 
46
                except OSError:
 
47
                        try:
 
48
                                self.dhcpcd = subprocess.Popen(["dhcpcd", "--version"])
 
49
                                self.dhcpcd.terminate()
 
50
                                self.DHCP_CLIENT = "DHCPCD"
 
51
                        except OSError:
 
52
                                self.DHCP_CLIENT = "NONE"
40
53
        
41
54
        def Connect(self):
 
55
                if self.DHCP_CLIENT == "NONE":
 
56
                        raise Exception("no DHCP client found, please install dhcpcd or dhclient")
 
57
 
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")
44
60
                else:
45
61
                        DhcpClient.quering.append(self.interface)
46
 
                        
47
 
                try:
48
 
                        self.dhclient = subprocess.Popen(["dhclient", "-e", "IF_METRIC=100", "-1", self.interface])
49
 
                except:
50
 
                        raise Exception("dhclient binary not found")
51
 
                        
52
 
                gobject.timeout_add(1000, self.check_dhclient)
53
 
                gobject.timeout_add(self.timeout*1000, self.on_timeout)
 
62
 
 
63
                if self.DHCP_CLIENT == "DHCLIENT":
 
64
                        try:    
 
65
                                self.dhclient = subprocess.Popen(["dhclient", "-e", "IF_METRIC=100", "-1", self.interface])
 
66
                        except:
 
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":
 
71
                        try:    
 
72
                                self.dhcpcd = subprocess.Popen(["dhcpcd", "-m", "100", self.interface])
 
73
                        except: 
 
74
                                #Should never happen now...
 
75
                                raise Exception("dhcpcd binary not found")              
 
76
                        GObject.timeout_add(1000, self.check_dhcpcd)
 
77
 
 
78
                GObject.timeout_add(self.timeout*1000, self.on_timeout)
54
79
 
55
80
                
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()
60
90
                
61
91
        def check_dhclient(self):
62
92
                status = self.dhclient.poll()
67
97
                                        dprint("bound to", ip)
68
98
                                        self.emit("connected", ip)      
69
99
                                
70
 
                                gobject.timeout_add(1000, complete)
71
 
                                DhcpClient.quering.remove(self.interface)
72
 
                                return False
73
 
                                
74
 
                        else:
75
 
                                dprint("dhclient failed with status code", status)
 
100
                                GObject.timeout_add(1000, complete)
 
101
                                DhcpClient.quering.remove(self.interface)
 
102
                                return False
 
103
                                
 
104
                        else:
 
105
                                dprint("dhcp client failed with status code", status)
 
106
                                self.emit("error-occurred", status)
 
107
                                DhcpClient.quering.remove(self.interface)
 
108
                                return False
 
109
                        
 
110
                return True     
 
111
        
 
112
        def check_dhcpcd(self):
 
113
                status = self.dhcpcd.poll()
 
114
                if status != None:
 
115
                        if status == 0:
 
116
                                def complete():
 
117
                                        ip = get_net_address(self.interface)
 
118
                                        dprint("bound to", ip)
 
119
                                        self.emit("connected", ip)      
 
120
                                
 
121
                                GObject.timeout_add(1000, complete)
 
122
                                DhcpClient.quering.remove(self.interface)
 
123
                                return False
 
124
                                
 
125
                        else:
 
126
                                dprint("dhcpcd failed with status code", status)
76
127
                                self.emit("error-occurred", status)
77
128
                                DhcpClient.quering.remove(self.interface)
78
129
                                return False