~ubuntu-branches/ubuntu/oneiric/packeth/oneiric

« back to all changes in this revision

Viewing changes to src/function_send.c

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2008-02-21 10:42:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080221104200-xsp0zp1ujbsqsj7a
Tags: 1.6-1
* New upstream release
* debian/control:
  - "Ethernet" not capitalized in descriptions (Closes: #466497)
  - debhelper dependency bumped to 6 (also debian/compat)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * packETH - ethernet packet generator
 
3
 * By Miha Jemec <m.jemec@iskratel.si>
 
4
 * Copyright 2003 Miha Jemec, Iskratel
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 *
 
20
 * function_send.c - routines for sending packet
 
21
 *
 
22
 * 
 
23
 */
 
24
 
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
#include <gtk/gtk.h>
 
30
 
 
31
#include <unistd.h>
 
32
#include <sys/types.h>
 
33
 
 
34
#include <net/if.h>
 
35
#include <netpacket/packet.h>
 
36
#include <net/ethernet.h>
 
37
#include <netinet/in.h>
 
38
#include <sys/ioctl.h>
 
39
#include <netdb.h>
 
40
 
 
41
#include "callbacks.h"
 
42
#include "function_send.h"
 
43
#include "function.h"
 
44
 
 
45
#include <sys/time.h>
 
46
 
 
47
extern int number;
 
48
extern unsigned char packet[1522];
 
49
extern gboolean stop_flag;
 
50
//extern char iftext[20];
 
51
char iftext[20];
 
52
 
 
53
struct params  {
 
54
        long del;
 
55
        long count;
 
56
        int inc;
 
57
        int type;
 
58
        GtkWidget *button;
 
59
        GtkWidget *button1;
 
60
        GtkWidget *button2;
 
61
        GtkWidget *button3;
 
62
        GtkWidget *button4;
 
63
        GtkWidget *button5;
 
64
        GtkWidget *button6;
 
65
        GtkWidget *toolbar;
 
66
        GtkWidget *stopbt;
 
67
        gint context_id;
 
68
        gint timeflag;
 
69
        int udpstart;
 
70
        int tcpstart;
 
71
        int ipstart;
 
72
        int ethstart;
 
73
        int xbyte;
 
74
        int ybyte;
 
75
        int xchange;
 
76
        int ychange;
 
77
        unsigned long xrange;
 
78
        unsigned long yrange;
 
79
        char xstart[4];
 
80
        char ystart[4];
 
81
        unsigned char pkttable[10][1518];
 
82
        long int partable[10][6];
 
83
};
 
84
/* end */
 
85
 
 
86
 
 
87
/* go men go! */
 
88
int packet_go_on_the_link(unsigned char *pkt, int nr)
 
89
{
 
90
        int c, fd;
 
91
        struct sockaddr_ll sa;
 
92
        struct ifreq ifr;
 
93
        char buff[100];
 
94
        
 
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!");
 
99
                return -2;
 
100
        }
 
101
        
 
102
        /* open socket in raw mode */
 
103
        fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
 
104
        if (fd == -1) {
 
105
                //printf("Error: Could not open socket!\n");
 
106
                error("Error: Could not open socket!");
 
107
                return -2;
 
108
        }
 
109
 
 
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';
 
114
 
 
115
        if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
 
116
                //printf("No such interface: %s\n", iftext);
 
117
                snprintf(buff, 100, "No such interface: %s", iftext);
 
118
                error(buff);
 
119
                close(fd);
 
120
                return -2;
 
121
        }       
 
122
 
 
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);
 
128
                error(buff);
 
129
                close(fd);
 
130
                return -2;
 
131
        }
 
132
 
 
133
        /* just write in the structure again */
 
134
        ioctl(fd, SIOCGIFINDEX, &ifr);
 
135
        
 
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);
 
141
 
 
142
        c = sendto(fd, pkt, nr, 0, (struct sockaddr *)&sa, sizeof (sa));
 
143
 
 
144
        //printf("There were %d bytes sent on the wire (in case of an error we get -1)\n", c);
 
145
 
 
146
        if (close(fd) == 0) {
 
147
                return (c);
 
148
        }
 
149
        else {
 
150
                //printf("Warning! close(fd) returned -1!\n");
 
151
                error("Warning! close(fd) returned -1!");
 
152
                return (c);
 
153
        }
 
154
 
 
155
}
 
156
 
 
157
 
 
158
/* thread for sending packets */
 
159
void* sendbuilt (void *parameters)
 
160
{
 
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;
 
167
        char buff[100];
 
168
        struct sockaddr_ll sa;
 
169
        struct ifreq ifr;
 
170
        guint32 ipcks, pseudo_header, udpcksum, tcpcksum;
 
171
        guint32 *stevec32;
 
172
 
 
173
        struct params* p = (struct params*) parameters;
 
174
 
 
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);
 
185
                        
 
186
                        snprintf(buff, 100, "  Problems with sending");
 
187
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
188
 
 
189
                        error("Sorry but need the su rights!");
 
190
                gdk_flush();gdk_threads_leave ();
 
191
                return NULL;
 
192
        }
 
193
 
 
194
        /* open socket in raw mode */
 
195
        fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
 
196
        if (fd == -1) {
 
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);
 
205
 
 
206
                        snprintf(buff, 100, "  Problems with sending");
 
207
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
208
 
 
209
                        error("Error: Could not open socket!");
 
210
                gdk_flush();gdk_threads_leave ();
 
211
                return NULL;
 
212
        }
 
213
 
 
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';
 
218
 
 
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);
 
229
 
 
230
                        snprintf(buff, 100, "  Problems with sending");
 
231
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
232
 
 
233
                        snprintf(buff, 100, "No such interface: %s", iftext);
 
234
                        error(buff);
 
235
                gdk_flush();gdk_threads_leave ();
 
236
                close(fd);
 
237
                return NULL;
 
238
        }
 
239
 
 
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);
 
251
 
 
252
                        snprintf(buff, 100, "  Problems with sending");
 
253
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
254
 
 
255
                        snprintf(buff, 100, "Interface %s is down", iftext);
 
256
                        error(buff);
 
257
                gdk_flush();gdk_threads_leave ();
 
258
                close(fd);
 
259
                return NULL;
 
260
        }
 
261
 
 
262
        /* just write in the structure again */
 
263
        ioctl(fd, SIOCGIFINDEX, &ifr);
 
264
 
 
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);
 
270
 
 
271
        /* this is the time we started */
 
272
        gettimeofday(&first, NULL);
 
273
        gettimeofday(&last, NULL);
 
274
        gettimeofday(&nowstr, NULL);
 
275
 
 
276
        /* to send first packet immedialtelly */
 
277
        gap = p->del;
 
278
 
 
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;
 
282
        if (number < 60)
 
283
                number = 60;
 
284
 
 
285
 
 
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);
 
293
 
 
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);
 
298
 
 
299
                        if (p->timeflag == 1)
 
300
                                gap = (nowstr.tv_sec*1000000 + nowstr.tv_usec) -
 
301
                                                (last.tv_sec*1000000 + last.tv_usec);
 
302
                        else
 
303
                                gap = (nowstr.tv_sec*1000000 + nowstr.tv_usec) -
 
304
                                                (first.tv_sec*1000000 + first.tv_usec) - ( (li - 1) * p->del);
 
305
 
 
306
                        gap2 = nowstr.tv_sec - first.tv_sec;
 
307
 
 
308
                        /* if the flag is set - the user clicked the stop button, we quit */
 
309
                        if (stop_flag == 1) {
 
310
                                gdk_threads_enter ();
 
311
 
 
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);
 
319
 
 
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);
 
323
 
 
324
                                gdk_flush();gdk_threads_leave ();
 
325
                                close(fd);
 
326
                                return NULL;
 
327
                        }
 
328
                }
 
329
 
 
330
                c = sendto(fd, packet, number, 0, (struct sockaddr *)&sa, sizeof (sa));
 
331
 
 
332
                //printf("There were %d bytes sent on the wire (in case of an error we get -1)\n", c);
 
333
 
 
334
                last.tv_sec = nowstr.tv_sec;
 
335
                last.tv_usec = nowstr.tv_usec;
 
336
                gap = 0;
 
337
 
 
338
                if (c > 0)
 
339
                        sentnumber++;
 
340
//              else
 
341
//                      break;
 
342
 
 
343
                 /* update the status bar every second and display number of sent packets */
 
344
                if (gap2 > seconds) {
 
345
                        gdk_threads_enter ();
 
346
                        
 
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;
 
352
 
 
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);
 
355
 
 
356
                        gdk_flush();gdk_threads_leave ();
 
357
 
 
358
                        seconds++;
 
359
                }
 
360
 
 
361
                /* do we need to change any fields */
 
362
                switch (p->inc) {
 
363
                        /* changing source MAC address */
 
364
                        case 1: {
 
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));
 
371
                                break;
 
372
                        }
 
373
                        /* change source IP address - make it random 
 
374
                         * and correct IP checksum */
 
375
                        case 2: {
 
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);
 
387
                                break;
 
388
                        }
 
389
                        /* source MAC address and source IP address 
 
390
                         * and correct IP checksum */
 
391
                        case 3: {
 
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);
 
409
                                break;
 
410
                        }
 
411
                        /* for arp reply messages, change source MAC (ethernet part) *
 
412
                         * sender MAC and sender IP (arp part) */
 
413
                        case 4: {
 
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];
 
430
                                break;
 
431
                                
 
432
                        }
 
433
                        /* set random source TCP port and IP source address (syn flood) */ 
 
434
                        case 5: {
 
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);
 
448
                                break;
 
449
                        }
 
450
                        /* increase the udp first payload byte value by one */
 
451
                        case 6:  {
 
452
                                packet[p->udpstart]++;
 
453
                                break;
 
454
                        }
 
455
                        /* changing RTP values: seq number++, timestamp for 10ms */
 
456
                        case 7: {
 
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);
 
463
                                break;
 
464
                        }
 
465
                        /* changing RTP values: seq number++, timestamp for 20ms */
 
466
                        case 8: {
 
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);
 
473
                                break;
 
474
                        }
 
475
                        /* changing RTP values: seq number++, timestamp for 30ms */
 
476
                        case 9: {
 
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);
 
483
                                break;
 
484
                        }
 
485
                        /* changing byte x value */
 
486
                        case 10: {
 
487
                                /* increment it within specified range */
 
488
                                if (p->xchange == 1) {
 
489
                                        if (xc < (p->xrange)) {
 
490
                                                stevec32 = (guint32*) &packet[p->xbyte-1];
 
491
                                                (*stevec32)++;
 
492
                                                xc++;
 
493
                                        }
 
494
                                        else    {
 
495
                                                memcpy(&packet[p->xbyte-1], p->xstart, 4);
 
496
                                                xc=0;
 
497
                                        }
 
498
                                }
 
499
                                /* decrement it within specified range */
 
500
                                else if (p->xchange == 2) {
 
501
                                        if (xc < (p->xrange)) {
 
502
                                                stevec32 = (guint32*) &packet[p->xbyte-1];
 
503
                                                (*stevec32)--;
 
504
                                                xc++;
 
505
                                        }
 
506
                                        else    {
 
507
                                                memcpy(&packet[p->xbyte-1], p->xstart, 4);
 
508
                                                xc=0;
 
509
                                        }
 
510
                                }
 
511
                                /* set it random */
 
512
                                else if (p->xchange == 0)
 
513
                                        packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
 
514
 
 
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));
 
518
                                }       
 
519
                                        
 
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));
 
524
                                }       
 
525
                                        
 
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));
 
531
                                }       
 
532
                                        
 
533
                                if(p->type >1) {
 
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);
 
539
                                }
 
540
 
 
541
                                break;
 
542
                        }
 
543
                        /* change byte x and y */
 
544
                        case 11: {
 
545
                                /* byte x, increment */
 
546
                                if (p->xchange == 1) {
 
547
                                        if (xc < (p->xrange)) {
 
548
                                                stevec32 = (guint32*) &packet[p->xbyte-1];
 
549
                                                (*stevec32)++;
 
550
                                                xc++;
 
551
                                        }
 
552
                                        else    {
 
553
                                                memcpy(&packet[p->xbyte-1], p->xstart, 4);
 
554
                                                xc=0;
 
555
                                        }
 
556
                                }
 
557
                                /* decrement it within specified range */
 
558
                                else if (p->xchange == 2) {
 
559
                                        if (xc < (p->xrange)) {
 
560
                                                stevec32 = (guint32*) &packet[p->xbyte-1];
 
561
                                                (*stevec32)--;
 
562
                                                xc++;
 
563
                                        }
 
564
                                        else    {
 
565
                                                memcpy(&packet[p->xbyte-1], p->xstart, 4);
 
566
                                                xc=0;
 
567
                                        }
 
568
                                }
 
569
                                /* set it random */
 
570
                                else if (p->xchange == 0)
 
571
                                        packet[p->xbyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
 
572
 
 
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));
 
576
                                }       
 
577
                                        
 
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));
 
582
                                }       
 
583
                                        
 
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));
 
589
                                }       
 
590
 
 
591
                                /* byte y increment */
 
592
                                if (p->ychange == 1) {
 
593
                                        if (yc < (p->yrange)) {
 
594
                                                stevec32 = (guint32*) &packet[p->ybyte-1];
 
595
                                                (*stevec32)++;
 
596
                                                yc++;
 
597
                                        }
 
598
                                        else    {
 
599
                                                memcpy(&packet[p->ybyte-1], p->ystart, 4);
 
600
                                                yc=0;
 
601
                                        }
 
602
                                }
 
603
                                /* decrement it within specified range */
 
604
                                else if (p->ychange == 2) {
 
605
                                        if (yc < (p->yrange)) {
 
606
                                                stevec32 = (guint32*) &packet[p->ybyte-1];
 
607
                                                (*stevec32)--;
 
608
                                                yc++;
 
609
                                        }
 
610
                                        else    {
 
611
                                                memcpy(&packet[p->ybyte-1], p->ystart, 4);
 
612
                                                yc=0;
 
613
                                        }
 
614
                                }
 
615
                                /* set it random */
 
616
                                else if (p->ychange == 0)
 
617
                                        packet[p->ybyte-1] = 1+(int) (255.0*rand()/(RAND_MAX+1.0));
 
618
 
 
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));
 
622
                                }       
 
623
                                        
 
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));
 
628
                                }       
 
629
                                        
 
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));
 
635
                                }       
 
636
 
 
637
                                if(p->type >1) {
 
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);
 
643
                                }
 
644
 
 
645
                                break;
 
646
                        }
 
647
                }
 
648
 
 
649
                /* correct the UDP checksum value???
 
650
                 * we do it (udp checksum in not mandatory (if set to 0x00)) */
 
651
                if (p->type == 4) {
 
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);
 
657
 
 
658
                        /* pseudo header (ip part) + udplength + nr of cicles over guint16 */
 
659
                        udpcksum = pseudo_header + udpcksum;
 
660
 
 
661
                        /* what if length is odd */
 
662
                        if( (actualnumber - p->udpstart)%2 != 0) 
 
663
                                odd = 1;
 
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);
 
668
        
 
669
                                /* the one's complement */
 
670
                        udpcksum = (-1) - udpcksum;
 
671
                        
 
672
                        /* let's write it */
 
673
                        packet[p->udpstart-2] = (char)(udpcksum/256);
 
674
                        packet[p->udpstart-1] =  (char)(udpcksum%256);
 
675
                }
 
676
                /* or tcp checksum*/
 
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) 
 
687
                                odd = 1;
 
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;
 
694
                        /* let's write it */
 
695
                        packet[p->tcpstart+16] = (char)(tcpcksum/256);
 
696
                        packet[p->tcpstart+17] =  (char)(tcpcksum%256);
 
697
                }
 
698
        }
 
699
 
 
700
        /* we sent all the packets, let's activate the toolbar */
 
701
        gdk_threads_enter ();
 
702
 
 
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);
 
710
 
 
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);
 
713
 
 
714
        if (close(fd) != 0) {
 
715
                //printf("Warning! close(fd) returned -1!\n");
 
716
                error("Warning! close(fd) returned -1!");
 
717
        }
 
718
 
 
719
        gdk_flush();gdk_threads_leave ();
 
720
 
 
721
        return NULL;
 
722
 
 
723
}
 
724
 
 
725
 
 
726
/* thread for sending sequences */
 
727
void* sendsequence (void *parameters)
 
728
{
 
729
 
 
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;
 
733
        int j, c, fd;
 
734
        char buff[100];
 
735
        struct sockaddr_ll sa;
 
736
        struct ifreq ifr;
 
737
 
 
738
        struct params* p = (struct params*) parameters;
 
739
 
 
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 ();
 
744
                        
 
745
                        snprintf(buff, 100, "  Problems with sending");
 
746
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
747
 
 
748
                        error("Sorry but need the su rights!");
 
749
                gdk_flush();gdk_threads_leave ();
 
750
                return NULL;
 
751
        }
 
752
 
 
753
        /* open socket in raw mode */
 
754
        fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
 
755
        if (fd == -1) {
 
756
                //printf("Error: Could not open socket!\n");
 
757
                gdk_threads_enter ();
 
758
 
 
759
                        snprintf(buff, 100, "  Problems with sending");
 
760
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
761
 
 
762
                        error("Error: Could not open socket!");
 
763
                gdk_flush();gdk_threads_leave ();
 
764
                return NULL;
 
765
        }
 
766
 
 
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';
 
771
 
 
772
        if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
 
773
                //printf("No such interface: %s\n", iftext);
 
774
                gdk_threads_enter ();
 
775
 
 
776
                        snprintf(buff, 100, "  Problems with sending");
 
777
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
778
 
 
779
                        snprintf(buff, 100, "No such interface: %s", iftext);
 
780
                        error(buff);
 
781
                gdk_flush();gdk_threads_leave ();
 
782
                close(fd);
 
783
                return NULL;
 
784
        }
 
785
 
 
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 ();
 
791
 
 
792
                        snprintf(buff, 100, "  Problems with sending");
 
793
                        gtk_statusbar_push(GTK_STATUSBAR(p->button), GPOINTER_TO_INT(p->context_id), buff);
 
794
 
 
795
                        snprintf(buff, 100, "Interface %s is down", iftext);
 
796
                        error(buff);
 
797
                gdk_flush();gdk_threads_leave ();
 
798
                close(fd);
 
799
                return NULL;
 
800
        }
 
801
 
 
802
        /* just write in the structure again */
 
803
        ioctl(fd, SIOCGIFINDEX, &ifr);
 
804
 
 
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);
 
810
 
 
811
        /* this is the time we started */
 
812
        gettimeofday(&first, NULL);
 
813
 
 
814
        /* to start first sequence immedialtelly */
 
815
        gap = p->del;
 
816
 
 
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);
 
823
 
 
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);
 
829
 
 
830
                        gap = (nowstr.tv_sec*1000000 + nowstr.tv_usec) -
 
831
                                                (last.tv_sec*1000000 + last.tv_usec);
 
832
 
 
833
                        gap4 = nowstr.tv_sec - first.tv_sec;
 
834
 
 
835
                        /* if the flag is set - the user clicked the stop button, we quit */
 
836
                        if (stop_flag == 1) {
 
837
                                gdk_threads_enter ();
 
838
 
 
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);
 
845
 
 
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);
 
850
 
 
851
                                gdk_flush();gdk_threads_leave ();
 
852
                                close(fd);
 
853
                                return NULL;
 
854
                        }
 
855
 
 
856
                        /* update the status bar every second and display number of sent packets */
 
857
                        if (gap4 > seconds) {
 
858
                                gdk_threads_enter ();
 
859
 
 
860
                                snprintf(buff, 100, "   Cycle: %ld   Starting ...           Sent %ld packets on %s",    li+1, sentnumber, iftext);
 
861
 
 
862
                                gtk_statusbar_push(GTK_STATUSBAR(p->button), 
 
863
                                                        GPOINTER_TO_INT(p->context_id), buff);
 
864
 
 
865
                                gdk_flush();gdk_threads_leave ();
 
866
 
 
867
                                seconds++;
 
868
                        }
 
869
                }
 
870
                
 
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)
 
876
                                continue;
 
877
                        /* skip it if disable button is activated */
 
878
                        if (p->partable[j][5] == 0)
 
879
                                continue;
 
880
 
 
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);
 
886
 
 
887
                        /* to send first packet immedialtelly */
 
888
                        gap3 = p->partable[j][3];
 
889
 
 
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);
 
895
                                        /* timing type */
 
896
                                        if (p->timeflag == 1)
 
897
                                                gap3 = (nowstr1.tv_sec*1000000 + nowstr1.tv_usec) -
 
898
                                                        (last1.tv_sec*1000000 + last1.tv_usec);
 
899
                                        else
 
900
                                                gap3 = (nowstr1.tv_sec*1000000 + nowstr1.tv_usec) -
 
901
                                                        (first1.tv_sec*1000000 + first1.tv_usec) - 
 
902
                                                        ( (li2 - 1) * p->partable[j][3]);
 
903
 
 
904
                                        gap4 = nowstr1.tv_sec - first.tv_sec;
 
905
 
 
906
                                        /* if the flag is set - the user clicked the stop button, we quit */
 
907
                                        if (stop_flag == 1) {
 
908
                                                gdk_threads_enter ();
 
909
 
 
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);
 
916
 
 
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);
 
920
 
 
921
                                                gdk_flush();gdk_threads_leave ();
 
922
                                                close(fd);
 
923
                                                return NULL;
 
924
                                        }
 
925
 
 
926
                                        /* update the status bar every second and display number 
 
927
                                         * of sent packets */
 
928
                                        if (gap4 > seconds) {
 
929
                                                gdk_threads_enter ();
 
930
 
 
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);
 
934
 
 
935
                                                gdk_flush();gdk_threads_leave ();
 
936
 
 
937
                                                seconds++;
 
938
                                        }
 
939
 
 
940
                                }
 
941
 
 
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));
 
946
 
 
947
                                last1.tv_sec = nowstr1.tv_sec;
 
948
                                last1.tv_usec = nowstr1.tv_usec;
 
949
                                gap3 = 0;
 
950
 
 
951
                                if (c > 0)
 
952
                                        sentnumber++;
 
953
 
 
954
                        }
 
955
                        
 
956
                        /* here we gonna wait the desired time before sending the next row */
 
957
                        gettimeofday(&last1, NULL);
 
958
                        gap3 = 0;
 
959
 
 
960
                        while (gap3 < p->partable[j][4]) {
 
961
 
 
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;
 
966
 
 
967
                                /* if the flag is set - the user clicked the stop button, we quit */
 
968
                                if (stop_flag == 1) {
 
969
                                        gdk_threads_enter ();
 
970
 
 
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);
 
977
 
 
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);
 
981
 
 
982
                                        gdk_flush();gdk_threads_leave ();
 
983
                                        close(fd);
 
984
                                        return NULL;
 
985
                                }
 
986
 
 
987
                                /* update the status bar every second and display number of sent packets */
 
988
                                if (gap4 > seconds) {
 
989
                                        gdk_threads_enter ();
 
990
 
 
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);
 
994
 
 
995
                                        gdk_flush();gdk_threads_leave ();
 
996
 
 
997
                                        seconds++;
 
998
                                }
 
999
 
 
1000
                        }
 
1001
                }               
 
1002
                gettimeofday(&last, NULL);
 
1003
                gap = 0;
 
1004
        }
 
1005
 
 
1006
        /* we sent all the packets, let's activate the toolbar */
 
1007
        gdk_threads_enter ();
 
1008
 
 
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);
 
1015
 
 
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);
 
1018
 
 
1019
        if (close(fd) != 0) {
 
1020
                //printf("Warning! close(fd) returned -1!\n");
 
1021
                error("Warning! close(fd) returned -1!");
 
1022
        }
 
1023
 
 
1024
        gdk_flush();gdk_threads_leave ();
 
1025
 
 
1026
        return NULL;
 
1027
 
 
1028
}
 
1029