| 1 | /* |
| 2 | * File: CtcProperties.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "HerculesStudio.h" |
| 26 | #include "CtcProperties.h" |
| 27 | #include "ConfigurationEditor.h" |
| 28 | #include "Preferences.h" |
| 29 | |
| 30 | #include <QMessageBox> |
| 31 | #include <QFileDialog> |
| 32 | |
| 33 | #include <sstream> |
| 34 | |
| 35 | CtcProperties::CtcProperties(ConfigLine& configLine, QWidget *parent) |
| 36 | : GenericDeviceProperties(configLine, parent) |
| 37 | { |
| 38 | ui.setupUi(this); |
| 39 | initialize(configLine); |
| 40 | |
| 41 | connect(ui.okButton, SIGNAL(clicked()), this, SLOT(ok())); |
| 42 | connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); |
| 43 | connect(ui.typeCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(typeChanged(const QString &))); |
| 44 | connect(ui.oatBrowseButton, SIGNAL(clicked()), this, SLOT(oatBrowse())); |
| 45 | connect(ui.tun0CheckBox, SIGNAL(stateChanged(int)), this, SLOT(tun0Check(int))); |
| 46 | } |
| 47 | |
| 48 | CtcProperties::~CtcProperties() |
| 49 | { |
| 50 | |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void CtcProperties::ok() |
| 55 | { |
| 56 | if (!deviceNumberValidator(ui.deviceNumber)) |
| 57 | return; |
| 58 | std::stringstream newLineBuff; |
| 59 | |
| 60 | newLineBuff << ui.deviceNumber->text().toStdString(); |
| 61 | newLineBuff << " " << ui.typeCombo->currentText().toStdString(); |
| 62 | |
| 63 | if (ui.typeCombo->currentIndex() == 0) // lcs |
| 64 | { |
| 65 | if (!ui.tun->text().isEmpty()) |
| 66 | { |
| 67 | newLineBuff << " -n " << ui.tun->text().toStdString(); |
| 68 | } |
| 69 | if (!ui.oat->text().isEmpty()) |
| 70 | { |
| 71 | newLineBuff << " -o " << ui.oat->text().toStdString(); |
| 72 | } |
| 73 | if (!(ui.mac->text() == ":::::")) |
| 74 | { |
| 75 | if (macValidator(ui.mac->text().toStdString())) |
| 76 | { |
| 77 | newLineBuff << " -m " << ui.mac->text().toStdString() ; |
| 78 | } |
| 79 | else |
| 80 | return; |
| 81 | } |
| 82 | if ( !ui.guestIP->text().isEmpty() && (ui.guestIP->text() != "..." ) ) |
| 83 | { |
| 84 | if (validateIp(ui.guestIP, false, false)) |
| 85 | { |
| 86 | newLineBuff << " " << ui.guestIP->text().toStdString() ; |
| 87 | } |
| 88 | else |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | else if (ui.typeCombo->currentIndex() == 2) // ctct |
| 93 | { |
| 94 | if (!validateIp(ui.hostIP,false, false)) |
| 95 | return; |
| 96 | newLineBuff << " " << ui.lportSpinbox->text().toStdString() ; |
| 97 | newLineBuff << " " << ui.hostIP->text().toStdString(); |
| 98 | newLineBuff << " " << ui.hostPortSpinbox->text().toStdString(); |
| 99 | newLineBuff << " " << ui.buffsizeSpinBox->text().toStdString(); |
| 100 | } |
| 101 | else if (ui.typeCombo->currentIndex() == 1) // ctci |
| 102 | { |
| 103 | if (ui.tun0CheckBox->isChecked()) |
| 104 | { |
| 105 | newLineBuff << " tun0"; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | if (!validateIp(ui.guestIP,false, false)) |
| 110 | return; |
| 111 | if (!validateIp(ui.hostIP,false, false)) |
| 112 | return; |
| 113 | if (!ui.tun->text().isEmpty()) |
| 114 | { |
| 115 | newLineBuff << " -n " << ui.tun->text().toStdString(); |
| 116 | } |
| 117 | if (!(ui.mac->text() == ":::::")) |
| 118 | { |
| 119 | if (macValidator(ui.mac->text().toStdString())) |
| 120 | { |
| 121 | newLineBuff << " -m " << ui.mac->text().toStdString() ; |
| 122 | } |
| 123 | else |
| 124 | return; |
| 125 | } |
| 126 | if (!(ui.mask->text() == "...")) |
| 127 | { |
| 128 | if (validateIp(ui.mask, false, false)) |
| 129 | { |
| 130 | newLineBuff << " -s " << ui.mask->text().toStdString() ; |
| 131 | } |
| 132 | else |
| 133 | return; |
| 134 | } |
| 135 | if (!(ui.guestIP->text() == "...")) |
| 136 | { |
| 137 | if (validateIp(ui.guestIP, false, false)) |
| 138 | { |
| 139 | newLineBuff << " " << ui.guestIP->text().toStdString() ; |
| 140 | } |
| 141 | else |
| 142 | return; |
| 143 | } |
| 144 | if (!(ui.hostIP->text() == "...")) |
| 145 | { |
| 146 | if (validateIp(ui.hostIP, false, false)) |
| 147 | { |
| 148 | newLineBuff << " " << ui.hostIP->text().toStdString() ; |
| 149 | } |
| 150 | else |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | else // qeth |
| 156 | { |
| 157 | if (!(ui.hostIP->text() == "...")) |
| 158 | { |
| 159 | if (validateIp(ui.hostIP, false, false)) |
| 160 | { |
| 161 | newLineBuff << " " << ui.hostIP->text().toStdString() ; |
| 162 | } |
| 163 | else |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | newLineBuff << "\n"; |
| 170 | mLine.replaceLine(newLineBuff.str()); |
| 171 | emit updateLine(true); |
| 172 | this->deleteLater(); |
| 173 | } |
| 174 | |
| 175 | void CtcProperties::cancel() |
| 176 | { |
| 177 | emit updateLine(false); |
| 178 | this->deleteLater(); |
| 179 | } |
| 180 | |
| 181 | bool CtcProperties::macValidator(const std::string& mac) |
| 182 | { |
| 183 | bool ret = true; |
| 184 | if (mac.length() != 17) |
| 185 | ret = false; |
| 186 | QString qmac = mac.c_str(); |
| 187 | for (int i=0; i<6 && ret; i++) |
| 188 | { |
| 189 | int pos = i*3; |
| 190 | if (i>0 && qmac.toStdString().substr(i*3-1,1) != ":") |
| 191 | ret=false; |
| 192 | std::string c1 = qmac.toLower().toStdString().substr(pos,1).c_str(); |
| 193 | std::string c2 = qmac.toLower().toStdString().substr(pos+1,1).c_str(); |
| 194 | if (c1[0] < '0' || c1[0] > 'f' || c2[0] < '0' || c2[0] > 'f') |
| 195 | { |
| 196 | ret=false; |
| 197 | } |
| 198 | } |
| 199 | if (!ret) |
| 200 | { |
| 201 | QMessageBox::warning(this, "Illegal MAC address", "Please provide device name\nhh:hh:hh:hh:hh:hh", QMessageBox::Ok); |
| 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void CtcProperties::typeChanged(const QString &newValue) |
| 209 | { |
| 210 | if (newValue.toLower() == "qeth") arrangeQeth(true); |
| 211 | else if (newValue.toLower() == "lcs") arrangeLcs(true); |
| 212 | else if (newValue.toLower() == "ctct") arrangeCtct(true); |
| 213 | else arrangeCtc(true); |
| 214 | } |
| 215 | |
| 216 | void CtcProperties::arrangeQeth(bool set) |
| 217 | { |
| 218 | hOutDebug(5, "qeth " << (set ? "true" : "false")); |
| 219 | if (set) |
| 220 | { |
| 221 | arrangeCtc(!set); |
| 222 | arrangeCtct(!set); |
| 223 | arrangeLcs(!set); |
| 224 | ui.hostIP->setInputMask("000.000.000.000;"); |
| 225 | } |
| 226 | ui.hostIPLabel->setVisible(set); |
| 227 | ui.hostIP->setVisible(set); |
| 228 | |
| 229 | ui.hostIPLabel->setGeometry(20,50,53,16); |
| 230 | ui.hostIP->setGeometry(110,40,113,26); |
| 231 | } |
| 232 | |
| 233 | void CtcProperties::arrangeLcs(bool set) |
| 234 | { |
| 235 | hOutDebug(5, "lcs " << (set ? "true" : "false") ); |
| 236 | if (set) |
| 237 | { |
| 238 | arrangeCtc(!set); |
| 239 | arrangeCtct(!set); |
| 240 | arrangeQeth(!set); |
| 241 | ui.hostIP->setInputMask("000.000.000.000;"); |
| 242 | } |
| 243 | ui.guestIPLabel->setVisible(set); |
| 244 | ui.guestIP->setVisible(set); |
| 245 | ui.tunLabel->setVisible(set); |
| 246 | ui.tun->setVisible(set); |
| 247 | ui.macLabel->setVisible(set); |
| 248 | ui.mac->setVisible(set); |
| 249 | ui.macLabel2->setVisible(set); |
| 250 | ui.oatFilenameLabel->setVisible(set); |
| 251 | ui.oat->setVisible(set); |
| 252 | ui.oatBrowseButton->setVisible(set); |
| 253 | //ui.oatEditButton->setVisible(set); |
| 254 | |
| 255 | ui.hostIPLabel->setGeometry(20,70,53,16); |
| 256 | ui.hostIP->setGeometry(110,60,113,26); |
| 257 | ui.tunLabel->setGeometry(20,70,81,16); |
| 258 | ui.tun->setGeometry(110,60,301,26); |
| 259 | ui.macLabel->setGeometry(20,110,53,16); |
| 260 | ui.mac->setGeometry(110,100,171,26); |
| 261 | ui.macLabel2->setGeometry(300,100,53,16); |
| 262 | ui.oatFilenameLabel->setGeometry(20,150,91,16); |
| 263 | ui.oat->setGeometry(110,140,211,26); |
| 264 | ui.oatBrowseButton->setGeometry(330,140,21,26); |
| 265 | //ui.oatEditButton->setGeometry(360,140,52,26); |
| 266 | } |
| 267 | |
| 268 | void CtcProperties::arrangeCtc(bool set) |
| 269 | { |
| 270 | bool nonTunSet = set; |
| 271 | hOutDebug(5, "ctc " << (set ? "true" : "false")); |
| 272 | if (set) |
| 273 | { |
| 274 | arrangeLcs(!set); |
| 275 | arrangeCtct(!set); |
| 276 | arrangeQeth(!set); |
| 277 | ui.hostIP->setInputMask("000.000.000.000;"); |
| 278 | } |
| 279 | |
| 280 | ui.tun0CheckBox->setVisible(set); |
| 281 | nonTunSet = !(ui.tun0CheckBox->isChecked()); |
| 282 | |
| 283 | ui.guestIPLabel->setVisible(set); |
| 284 | ui.guestIP->setVisible(set); |
| 285 | ui.maskLabel->setVisible(set); |
| 286 | ui.mask->setVisible(set); |
| 287 | ui.hostIPLabel->setVisible(set); |
| 288 | ui.hostIP->setVisible(set); |
| 289 | ui.tunLabel->setVisible(set); |
| 290 | ui.tun->setVisible(set); |
| 291 | ui.macLabel->setVisible(set); |
| 292 | ui.mac->setVisible(set); |
| 293 | ui.macLabel2->setVisible(set); |
| 294 | |
| 295 | //disable if tun0 is set |
| 296 | ui.guestIPLabel->setEnabled(nonTunSet); |
| 297 | ui.guestIP->setEnabled(nonTunSet); |
| 298 | ui.maskLabel->setEnabled(nonTunSet); |
| 299 | ui.mask->setEnabled(nonTunSet); |
| 300 | ui.hostIPLabel->setEnabled(nonTunSet); |
| 301 | ui.hostIP->setEnabled(nonTunSet); |
| 302 | ui.tunLabel->setEnabled(nonTunSet); |
| 303 | ui.tun->setEnabled(nonTunSet); |
| 304 | ui.macLabel->setEnabled(nonTunSet); |
| 305 | ui.mac->setEnabled(nonTunSet); |
| 306 | ui.macLabel2->setEnabled(nonTunSet); |
| 307 | |
| 308 | ui.hostIPLabel->setGeometry(20,70,53,16); |
| 309 | ui.hostIP->setGeometry(110,60,113,26); |
| 310 | ui.tunLabel->setGeometry(20,110,81,16); |
| 311 | ui.tun->setGeometry(110,100,301,26); |
| 312 | ui.macLabel->setGeometry(20,150,53,16); |
| 313 | ui.mac->setGeometry(110,140,171,26); |
| 314 | ui.macLabel2->setGeometry(300,140,53,16); |
| 315 | ui.tun0CheckBox->setGeometry(250,110,140,24); |
| 316 | } |
| 317 | |
| 318 | void CtcProperties::arrangeCtct(bool set) |
| 319 | { |
| 320 | hOutDebug(3,"ctct " << (set ? "true" : "false")); |
| 321 | if (set) |
| 322 | { |
| 323 | arrangeCtc(!set); |
| 324 | arrangeLcs(!set); |
| 325 | arrangeQeth(!set); |
| 326 | ui.hostIP->setInputMask("000.000.000.000;"); |
| 327 | } |
| 328 | ui.hostIPLabel->setVisible(set); |
| 329 | ui.hostIP->setVisible(set); |
| 330 | ui.hostPortLabel->setVisible(set); |
| 331 | ui.hostPortSpinbox->setVisible(set); |
| 332 | ui.lportLabel->setVisible(set); |
| 333 | ui.lportSpinbox->setVisible(set); |
| 334 | ui.buffsizeLabel->setVisible(set); |
| 335 | ui.buffsizeSpinBox->setVisible(set); |
| 336 | |
| 337 | ui.hostIPLabel->setGeometry(20,30,53,16); |
| 338 | ui.hostIP->setGeometry(110,20,113,26); |
| 339 | ui.hostPortLabel->setGeometry(240,30,61,16); |
| 340 | ui.hostPortSpinbox->setGeometry(320,20,61,26); |
| 341 | ui.lportLabel->setGeometry(20,70,61,16); |
| 342 | ui.lportSpinbox->setGeometry(110,60,61,26); |
| 343 | ui.buffsizeLabel->setGeometry(20,110,71,16); |
| 344 | ui.buffsizeSpinBox->setGeometry(110,100,71,26); |
| 345 | } |
| 346 | |
| 347 | void CtcProperties::arrangeByType() |
| 348 | { |
| 349 | if (ui.typeCombo->currentText().toLower() == "qeth") |
| 350 | { |
| 351 | arrangeQeth(true); |
| 352 | } |
| 353 | else if (ui.typeCombo->currentText().toLower() == "lcs") |
| 354 | { |
| 355 | arrangeLcs(true); |
| 356 | } |
| 357 | else if (ui.typeCombo->currentText().toLower() == "ctct") |
| 358 | { |
| 359 | arrangeCtct(true); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | arrangeCtc(true); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void CtcProperties::oatBrowse() |
| 368 | { |
| 369 | std::string s = QFileDialog::getOpenFileName(this, |
| 370 | "Locate OAT File Name", |
| 371 | Preferences::getInstance().configDir().c_str(), |
| 372 | tr("All files(*)")).toUtf8().data(); |
| 373 | ui.oat->setText(s.c_str()); |
| 374 | } |
| 375 | |
| 376 | void CtcProperties::tun0Check(int) |
| 377 | { |
| 378 | arrangeByType(); |
| 379 | } |
| 380 | |
| 381 | void CtcProperties::initialize(ConfigLine & configLine) |
| 382 | { |
| 383 | if (configLine.size() > 0) |
| 384 | ui.deviceNumber->setText(configLine.getToken(0).c_str()); |
| 385 | |
| 386 | QString type = configLine.getToken(1).c_str(); |
| 387 | if (type.toLower() == "qeth") |
| 388 | { |
| 389 | ui.typeCombo->setCurrentIndex(3); |
| 390 | if (configLine.size() >= 2) |
| 391 | { |
| 392 | ui.hostIP->setText(configLine.getToken(2).c_str()); |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | else if (type.toLower() == "lcs") |
| 397 | { |
| 398 | ui.typeCombo->setCurrentIndex(0); |
| 399 | |
| 400 | for (int i=2; i<configLine.size(); i++) |
| 401 | { |
| 402 | if (configLine.getToken(i) == "-n" || |
| 403 | configLine.getToken(i) == "--dev") |
| 404 | { |
| 405 | ui.tun->setText(configLine.getToken(++i).c_str()); |
| 406 | } |
| 407 | else if (configLine.getToken(i) == "-o" || |
| 408 | configLine.getToken(i) == "--oat") |
| 409 | { |
| 410 | ui.oat->setText(configLine.getToken(++i).c_str()); |
| 411 | } |
| 412 | else if (configLine.getToken(i) == "-m" || |
| 413 | configLine.getToken(i) == "--mac") |
| 414 | { |
| 415 | ui.mac->setText(configLine.getToken(++i).c_str()); |
| 416 | } |
| 417 | else if (i+1 == configLine.size()) |
| 418 | { |
| 419 | ui.guestIP->setText(configLine.getToken(i).c_str()); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | else if (type.toLower() == "ctct") |
| 424 | { |
| 425 | ui.typeCombo->setCurrentIndex(2); |
| 426 | |
| 427 | int port = atoi(configLine.getToken(2).c_str()); |
| 428 | ui.lportSpinbox->setValue(port); |
| 429 | ui.hostIP->setInputMask(""); |
| 430 | ui.hostIP->setText(configLine.getToken(3).c_str()); |
| 431 | port = atoi(configLine.getToken(4).c_str()); |
| 432 | ui.hostPortSpinbox->setValue(port); |
| 433 | if (configLine.size() > 5) |
| 434 | { |
| 435 | int size = atoi(configLine.getToken(5).c_str()); |
| 436 | ui.buffsizeSpinBox->setValue(size); |
| 437 | } |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | ui.typeCombo->setCurrentIndex(1); |
| 442 | |
| 443 | QString tun0; |
| 444 | if (configLine.size() > 2) tun0 = configLine.getToken(2).c_str(); |
| 445 | if (tun0 == "tun0") |
| 446 | { |
| 447 | ui.tun0CheckBox->setChecked(true); |
| 448 | } |
| 449 | else |
| 450 | { |
| 451 | ui.tun0CheckBox->setChecked(false); |
| 452 | for (int i=2; i<configLine.size(); i++) |
| 453 | { |
| 454 | |
| 455 | if (configLine.getToken(i) == "-n" || |
| 456 | configLine.getToken(i) == "--dev") |
| 457 | { |
| 458 | ui.tun->setText(configLine.getToken(++i).c_str()); |
| 459 | } |
| 460 | else if (configLine.getToken(i) == "-m" || |
| 461 | configLine.getToken(i) == "--mac") |
| 462 | { |
| 463 | ui.mac->setText(configLine.getToken(++i).c_str()); |
| 464 | } |
| 465 | else if (configLine.getToken(i) == "-s") |
| 466 | { |
| 467 | ui.mask->setText(configLine.getToken(++i).c_str()); |
| 468 | } |
| 469 | else if (i+1 == configLine.size()) |
| 470 | { |
| 471 | ui.hostIP->setText(configLine.getToken(i).c_str()); |
| 472 | } |
| 473 | else if (i+2 == configLine.size()) |
| 474 | { |
| 475 | ui.guestIP->setText(configLine.getToken(i).c_str()); |
| 476 | } |
| 477 | } |
| 478 | ui.tun0CheckBox->setChecked(false); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | arrangeByType(); |
| 483 | |
| 484 | |
| 485 | } |