2
* packETH - ethernet packet generator
3
* By Miha Jemec <m.jemec@iskratel.si>
4
* Copyright 2003 Miha Jemec, Iskratel
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* function_send.c - routines for sending packet
32
#include <sys/types.h>
35
#include <netpacket/packet.h>
36
#include <net/ethernet.h>
37
#include <netinet/in.h>
38
#include <sys/ioctl.h>
41
#include "callbacks.h"
42
#include "function_send.h"
48
extern unsigned char packet[1522];
49
extern gboolean stop_flag;
50
//extern char iftext[20];
81
unsigned char pkttable[10][1518];
82
long int partable[10][6];
88
int packet_go_on_the_link(unsigned char *pkt, int nr)
91
struct sockaddr_ll sa;
95
/* do we have the rights to do that? */
96
if (getuid() && geteuid()) {
97
//printf("Sorry but need the su rights!\n");
98
error("Sorry but need the su rights!");
102
/* open socket in raw mode */
103
fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
105
//printf("Error: Could not open socket!\n");
106
error("Error: Could not open socket!");
110
/* which interface would you like to use? */
111
memset(&ifr, 0, sizeof(ifr));
112
strncpy (ifr.ifr_name, iftext, sizeof(ifr.ifr_name) - 1);
113
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
115
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
116
//printf("No such interface: %s\n", iftext);
117
snprintf(buff, 100, "No such interface: %s", iftext);
123
/* is the interface up? */
124
ioctl(fd, SIOCGIFFLAGS, &ifr);
125
if ( (ifr.ifr_flags & IFF_UP) == 0) {
126
//printf("Interface %s is down\n", iftext);
127
snprintf(buff, 100, "Interface %s is down", iftext);
133
/* just write in the structure again */
134
ioctl(fd, SIOCGIFINDEX, &ifr);
136
/* well we need this to work */
137
memset(&sa, 0, sizeof (sa));
138
sa.sll_family = AF_PACKET;
139
sa.sll_ifindex = ifr.ifr_ifindex;
140
sa.sll_protocol = htons(ETH_P_ALL);
142
c = sendto(fd, pkt, nr, 0, (struct sockaddr *)&sa, sizeof (sa));
144
//printf("There were %d bytes sent on the wire (in case of an error we get -1)\n", c);
146
if (close(fd) == 0) {
150
//printf("Warning! close(fd) returned -1!\n");
151
error("Warning! close(fd) returned -1!");
158
/* thread for sending packets */
159
void* sendbuilt (void *parameters)
161
/* YYY check if li,... are long enough if inifinite number will be sent. Maybe put them into double */
162
long li, gap = 0, gap2 = 0, sentnumber = 0, lastnumber = 0, seconds = 0;
163
struct timeval nowstr, first, last;
164
int c, fd, odd=0, actualnumber;
165
unsigned int mbps, pkts, link;
166
unsigned long xc=0, yc=0;
168
struct sockaddr_ll sa;
170
guint32 ipcks, pseudo_header, udpcksum, tcpcksum;
173
struct params* p = (struct params*) parameters;
175
/* do we have the rights to do that? */
176
if (getuid() && geteuid()) {
177
//printf("Sorry but need the su rights!\n");
178
gdk_threads_enter ();
179
//gtk_widget_set_sensitive (p->button1, TRUE);
180
//gtk_widget_set_sensitive (p->button2, TRUE);
181
//gtk_widget_set_sensitive (p->button3, TRUE);
182
//gtk_widget_set_sensitive (p->button4, TRUE);
183
//gtk_widget_set_sensitive (p->button5, TRUE);
184
//gtk_widget_set_sensitive (p->stopbt, FALSE);
186
snprintf(buff, 100, " Problems with sending");
187
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
189
error("Sorry but need the su rights!");
190
gdk_flush();gdk_threads_leave ();
194
/* open socket in raw mode */
195
fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
197
//printf("Error: Could not open socket!\n");
198
gdk_threads_enter ();
199
//gtk_widget_set_sensitive (p->button1, TRUE);
200
//gtk_widget_set_sensitive (p->button2, TRUE);
201
//gtk_widget_set_sensitive (p->button3, TRUE);
202
//gtk_widget_set_sensitive (p->button4, TRUE);
203
//gtk_widget_set_sensitive (p->button5, TRUE);
204
//gtk_widget_set_sensitive (p->stopbt, FALSE);
206
snprintf(buff, 100, " Problems with sending");
207
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
209
error("Error: Could not open socket!");
210
gdk_flush();gdk_threads_leave ();
214
/* which interface would you like to use? */
215
memset(&ifr, 0, sizeof(ifr));
216
strncpy (ifr.ifr_name, iftext, sizeof(ifr.ifr_name) - 1);
217
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
219
/* does the interface exists? */
220
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
221
//printf("No such interface: %s\n", iftext);
222
gdk_threads_enter ();
223
//gtk_widget_set_sensitive (p->button1, TRUE);
224
//gtk_widget_set_sensitive (p->button2, TRUE);
225
//gtk_widget_set_sensitive (p->button3, TRUE);
226
//gtk_widget_set_sensitive (p->button4, TRUE);
227
//gtk_widget_set_sensitive (p->button5, TRUE);
228
//gtk_widget_set_sensitive (p->stopbt, FALSE);
230
snprintf(buff, 100, " Problems with sending");
231
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
233
snprintf(buff, 100, "No such interface: %s", iftext);
235
gdk_flush();gdk_threads_leave ();
240
/* is the interface up? */
241
ioctl(fd, SIOCGIFFLAGS, &ifr);
242
if ( (ifr.ifr_flags & IFF_UP) == 0) {
243
//printf("Interface %s is down\n", iftext);
244
gdk_threads_enter ();
245
//gtk_widget_set_sensitive (p->button1, TRUE);
246
//gtk_widget_set_sensitive (p->button2, TRUE);
247
//gtk_widget_set_sensitive (p->button3, TRUE);
248
//gtk_widget_set_sensitive (p->button4, TRUE);
249
//gtk_widget_set_sensitive (p->button5, TRUE);
250
//gtk_widget_set_sensitive (p->stopbt, FALSE);
252
snprintf(buff, 100, " Problems with sending");
253
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
255
snprintf(buff, 100, "Interface %s is down", iftext);
257
gdk_flush();gdk_threads_leave ();
262
/* just write in the structure again */
263
ioctl(fd, SIOCGIFINDEX, &ifr);
265
/* well we need this to work, don't ask me what is it about */
266
memset(&sa, 0, sizeof (sa));
267
sa.sll_family = AF_PACKET;
268
sa.sll_ifindex = ifr.ifr_ifindex;
269
sa.sll_protocol = htons(ETH_P_ALL);
271
/* this is the time we started */
272
gettimeofday(&first, NULL);
273
gettimeofday(&last, NULL);
274
gettimeofday(&nowstr, NULL);
276
/* to send first packet immedialtelly */
279
/* if packet is shorter than 60 bytes, we need real packet length for calculating checksum,
280
* we use actualnumber for this */
281
actualnumber = number;
286
gtk_widget_set_sensitive (p->button1, FALSE);
287
gtk_widget_set_sensitive (p->button2, FALSE);
288
gtk_widget_set_sensitive (p->button3, FALSE);
289
gtk_widget_set_sensitive (p->button4, FALSE);
290
gtk_widget_set_sensitive (p->button5, FALSE);
291
gtk_widget_set_sensitive (p->button6, FALSE);
292
// gtk_widget_set_sensitive (p->stopbt, TRUE);
294
/* we check with == -3 if the infinite option was choosed */
295
for(li = 0; p->count == -3 ? : li < p->count; li++) {
296
while (gap < p->del) {
297
gettimeofday(&nowstr, NULL);
299
if (p->timeflag == 1)
300
gap = (nowstr.tv_sec*1000000 + nowstr.tv_usec) -
301
(last.tv_sec*1000000 + last.tv_usec);
303
gap = (nowstr.tv_sec*1000000 + nowstr.tv_usec) -
304
(first.tv_sec*1000000 + first.tv_usec) - ( (li - 1) * p->del);
306
gap2 = nowstr.tv_sec - first.tv_sec;
308
/* if the flag is set - the user clicked the stop button, we quit */
309
if (stop_flag == 1) {
310
gdk_threads_enter ();
312
gtk_widget_set_sensitive (p->button1, TRUE);
313
gtk_widget_set_sensitive (p->button2, TRUE);
314
gtk_widget_set_sensitive (p->button3, TRUE);
315
gtk_widget_set_sensitive (p->button4, TRUE);
316
gtk_widget_set_sensitive (p->button5, TRUE);
317
gtk_widget_set_sensitive (p->button6, TRUE);
318
// gtk_widget_set_sensitive (p->stopbt, FALSE);
320
snprintf(buff, 100, " Sent %ld packets on %s", sentnumber, iftext);
321
gtk_statusbar_push(GTK_STATUSBAR(p->button),
322
GPOINTER_TO_INT(p->context_id), buff);
324
gdk_flush();gdk_threads_leave ();
330
c = sendto(fd, packet, number, 0, (struct sockaddr *)&sa, sizeof (sa));
332
//printf("There were %d bytes sent on the wire (in case of an error we get -1)\n", c);
334
last.tv_sec = nowstr.tv_sec;
335
last.tv_usec = nowstr.tv_usec;
343
/* update the status bar every second and display number of sent packets */
344
if (gap2 > seconds) {
345
gdk_threads_enter ();
347
pkts = sentnumber - lastnumber;
348
mbps = pkts * number / 128; // 8 bits per byte / 1024 for kbit
349
/* +12 bytes for interframe gap time and 12 for preamble, sfd and checksum */
350
link = pkts * (number + 24) / 128;
351
lastnumber = sentnumber;
353
snprintf(buff, 100, " Sent %ld packets on %s (%d packets/s, %d kbit/s data rate, %d kbit/s link utilization)", sentnumber, iftext, pkts, mbps, link);
354
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
356
gdk_flush();gdk_threads_leave ();
361
/* do we need to change any fields */
363
/* changing source MAC address */
365
/*packet[6] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));*/
366
packet[7] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
367
packet[8] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
368
packet[9] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
369
packet[10] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
370
packet[11] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
373
/* change source IP address - make it random
374
* and correct IP checksum */
376
packet[p->ipstart] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
377
packet[p->ipstart+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
378
packet[p->ipstart+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
379
packet[p->ipstart+3] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
380
/* we have to correct the ip checksum
381
* first we set 0x00 in both fields and then recalculate it */
382
packet[p->ipstart-2] = 0x00;
383
packet[p->ipstart-1] = 0x00;
384
ipcks = ((-1) - get_checksum16(p->ipstart-12, p->ipstart+7) % 0x10000);
385
packet[p->ipstart-2] = (char)(ipcks/256);
386
packet[p->ipstart-1] = (char)(ipcks%256);
389
/* source MAC address and source IP address
390
* and correct IP checksum */
392
/*packet[6] = 1+(int) (16.0*rand()/(RAND_MAX+1.0));*/
393
packet[7] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
394
packet[8] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
395
packet[9] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
396
packet[10] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
397
packet[11] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
398
packet[p->ipstart] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
399
packet[p->ipstart+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
400
packet[p->ipstart+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
401
packet[p->ipstart+3] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
402
/* we have to correct the ip checksum
403
* first we set 0x00 in both fields and then recalculate it */
404
packet[p->ipstart-2] = 0x00;
405
packet[p->ipstart-1] = 0x00;
406
ipcks = ((-1) - get_checksum16(p->ipstart-12, p->ipstart+7) % 0x10000);
407
packet[p->ipstart-2] = (char)(ipcks/256);
408
packet[p->ipstart-1] = (char)(ipcks%256);
411
/* for arp reply messages, change source MAC (ethernet part) *
412
* sender MAC and sender IP (arp part) */
414
//packet[p->ethstart] = 1+(int) (16.0*rand()/(RAND_MAX+1.0));
415
packet[p->ethstart+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
416
packet[p->ethstart+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
417
packet[p->ethstart+3] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
418
packet[p->ethstart+4] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
419
packet[p->ethstart+5] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
420
packet[p->ethstart+6] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
421
packet[p->ethstart+7] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
422
packet[p->ethstart+8] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
423
packet[p->ethstart+9] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
424
//packet[6] = packet[p->ethstart];
425
packet[7] = packet[p->ethstart+1];
426
packet[8] = packet[p->ethstart+2];
427
packet[9] = packet[p->ethstart+3];
428
packet[10] = packet[p->ethstart+4];
429
packet[11] = packet[p->ethstart+5];
433
/* set random source TCP port and IP source address (syn flood) */
435
packet[p->ipstart] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
436
packet[p->ipstart+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
437
packet[p->ipstart+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
438
packet[p->ipstart+3] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
439
packet[p->tcpstart] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
440
packet[p->tcpstart+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
441
/* we have to correct the ip checksum
442
* first we set 0x00 in both fields and then recalculate it */
443
packet[p->ipstart-2] = 0x00;
444
packet[p->ipstart-1] = 0x00;
445
ipcks = ((-1) - get_checksum16(p->ipstart-12, p->ipstart+7) % 0x10000);
446
packet[p->ipstart-2] = (char)(ipcks/256);
447
packet[p->ipstart-1] = (char)(ipcks%256);
450
/* increase the udp first payload byte value by one */
452
packet[p->udpstart]++;
455
/* changing RTP values: seq number++, timestamp for 10ms */
457
packet[p->udpstart+2] = (li+1)/256;
458
packet[p->udpstart+3] = (li+1)%256;
459
packet[p->udpstart+4] = ((li+1)*80)/16777216;
460
packet[p->udpstart+5] = ((li+1)*80)/65536;
461
packet[p->udpstart+6] = ((li+1)*80)/256;
462
packet[p->udpstart+7] = (signed int)(((li+1)*80)%256);
465
/* changing RTP values: seq number++, timestamp for 20ms */
467
packet[p->udpstart+2] = (li+1)/256;
468
packet[p->udpstart+3] = (li+1)%256;
469
packet[p->udpstart+4] = ((li+1)*160)/16777216;
470
packet[p->udpstart+5] = ((li+1)*160)/65536;
471
packet[p->udpstart+6] = ((li+1)*160)/256;
472
packet[p->udpstart+7] = (signed int)(((li+1)*160)%256);
475
/* changing RTP values: seq number++, timestamp for 30ms */
477
packet[p->udpstart+2] = (li+1)/256;
478
packet[p->udpstart+3] = (li+1)%256;
479
packet[p->udpstart+4] = ((li+1)*240)/16777216;
480
packet[p->udpstart+5] = ((li+1)*240)/65536;
481
packet[p->udpstart+6] = ((li+1)*240)/256;
482
packet[p->udpstart+7] = (signed int)(((li+1)*240)%256);
485
/* changing byte x value */
487
/* increment it within specified range */
488
if (p->xchange == 1) {
489
if (xc < (p->xrange)) {
490
stevec32 = (guint32*) &packet[p->xbyte-1];
495
memcpy(&packet[p->xbyte-1], p->xstart, 4);
499
/* decrement it within specified range */
500
else if (p->xchange == 2) {
501
if (xc < (p->xrange)) {
502
stevec32 = (guint32*) &packet[p->xbyte-1];
507
memcpy(&packet[p->xbyte-1], p->xstart, 4);
512
else if (p->xchange == 0)
513
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
515
else if (p->xchange == 3) {
516
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
517
packet[p->xbyte-0] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
520
else if (p->xchange == 4) {
521
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
522
packet[p->xbyte] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
523
packet[p->xbyte+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
526
else if (p->xchange == 5) {
527
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
528
packet[p->xbyte] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
529
packet[p->xbyte+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
530
packet[p->xbyte+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
534
packet[p->ipstart-2] = 0x00;
535
packet[p->ipstart-1] = 0x00;
536
ipcks = ((-1) - get_checksum16(p->ipstart-12, p->ipstart+7) % 0x10000);
537
packet[p->ipstart-2] = (char)(ipcks/256);
538
packet[p->ipstart-1] = (char)(ipcks%256);
543
/* change byte x and y */
545
/* byte x, increment */
546
if (p->xchange == 1) {
547
if (xc < (p->xrange)) {
548
stevec32 = (guint32*) &packet[p->xbyte-1];
553
memcpy(&packet[p->xbyte-1], p->xstart, 4);
557
/* decrement it within specified range */
558
else if (p->xchange == 2) {
559
if (xc < (p->xrange)) {
560
stevec32 = (guint32*) &packet[p->xbyte-1];
565
memcpy(&packet[p->xbyte-1], p->xstart, 4);
570
else if (p->xchange == 0)
571
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
573
else if (p->xchange == 3) {
574
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
575
packet[p->xbyte-0] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
578
else if (p->xchange == 4) {
579
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
580
packet[p->xbyte] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
581
packet[p->xbyte+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
584
else if (p->xchange == 5) {
585
packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
586
packet[p->xbyte] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
587
packet[p->xbyte+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
588
packet[p->xbyte+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
591
/* byte y increment */
592
if (p->ychange == 1) {
593
if (yc < (p->yrange)) {
594
stevec32 = (guint32*) &packet[p->ybyte-1];
599
memcpy(&packet[p->ybyte-1], p->ystart, 4);
603
/* decrement it within specified range */
604
else if (p->ychange == 2) {
605
if (yc < (p->yrange)) {
606
stevec32 = (guint32*) &packet[p->ybyte-1];
611
memcpy(&packet[p->ybyte-1], p->ystart, 4);
616
else if (p->ychange == 0)
617
packet[p->ybyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
619
else if (p->ychange == 3) {
620
packet[p->ybyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
621
packet[p->ybyte-0] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
624
else if (p->ychange == 4) {
625
packet[p->ybyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
626
packet[p->ybyte] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
627
packet[p->ybyte+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
630
else if (p->ychange == 5) {
631
packet[p->ybyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
632
packet[p->ybyte] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
633
packet[p->ybyte+1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
634
packet[p->ybyte+2] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
638
packet[p->ipstart-2] = 0x00;
639
packet[p->ipstart-1] = 0x00;
640
ipcks = ((-1) - get_checksum16(p->ipstart-12, p->ipstart+7) % 0x10000);
641
packet[p->ipstart-2] = (char)(ipcks/256);
642
packet[p->ipstart-1] = (char)(ipcks%256);
649
/* correct the UDP checksum value???
650
* we do it (udp checksum in not mandatory (if set to 0x00)) */
652
packet[p->udpstart-2] = (char)(0);
653
packet[p->udpstart-1] = (char)(0);
654
pseudo_header = (guint32)(packet[p->ipstart-3]);
655
pseudo_header = pseudo_header + get_checksum32(p->ipstart,p->ipstart+7);
656
udpcksum = (guint32)(actualnumber - p->udpstart +8);
658
/* pseudo header (ip part) + udplength + nr of cicles over guint16 */
659
udpcksum = pseudo_header + udpcksum;
661
/* what if length is odd */
662
if( (actualnumber - p->udpstart)%2 != 0)
664
/* previos value + part from udp checksum */
665
udpcksum = udpcksum + get_checksum32(p->udpstart-8, actualnumber+odd);
666
while (udpcksum >> 16)
667
udpcksum = (udpcksum & 0xFFFF)+(udpcksum >> 16);
669
/* the one's complement */
670
udpcksum = (-1) - udpcksum;
673
packet[p->udpstart-2] = (char)(udpcksum/256);
674
packet[p->udpstart-1] = (char)(udpcksum%256);
677
else if (p->type == 3) {
678
packet[p->tcpstart+16] = (char)(0);
679
packet[p->tcpstart+17] = (char)(0);
680
pseudo_header = (guint32)(packet[p->ipstart-3]);
681
pseudo_header = pseudo_header + get_checksum32(p->ipstart,p->ipstart+7);
682
tcpcksum = (guint32)(actualnumber - p->tcpstart);
683
/* pseudo header (ip part) + tcplength + nr of cicles over guint16 */
684
tcpcksum = pseudo_header + tcpcksum;
685
/* what if length is odd */
686
if( (actualnumber - p->tcpstart)%2 != 0)
688
/* previos value + part from tcp checksum */
689
tcpcksum = tcpcksum + get_checksum32(p->tcpstart, actualnumber+odd);
690
while (tcpcksum >> 16)
691
tcpcksum = (tcpcksum & 0xFFFF) + (tcpcksum >> 16);
692
/* the one's complement */
693
tcpcksum = (-1) - tcpcksum;
695
packet[p->tcpstart+16] = (char)(tcpcksum/256);
696
packet[p->tcpstart+17] = (char)(tcpcksum%256);
700
/* we sent all the packets, let's activate the toolbar */
701
gdk_threads_enter ();
703
gtk_widget_set_sensitive (p->button1, TRUE);
704
gtk_widget_set_sensitive (p->button2, TRUE);
705
gtk_widget_set_sensitive (p->button3, TRUE);
706
gtk_widget_set_sensitive (p->button4, TRUE);
707
gtk_widget_set_sensitive (p->button5, TRUE);
708
gtk_widget_set_sensitive (p->button6, TRUE);
709
// gtk_widget_set_sensitive (p->stopbt, FALSE);
711
snprintf(buff, 100, " Sent %ld packets on %s", sentnumber, iftext);
712
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
714
if (close(fd) != 0) {
715
//printf("Warning! close(fd) returned -1!\n");
716
error("Warning! close(fd) returned -1!");
719
gdk_flush();gdk_threads_leave ();
726
/* thread for sending sequences */
727
void* sendsequence (void *parameters)
730
/* YYY check if li,... are long enough if inifinite number will be sent. Maybe put them into double */
731
long li2, li, gap = 0, sentnumber = 0, seconds = 0, gap3 = 0, gap4 = 0;
732
struct timeval nowstr, nowstr1, first, first1, last, last1;
735
struct sockaddr_ll sa;
738
struct params* p = (struct params*) parameters;
740
/* do we have the rights to do that? */
741
if (getuid() && geteuid()) {
742
//printf("Sorry but need the su rights!\n");
743
gdk_threads_enter ();
745
snprintf(buff, 100, " Problems with sending");
746
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
748
error("Sorry but need the su rights!");
749
gdk_flush();gdk_threads_leave ();
753
/* open socket in raw mode */
754
fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
756
//printf("Error: Could not open socket!\n");
757
gdk_threads_enter ();
759
snprintf(buff, 100, " Problems with sending");
760
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
762
error("Error: Could not open socket!");
763
gdk_flush();gdk_threads_leave ();
767
/* which interface would you like to use? */
768
memset(&ifr, 0, sizeof(ifr));
769
strncpy (ifr.ifr_name, iftext, sizeof(ifr.ifr_name) - 1);
770
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
772
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
773
//printf("No such interface: %s\n", iftext);
774
gdk_threads_enter ();
776
snprintf(buff, 100, " Problems with sending");
777
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
779
snprintf(buff, 100, "No such interface: %s", iftext);
781
gdk_flush();gdk_threads_leave ();
786
/* is the interface up? */
787
ioctl(fd, SIOCGIFFLAGS, &ifr);
788
if ( (ifr.ifr_flags & IFF_UP) == 0) {
789
//printf("Interface %s is down\n", iftext);
790
gdk_threads_enter ();
792
snprintf(buff, 100, " Problems with sending");
793
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
795
snprintf(buff, 100, "Interface %s is down", iftext);
797
gdk_flush();gdk_threads_leave ();
802
/* just write in the structure again */
803
ioctl(fd, SIOCGIFINDEX, &ifr);
805
/* well we need this to work, don't ask me what is it about */
806
memset(&sa, 0, sizeof (sa));
807
sa.sll_family = AF_PACKET;
808
sa.sll_ifindex = ifr.ifr_ifindex;
809
sa.sll_protocol = htons(ETH_P_ALL);
811
/* this is the time we started */
812
gettimeofday(&first, NULL);
814
/* to start first sequence immedialtelly */
817
gtk_widget_set_sensitive (p->button1, FALSE);
818
gtk_widget_set_sensitive (p->button2, FALSE);
819
gtk_widget_set_sensitive (p->button3, FALSE);
820
gtk_widget_set_sensitive (p->button4, FALSE);
821
gtk_widget_set_sensitive (p->button5, FALSE);
822
gtk_widget_set_sensitive (p->button6, FALSE);
824
/* we check with == -3 if the infinite option was choosed */
825
for (li = 0; p->count == -3 ? : li < p->count; li++) {
826
/* so wait the delay between sequences */
827
while (gap < p->del) {
828
gettimeofday(&nowstr, NULL);
830
gap = (nowstr.tv_sec*1000000 + nowstr.tv_usec) -
831
(last.tv_sec*1000000 + last.tv_usec);
833
gap4 = nowstr.tv_sec - first.tv_sec;
835
/* if the flag is set - the user clicked the stop button, we quit */
836
if (stop_flag == 1) {
837
gdk_threads_enter ();
839
gtk_widget_set_sensitive (p->button1, TRUE);
840
gtk_widget_set_sensitive (p->button2, TRUE);
841
gtk_widget_set_sensitive (p->button3, TRUE);
842
gtk_widget_set_sensitive (p->button4, TRUE);
843
gtk_widget_set_sensitive (p->button5, TRUE);
844
gtk_widget_set_sensitive (p->button6, TRUE);
846
snprintf(buff, 100, " Completed %ld cycles. Sent %ld packets on %s",
847
li, sentnumber, iftext);
848
gtk_statusbar_push(GTK_STATUSBAR(p->button),
849
GPOINTER_TO_INT(p->context_id), buff);
851
gdk_flush();gdk_threads_leave ();
856
/* update the status bar every second and display number of sent packets */
857
if (gap4 > seconds) {
858
gdk_threads_enter ();
860
snprintf(buff, 100, " Cycle: %ld Starting ... Sent %ld packets on %s", li+1, sentnumber, iftext);
862
gtk_statusbar_push(GTK_STATUSBAR(p->button),
863
GPOINTER_TO_INT(p->context_id), buff);
865
gdk_flush();gdk_threads_leave ();
871
/* so we waited the desired time between sequences, now we go through all ten fields
872
and send it if there is a name for a packet, and disable button is not on */
873
for(j = 0; j < 10; j++) {
874
/* skip it if there is no packet name */
875
if (p->partable[j][0] == 0)
877
/* skip it if disable button is activated */
878
if (p->partable[j][5] == 0)
881
/* now we are inside one sequence */
882
/* this is the time we started */
883
gettimeofday(&first1, NULL);
884
gettimeofday(&last1, NULL);
885
gettimeofday(&nowstr1, NULL);
887
/* to send first packet immedialtelly */
888
gap3 = p->partable[j][3];
890
/* now we will send this packet partable[j][2] number of times */
891
for (li2 = 0; li2 < p->partable[j][2]; li2++) {
892
/* wait enough time */
893
while (gap3 < p->partable[j][3]) {
894
gettimeofday(&nowstr1, NULL);
896
if (p->timeflag == 1)
897
gap3 = (nowstr1.tv_sec*1000000 + nowstr1.tv_usec) -
898
(last1.tv_sec*1000000 + last1.tv_usec);
900
gap3 = (nowstr1.tv_sec*1000000 + nowstr1.tv_usec) -
901
(first1.tv_sec*1000000 + first1.tv_usec) -
902
( (li2 - 1) * p->partable[j][3]);
904
gap4 = nowstr1.tv_sec - first.tv_sec;
906
/* if the flag is set - the user clicked the stop button, we quit */
907
if (stop_flag == 1) {
908
gdk_threads_enter ();
910
gtk_widget_set_sensitive (p->button1, TRUE);
911
gtk_widget_set_sensitive (p->button2, TRUE);
912
gtk_widget_set_sensitive (p->button3, TRUE);
913
gtk_widget_set_sensitive (p->button4, TRUE);
914
gtk_widget_set_sensitive (p->button5, TRUE);
915
gtk_widget_set_sensitive (p->button6, TRUE);
917
snprintf(buff, 100, " Stoped in %ld cycle and row %d. Sent %ld packets on %s", li+1, j+1, sentnumber, iftext);
918
gtk_statusbar_push(GTK_STATUSBAR(p->button),
919
GPOINTER_TO_INT(p->context_id), buff);
921
gdk_flush();gdk_threads_leave ();
926
/* update the status bar every second and display number
928
if (gap4 > seconds) {
929
gdk_threads_enter ();
931
snprintf(buff, 100, " Cycle: %ld Row: %d Sending... Sent %ld packets on %s", li+1, j+1, sentnumber, iftext);
932
gtk_statusbar_push(GTK_STATUSBAR(p->button),
933
GPOINTER_TO_INT(p->context_id), buff);
935
gdk_flush();gdk_threads_leave ();
942
/* put the packet on the wire */
943
//printf("takle je paket: %s\n", p->pkttable[j]);
944
c = sendto(fd, p->pkttable[j], p->partable[j][1], 0,
945
(struct sockaddr *)&sa, sizeof (sa));
947
last1.tv_sec = nowstr1.tv_sec;
948
last1.tv_usec = nowstr1.tv_usec;
956
/* here we gonna wait the desired time before sending the next row */
957
gettimeofday(&last1, NULL);
960
while (gap3 < p->partable[j][4]) {
962
gettimeofday(&nowstr1, NULL);
963
gap3 = (nowstr1.tv_sec*1000000 + nowstr1.tv_usec) -
964
(last1.tv_sec*1000000 + last1.tv_usec);
965
gap4 = nowstr1.tv_sec - first.tv_sec;
967
/* if the flag is set - the user clicked the stop button, we quit */
968
if (stop_flag == 1) {
969
gdk_threads_enter ();
971
gtk_widget_set_sensitive (p->button1, TRUE);
972
gtk_widget_set_sensitive (p->button2, TRUE);
973
gtk_widget_set_sensitive (p->button3, TRUE);
974
gtk_widget_set_sensitive (p->button4, TRUE);
975
gtk_widget_set_sensitive (p->button5, TRUE);
976
gtk_widget_set_sensitive (p->button6, TRUE);
978
snprintf(buff, 100, " Stoped in %ld cycle and row %d. Sent %ld packets on %s", li+1, j+1, sentnumber, iftext);
979
gtk_statusbar_push(GTK_STATUSBAR(p->button),
980
GPOINTER_TO_INT(p->context_id), buff);
982
gdk_flush();gdk_threads_leave ();
987
/* update the status bar every second and display number of sent packets */
988
if (gap4 > seconds) {
989
gdk_threads_enter ();
991
snprintf(buff, 100, " Cycle: %ld Row: %d Waiting... Sent %ld packets on %s", li+1, j+1, sentnumber, iftext);
992
gtk_statusbar_push(GTK_STATUSBAR(p->button),
993
GPOINTER_TO_INT(p->context_id), buff);
995
gdk_flush();gdk_threads_leave ();
1002
gettimeofday(&last, NULL);
1006
/* we sent all the packets, let's activate the toolbar */
1007
gdk_threads_enter ();
1009
gtk_widget_set_sensitive (p->button1, TRUE);
1010
gtk_widget_set_sensitive (p->button2, TRUE);
1011
gtk_widget_set_sensitive (p->button3, TRUE);
1012
gtk_widget_set_sensitive (p->button4, TRUE);
1013
gtk_widget_set_sensitive (p->button5, TRUE);
1014
gtk_widget_set_sensitive (p->button6, TRUE);
1016
snprintf(buff, 100, " Completed %ld cycles. Sent %ld packets on %s", li, sentnumber, iftext);
1017
gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
1019
if (close(fd) != 0) {
1020
//printf("Warning! close(fd) returned -1!\n");
1021
error("Warning! close(fd) returned -1!");
1024
gdk_flush();gdk_threads_leave ();