-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
585 lines (489 loc) · 14 KB
/
mainwindow.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
snd = new Sender;
ui->ipAddr->setFocus();
ui->datasize->setRange(60,1500);
ui->datasize->setValue(100);
ui->pause->setEnabled(false);
ui->tos_dscp->setChecked(true);
ui->Output->setUniformItemSizes(true);
delimiter = "-----------------------------------------------------------------------------";
ECN = 4; //Explicit Congestion Notification
dscpList[0]=46*ECN; dscpList[1] = 0;
for(int i=0;i<=17;i++){
dscpList[i+2] = (i*2+8)*ECN;
}
dscpList[19] = 48*ECN; dscpList[20] = 56*ECN;
/*rand use for id generate in ip header and for sid generate in icmp header */
ui->sid->setText(QString::number(randomize(65535)));
QObject::connect(snd,SIGNAL(recPacket(long, long, bool, int, int, int, char, short, int)),this, SLOT(rPacket(long, long, bool, int, int, int, char, short, int)));
QObject::connect(snd, SIGNAL(endPing()),this, SLOT(ePing()));
QObject::connect(this, SIGNAL(ab()), snd, SLOT(abrt()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_click()
{
int ival, tmout;
int ct = ui->count->text().toInt();
if(ui->ping->text() == "Ping")
{
ui->Output->clear();
if(makepacket())
return;
ival = ui->inter->text().toInt();
tmout = ui->timeout->text().toInt();
ui->groupBox->setEnabled(false);
ui->groupBox_2->setEnabled(false);
ui->groupBox_3->setEnabled(false);
ui->pause->setEnabled(true);
ui->ping->setText("Stop");
jitter_sum = rtt_sum = pkt_num = pkt_loss = 0;
snd->mystart(ct,pkt,pktsize,tmout,ival,&wait,&mutex);
} else {
if(snd->pause)
{
snd->pause = false;
ui->pause->setText("Pause ||");
snd->abort = true;
wait.wakeAll();
}else{
snd->abort = true;
}
//emit ab();
}
}
int MainWindow::makepacket()
{
MIB_IPFORWARDROW pMib;
PIP_ADAPTER_INFO pAdapterInfo;
DWORD dwRetVal, dwError;
QString hoststr;
u_long host;
char *destination_ip;
QByteArray asc;
u_long best_route;
ULONG mac[2], size = 6;
BYTE *pbyte;
hoststr = ui->ipAddr->text();
asc = hoststr.toAscii();
destination_ip = asc.data();
if(hoststr.isEmpty())
return 1;
/*Getting ip address*/
WSAStartup(MAKEWORD(2,2),&wsaData);
hostip = gethostbyname(destination_ip);
if(hostip == NULL)
{
dwError = WSAGetLastError();
if (dwError == WSAHOST_NOT_FOUND)
{
wsaError.setText("Host not found");
wsaError.exec();
}else{
wsaError.setText("Unknown error");
wsaError.exec();
}
ui->ip->clear();
return 1;
}else{
host = *(u_long *) hostip->h_addr_list[0];
in_addr_struct.S_un.S_addr = host;
destination_ip = inet_ntoa(in_addr_struct);
}
ui->ip->setText(QString(destination_ip));
pktsize = ui->datasize->value();
if((pkt = (u_char *) malloc(pktsize)) == NULL)
return 1;
ethhdr = (eth_header *) pkt;
iphdr = (ip_header *) (pkt + sizeof(eth_header));
icmphdr = (icmp_header *) (pkt + sizeof(eth_header) + sizeof(ip_header));
/*filling data(zero)*/
for(int i=sizeof(eth_header) + sizeof(ip_header) + sizeof(icmp_header);i < pktsize;i++)
pkt[i] = 0;
/*Destination ip building*/
ctoi(destination_ip,&(iphdr->daddr));
/*Getting ip of best route*/
if(GetBestRoute(host, 0, &pMib))
return 1;
/*For local host dwForwardType = 3; for remote host dwForwardType = 4*/
if(pMib.dwForwardType == 3)
best_route = host;
else
best_route = (u_long) pMib.dwForwardNextHop;
/*Source ip and source mac building*/
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
qWarning("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
while (pAdapterInfo) {
if(pAdapterInfo->Index == pMib.dwForwardIfIndex)
{
if(ctoi(pAdapterInfo->IpAddressList.IpAddress.String,&(iphdr->saddr)) != 0)
return 1;
adrtopack(pAdapterInfo->Address,pAdapterInfo->AddressLength, ethhdr, true);
break;
}
pAdapterInfo = pAdapterInfo->Next;
}
}
if(pAdapterInfo == NULL)
return 1;
/*Destination mac building*/
if(SendARP(best_route,NULL,mac,&size))
return 1;
pbyte = (BYTE *) mac;
adrtopack(pbyte, 6, ethhdr, false);
/*test ip and mac adr*/
//testipandmac();
ethhdr->type = 0x8;
if(ui->tos_dscp->isChecked())
{
iphdr->tos = dscpList[ui->dscpBox->currentIndex()];
}else
{
if(!(ui->ipTos->text().isEmpty()))
iphdr->tos = ui->ipTos->text().toInt();
else
iphdr->tos = 0;
}
iphdr->ver_ihl = 0x45;
iphdr->proto = 0x1;
if(!(ui->ipTtl->text().isEmpty()))
iphdr->ttl = ui->ipTtl->text().toInt();
else
iphdr->ttl = 0x80;
iphdr->identification = (randomize(65535));
/*temp*/
if(ui->checkBox->isChecked())
iphdr->flags_fo = 0x40;
else
iphdr->flags_fo = 0;
iphdr->crc = 0;
icmphdr->crc = 0;
icmphdr->type = 0x8;
icmphdr->code = 0;
icmphdr->sid = ui->sid->text().toInt();
icmphdr->sn = ui->sn->text().toInt();
return 0;
}
int MainWindow::ctoi(char *str_ip, ip_address *ipadr)
{
u_char *pbt, bt=0;
pbt = (u_char *) ipadr;
while(*str_ip)
{
switch(*str_ip)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
bt = (bt * 10) + ((*str_ip) - 48);
break;
case '.':
*pbt = bt;
bt = 0;
pbt++;
break;
default:
return 1;
}
str_ip++;
}
*pbt = bt;
return 0;
}
void MainWindow::adrtopack(BYTE *bt, UINT cnt, eth_header *eth, bool sord)
{
u_int i, offset=0;
u_char *chbt;
chbt = (u_char *) eth;
if(sord)
offset = 6;
for(i=0;i<cnt;i++)
chbt[i+offset] = (u_char) bt[i];
}
void MainWindow::testipandmac()
{
u_char i, *ip;
ip =(u_char *) &(iphdr->saddr);
for(i=0;i<4;i++)
{
qWarning("%i.",ip[i]);
}
qWarning("\n");
ip = (u_char *) ethhdr;
for(i=0;i<6;i++)
{
qWarning("%x ", ip[i]);
}
}
void MainWindow::rPacket(long time, long jt, bool timeout, int type, int code, int len, char ttl, short sn, int tos)
{
QString tout = "timeout";
QString tm = QString::number((u_int) len) + \
" bytes. icmp_seq=" + QString::number((u_short) sn) + \
" ttl=" + QString::number((u_char) ttl) + \
" time=" + QString::number((u_long) time) +
"ms jitter=" + QString::number((u_long) jt) + \
" tos=" + QString::number((u_int) tos);
if(timeout)
{
pkt_loss++;
ui->Output->addItem(tout);
ui->Output->scrollToBottom();
return;
}
if(type == 0 && code == 0)
{
getTos(tos);
tm.append(tosCode);
pkt_num++;
rtt_sum += (u_long) time;
jitter_sum += (u_long) jt;
ui->Output->addItem(tm);
ui->Output->scrollToBottom();
return;
}
getIcmpError(type,code);
pkt_loss++;
ui->Output->addItem(err);
ui->Output->scrollToBottom();
return;
}
void MainWindow::ePing()
{
QString mid_res;
u_long pkt_res;
float percent = ((float)pkt_num/((float)pkt_num+(float)pkt_loss))*100;
if(pkt_num == 0)
pkt_res = 1;
else
pkt_res = pkt_num;
mid_res = "success="+QString::number(percent)+"% ("+QString::number(pkt_num)+ "/" + QString::number(pkt_num+pkt_loss)+\
") avg rtt=" + QString::number(rtt_sum/pkt_res) + \
"; avg jitter=" + QString::number(jitter_sum/pkt_res);
free(pkt);
ui->groupBox->setEnabled(true);
ui->groupBox_2->setEnabled(true);
ui->groupBox_3->setEnabled(true);
ui->pause->setEnabled(false);
ui->pause->setText("Pause ||");
ui->ping->setText("Ping");
ui->Output->addItem(delimiter);
ui->Output->addItem(mid_res);
ui->Output->addItem(delimiter);
ui->Output->scrollToBottom();
}
void MainWindow::pause_click()
{
QString mid_res;
u_long pkt_res;
float percent = ((float)pkt_num/((float)pkt_num+(float)pkt_loss))*100;
if(pkt_num == 0)
pkt_res = 1;
else
pkt_res = pkt_num;
mid_res = "success="+QString::number(percent)+"% ("+QString::number(pkt_num)+ "/" + QString::number(pkt_num+pkt_loss)+\
") avg rtt=" + QString::number(rtt_sum/pkt_res) + \
"; avg jitter=" + QString::number(jitter_sum/pkt_res);
if(ui->pause->text() == "Pause ||")
{
snd->pause = true;
ui->pause->setText("Pause >");
ui->Output->addItem(delimiter);
ui->Output->addItem(mid_res);
ui->Output->addItem(delimiter);
ui->Output->scrollToBottom();
} else {
snd->pause = false;
wait.wakeAll();
ui->pause->setText("Pause ||");
}
}
void MainWindow::radio_chg(bool chg)
{
ui->dscpBox->setEnabled(chg);
ui->ipTos->setEnabled(!chg);
return;
}
bool MainWindow::isIP(QString * host)
{
QStringList ipList;
bool ok;
u_long bt;
ipList = host->split(".");
for(int i = 0; i<ipList.size(); ++i)
{
bt = ipList.at(i).toInt(&ok);
if(ok && bt < 255)
continue;
return false;
}
return true;
}
void MainWindow::getIcmpError(int type, int code)
{
err.clear();
switch(type)
{
case 3:
switch(code)
{
case 1:
err.append("Host Unreachable");
break;
case 2:
err.append("Protocol Unreachable");
break;
case 3:
err.append("Port Unreachable");
break;
case 4:
err.append("Fragmentation Needed and Don't Fragment was Set");
break;
default:
err.append("Destination unreacheble");
break;
}
break;
case 11:
switch(code)
{
case 0:
err.append("Time to live exceeded in transit");
break;
case 1:
err.append("Fragment reassembly time exceeded");
break;
default:
err.append("Time Exceeded Message");
break;
}
break;
case 12:
err.append("Parameter Problem Message");
break;
case 4:
err.append("Source Quench Message");
break;
case 5:
switch(code)
{
case 0:
err.append("Redirect datagrams for the Network");
break;
case 1:
err.append("Redirect datagrams for the Host");
break;
case 2:
err.append("Redirect datagrams for the Type of Service and Network");
break;
case 3:
err.append("Redirect datagrams for the Type of Service and Host");
break;
default:
err.append("Redirect Message");
break;
}
break;
default:
err.append("Unknown error. type="+QString::number(type)+" code="+QString::number(code));
}
return;
}
void MainWindow::getTos(int tos)
{
tosCode.clear();
switch(tos)
{
case 32:
tosCode.append(" cs1");
break;
case 40:
tosCode.append(" af11");
break;
case 48:
tosCode.append(" af12");
break;
case 56:
tosCode.append(" af13");
break;
case 64:
tosCode.append(" cs2");
break;
case 72:
tosCode.append(" af21");
break;
case 80:
tosCode.append(" af22");
break;
case 88:
tosCode.append(" af23");
break;
case 96:
tosCode.append(" cs3");
break;
case 104:
tosCode.append(" af31");
break;
case 112:
tosCode.append(" af32");
break;
case 120:
tosCode.append(" af33");
break;
case 128:
tosCode.append(" cs4");
break;
case 136:
tosCode.append(" af41");
break;
case 144:
tosCode.append(" af42");
break;
case 152:
tosCode.append(" af43");
break;
case 160:
tosCode.append(" cs5");
break;
case 184:
tosCode.append(" ef");
break;
case 192:
tosCode.append(" cs6");
break;
case 224:
tosCode.append(" cs7");
break;
default:
tosCode.append(" ");
}
}
int MainWindow::randomize(int max)
{
qsrand(QTime::currentTime().msec());
return (rand() % max);
}