summaryrefslogtreecommitdiff
path: root/noncore/net/networksetup/interfaces.cpp
Unidiff
Diffstat (limited to 'noncore/net/networksetup/interfaces.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/networksetup/interfaces.cpp638
1 files changed, 0 insertions, 638 deletions
diff --git a/noncore/net/networksetup/interfaces.cpp b/noncore/net/networksetup/interfaces.cpp
deleted file mode 100644
index 377a6db..0000000
--- a/noncore/net/networksetup/interfaces.cpp
+++ b/dev/null
@@ -1,638 +0,0 @@
1#include "interfaces.h"
2
3#include <qfile.h>
4#include <qtextstream.h>
5#include <qregexp.h>
6
7#define AUTO "auto"
8#define IFACE "iface"
9#define MAPPING "mapping"
10
11/**
12 * Constructor. Reads in the interfaces file and then split the file up by
13 * the \n for interfaces variable.
14 * @param useInterfacesFile if an interface file other then the default is
15 * desired to be used it should be passed in.
16 */
17Interfaces::Interfaces(QString useInterfacesFile){
18 acceptedFamily.append(INTERFACES_FAMILY_INET);
19 acceptedFamily.append(INTERFACES_FAMILY_IPX);
20 acceptedFamily.append(INTERFACES_FAMILY_INET6);
21
22 interfacesFile = useInterfacesFile;
23 QFile file(interfacesFile);
24 if (!file.open(IO_ReadOnly)){
25 qDebug(QString("Interfaces: Can't open file: %1 for reading.").arg(interfacesFile).latin1());
26 currentIface = interfaces.end();
27 currentMapping = interfaces.end();
28 return;
29 }
30 QTextStream stream( &file );
31 QString line;
32 while ( !stream.eof() ) {
33 line += stream.readLine();
34 line += "\n";
35 }
36 file.close();
37 interfaces = QStringList::split("\n", line, true);
38
39 currentIface = interfaces.end();
40 currentMapping = interfaces.end();
41}
42
43
44/**
45 * Get a list of all interfaces in the interface file. Usefull for
46 * hardware that is not currently connected such as an 802.11b card
47 * not plugged in, but configured for when it is plugged in.
48 * @return Return string list of interfaces.
49 **/
50QStringList Interfaces::getInterfaceList(){
51 QStringList list;
52 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
53 QString line = (*it).simplifyWhiteSpace();
54 if(line.contains(IFACE) && line.at(0) != '#'){
55 line = line.mid(QString(IFACE).length() +1, line.length());
56 line = line.simplifyWhiteSpace();
57 int findSpace = line.find(" ");
58 if( findSpace >= 0){
59 line = line.mid(0, findSpace);
60 list.append(line);
61 }
62 }
63 }
64 return list;
65}
66
67/**
68 * Find out if interface is in an "auto" group or not.
69 * Report any duplicates such as eth0 being in two differnt auto's
70 * @param interface interface to check to see if it is on or not.
71 * @return true is interface is in auto
72 */
73bool Interfaces::isAuto(QString interface){
74 QStringList autoLines = interfaces.grep(QRegExp(AUTO));
75 QStringList awi = autoLines.grep(QRegExp(interface));
76 if(awi.count() > 1)
77 qDebug(QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1());
78 if(awi.count() < 1)
79 return false;
80 return true;
81}
82
83/**
84 * Attempt to set the auto option for interface to setAuto.
85 * @param interface the interface to set
86 * @param setAuto the value to set interface to.
87 * @return false if already set to setAuto.
88 * */
89bool Interfaces::setAuto(QString interface, bool setAuto){
90 // Don't need to set it if it is already set.
91 if(isAuto(interface) == setAuto)
92 return false;
93
94 bool changed = false;
95 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
96 if((*it).contains(AUTO)){
97 //We know that they are not in any group so let add to this auto.
98 if(setAuto){
99 (*it) = (*it) += " " + interface;
100 // Don't care to have such thins as: auto eth0 lo usb0
101 (*it) = (*it).simplifyWhiteSpace();
102 changed = true;
103 break;
104 }
105 else{
106 if((*it).contains(interface)){
107 (*it) = (*it).replace(QRegExp(interface), "");
108 // clean up
109 QString line = (*it).simplifyWhiteSpace();
110 line = line.replace(QRegExp(" "),"");
111 if(line == AUTO)
112 (*it) = "";
113 changed = true;
114 // Don't break because we want to make sure we remove all cases.
115 }
116 }
117 }
118 }
119 if(changed == false){
120 if(setAuto == true)
121 interfaces.append(QString(AUTO" %1").arg(interface));
122 else{
123 qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1());
124 }
125 }
126 return true;
127}
128
129/**
130 * Set the current interface to interface. This needs to be done before you
131 * can call getFamily(), getMethod, and get/setOption().
132 * @param interface the name of the interface to set. All whitespace is
133 * removed from the interface name.
134 * @return bool true if it is successfull.
135 */
136bool Interfaces::setInterface(QString interface){
137 interface = interface.simplifyWhiteSpace();
138 interface = interface.replace(QRegExp(" "), "");
139 return setStanza(IFACE, interface, currentIface);
140}
141
142/**
143 * A quick helper funtion to see if the current interface is set.
144 * @return bool true if set, false otherwise.
145 */
146bool Interfaces::isInterfaceSet(){
147 return (currentIface != interfaces.end());
148}
149
150/**
151 * Add a new interface of with the settings - family and method
152 * @param interface the name of the interface to set. All whitespace is
153 * removed from the interface name.
154 * @param family the family of this interface inet or inet, ipx or inet6
155 * Must of one of the families defined in interfaces.h
156 * @param method for the family. see interfaces man page for family methods.
157 * @return true if successfull.
158 */
159bool Interfaces::addInterface(QString interface, QString family, QString method){
160 if(acceptedFamily.contains(family)==0)
161 return false;
162 interface = interface.simplifyWhiteSpace();
163 interface = interface.replace(QRegExp(" "), "");
164 interfaces.append("");
165 interfaces.append(QString(IFACE " %1 %2 %3").arg(interface).arg(family).arg(method));
166 return true;
167}
168
169/**
170 * Copies interface with name interface to name newInterface
171 * @param newInterface name of the new interface.
172 * @return bool true if successfull
173 */
174bool Interfaces::copyInterface(QString interface, QString newInterface){
175 if(!setInterface(interface)) return false;
176
177 QStringList::Iterator it = currentIface;
178 it++;
179
180 bool error;
181 addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error));
182 if(!setInterface(newInterface)) return false;
183 QStringList::Iterator newIface = currentIface;
184 newIface++;
185
186 for ( it; it != interfaces.end(); ++it ){
187 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)))
188 break;
189 newIface = interfaces.insert(newIface, *it);
190 }
191
192 return true;
193}
194
195/**
196 * Remove the currently selected interface and all of its options.
197 * @return bool if successfull or not.
198 */
199bool Interfaces::removeInterface(){
200 if(currentIface == interfaces.end())
201 return false;
202 (*currentIface) = "";
203 return removeAllInterfaceOptions();
204}
205
206/**
207 * Gets the hardware name of the interface that is currently selected.
208 * @return QString name of the hardware interface (eth0, usb2, wlan1...).
209 * @param error set to true if any error occurs, false otherwise.
210 */
211QString Interfaces::getInterfaceName(bool &error){
212 if(currentIface == interfaces.end()){
213 error = true;
214 return QString();
215 }
216 QString line = (*currentIface);
217 line = line.mid(QString(IFACE).length() +1, line.length());
218 line = line.simplifyWhiteSpace();
219 int findSpace = line.find(" ");
220 if( findSpace < 0){
221 error = true;
222 return QString();
223 }
224 error = false;
225 return line.mid(0, findSpace);
226}
227
228/**
229 * Gets the family name of the interface that is currently selected.
230 * @return QString name of the family (inet, inet6, ipx).
231 * @param error set to true if any error occurs, false otherwise.
232 */
233QString Interfaces::getInterfaceFamily(bool &error){
234 QString name = getInterfaceName(error);
235 if(error){
236 error = true;
237 return QString();
238 }
239 QString line = (*currentIface);
240 line = line.mid(QString(IFACE).length() +1, line.length());
241 line = line.mid(name.length()+1, line.length());
242 line = line.simplifyWhiteSpace();
243 int findSpace = line.find(" ");
244 if( findSpace < 0){
245 error = true;
246 return QString();
247 }
248 error = false;
249 return line.mid(0, findSpace);
250}
251
252/**
253 * Gets the method of the interface that is currently selected.
254 * @return QString name of the method such as staic or dhcp.
255 * See the man page of interfaces for possible methods depending on the family.
256 * @param error set to true if any error occurs, false otherwise.
257 */
258QString Interfaces::getInterfaceMethod(bool &error){
259 QString name = getInterfaceName(error);
260 if(error){
261 error = true;
262 return QString();
263 }
264 QString family = getInterfaceFamily(error);
265 if(error){
266 error = true;
267 return QString();
268 }
269 QString line = (*currentIface);
270 line = line.mid(QString(IFACE).length()+1, line.length());
271 line = line.mid(name.length()+1, line.length());
272 line = line.mid(family.length()+1, line.length());
273 line = line.simplifyWhiteSpace();
274 error = false;
275 return line;
276}
277
278/**
279 * Sets the interface name to newName.
280 * @param newName the new name of the interface. All whitespace is removed.
281 * @return bool true if successfull.
282 */
283bool Interfaces::setInterfaceName(QString newName){
284 if(currentIface == interfaces.end())
285 return false;
286 newName = newName.simplifyWhiteSpace();
287 newName = newName.replace(QRegExp(" "), "");
288 bool returnValue = false;
289 (*currentIface) = QString("iface %1 %2 %3").arg(newName).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue));
290 return !returnValue;
291}
292
293/**
294 * Sets the interface family to newName.
295 * @param newName the new name of the interface. Must be one of the families
296 * defined in the interfaces.h file.
297 * @return bool true if successfull.
298 */
299bool Interfaces::setInterfaceFamily(QString newName){
300 if(currentIface == interfaces.end())
301 return false;
302 if(acceptedFamily.contains(newName)==0)
303 return false;
304 bool returnValue = false;
305 (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue));
306 return !returnValue;
307}
308
309/**
310 * Sets the interface method to newName
311 * @param newName the new name of the interface
312 * @return bool true if successfull.
313 */
314bool Interfaces::setInterfaceMethod(QString newName){
315 if(currentIface == interfaces.end())
316 return false;
317 bool returnValue = false;
318 (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName);
319 return !returnValue;
320}
321
322/**
323 * Get a value for an option in the currently selected interface. For example
324 * calling getInterfaceOption("address") on the following stanza would
325 * return 192.168.1.1.
326 * iface eth0 static
327 * address 192.168.1.1
328 * @param option the options to get the value.
329 * @param error set to true if any error occurs, false otherwise.
330 * @return QString the options value. QString::null if error == true
331 */
332QString Interfaces::getInterfaceOption(QString option, bool &error){
333 return getOption(currentIface, option, error);
334}
335
336/**
337 * Set a value for an option in the currently selected interface. If option
338 * doesn't exist then it is added along with the value.
339 * @param option the options to set the value.
340 * @param value the value that option should be set to.
341 * @param error set to true if any error occurs, false otherwise.
342 * @return QString the options value. QString::null if error == true
343 */
344bool Interfaces::setInterfaceOption(QString option, QString value){
345 return setOption(currentIface, option, value);
346}
347
348/**
349 * Removes a value for an option in the currently selected interface.
350 * @param option the options to set the value.
351 * @param value the value that option should be set to.
352 * @param error set to true if any error occurs, false otherwise.
353 * @return QString the options value. QString::null if error == true
354 */
355bool Interfaces::removeInterfaceOption(QString option, QString value){
356 return removeOption(currentIface, option, value);
357}
358
359/**
360 * Removes all of the options from the currently selected interface.
361 * @return bool error if if successfull
362 */
363bool Interfaces::removeAllInterfaceOptions(){
364 return removeAllOptions(currentIface);
365}
366
367/**
368 * Set the current map to interface's map. This needs to be done before you
369 * can call addMapping(), set/getMap(), and get/setScript().
370 * @param interface the name of the interface to set. All whitespace is
371 * removed from the interface name.
372 * @return bool true if it is successfull.
373 */
374bool Interfaces::setMapping(QString interface){
375 interface = interface.simplifyWhiteSpace();
376 interface = interface.replace(QRegExp(" "), "");
377 return setStanza(MAPPING, interface, currentMapping);
378}
379
380/**
381 * Adds a new Mapping to the interfaces file with interfaces.
382 * @param interface the name(s) of the interfaces to set to this mapping
383 */
384void Interfaces::addMapping(QString option){
385 interfaces.append("");
386 interfaces.append(QString(MAPPING " %1").arg(option));
387}
388
389/**
390 * Remove the currently selected map and all of its options.
391 * @return bool if successfull or not.
392 */
393bool Interfaces::removeMapping(){
394 if(currentMapping == interfaces.end())
395 return false;
396 (*currentMapping) = "";
397 return removeAllOptions(currentMapping);
398}
399
400/**
401 * Set a map option within a mapping.
402 * @param map map to use
403 * @param value value to go with map
404 * @return bool true if it is successfull.
405 */
406bool Interfaces::setMap(QString map, QString value){
407 return setOption(currentMapping, map, value);
408}
409
410/**
411 * Removes a map option within a mapping.
412 * @param map map to use
413 * @param value value to go with map
414 * @return bool true if it is successfull.
415 */
416bool Interfaces::removeMap(QString map, QString value){
417 return removeOption(currentMapping, map, value);
418}
419
420/**
421 * Get a map value within a mapping.
422 * @param map map to get value of
423 * @param bool true if it is successfull.
424 * @return value that goes to the map
425 */
426QString Interfaces::getMap(QString map, bool &error){
427 return getOption(currentMapping, map, error);
428}
429
430/**
431 * Sets a script value of the current mapping to argument.
432 * @param argument the script name.
433 * @return true if successfull.
434 */
435bool Interfaces::setScript(QString argument){
436 return setOption(currentMapping, "script", argument);
437}
438
439/**
440 * @param error true if could not retrieve the current script argument.
441 * @return QString the argument of the script for the current mapping.
442 */
443QString Interfaces::getScript(bool &error){
444 return getOption(currentMapping, "script", error);
445}
446
447/**
448 * Helper function used to parse through the QStringList and put pointers in
449 * the correct place.
450 * @param stanza The stanza (auto, iface, mapping) to look for.
451 * @param option string that must be in the stanza's main line.
452 * @param interator interator to place at location of stanza if successfull.
453 * @return bool true if the stanza is found.
454 */
455bool Interfaces::setStanza(QString stanza, QString option, QStringList::Iterator &iterator){
456 bool found = false;
457 iterator = interfaces.end();
458 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
459 QString line = (*it).simplifyWhiteSpace();
460 if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){
461 uint point = line.find(option);
462 bool valid = true;
463 if(point > 0){
464 // There are more chars in the line. check +1
465 if(line.at(point-1) != ' ')
466 valid = false;
467 }
468 point += option.length();
469 if(point < line.length()-1){
470 // There are more chars in the line. check -1
471 if(line.at(point) != ' ')
472 valid = false;
473 }
474 if(valid){
475 if(found == true){
476 qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1());
477 }
478 found = true;
479 iterator = it;
480 }
481 }
482 }
483 return found;
484}
485
486/**
487 * Sets a value of an option in a stanza
488 * @param start the start of the stanza
489 * @param option the option to use when setting value.
490 * @return bool true if successfull, false otherwise.
491 */
492bool Interfaces::setOption(QStringList::Iterator start, QString option, QString value){
493 if(start == interfaces.end())
494 return false;
495
496 bool found = false;
497 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
498 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
499 if(!found && value != ""){
500 // Got to the end of the stanza without finding it, so append it.
501 interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value));
502 }
503 found = true;
504 break;
505 }
506 if((*it).contains(option) && it != start && (*it).at(0) != '#'){
507 // Found it in stanza so replace it.
508 if(found)
509 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
510 found = true;
511 (*it) = QString("\t%1 %2").arg(option).arg(value);
512 }
513 }
514 if(!found){
515 QStringList::Iterator p = start;
516 interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value));
517 found = true;
518 }
519 return found;
520}
521/**
522 * Removes a option in a stanza
523 * @param start the start of the stanza
524 * @param option the option to use when setting value.
525 * @return bool true if successfull, false otherwise.
526 */
527bool Interfaces::removeOption(QStringList::Iterator start, QString option, QString value){
528 if(start == interfaces.end())
529 return false;
530
531 bool found = false;
532 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
533 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
534 // got to the end without finding it
535 break;
536 }
537 if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){
538 // Found it in stanza so replace it.
539 if(found)
540 qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1());
541 found = true;
542 (*it) = "";
543 }
544 }
545 return found;
546}
547
548/**
549 * Removes all options in a stanza
550 * @param start the start of the stanza
551 * @return bool true if successfull, false otherwise.
552 */
553bool Interfaces::removeAllOptions(QStringList::Iterator start){
554 if(start == interfaces.end())
555 return false;
556
557 QStringList::Iterator it = start;
558 it = ++it;
559 for (it; it != interfaces.end(); ++it ) {
560 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
561 break;
562 }
563 it = interfaces.remove(it);
564 it = --it;
565 }
566 // Leave a space between this interface and the next.
567 interfaces.insert(it, QString(""));
568 return true;
569}
570
571/**
572 * Gets a value of an option in a stanza
573 * @param start the start of the stanza
574 * @param option the option to use when getting the value.
575 * @param bool true if errors false otherwise.
576 * @return QString the value of option QString::null() if error == true.
577 */
578QString Interfaces::getOption(QStringList::Iterator start, QString option, bool &error){
579 if(start == interfaces.end()){
580 error = false;
581 return QString();
582 }
583
584 QString value;
585 bool found = false;
586 for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) {
587 if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){
588 break;
589 }
590 if((*it).contains(option) && (*it).at(0) != '#'){
591 if(found)
592 qDebug(QString("Interfaces: Get Options found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1());
593 found = true;
594 QString line = (*it).simplifyWhiteSpace();
595 int space = line.find(" ", option.length());
596 if(space != -1)
597 value = line.mid(space+1, line.length());
598 else
599 qDebug(QString("Interfaces: Option %1 with no value").arg(option).latin1());
600 }
601 }
602 error = !found;
603 return value;
604}
605
606/**
607 * Write out the interfaces file to the file passed into the constructor.
608 * Removes any excess blank lines over 1 line long.
609 * @return bool true if successfull, false if not.
610 */
611bool Interfaces::write(){
612 QFile::remove(interfacesFile);
613 QFile file(interfacesFile);
614
615 if (!file.open(IO_ReadWrite)){
616 qDebug(QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1());
617 return false;
618 }
619 QTextStream stream( &file );
620 int whiteSpaceCount = 0;
621 for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) {
622 QString line = (*it).simplifyWhiteSpace();
623 line = line.replace(QRegExp(" "),"");
624 if(line.length() == 0)
625 whiteSpaceCount++;
626 else
627 whiteSpaceCount = 0;
628 if(whiteSpaceCount < 2){
629 qDebug((*it).latin1());
630 stream << (*it) << '\n';
631 }
632 }
633 file.close();
634 return true;
635}
636
637// interfaces.cpp
638