-rw-r--r-- | noncore/settings/networksettings/interfaces/interface.cpp | 4 |
1 files changed, 1 insertions, 3 deletions
diff --git a/noncore/settings/networksettings/interfaces/interface.cpp b/noncore/settings/networksettings/interfaces/interface.cpp index 864d9b5..2ace5fd 100644 --- a/noncore/settings/networksettings/interfaces/interface.cpp +++ b/noncore/settings/networksettings/interfaces/interface.cpp | |||
@@ -1,198 +1,196 @@ | |||
1 | $Header$ | ||
2 | |||
3 | /** | 1 | /** |
4 | * $Author$ | 2 | * $Author$ |
5 | * $Date$ | 3 | * $Date$ |
6 | * $Name$ | 4 | * $Id$ |
7 | */ | 5 | */ |
8 | 6 | ||
9 | #include "interface.h" | 7 | #include "interface.h" |
10 | #include <qdatetime.h> | 8 | #include <qdatetime.h> |
11 | #include <qfile.h> | 9 | #include <qfile.h> |
12 | #include <qdir.h> | 10 | #include <qdir.h> |
13 | #include <qfileinfo.h> | 11 | #include <qfileinfo.h> |
14 | #include <qtextstream.h> | 12 | #include <qtextstream.h> |
15 | 13 | ||
16 | #define IFCONFIG "/sbin/ifconfig" | 14 | #define IFCONFIG "/sbin/ifconfig" |
17 | #define DHCP_INFO_DIR "/etc/dhcpc" | 15 | #define DHCP_INFO_DIR "/etc/dhcpc" |
18 | 16 | ||
19 | #include <stdio.h> | 17 | #include <stdio.h> |
20 | #include <stdlib.h> | 18 | #include <stdlib.h> |
21 | 19 | ||
22 | Interface::Interface(QObject * parent, const char * name, bool newSatus): QObject(parent, name), hardwareName("Unknown"), moduleOwner(NULL), status(newSatus), attached(false), dhcp(false), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"){ | 20 | Interface::Interface(QObject * parent, const char * name, bool newSatus): QObject(parent, name), hardwareName("Unknown"), moduleOwner(NULL), status(newSatus), attached(false), dhcp(false), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"){ |
23 | refresh(); | 21 | refresh(); |
24 | } | 22 | } |
25 | 23 | ||
26 | /** | 24 | /** |
27 | * Set status | 25 | * Set status |
28 | * @param newStatus - the new status | 26 | * @param newStatus - the new status |
29 | * emit updateInterface | 27 | * emit updateInterface |
30 | */ | 28 | */ |
31 | void Interface::setStatus(bool newStatus){ | 29 | void Interface::setStatus(bool newStatus){ |
32 | if(status != newStatus){ | 30 | if(status != newStatus){ |
33 | status = newStatus; | 31 | status = newStatus; |
34 | refresh(); | 32 | refresh(); |
35 | } | 33 | } |
36 | }; | 34 | }; |
37 | 35 | ||
38 | /** | 36 | /** |
39 | * Set if attached or not (802.11 card pulled out for example) | 37 | * Set if attached or not (802.11 card pulled out for example) |
40 | * @param isAttached - if attached | 38 | * @param isAttached - if attached |
41 | * emit updateInterface | 39 | * emit updateInterface |
42 | */ | 40 | */ |
43 | void Interface::setAttached(bool isAttached){ | 41 | void Interface::setAttached(bool isAttached){ |
44 | attached = isAttached; | 42 | attached = isAttached; |
45 | emit(updateInterface(this)); | 43 | emit(updateInterface(this)); |
46 | }; | 44 | }; |
47 | 45 | ||
48 | /** | 46 | /** |
49 | * Set Hardware name | 47 | * Set Hardware name |
50 | * @param name - the new name | 48 | * @param name - the new name |
51 | * emit updateInterface | 49 | * emit updateInterface |
52 | */ | 50 | */ |
53 | void Interface::setHardwareName(const QString &name){ | 51 | void Interface::setHardwareName(const QString &name){ |
54 | hardwareName = name; | 52 | hardwareName = name; |
55 | emit(updateInterface(this)); | 53 | emit(updateInterface(this)); |
56 | }; | 54 | }; |
57 | 55 | ||
58 | /** | 56 | /** |
59 | * Set Module owner | 57 | * Set Module owner |
60 | * @param owner - the new owner | 58 | * @param owner - the new owner |
61 | * emit updateInterface | 59 | * emit updateInterface |
62 | */ | 60 | */ |
63 | void Interface::setModuleOwner(Module *owner){ | 61 | void Interface::setModuleOwner(Module *owner){ |
64 | moduleOwner = owner; | 62 | moduleOwner = owner; |
65 | emit(updateInterface(this)); | 63 | emit(updateInterface(this)); |
66 | }; | 64 | }; |
67 | 65 | ||
68 | 66 | ||
69 | /** | 67 | /** |
70 | * Try to start the interface. | 68 | * Try to start the interface. |
71 | */ | 69 | */ |
72 | void Interface::start(){ | 70 | void Interface::start(){ |
73 | // check to see if we are already running. | 71 | // check to see if we are already running. |
74 | if(true == status){ | 72 | if(true == status){ |
75 | emit (updateMessage("Unable to start interface,\n already started")); | 73 | emit (updateMessage("Unable to start interface,\n already started")); |
76 | return; | 74 | return; |
77 | } | 75 | } |
78 | 76 | ||
79 | int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(this->name()).latin1()); | 77 | int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(this->name()).latin1()); |
80 | // See if it was successfull... | 78 | // See if it was successfull... |
81 | if(ret != 0){ | 79 | if(ret != 0){ |
82 | emit (updateMessage("Starting interface failed")); | 80 | emit (updateMessage("Starting interface failed")); |
83 | return; | 81 | return; |
84 | } | 82 | } |
85 | 83 | ||
86 | status = true; | 84 | status = true; |
87 | refresh(); | 85 | refresh(); |
88 | emit (updateMessage("Start successfull")); | 86 | emit (updateMessage("Start successfull")); |
89 | } | 87 | } |
90 | 88 | ||
91 | /** | 89 | /** |
92 | * Try to stop the interface. | 90 | * Try to stop the interface. |
93 | */ | 91 | */ |
94 | void Interface::stop(){ | 92 | void Interface::stop(){ |
95 | // check to see if we are already stopped. | 93 | // check to see if we are already stopped. |
96 | if(false == status){ | 94 | if(false == status){ |
97 | emit (updateMessage("Unable to stop interface,\n already stopped")); | 95 | emit (updateMessage("Unable to stop interface,\n already stopped")); |
98 | return; | 96 | return; |
99 | } | 97 | } |
100 | 98 | ||
101 | int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(this->name()).latin1()); | 99 | int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(this->name()).latin1()); |
102 | if(ret != 0){ | 100 | if(ret != 0){ |
103 | emit (updateMessage("Stopping interface failed")); | 101 | emit (updateMessage("Stopping interface failed")); |
104 | return; | 102 | return; |
105 | } | 103 | } |
106 | 104 | ||
107 | status = false; | 105 | status = false; |
108 | refresh(); | 106 | refresh(); |
109 | emit (updateMessage("Stop successfull")); | 107 | emit (updateMessage("Stop successfull")); |
110 | } | 108 | } |
111 | 109 | ||
112 | /** | 110 | /** |
113 | * Try to restart the interface. | 111 | * Try to restart the interface. |
114 | */ | 112 | */ |
115 | void Interface::restart(){ | 113 | void Interface::restart(){ |
116 | stop(); | 114 | stop(); |
117 | start(); | 115 | start(); |
118 | } | 116 | } |
119 | 117 | ||
120 | /** | 118 | /** |
121 | * Try to refresh the information about the interface. | 119 | * Try to refresh the information about the interface. |
122 | * First call ifconfig, then check the dhcp-info file | 120 | * First call ifconfig, then check the dhcp-info file |
123 | * @return bool true if successfull. | 121 | * @return bool true if successfull. |
124 | */ | 122 | */ |
125 | bool Interface::refresh(){ | 123 | bool Interface::refresh(){ |
126 | // See if we are up. | 124 | // See if we are up. |
127 | if(status == false){ | 125 | if(status == false){ |
128 | macAddress = ""; | 126 | macAddress = ""; |
129 | ip = "0.0.0.0"; | 127 | ip = "0.0.0.0"; |
130 | subnetMask = "0.0.0.0"; | 128 | subnetMask = "0.0.0.0"; |
131 | broadcast = ""; | 129 | broadcast = ""; |
132 | dhcp = false; | 130 | dhcp = false; |
133 | dhcpServerIp = ""; | 131 | dhcpServerIp = ""; |
134 | leaseObtained = ""; | 132 | leaseObtained = ""; |
135 | leaseExpires = ""; | 133 | leaseExpires = ""; |
136 | emit(updateInterface(this)); | 134 | emit(updateInterface(this)); |
137 | return true; | 135 | return true; |
138 | } | 136 | } |
139 | 137 | ||
140 | QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name()); | 138 | QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name()); |
141 | int ret = system(QString("%1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1()); | 139 | int ret = system(QString("%1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1()); |
142 | if(ret != 0){ | 140 | if(ret != 0){ |
143 | qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1()); | 141 | qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1()); |
144 | return false; | 142 | return false; |
145 | } | 143 | } |
146 | 144 | ||
147 | QFile file(fileName); | 145 | QFile file(fileName); |
148 | if (!file.open(IO_ReadOnly)){ | 146 | if (!file.open(IO_ReadOnly)){ |
149 | qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1()); | 147 | qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1()); |
150 | return false; | 148 | return false; |
151 | } | 149 | } |
152 | 150 | ||
153 | // Set to the defaults | 151 | // Set to the defaults |
154 | macAddress = ""; | 152 | macAddress = ""; |
155 | ip = "0.0.0.0"; | 153 | ip = "0.0.0.0"; |
156 | subnetMask = "0.0.0.0"; | 154 | subnetMask = "0.0.0.0"; |
157 | broadcast = ""; | 155 | broadcast = ""; |
158 | 156 | ||
159 | QTextStream stream( &file ); | 157 | QTextStream stream( &file ); |
160 | QString line; | 158 | QString line; |
161 | while ( !stream.eof() ) { | 159 | while ( !stream.eof() ) { |
162 | line = stream.readLine(); | 160 | line = stream.readLine(); |
163 | if(line.contains("HWaddr")){ | 161 | if(line.contains("HWaddr")){ |
164 | int mac = line.find("HWaddr"); | 162 | int mac = line.find("HWaddr"); |
165 | macAddress = line.mid(mac+7, line.length()); | 163 | macAddress = line.mid(mac+7, line.length()); |
166 | } | 164 | } |
167 | if(line.contains("inet addr")){ | 165 | if(line.contains("inet addr")){ |
168 | int ipl = line.find("inet addr"); | 166 | int ipl = line.find("inet addr"); |
169 | int space = line.find(" ", ipl+10); | 167 | int space = line.find(" ", ipl+10); |
170 | ip = line.mid(ipl+10, space-ipl-10); | 168 | ip = line.mid(ipl+10, space-ipl-10); |
171 | } | 169 | } |
172 | if(line.contains("Mask")){ | 170 | if(line.contains("Mask")){ |
173 | int mask = line.find("Mask"); | 171 | int mask = line.find("Mask"); |
174 | subnetMask = line.mid(mask+5, line.length()); | 172 | subnetMask = line.mid(mask+5, line.length()); |
175 | } | 173 | } |
176 | if(line.contains("Bcast")){ | 174 | if(line.contains("Bcast")){ |
177 | int mask = line.find("Bcast"); | 175 | int mask = line.find("Bcast"); |
178 | int space = line.find(" ", mask+6); | 176 | int space = line.find(" ", mask+6); |
179 | broadcast = line.mid(mask+6, space-mask-6); | 177 | broadcast = line.mid(mask+6, space-mask-6); |
180 | } | 178 | } |
181 | } | 179 | } |
182 | file.close(); | 180 | file.close(); |
183 | QFile::remove(fileName); | 181 | QFile::remove(fileName); |
184 | 182 | ||
185 | // DHCP TESTING | 183 | // DHCP TESTING |
186 | // reset DHCP info | 184 | // reset DHCP info |
187 | dhcpServerIp = ""; | 185 | dhcpServerIp = ""; |
188 | leaseObtained = ""; | 186 | leaseObtained = ""; |
189 | leaseExpires = ""; | 187 | leaseExpires = ""; |
190 | dhcp = false; | 188 | dhcp = false; |
191 | 189 | ||
192 | QString dhcpDirectory(DHCP_INFO_DIR); | 190 | QString dhcpDirectory(DHCP_INFO_DIR); |
193 | QDir d(dhcpDirectory); | 191 | QDir d(dhcpDirectory); |
194 | if(!d.exists(dhcpDirectory)) | 192 | if(!d.exists(dhcpDirectory)) |
195 | dhcpDirectory = "/var/run"; | 193 | dhcpDirectory = "/var/run"; |
196 | 194 | ||
197 | // See if we have | 195 | // See if we have |
198 | QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name())); | 196 | QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name())); |