summaryrefslogtreecommitdiffstats
path: root/src/netparams.h
blob: 298b5afc5f1c19cb83a8be502afc28350913508d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
 *   Copyright (C) 2005 by Pawel Nawrocki                                  *
 *   [email protected]                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#ifndef WA_NETPARAMS_H
#define WA_NETPARAMS_H

#include <iostream>
#include <tqfile.h>
#include <tdemessagebox.h>
#include <tdelocale.h>

class WANetParams
{
public:
        TQString iface;
        TQString essid;
        //TQString mode;
        TQString channel;
        TQString ap;
        bool wep;
        TQString wepMode;
        TQString wepKey;
	bool wpa;
	TQStringList wpaSettings;
	TQString wpaKey;

        bool dhcp;
        TQString ip;
        TQString netmask;
        TQString broadcast;
        TQString gateway;
        TQString domain;
        TQString dns1;
        TQString dns2;

        bool hiddenEssid;
        bool wasHiddenEssid;
        bool wasWep;

	TQString preConnectionCommand;
	TQString postConnectionCommand;
	TQString preDisconnectionCommand;
	TQString postDisconnectionCommand;
	int preConnectionTimeout;
	int postConnectionTimeout;
	int preDisconnectionTimeout;
	int postDisconnectionTimeout;
	bool preConnectionDetached;
	bool postConnectionDetached;
	bool preDisconnectionDetached;
	bool postDisconnectionDetached;

        bool review()
        {
                bool r = false; //DEFAULTS TO 'no review needed'
                if (wep)
                        if ( (wepMode.isEmpty()) || (wepKey.isEmpty()) ) {
                                if (!wasWep) {
                                        KMessageBox::information(0, i18n("<qt><p>The network changed its security settings.</p><p>Please go to <i>Security</i> tab of the following dialog and configure WEP settings.</p></qt>") );
                                } else
                                        KMessageBox::error(0, i18n("<qt><p>Your WEP Key is not set properly.</p><p>Please go to <i>Security</i> tab of the following dialog and enter the required key.</p></qt>") );
                                r = true;
                        }
                if ( (hiddenEssid) && (!wasHiddenEssid) )
                        if ( KMessageBox::questionYesNo(0, i18n("<qt><p>The network has stopped broadcasting its ESSID since the last time you were connected.</p><p>Would you like to use '<b>%1</b>' as an ESSID for this network?</p><p><i>NOTE: If you answer No, a dialog will appear where you will be able to specify a different ESSID.</i></p></qt>").arg(essid) ) != 3 ) // !=YES
                                r = true;

                wasHiddenEssid = hiddenEssid;
                wasWep = wep;
                return r;
        }

        TQString netParamsString()
        {
                TQStringList mNPS;
                mNPS << boolToString(hiddenEssid) << essid << ap << channel << boolToString(wep) << wepMode << wepKey << boolToString(dhcp) << ip << netmask << broadcast << gateway << domain << dns1 << dns2 << boolToString(wasHiddenEssid) << boolToString(wasWep) << \
		preConnectionCommand << TQString::number(preConnectionTimeout) << boolToString(preConnectionDetached) << \
		postConnectionCommand << TQString::number(postConnectionTimeout) << boolToString(postConnectionDetached) << \
		preDisconnectionCommand << TQString::number(preDisconnectionTimeout) << boolToString(preDisconnectionDetached) << \
		postDisconnectionCommand << TQString::number(postDisconnectionTimeout) << boolToString(postDisconnectionDetached) << \
		wpaSettings.join(",") << wpaKey;
                return mNPS.join(",");
        }

        void loadNetParamsString( const TQString & nps )
        {
                /*if (nps.section(",",0,0)=="true")
                        hiddenEssid=true;
                else
                        hiddenEssid=false;*/	// COMMENTED OUT because hiddenEssid boolean is set from the list item
                essid = nps.section(",",1,1);
                ap = nps.section(",",2,2);
                //channel = nps.section(",",3,3);	COMMENTED OUT because channel is set from the list item
                /*if (nps.section(",",4,4)=="true")
                	wep=true;
                else
                	wep=false;*/	// COMMENTED OUT because wep boolean is set from the list item
                wepMode = nps.section(",",5,5);
                wepKey = nps.section(",",6,6);
		dhcp = ( nps.section(",",7,7) == "true" );
                ip = nps.section(",",8,8);
                netmask = nps.section(",",9,9);
                broadcast = nps.section(",",10,10);
                gateway = nps.section(",",11,11);
                domain = nps.section(",",12,12);
                dns1 = nps.section(",",13,13);
                dns2 = nps.section(",",14,14);
		wasHiddenEssid = ( nps.section(",",15,15)=="true" );
		wasWep = ( nps.section(",",16,16)=="true" );

		preConnectionCommand = nps.section(",",17,17);
		preConnectionTimeout = nps.section(",",18,18).toInt();
		preConnectionDetached = ( nps.section(",",19,19) == "true" );

		postConnectionCommand = nps.section(",",20,20);
		postConnectionTimeout = nps.section(",",21,21).toInt();
		postConnectionDetached = ( nps.section(",",22,22) == "true" );

		preDisconnectionCommand = nps.section(",",23,23);
		preDisconnectionTimeout = nps.section(",",24,24).toInt();
		preDisconnectionDetached = ( nps.section(",",25,25) == "true" );

		postDisconnectionCommand = nps.section(",",26,26);
		postDisconnectionTimeout = nps.section(",",27,27).toInt();
		postDisconnectionDetached = ( nps.section(",",28,28) == "true" );
		wpaSettings = TQStringList::split( ",", nps.section(",",29,32) ); // 4 fields
		wpaKey = nps.section(",",33,33);

        }
private:
        TQString boolToString( bool b )
        {
                TQString result;
                b ? result = "true" : result = "false";
                return result;
        }
};

class WACommands
{
public:
        bool allFound;
        TQStringList notFound;

        void init()
        {
                TQStringList binDirs;
                binDirs << "/sbin" << "/usr/sbin" << "/usr/local/sbin" << "/bin" << "/usr/bin" << "/usr/local/bin";

		wpa_supplicant = getPath("wpa_supplicant", binDirs);
		wpa_cli = getPath("wpa_cli", binDirs);
                dhcp = getPath("dhcpcd", binDirs); //these 2 checks have to be first, so allFound flag is properly set.
                if (dhcp.isEmpty())
                        dhcp = getPath("dhclient", binDirs);
                if (!dhcp.isEmpty()) {
                        allFound=1;
                        dhcpClient = dhcp.section("/",-1,-1);
                        std::cout << "DHCP Client: " << dhcpClient.ascii() << std::endl;
                }

                ifconfig = getPath("ifconfig", binDirs);
                iwconfig = getPath("iwconfig", binDirs);
                iwlist = getPath("iwlist", binDirs);
                route = getPath("route", binDirs);
                pidof = getPath("pidof", binDirs);

                if (!allFound)
                        std::cout << "Executable(s) not found:" << notFound.join(", ").ascii() << std::endl;
                else
                        std::cout << "All executables found." << std::endl;
        }

        TQStringList cmd( const TQString & action, const WANetParams & np, const bool & quiet = false )
        {
                TQStringList mCmd;

                /*if (action=="ifup")
                        mCmd << ifconfig << np.iface << "up";

                else if (action=="ifdown")
                        mCmd << ifconfig << np.iface << "down";

                else*/ if (action=="radio_on")
                        mCmd << iwconfig << np.iface << "txpower" << "auto";

                else if (action=="scan")
                        mCmd << iwlist << np.iface << "scan";

                else if (action=="disconnect")
                        mCmd << iwconfig << np.iface << "mode" << "managed" << "key" << "off" << "ap" << "off" << "essid" << "off";

                else if (action=="iwconfig_set") {
                        mCmd << iwconfig << np.iface << "mode" << "managed";
                        if (np.channel.toInt()>0)
                        	mCmd << "channel" << np.channel;
                        mCmd << "key";
                        if (np.wep && !np.wepKey.isEmpty())
                                mCmd << np.wepMode << np.wepKey;
                        else
                                mCmd << "off";
			mCmd << "essid" << np.essid;

                } else if (action=="iwconfig_ap") {
                        mCmd << iwconfig << np.iface << "ap" << np.ap;

                } else if (action=="ifconfig_dhcp") {
                        if (dhcpClient=="dhcpcd")
                                mCmd << dhcp << "-nd" << np.iface;
                        else if (dhcpClient=="dhclient")
                                mCmd << dhcp << np.iface; // << "-1" << "-q"

                } else if (action=="kill_dhcp") {
                        if ( dhcpClient=="dhcpcd")	//dhcpcd
                                mCmd << dhcp << "-k" << np.iface;
                        else //dhclient
                                mCmd << dhcp << "-r" << np.iface;

                } else if (action=="ifconfig_manual") {
                        mCmd << ifconfig << np.iface << np.ip;
                        if (!np.netmask.isEmpty())
                                mCmd << "netmask" << np.netmask;
                        if (!np.broadcast.isEmpty())
                                mCmd << "broadcast" << np.broadcast;

                } else if (action=="route_add") {
                        if (!np.gateway.isEmpty())
                                mCmd << route << "add" << "default" << "gw" << np.gateway;

                } else if (action=="route_del") {
                        if (!np.gateway.isEmpty())
                                mCmd << route << "del" << "default" << "gw" << np.gateway;

                } else
                        std::cout << "Unknown action: " << action.ascii() << std::endl;

                if ( (!mCmd.isEmpty()) && (!quiet) ) {//mCmd = TQStringList();
			TQString mCmdString = mCmd.join(" ");
			if (!np.wepKey.isEmpty()) mCmdString.replace(np.wepKey, "xxxxxxxxxx");
                        std::cout << action.ascii() << ": " << mCmdString.ascii() << std::endl;
		}
                return mCmd;
        }

	TQString route;
	TQString dhcpClient;
	TQString wpa_supplicant;
	TQString wpa_cli;
private:
        TQString ifconfig;
        TQString iwconfig;
        TQString iwlist;
        TQString dhcp;
        TQString pidof;

        TQString getPath(TQString file, TQStringList dirs)
        {
                TQString s;
                for ( TQStringList::Iterator it = dirs.begin(); it != dirs.end(); it++ ) {
                        if (TQFile( TQString(*it+"/"+file) ).exists()) {
                                s = TQString(*it+"/"+file);
                                break;
                        }
                }
                if (s.isEmpty()) {
                        allFound = 0;
                        notFound << file;
                }
                return s;
        }
};

#endif //WA_NETPARAMS_H