-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwolTool.cpp
More file actions
155 lines (130 loc) · 3.63 KB
/
Copy pathwolTool.cpp
File metadata and controls
155 lines (130 loc) · 3.63 KB
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
#include "wolTool.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QUdpSocket>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->setWindowTitle(tr("wolTool"));
this->setWindowIcon(QIcon(":/gu.jpg"));
qDebug() << this->windowTitle();
readConfigFile();
ui->setupUi(this);
ui->lineEdit_mac->setInputMask("HH:HH:HH:HH:HH:HH");
ui->lineEdit_mac->setText(mac);
ui->lineEdit_doman->setText(domain);
ui->lineEdit_ip->setText(ip);
ui->lineEdit_port->setText(QString::number(port));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_send_clicked()
{
mac = ui->lineEdit_mac->text();
domain = ui->lineEdit_doman->text();
QHostInfo info;
if(domain.isEmpty() == false)
info = QHostInfo::fromName(domain);
if(info.addresses().isEmpty() == false)
{
ip = info.addresses().first().toString();
ui->lineEdit_ip->setText(ip);
}
else
{
qDebug()<<"domain canot parse";
}
ip = ui->lineEdit_ip->text();
// ip = "172.16.42.199";
port = ui->lineEdit_port->text().toInt();
qDebug() << "mac is " << mac;
qDebug() << "domain is " << domain;
qDebug() << "ip address is " << ip;
qDebug() << "port is " << port;
QString data;
data.append("FFFFFFFFFFFF");
mac.remove(':');
for(int i=0;i<16;i++)
data.append(mac);
qDebug() << data;
QByteArray senddata;
StringToHex(data,senddata);
qDebug() << senddata;
QUdpSocket mSocket;
int ret;
if(info.addresses().isEmpty() == false)
ret = mSocket.writeDatagram(senddata, QHostAddress(ip), port);
else
ret = mSocket.writeDatagram(senddata, QHostAddress::Broadcast, port);
if(ret)
{
QString info;
info = QString("send a magic packet to %1:%2 with %3 bytes via UDP ").arg(ip.toLatin1().data()).arg(port).arg(ret);
info.append('\n');
// info.append(data);
qDebug() << "send magic packet to" << ip.toLatin1().data() << ":" << port << ret << "bytes via UDP";
QMessageBox::about(NULL, NULL, info);
}
else
{
}
}
void MainWindow::StringToHex(QString str, QByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len/2);
char lstr,hstr;
for(int i=0; i<len; )
{
//char lstr,
hstr=str[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
}
char MainWindow::ConvertHexChar(char ch)
{
if((ch >= '0') && (ch <= '9'))
return ch-0x30;
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+10;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+10;
else return (-1);
}
void MainWindow::readConfigFile()
{
configFile = new QSettings("config.ini", QSettings::IniFormat);
qDebug() << configFile->fileName();
mac = configFile->value("/sys/mac").toString();
domain = configFile->value("/sys/domain").toString();
ip = configFile->value("/sys/ip").toString();
port = configFile->value("/sys/port").toInt();
}
void MainWindow::on_pushButton_send_2_clicked()
{
exit(0);
}