diff --git a/src/main.cpp b/src/main.cpp index 7a808500..c7ec3341 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -274,8 +274,8 @@ int main(int argc, char *argv[]) "name"); parser.addOption(nameOption); - QCommandLineOption wakeOption(QStringList() << "wake" << "Send Wake-On-LAN / Magic Packet to and ."); - parser.addOption(wakeOption); + QCommandLineOption wolOption(QStringList() << "wol", "Send Wake-On-LAN / Magic Packet to and (optional) .", "mac"); + parser.addOption(wolOption); @@ -328,7 +328,7 @@ int main(int argc, char *argv[]) bool ipv4 = parser.isSet(bindIPv4Option); bool http = parser.isSet(httpOption); - bool wake = parser.isSet(wakeOption); + bool wol = parser.isSet(wolOption); bool okbps = false; bool okrate = false; @@ -372,22 +372,32 @@ int main(int argc, char *argv[]) QString data, dataString; data.clear(); dataString.clear(); - if (argssize >= 1) { - address = args[0]; - } - - if(wake) { - if (argssize >= 2) { - port = QString(args[1]).toUInt(); + if(wol) { + QString targetMAC = parser.value(wolOption).trimmed().toUpper(); + if (argssize >= 1) { + port = QString(args[0]).toUInt(); } if(port < 1) { port = 7; } - Packet wakePkt = Packet::generateWakeOnLAN(address, port); - OUTIF() << wakePkt.asciiString(); - OUTPUT() - return 0; + Packet wolPkt = Packet::generateWakeOnLAN(targetMAC, port); + + if(wolPkt.errorString.isEmpty()) { + OUTIF() << "Sending broadcast Wake-On-LAN to target: " + targetMAC + " on port " + QString::number(port); + udp = true; + tcp = false; + ssl = false; + http = false; + address = wolPkt.toIP; + data = wolPkt.hexString; + + } else { + OUTIF() << "Error generating Wake-On-LAN: " + wolPkt.errorString; + OUTPUT(); + return -1; + } + } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c606d300..f3b2448e 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2788,13 +2788,35 @@ void MainWindow::on_udptcpComboBox_currentIndexChanged(int index) void MainWindow::on_actionWake_On_LAN_Magic_Packet_triggered() { + static QString previousMAC = ""; + static int previousPortIndex = 0; WakeOnLAN wol = WakeOnLAN(); + wol.setTarget(previousMAC, previousPortIndex); wol.setModal(true); wol.exec(); + if(!wol.generatedPacket.errorString.isEmpty()) { + + QMessageBox msgBox; + msgBox.setWindowIcon(QIcon(":pslogo.png")); + msgBox.setWindowTitle(tr("Invalid MAC Address.")); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.setIcon(QMessageBox::Critical); + msgBox.setText(wol.generatedPacket.errorString); + msgBox.setModal(true); + msgBox.exec(); + + on_actionWake_On_LAN_Magic_Packet_triggered(); + return; + } + if(wol.generatedPacket.toIP.isEmpty()) { return; } + previousMAC = wol.mac; + previousPortIndex = wol.portIndex; + ui->packetIPEdit->setText(wol.generatedPacket.toIP); ui->packetPortEdit->setText(QString::number(wol.generatedPacket.port)); int findtext = ui->udptcpComboBox->findText(wol.generatedPacket.tcpOrUdp); @@ -2802,7 +2824,12 @@ void MainWindow::on_actionWake_On_LAN_Magic_Packet_triggered() ui->udptcpComboBox->setCurrentIndex(findtext); } ui->packetHexEdit->setText(wol.generatedPacket.hexString); + on_packetHexEdit_editingFinished(); on_udptcpComboBox_currentIndexChanged(""); + if(ui->packetNameEdit->text().isEmpty()) { + ui->packetNameEdit->setFocus(); + } + } diff --git a/src/packet.cpp b/src/packet.cpp index 7993b08a..9bcf07b4 100755 --- a/src/packet.cpp +++ b/src/packet.cpp @@ -596,7 +596,7 @@ void Packet::saveToDB() } -Packet Packet::generateWakeOnLAN(QString mac, int port) +Packet Packet::generateWakeOnLAN(QString &mac, int port) { /* * The Wake-On-LAN / Magic Packet format is as follows: @@ -620,8 +620,22 @@ Packet Packet::generateWakeOnLAN(QString mac, int port) } QByteArray macBytes = HEXtoByteArray(mac); - macBytes.truncate(6); - wakeBytes.append(macBytes); + + if(macBytes.size() != 6) { + QDEBUG() << "Received an invalid MAC address" << mac; + wakeOnLAN.init(); + wakeOnLAN.errorString = "Received an invalid MAC address"; + return wakeOnLAN; + } + + // Corrected mac format + QString correctedMAC = Packet::byteArrayToHex(macBytes).trimmed(); + correctedMAC.replace(" ", ":"); + mac = correctedMAC; + + for(int i=0; i<16; i++) { + wakeBytes.append(macBytes); + } wakeOnLAN.hexString = byteArrayToHex(wakeBytes); diff --git a/src/packet.h b/src/packet.h index 907cbc9f..d20dccd5 100755 --- a/src/packet.h +++ b/src/packet.h @@ -85,7 +85,7 @@ class Packet void saveToDB(); - static Packet generateWakeOnLAN(QString mac, int port); + static Packet generateWakeOnLAN(QString &mac, int port); static Packet fetchFromDB(QString thename); static QList fetchAllfromDB(QString importFile); diff --git a/src/wakeonlan.cpp b/src/wakeonlan.cpp index c969be67..fbaae181 100644 --- a/src/wakeonlan.cpp +++ b/src/wakeonlan.cpp @@ -9,16 +9,31 @@ WakeOnLAN::WakeOnLAN(QWidget *parent) : ui(new Ui::WakeOnLAN) { ui->setupUi(this); + + mac.clear(); + portIndex = 0; + + } + WakeOnLAN::~WakeOnLAN() { delete ui; } +void WakeOnLAN::setTarget(QString mac, int portIndex) +{ + ui->macEdit->setText(mac); + ui->portBox->setCurrentIndex(portIndex); + +} + void WakeOnLAN::on_buttonBox_accepted() { - generatedPacket = Packet::generateWakeOnLAN(ui->macEdit->text(), ui->portBox->currentText().toUInt()); + mac = ui->macEdit->text().trimmed().toUpper(); + portIndex = ui->portBox->currentIndex(); + generatedPacket = Packet::generateWakeOnLAN(mac, ui->portBox->currentText().toUInt()); } diff --git a/src/wakeonlan.h b/src/wakeonlan.h index 2fd0bf8a..7fb867dd 100644 --- a/src/wakeonlan.h +++ b/src/wakeonlan.h @@ -16,7 +16,10 @@ class WakeOnLAN : public QDialog public: explicit WakeOnLAN(QWidget *parent = nullptr); ~WakeOnLAN(); + void setTarget(QString mac, int portIndex); Packet generatedPacket; + QString mac; + int portIndex; private slots: void on_buttonBox_accepted(); diff --git a/src/wakeonlan.ui b/src/wakeonlan.ui index 18fdffce..ce0f6c86 100644 --- a/src/wakeonlan.ui +++ b/src/wakeonlan.ui @@ -6,12 +6,12 @@ 0 0 - 400 + 454 143 - Dialog + Wake-On-LAN @@ -19,7 +19,7 @@ - MAC + Target MAC @@ -39,15 +39,38 @@ + + true + + + + 0 + 0 + + + + + 80 + 20 + + 7 + + + :/icons/outline_lightbulb_white_24dp.png:/icons/outline_lightbulb_white_24dp.png + 9 + + + :/icons/outline_lightbulb_white_24dp.png:/icons/outline_lightbulb_white_24dp.png + @@ -65,7 +88,9 @@ - + + + buttonBox