Remove the DhcpcdWi instance if the wpa_supplicant connection is lost.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-wi.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <QObject>
28 #include <QInputDialog>
29 #include <QLineEdit>
30 #include <QMenu>
31 #include <QMessageBox>
32 #include <QSocketNotifier>
33 #include <QTimer>
34 #include <QWidgetAction>
35
36 #include <cerrno>
37
38 #include "config.h"
39 #include "dhcpcd-wi.h"
40 #include "dhcpcd-qt.h"
41 #include "dhcpcd-ifmenu.h"
42 #include "dhcpcd-ssidmenu.h"
43
44 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
45 {
46
47         this->dhcpcdQt = parent;
48         this->wpa = wpa;
49         menu = NULL;
50         scans = NULL;
51
52         int fd = dhcpcd_wpa_get_fd(wpa);
53         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
54         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
55         retryOpenTimer = NULL;
56 }
57
58 DhcpcdWi::~DhcpcdWi()
59 {
60
61         if (menu) {
62                 delete menu;
63                 menu = NULL;
64         }
65
66         dhcpcd_wi_scans_free(scans);
67
68         if (notifier)
69                 delete notifier;
70 }
71
72 DHCPCD_WPA *DhcpcdWi::getWpa()
73 {
74
75         return wpa;
76 }
77
78 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
79 {
80
81         return scans;
82 }
83
84 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
85 {
86         int changed = 0;
87
88         if (menu) {
89                 QList<DhcpcdSsidMenu*> lst;
90                 DHCPCD_WI_SCAN *scan;
91
92                 lst = menu->findChildren<DhcpcdSsidMenu*>();
93                 for (scan = scans; scan; scan = scan->next) {
94                         bool found = false;
95
96                         foreach(DhcpcdSsidMenu *sm, lst) {
97                                 DHCPCD_WI_SCAN *s = sm->getScan();
98                                 if (memcmp(scan->bssid, s->bssid,
99                                     sizeof(scan->bssid)) == 0)
100                                 {
101                                         sm->setScan(scan);
102                                         found = true;
103                                         break;
104                                 }
105                         }
106
107                         if (!found) {
108                                 createMenuItem(menu, scan);
109                                 changed++;
110                         }
111                 }
112
113                 foreach(DhcpcdSsidMenu *sm, lst) {
114                         DHCPCD_WI_SCAN *s = sm->getScan();
115                         for (scan = scans; scan; scan = scan->next) {
116                                 if (memcmp(scan->bssid, s->bssid,
117                                     sizeof(scan->bssid)) == 0)
118                                         break;
119                         }
120                         if (scan == NULL) {
121                                 menu->removeAction(sm->getWidgetAction());
122                                 changed--;
123                         }
124                 }
125         }
126
127         dhcpcd_wi_scans_free(this->scans);
128         this->scans = scans;
129
130         return !(changed == 0);
131 }
132
133 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan)
134 {
135         QWidgetAction *wa = new QWidgetAction(menu);
136         DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, wa, this, scan);
137         wa->setDefaultWidget(ssidMenu);
138         menu->addAction(wa);
139         connect(ssidMenu, SIGNAL(selected(DHCPCD_WI_SCAN *)),
140             this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
141 }
142
143 void DhcpcdWi::createMenu1(QMenu *menu)
144 {
145         DHCPCD_WI_SCAN *scan;
146
147         for (scan = scans; scan; scan = scan->next)
148                 createMenuItem(menu, scan);
149 }
150
151 void DhcpcdWi::createMenu(QMenu *menu)
152 {
153
154         this->menu = menu;
155         createMenu1(menu);
156 }
157
158 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
159 {
160         DHCPCD_IF *ifp;
161
162         ifp = dhcpcd_wpa_if(wpa);
163         menu = new DhcpcdIfMenu(ifp, parent);
164         menu->setIcon(QIcon::fromTheme("network-wireless"));
165         createMenu1(menu);
166         return menu;
167 }
168
169 void DhcpcdWi::wpaOpen()
170 {
171         int fd = dhcpcd_wpa_open(wpa);
172         static int last_error;
173
174         if (fd == -1) {
175                 if (errno != last_error) {
176                         last_error = errno;
177                         qCritical("%s: dhcpcd_wpa_open: %s",
178                             dhcpcd_wpa_if(wpa)->ifname,
179                             strerror(last_error));
180                 }
181                 return;
182         }
183
184         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
185         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
186         if (retryOpenTimer) {
187                 delete retryOpenTimer;
188                 retryOpenTimer = NULL;
189         }
190 }
191
192 void DhcpcdWi::dispatch()
193 {
194
195         if (dhcpcd_wpa_get_fd(wpa) == -1) {
196                 delete notifier;
197                 notifier = NULL;
198                 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
199                 if (i == NULL ||
200                     strcmp(i->reason, "DEPARTED") == 0 ||
201                     strcmp(i->reason, "STOPPED") == 0)
202                         return;
203                 qWarning("%s: %s",
204                     i->ifname,
205                     qPrintable(tr("dhcpcd WPA connection lost")));
206                 if (retryOpenTimer == NULL) {
207                         retryOpenTimer = new QTimer(this);
208                         connect(retryOpenTimer, SIGNAL(timeout()),
209                             this, SLOT(wpaOpen()));
210                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
211                 }
212                 return;
213         }
214
215         dhcpcd_wpa_dispatch(wpa);
216 }
217
218 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
219 {
220         bool ok;
221         DHCPCD_WI_SCAN s;
222
223         /* Take a copy of scan incase it's destroyed by a scan update */
224         memcpy(&s, scan, sizeof(s));
225         s.next = NULL;
226
227         QString psk = QInputDialog::getText(dhcpcdQt, s.ssid,
228             tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
229
230         if (!ok)
231                 return;
232
233         QString errt;
234
235         switch (dhcpcd_wpa_configure_psk(wpa, &s, psk.toAscii())) {
236         case DHCPCD_WPA_SUCCESS:
237                 return;
238         case DHCPCD_WPA_ERR_SET:
239                 errt = tr("Failed to set key management.");
240                 break;
241         case DHCPCD_WPA_ERR_SET_PSK:
242                 errt = tr("Failed to set password, probably too short.");
243                 break;
244         case DHCPCD_WPA_ERR_ENABLE:
245                 errt = tr("Failed to enable the network.");
246                 break;
247         case DHCPCD_WPA_ERR_ASSOC:
248                 errt = tr("Failed to start association.");
249                 break;
250         case DHCPCD_WPA_ERR_WRITE:
251                 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
252                 break;
253         default:
254                 errt = strerror(errno);
255                 break;
256         }
257
258         QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
259             errt);
260 }