~ubuntu-branches/ubuntu/intrepid/packeth/intrepid

« back to all changes in this revision

Viewing changes to src/function.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.c - all routines except callbacks and routines for sending
 
21
 *
 
22
 * 
 
23
 */
 
24
 
 
25
 
 
26
#include <gtk/gtk.h>
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
#include <ctype.h>
 
31
#include <time.h>
 
32
#include <sys/time.h>
 
33
#include <sys/timeb.h>
 
34
#include <math.h>
 
35
#include <pthread.h>
 
36
 
 
37
#include <net/if.h>
 
38
#include <netpacket/packet.h>
 
39
#include <net/ethernet.h>
 
40
#include <netinet/in.h>
 
41
#include <sys/ioctl.h>
 
42
#include <netdb.h>
 
43
 
 
44
#include "callbacks.h"
 
45
#include "support.h"
 
46
#include "function.h"
 
47
#include "function_send.h"
 
48
#include "headers.h"
 
49
 
 
50
/* some global variables:
 
51
 * packet [] - the packet contents 
 
52
 * number - packet length
 
53
 * ... */
 
54
unsigned char packet[1522];
 
55
int number = 0;
 
56
gint autolength = 0;
 
57
int udp_start = 0;
 
58
int tcp_start = 0;
 
59
int ip_start = 0;
 
60
int eth_start = 0;
 
61
gboolean stop_flag = 0;
 
62
extern char iftext[20];
 
63
static unsigned long crc32_table[256];
 
64
int crc32_table_init = 0;
 
65
 
 
66
/* structure that holds parameters for generator */
 
67
struct params {
 
68
        long del;
 
69
        long count;
 
70
        int inc;
 
71
        int type;
 
72
        GtkWidget *button;
 
73
        GtkWidget *button1;
 
74
        GtkWidget *button2;
 
75
        GtkWidget *button3;
 
76
        GtkWidget *button4;
 
77
        GtkWidget *button5;
 
78
        GtkWidget *button6;
 
79
        GtkWidget *stopbt;
 
80
        GtkWidget *toolbar;
 
81
        gint context_id;
 
82
        gint timeflag;
 
83
        int udpstart; //udp payload start
 
84
        int tcpstart; //tcp header start
 
85
        int ipstart;  //ip source address start
 
86
        int ethstart;  //start of address position in arp header
 
87
        int xbyte;
 
88
        int ybyte;
 
89
        int xchange;
 
90
        int ychange;
 
91
        unsigned long xrange;
 
92
        unsigned long yrange;
 
93
        char xstart[4];
 
94
        char ystart[4];
 
95
        unsigned char pkttable[10][1518];
 
96
        /* partable columns mean: [0] - is there a packet or not (1)/(0), [1] - length of packet, [2] - 
 
97
        number of packets  [3] - gap between, [4] - gap to the next sequence, [5] - enable(1) / disable(0) */ 
 
98
        long int partable[10][6]; 
 
99
} params1;              
 
100
 
 
101
/* be carefull with number. when you build a packet you should make number++ after the last 
 
102
 * copied value in the packet[] field since you start with number = 0, but when you call 
 
103
 * packet_go_on_the_link() you pass that number as the number of packets which is one
 
104
 * more than in the last packet[number] = ... line */
 
105
 
 
106
 
 
107
/* send button was pressed */
 
108
int send_packet(GtkButton *button, gpointer user_data)
 
109
{
 
110
        GtkWidget *statusbar, *notebk, *abstime, *reltime, *en5, *en6, *ntbk7;
 
111
        GtkWidget *en1, *en2, *en3, *en4, *ckbt1, *ckbt2, *xoptm, *yoptm, *xmenu_item, *ymenu_item; 
 
112
        GtkWidget *optm1, *optm2, *optm3, *menu, *xmenu, *ymenu, *menu_item, *stopbt, *toolbar;
 
113
        GtkWidget *button1, *button2, *button3, *button4, *button5, *button6;
 
114
 
 
115
        int c, i, m, length;
 
116
        gchar *en1_t, *en2_t, *en3_t, *en4_t, *en5_t, *en6_t;
 
117
        gint context_id, page, cpage;
 
118
        char buff[100], buf2[80];
 
119
        struct tm *ptr;
 
120
        struct timeb tp;
 
121
        time_t now;
 
122
        pthread_t thread_id;
 
123
 
 
124
        stop_flag = 0;
 
125
        
 
126
        statusbar = lookup_widget(GTK_WIDGET (button), "statusbar1");
 
127
        context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "Statusbar example");
 
128
 
 
129
        notebk = lookup_widget(GTK_WIDGET (button), "notebook1");
 
130
 
 
131
        /* now we have to decide what happens when the send button is pressed */
 
132
        page =  gtk_notebook_get_current_page(GTK_NOTEBOOK(notebk));
 
133
        
 
134
        if ( page == 0 ) { /* so we have the build notebook open, it means we send only one packet */   
 
135
 
 
136
                if (make_packet(button, user_data) == -1) {
 
137
                        //printf("problems with making packet!\n");
 
138
                        snprintf(buff, 100, "  Problems with making packet!");
 
139
                        gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
140
                        return -1;
 
141
                }
 
142
                
 
143
                /* YYY if the built packet is shorter then 60 bytes, we add padding zero bytes 
 
144
                 * to fill up the length till 60 (min ethrenet frame length). This bytes will be 
 
145
                 * added anyway by the device driver, so we do this just to prevent misunderstanding: 
 
146
                 * till now if your packet  was 20 bytes long, then it was also said -> 20 bytes 
 
147
                 * sent on eth0... but actually 60 bytes (+CRC) were sent */
 
148
                if (number < 60) {
 
149
                        memset(&packet[number], 0x00, ( 60 - number ) );
 
150
                        number = 60;
 
151
                }
 
152
 
 
153
                /* thats how the packet looks like */
 
154
                //for (i = 0; i < number; i++)
 
155
                //      printf("%x ", packet[i]);
 
156
                //printf("\nnumber je %d\n", number);
 
157
 
 
158
                /* let's send the packet */
 
159
                c = packet_go_on_the_link(packet, number);
 
160
                
 
161
                if ( c == -2) {
 
162
                        //printf("problems with sending\n");
 
163
                        snprintf(buff, 100, "  Problems with sending!");
 
164
                        gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
165
                        return -1;
 
166
                }
 
167
                else {
 
168
                        ftime(&tp);
 
169
                        now=tp.time;
 
170
                        ptr=localtime(&now);
 
171
                        strftime(buf2,80, "%H:%M:%S", ptr);
 
172
                        snprintf(buff, 100, " %s  -----> %d bytes sent on %s", buf2, c, iftext);
 
173
                        gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
174
                }       
 
175
 
 
176
                return 1;
 
177
        }
 
178
        
 
179
        /* is it the generator that sends the build packets? */
 
180
        else if (page == 1) { 
 
181
                if (make_packet(button, user_data) == -1) {
 
182
                        //printf("problems with making packet!\n");
 
183
                        snprintf(buff, 100, "  Problems with making packet!");
 
184
                        gtk_statusbar_push(GTK_STATUSBAR(statusbar), 
 
185
                                                        GPOINTER_TO_INT(context_id), buff);
 
186
                        return -1;
 
187
                }
 
188
 
 
189
                abstime = lookup_widget(GTK_WIDGET (button), "radiobutton34");
 
190
                reltime = lookup_widget(GTK_WIDGET (button), "radiobutton35");
 
191
                button1 = lookup_widget(GTK_WIDGET (button), "Build_button");
 
192
                button2 = lookup_widget(GTK_WIDGET (button), "Gen_button");
 
193
                button3 = lookup_widget(GTK_WIDGET (button), "Genp");
 
194
                button4 = lookup_widget(GTK_WIDGET (button), "Interface_button");
 
195
                button5 = lookup_widget(GTK_WIDGET (button), "Send_button");
 
196
                button6 = lookup_widget(GTK_WIDGET (button), "Gensbt");
 
197
                stopbt = lookup_widget(GTK_WIDGET (button), "Stop_button");
 
198
                en1 = lookup_widget(GTK_WIDGET (button), "entry109");
 
199
                en2 = lookup_widget(GTK_WIDGET (button), "entry110");
 
200
                ckbt1 = lookup_widget(GTK_WIDGET(button), "checkbutton35");
 
201
                ckbt2 = lookup_widget(GTK_WIDGET(button), "checkbutton37");
 
202
                ntbk7 = lookup_widget(GTK_WIDGET(button), "notebook7");
 
203
 
 
204
                en1_t = (char *)gtk_entry_get_text(GTK_ENTRY(en1));
 
205
                en2_t = (char *)gtk_entry_get_text(GTK_ENTRY(en2));
 
206
 
 
207
                /* do we have to adjust any parameters while sending? */
 
208
 
 
209
                cpage = gtk_notebook_get_current_page(GTK_NOTEBOOK(ntbk7));
 
210
 
 
211
                /* depending on what "packet type" is currently open we take some action */
 
212
                switch (cpage) {
 
213
                        /* ethernet only */
 
214
                        case 0: {
 
215
                                optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu9");
 
216
                                menu = GTK_OPTION_MENU(optm1)->menu;
 
217
                                menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
218
                                params1.inc = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
219
                                params1.type = 0;
 
220
                                break;
 
221
                        }
 
222
                        /* ip frame with user def option or icmp option */
 
223
                        case 1: {
 
224
                                optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu16");
 
225
                                menu = GTK_OPTION_MENU(optm1)->menu;
 
226
                                menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
227
                                params1.inc = 10 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
228
                                params1.type = 2;
 
229
                                break;
 
230
                        }
 
231
                        /* arp frame */
 
232
                        case 2: {
 
233
                                optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu17");
 
234
                                menu = GTK_OPTION_MENU(optm1)->menu;
 
235
                                menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
236
                                params1.inc = 20 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
237
                                params1.type = 1;
 
238
                                break;
 
239
                        }
 
240
                        /* tcp frame */
 
241
                        case 3: {
 
242
                                optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu18");
 
243
                                menu = GTK_OPTION_MENU(optm1)->menu;
 
244
                                menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
245
                                params1.inc = 30 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
246
                                params1.type = 3;
 
247
                                break;
 
248
                        }
 
249
                        /* udp frame */
 
250
                        case 4: {
 
251
                                optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu19");
 
252
                                menu = GTK_OPTION_MENU(optm1)->menu;
 
253
                                menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
254
                                params1.inc = 40 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
255
                                params1.type = 4;
 
256
                                break;
 
257
                        }
 
258
                }
 
259
 
 
260
                /* changing mac address */
 
261
                if ((params1.inc%10) == 1) {
 
262
                        if (number < 14) {              
 
263
                                error("Error: Packets is not long enough to change MAC address");
 
264
                                return -1;      
 
265
                        }
 
266
                        params1.inc = 1;
 
267
                }
 
268
                /* changing ip source address */
 
269
                else if ((params1.inc == 12) || (params1.inc == 32) || (params1.inc == 42)) {
 
270
                        if (number < ip_start + 1 + 4) {
 
271
                                error("Error: Packet is not long enough to change IP values");
 
272
                                return -1;
 
273
                        }
 
274
                        params1.inc = 2;
 
275
                }
 
276
                /* changing ip and mac source address */
 
277
                else if ((params1.inc == 13) || (params1.inc == 33) || (params1.inc == 43)) {
 
278
                        if (number < ip_start + 1 + 4) {
 
279
                                error("Error: Packet is not long enough to change MAC & IP values");
 
280
                                return -1;
 
281
                        }
 
282
                        params1.inc = 3;
 
283
                }
 
284
                /* arp values */
 
285
                else if (params1.inc == 22) {
 
286
                        if (number < eth_start + 1 + 6 + 4) {
 
287
                                error("Error: Packet is not long enough to change ARP values");
 
288
                                return -1;
 
289
                        }
 
290
                        params1.inc = 4;
 
291
                }
 
292
                /* ip source and tcp port values */
 
293
                else if (params1.inc == 34) {
 
294
                        if (number < tcp_start + 2) {           
 
295
                                error("Error: Packet isn't long enough to change TCP port");
 
296
                                return -1;      
 
297
                        }
 
298
                        params1.inc = 5;
 
299
                }
 
300
                /* increase udp payload by one */
 
301
                else if (params1.inc == 44) {
 
302
                        if (number < udp_start + 1) {           
 
303
                                error("Error: Packet is not long enough to increase UDP payload");
 
304
                                return -1;      
 
305
                        }
 
306
                        params1.inc = 6;
 
307
                }
 
308
                /* rtp values */
 
309
                else if ( (params1.inc >= 45) && (params1.inc <=47) ) {
 
310
                        if (number < udp_start + 1 + 12) {
 
311
                                error("Error: Packet is not long enough to increase RTP values");
 
312
                                return -1;
 
313
                        }
 
314
                        params1.inc = params1.inc - 38;
 
315
                }
 
316
                /* changing byte x */
 
317
                /* YYY this part of code is a mess (it's friday 15.30 now))*/
 
318
                else if ((params1.inc == 2) || (params1.inc == 14) || (params1.inc == 23) ||
 
319
                                (params1.inc == 35) || (params1.inc == 48)) {
 
320
 
 
321
                        /* offset x field, is it ok */
 
322
                        en5 = lookup_widget(GTK_WIDGET (button), "entry160");
 
323
                        en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
 
324
                        if ( (strtol(en5_t, (char **)NULL, 10) == 0) || 
 
325
                                        (number < strtol(en5_t, (char **)NULL, 10)) ) {
 
326
                                error("Error: Wrong byte x offset!");
 
327
                                return -1;      
 
328
                        }
 
329
                        length = strlen(en5_t);
 
330
                        for(m=0; m<length; m++) {
 
331
                                if (isdigit(*(en5_t+m)) == 0) {
 
332
                                error("Error: Wrong byte x entry!");
 
333
                                return -1;
 
334
                                }
 
335
                        }
 
336
                        params1.xbyte = strtol(en5_t, (char **)NULL, 10);
 
337
 
 
338
                        /* option menu button for x byte */
 
339
                        xoptm = lookup_widget(GTK_WIDGET (button), "optionmenu14");
 
340
                        xmenu = GTK_OPTION_MENU(xoptm)->menu;
 
341
                        xmenu_item = gtk_menu_get_active (GTK_MENU (xmenu));
 
342
                        params1.xchange = g_list_index (GTK_MENU_SHELL (xmenu)->children, xmenu_item);
 
343
                        memcpy(params1.xstart, &packet[params1.xbyte-1], 4);
 
344
 
 
345
                        if ((params1.xchange==1) || (params1.xchange==2)) {
 
346
                                /* range button for x byte */
 
347
                                en5 = lookup_widget(GTK_WIDGET (button), "entry161");
 
348
                                en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
 
349
                                if ( (strtol(en5_t, (char **)NULL, 10) == 0) ) { 
 
350
                                                // || (4294967294 < strtol(en5_t, (char **)NULL, 10)) ) {
 
351
                                        error("Error: Wrong byte x range!");
 
352
                                        return -1;      
 
353
                                }
 
354
                                length = strlen(en5_t);
 
355
                                for(m=0; m<length; m++) {
 
356
                                        if (isdigit(*(en5_t+m)) == 0) {
 
357
                                        error("Error: Wrong byte x range entry!");
 
358
                                        return -1;
 
359
                                        }
 
360
                                }
 
361
                                params1.xrange = strtol(en5_t, (char **)NULL, 10);
 
362
                        }
 
363
 
 
364
                        params1.inc = 10;
 
365
                }
 
366
                /* changing byte x and y */
 
367
                else if ((params1.inc == 3) || (params1.inc == 15) || (params1.inc == 24) ||
 
368
                                (params1.inc == 36) || (params1.inc == 49)) {
 
369
                        en5 = lookup_widget(GTK_WIDGET (button), "entry160");
 
370
                        en6 = lookup_widget(GTK_WIDGET (button), "entry162");
 
371
                        en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
 
372
                        en6_t = (char *)gtk_entry_get_text(GTK_ENTRY(en6));
 
373
                        if ( (number < strtol(en5_t, (char **)NULL, 10)) || 
 
374
                                                (number < strtol(en6_t, (char **)NULL, 10)) ) {
 
375
                                error("Error: Packets is not long enough to change specified byte");
 
376
                                return -1;      
 
377
                        }
 
378
                        length = strlen(en5_t);
 
379
                        for(m=0; m<length; m++) {
 
380
                                if (isdigit(*(en5_t+m)) == 0) {
 
381
                                error("Error: Wrong byte x entry!");
 
382
                                return -1;
 
383
                                }
 
384
                        }
 
385
                        length = strlen(en6_t);
 
386
                        for(m=0; m<length; m++) {
 
387
                                if (isdigit(*(en6_t+m)) == 0) {
 
388
                                error("Error: Wrong byte y entry!");
 
389
                                return -1;
 
390
                                }
 
391
                        }
 
392
                        params1.xbyte = strtol(en5_t, (char **)NULL, 10);
 
393
                        xoptm = lookup_widget(GTK_WIDGET (button), "optionmenu14");
 
394
                        xmenu = GTK_OPTION_MENU(xoptm)->menu;
 
395
                        xmenu_item = gtk_menu_get_active (GTK_MENU (xmenu));
 
396
                        memcpy(params1.xstart, &packet[params1.xbyte-1], 4);
 
397
                        params1.xchange = g_list_index (GTK_MENU_SHELL (xmenu)->children, xmenu_item);
 
398
                        params1.ybyte = strtol(en6_t, (char **)NULL, 10);
 
399
                        yoptm = lookup_widget(GTK_WIDGET (button), "optionmenu15");
 
400
                        ymenu = GTK_OPTION_MENU(yoptm)->menu;
 
401
                        ymenu_item = gtk_menu_get_active (GTK_MENU (ymenu));
 
402
                        memcpy(params1.ystart, &packet[params1.ybyte-1], 4);
 
403
                        params1.ychange = g_list_index (GTK_MENU_SHELL (ymenu)->children, ymenu_item);
 
404
 
 
405
                        if ((params1.xchange==1) || (params1.xchange==2)) {
 
406
                                en5 = lookup_widget(GTK_WIDGET (button), "entry161");
 
407
                                en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
 
408
                                if ( (strtol(en5_t, (char **)NULL, 10) == 0) ) { 
 
409
                                                // || (4294967295 < strtol(en5_t, (char **)NULL, 10)) ) {
 
410
                                        error("Error: Wrong byte x range!");
 
411
                                        return -1;      
 
412
                                }
 
413
                                length = strlen(en5_t);
 
414
                                for(m=0; m<length; m++) {
 
415
                                        if (isdigit(*(en5_t+m)) == 0) {
 
416
                                        error("Error: Wrong byte x range entry!");
 
417
                                        return -1;
 
418
                                        }
 
419
                                }
 
420
                                params1.xrange = strtol(en5_t, (char **)NULL, 10);
 
421
                        }
 
422
 
 
423
                        if ((params1.ychange==1) || (params1.ychange==2)) {
 
424
                                en5 = lookup_widget(GTK_WIDGET (button), "entry163");
 
425
                                en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
 
426
                                if ( (strtol(en5_t, (char **)NULL, 10) == 0) ) {
 
427
                                                // || (4294967295 < strtol(en5_t, (char **)NULL, 10)) ) {
 
428
                                        error("Error: Wrong byte y range!");
 
429
                                        return -1;      
 
430
                                }
 
431
                                length = strlen(en5_t);
 
432
                                for(m=0; m<length; m++) {
 
433
                                        if (isdigit(*(en5_t+m)) == 0) {
 
434
                                        error("Error: Wrong byte y range entry!");
 
435
                                        return -1;
 
436
                                        }
 
437
                                }
 
438
                                params1.yrange = strtol(en5_t, (char **)NULL, 10);
 
439
                        }
 
440
 
 
441
                        params1.inc = 11;
 
442
                }
 
443
 
 
444
                /* if the togglebutton is active, just keep on sending till stop is pressed */
 
445
                if (GTK_TOGGLE_BUTTON(ckbt1)->active) {
 
446
                        params1.count = -3;
 
447
                }
 
448
                else {
 
449
                        /* there can be rubbish in this field */
 
450
                        if (check_digit(en1_t, strlen(en1_t), "Error: Number of packets to send field") == -1)
 
451
                                        return -1;
 
452
 
 
453
                        params1.count = strtol(en1_t, (char **)NULL, 10);
 
454
                        /* we allow to send 9999999 max */
 
455
                        if ( (params1.count > 9999999) || (params1.count < 1) ) {
 
456
                                //printf("Error: Packets send number value\n");
 
457
                                error("Error: Packets send number value (1-9999999)");
 
458
                                return -1;
 
459
                        }
 
460
                }
 
461
 
 
462
                if (GTK_TOGGLE_BUTTON(ckbt2)->active) {
 
463
                        params1.del = 1;
 
464
                }
 
465
                else {
 
466
                        /* there can be rubbish in this field */
 
467
                        if (check_digit(en2_t, strlen(en2_t), "Error: Delay between packets field") == -1)
 
468
                                        return -1;
 
469
 
 
470
                        params1.del = strtol(en2_t, (char **)NULL, 10);
 
471
                        /* max delay 999,999999 s */
 
472
                        if ( (params1.del > 999999999) || (params1.del < 1) ) {
 
473
                                //printf("Error: Delay between packets value\n");
 
474
                                error("Error: Delay between packets value (1-999999999)");
 
475
                                return -1;
 
476
                        }
 
477
                }
 
478
 
 
479
                /* YYY if the built packet is shorter then 60 bytes, we add padding zero bytes 
 
480
                 * to fill up the length till 60 (min ethrenet frame length). This bytes will be 
 
481
                 * added anyway by the device driver, so we do this just to prevent misunderstanding: 
 
482
                 * till now if your packet  was 20 bytes long, then it was also said -> 20 bytes 
 
483
                 * sent on eth0... but actually 60 bytes (+CRC) were sent */
 
484
                if (number < 60) {
 
485
                        memset(&packet[number], 0x00, ( 60 - number ) );
 
486
                        /* there were problems with sendbuilt function, if the packet was shorter then
 
487
                         * 60 bytes. Checksum was wrong calculated */
 
488
                        /* number = 60; */
 
489
                }
 
490
 
 
491
                params1.button = statusbar;                     
 
492
                params1.context_id = context_id;
 
493
                params1.button1 =  button1;
 
494
                params1.button2 =  button2;
 
495
                params1.button3 =  button3;
 
496
                params1.button4 =  button4;
 
497
                params1.button5 =  button5;
 
498
                params1.button6 =  button6;
 
499
                params1.stopbt = stopbt;
 
500
                params1.udpstart = udp_start;
 
501
                params1.tcpstart = tcp_start;
 
502
                params1.ipstart = ip_start;
 
503
                params1.ethstart = eth_start;
 
504
 
 
505
                if (GTK_TOGGLE_BUTTON(reltime)->active) 
 
506
                        params1.timeflag = 1;
 
507
                else
 
508
                        params1.timeflag = 0;
 
509
 
 
510
                //gtk_widget_set_sensitive (button1, FALSE);
 
511
                //gtk_widget_set_sensitive (button2, FALSE);
 
512
                //gtk_widget_set_sensitive (button3, FALSE);
 
513
                //gtk_widget_set_sensitive (button4, FALSE);
 
514
                //gtk_widget_set_sensitive (button5, FALSE);
 
515
                //gtk_widget_set_sensitive (stopbt, TRUE);
 
516
 
 
517
                snprintf(buff, 100, "  Starting generator...");
 
518
                gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
519
 
 
520
                pthread_create(&thread_id, NULL, &sendbuilt, &params1);
 
521
                return 1;
 
522
        }
 
523
 
 
524
        /* is it the generator that sends different sequences? */
 
525
        else if (page == 2) { 
 
526
                char buff4[101];
 
527
                FILE *file_p;
 
528
                int j = 0, sum = 0, sum1 = 0;
 
529
                
 
530
                abstime = lookup_widget(GTK_WIDGET (button), "radiobutton36");
 
531
                reltime = lookup_widget(GTK_WIDGET (button), "radiobutton37");
 
532
                toolbar = lookup_widget(GTK_WIDGET (button), "toolbar1");
 
533
                stopbt = lookup_widget(GTK_WIDGET (button), "Stop_button");
 
534
                optm1 = lookup_widget(GTK_WIDGET (button), "checkbutton36");
 
535
                optm2 = lookup_widget(GTK_WIDGET (button), "entry151");
 
536
                optm3 = lookup_widget(GTK_WIDGET (button), "entry152");
 
537
                button1 = lookup_widget(GTK_WIDGET (button), "Build_button");
 
538
                button2 = lookup_widget(GTK_WIDGET (button), "Gen_button");
 
539
                button3 = lookup_widget(GTK_WIDGET (button), "Genp");
 
540
                button4 = lookup_widget(GTK_WIDGET (button), "Interface_button");
 
541
                button5 = lookup_widget(GTK_WIDGET (button), "Send_button");
 
542
                button6 = lookup_widget(GTK_WIDGET (button), "Gensbt");
 
543
 
 
544
                en1_t = (char *)gtk_entry_get_text(GTK_ENTRY(optm2));
 
545
                en2_t = (char *)gtk_entry_get_text(GTK_ENTRY(optm3));
 
546
 
 
547
                if (GTK_TOGGLE_BUTTON(reltime)->active) 
 
548
                        params1.timeflag = 1;
 
549
                else
 
550
                        params1.timeflag = 0;
 
551
 
 
552
                /* if the togglebutton is active, just keep on sending till stop is pressed */
 
553
                if (GTK_TOGGLE_BUTTON(optm1)->active) {
 
554
                        params1.count = -3;
 
555
                }
 
556
                else {
 
557
                        /* there can be rubbish in this field */
 
558
                        if (check_digit(en1_t, strlen(en1_t), 
 
559
                                                        "Error: Number of sequences to send field") == -1)
 
560
                                        return -1;
 
561
 
 
562
                        params1.count = strtol(en1_t, (char **)NULL, 10);
 
563
                        /* we allow to send 9999999 max */
 
564
                        if ( (params1.count > 9999999) || (params1.count < 1) ) {
 
565
                                //printf("Error: Number of sequences to send field\n");
 
566
                                error("Error: Number of sequences to send field (1 - 9999999)");
 
567
                                return -1;
 
568
                        }
 
569
                }
 
570
 
 
571
                /* if the above number is 1, we don't need to check for this delay */
 
572
                if (params1.count == 1) 
 
573
                        params1.del = 0;
 
574
                else {
 
575
                        /* there can be rubbish in this field */
 
576
                        if (check_digit(en2_t, strlen(en2_t), "Error: Delay between sequences field") == -1)
 
577
                                        return -1;
 
578
 
 
579
                        params1.del = strtol(en2_t, (char **)NULL, 10);
 
580
                        /* max delay 999,999999 s */
 
581
                        if ( (params1.del > 999999999) || (params1.del < 0) ) {
 
582
                                //printf("Error: Delay between sequences field\n");
 
583
                                error("Error: Delay between sequences field (0 - 999999999)");
 
584
                                return -1;
 
585
                        }
 
586
                }
 
587
 
 
588
                /* we fill in a table with the parameters */
 
589
                for (i=0; i<10; i++) {
 
590
 
 
591
                        /* name of the packet and packet contents */
 
592
                        snprintf(buff4, 100, "entry%d", 111+i);
 
593
                        en1 = lookup_widget(GTK_WIDGET (button), buff4);
 
594
                        en1_t = (char *)gtk_entry_get_text(GTK_ENTRY(en1));
 
595
 
 
596
                        /* enable or disable */
 
597
                        snprintf(buff4, 100, "checkbutton%d", 25+i);
 
598
                        ckbt1 = lookup_widget(GTK_WIDGET(button), buff4);
 
599
                        if (GTK_TOGGLE_BUTTON(ckbt1)->active) {
 
600
                                params1.partable[i][5] = 0;
 
601
                                continue;
 
602
                        }
 
603
                        else 
 
604
                                params1.partable[i][5] = 1;
 
605
 
 
606
                        /* if there is no name, skip it */
 
607
                        if ( strlen(en1_t) == 0 )  {
 
608
                                params1.partable[i][0] = 0;
 
609
                                continue;
 
610
                        }
 
611
                        else
 
612
                                params1.partable[i][0] = 1;
 
613
 
 
614
                        /* open file for reading */
 
615
                        if ( (file_p = fopen(en1_t, "r")) == NULL) {
 
616
                                snprintf(buff4, 100, "Error: Can not open file for reading:%s", en1_t);
 
617
                                //printf("Error: Can not open file for reading %s\n", en1_t);
 
618
                                error(buff4);
 
619
                                return -1;
 
620
                        }
 
621
 
 
622
                        /* we have to read the packet contents stored in a file */
 
623
                        {
 
624
                        struct pcap_hdr fh;
 
625
                        struct pcaprec_hdr ph;
 
626
                        char pkt_temp[100];
 
627
                        int freads;
 
628
 
 
629
                        /* first we read the pcap file header */
 
630
                        freads = fread(pkt_temp, sizeof(fh), 1, file_p);
 
631
                        /* if EOF, exit */
 
632
                        if (freads == 0)
 
633
                                return 1;
 
634
 
 
635
                         memcpy(&fh, pkt_temp, 24);
 
636
 
 
637
                         /* if magic number in NOK, exit */
 
638
                         if (fh.magic != PCAP_MAGIC)
 
639
                                return -1;
 
640
 
 
641
                        /* next the  pcap packet header */
 
642
                        freads = fread(pkt_temp, sizeof(ph), 1, file_p);
 
643
 
 
644
                        /* if EOF, exit */
 
645
                        if (freads == 0)
 
646
                                return 1;
 
647
 
 
648
                        /* copy the 16 bytes into ph structure */
 
649
                        memcpy(&ph, pkt_temp, 16);
 
650
 
 
651
                        /* and the packet itself, but only up to the capture length */
 
652
                        freads = fread(&params1.pkttable[i][0], ph.incl_len, 1, file_p);
 
653
 
 
654
                        /* if EOF, exit */
 
655
                        if (freads == 0)
 
656
                                return 1;
 
657
 
 
658
                        fclose(file_p);
 
659
                        params1.partable[i][1] = ph.incl_len;
 
660
                        }
 
661
 
 
662
                        /* number of packets to send */
 
663
                        snprintf(buff4, 100, "entry%d", 121+i);
 
664
                        en2 = lookup_widget(GTK_WIDGET (button), buff4);
 
665
                        en2_t = (char *)gtk_entry_get_text(GTK_ENTRY(en2));
 
666
                        snprintf(buff4, 100, "Error: Number of packets field in row %d", i+1);
 
667
                        if (check_digit(en2_t,strlen(en2_t), buff4) == -1)
 
668
                                        return -1;
 
669
 
 
670
                        params1.partable[i][2] = strtol(en2_t, (char **)NULL, 10);
 
671
                        /* we allow to send 9999999 max */
 
672
                        if ( (params1.partable[i][2] > 9999999) || (params1.partable[i][2] < 0) ) {
 
673
                                snprintf(buff4, 100, "Error: number of packets value in row %d", i+1);
 
674
                                //printf("Error: number of packets value in row %d\n", i+1);
 
675
                                error(buff4);
 
676
                                return -1;
 
677
                        }
 
678
 
 
679
                        /* delay between packets */
 
680
                        snprintf(buff4, 100, "entry%d", 131+i);
 
681
                        en3 = lookup_widget(GTK_WIDGET (button), buff4);
 
682
                        en3_t = (char *)gtk_entry_get_text(GTK_ENTRY(en3));
 
683
                        snprintf(buff4, 100, "Error: Delay between packets field in row %d", i+1);
 
684
                        if (check_digit(en3_t,strlen(en3_t), buff4) == -1)
 
685
                                        return -1;
 
686
 
 
687
                        params1.partable[i][3] = strtol(en3_t, (char **)NULL, 10);
 
688
                        /* max delay 999,999999 s */
 
689
                        if ( (params1.partable[i][3] > 999999999) || (params1.partable[i][3] < 0) ) {
 
690
                                snprintf(buff4, 100, "Error: delay between value in row %d", i+1);
 
691
                                //printf("Error: delay between value in row %d\n", i+1);
 
692
                                error(buff4);
 
693
                                return -1;
 
694
                        }
 
695
 
 
696
                        /* delay to next sequence */
 
697
                        snprintf(buff4, 100, "entry%d", 141+i);
 
698
                        en4 = lookup_widget(GTK_WIDGET (button), buff4);
 
699
                        en4_t = (char *)gtk_entry_get_text(GTK_ENTRY(en4));
 
700
                        snprintf(buff4, 100, "Error: Delay to next value in row %d", i+1);
 
701
                        if (check_digit(en4_t,strlen(en4_t), buff4) == -1)
 
702
                                        return -1;
 
703
 
 
704
                        params1.partable[i][4] = strtol(en4_t, (char **)NULL, 10);
 
705
                        /* max delay 999,999999 s */
 
706
                        if ( (params1.partable[i][4] > 999999999) || (params1.partable[i][4] < 0) ) {
 
707
                                snprintf(buff4, 100, "Error: delay to next value in row %d", i+1);
 
708
                                //printf("Error: delay to next value in row %d\n", i+1);
 
709
                                error(buff4);
 
710
                                return -1;
 
711
                        }
 
712
                        
 
713
                }       
 
714
 
 
715
                /* if all the fields are empty or disabled we return immediattely */
 
716
                for (j=0; j<10; j++) {
 
717
                        sum = sum + params1.partable[j][0];
 
718
                        sum1 = sum1 + params1.partable[j][5];
 
719
                }
 
720
 
 
721
                if ( (sum ==0 ) || (sum1 == 0) ) {
 
722
                        snprintf(buff, 100, "  Nothing to send...");
 
723
                        gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
724
                        return 1;
 
725
                }
 
726
 
 
727
                params1.button = statusbar;                     
 
728
                params1.context_id = context_id;
 
729
                params1.toolbar = toolbar;
 
730
                params1.stopbt = stopbt;
 
731
                params1.button1 =  button1;
 
732
                params1.button2 =  button2;
 
733
                params1.button3 =  button3;
 
734
                params1.button4 =  button4;
 
735
                params1.button5 =  button5;
 
736
                params1.button6 =  button6;
 
737
        
 
738
                snprintf(buff, 100, "  Starting sequence generator...");
 
739
                gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
740
 
 
741
                pthread_create(&thread_id, NULL, &sendsequence, &params1);
 
742
                return 1;
 
743
        }
 
744
 
 
745
        /* is it the generator that uses the kernel module? */
 
746
        else if (page == 3) { 
 
747
                ;
 
748
        }
 
749
 
 
750
        return 1;
 
751
}
 
752
 
 
753
 
 
754
int make_packet(GtkButton *button, gpointer user_data)
 
755
{
 
756
        GtkWidget *ipv4, *ipv6, *arp, *usedef;
 
757
        GtkWidget *auto_bt, *text_e;
 
758
        int max, length;
 
759
        gchar *text;
 
760
 
 
761
        /* first we fill packet field with 0 */
 
762
        memset(packet, 0x00, 1522);
 
763
 
 
764
        /* YYY what about auto selection for link layer is on?
 
765
         * in case of saving packet we don't get here and it is ok
 
766
         * in case of user defined payload we automatically disable this feature and it is ok
 
767
         * so what about arp, ipv4 and ipv6?
 
768
         * in case of an arp packet we accept the auto get mac option and it means that 
 
769
         * we take the source and destination mac address from the arp protokol field
 
770
         * in case of an ipv4 packet this means that we don't open the raw socket but
 
771
         * do all the sending on ip socket which helps us getting the mac address */
 
772
        
 
773
        auto_bt = lookup_widget(GTK_WIDGET (button), "auto_get_mac_cbt");
 
774
        ipv4 = lookup_widget(GTK_WIDGET (button), "ippkt_radibt");
 
775
        ipv6 = lookup_widget(GTK_WIDGET (button), "IPv6_rdbt");
 
776
        arp = lookup_widget(GTK_WIDGET (button), "arppkt_radiobt");
 
777
        usedef = lookup_widget(GTK_WIDGET (button), "usedef2_radibt");
 
778
 
 
779
        /* what about next layer: ipv4, ipv6, arp or manually attached payload? */
 
780
        if (GTK_TOGGLE_BUTTON(ipv4)->active) {
 
781
                /* YYY - to be done */
 
782
                if (GTK_TOGGLE_BUTTON(auto_bt)->active) {
 
783
                                error("Not yet implemented");
 
784
                                //printf("Not yet implemented\n");
 
785
                                return -1;
 
786
                }
 
787
                
 
788
                /* now we get the link layer info */
 
789
                else if (link_level_get(button, user_data) == -1) {
 
790
                        //printf("Error: problem on link layer with IPv4 packet\n");
 
791
                        return -1;
 
792
                }
 
793
 
 
794
                /* call the function that gets the ipv4 protocol information */
 
795
                if (ipv4_get(button, user_data) == -1) {
 
796
                        //printf("Error: problem with IPv4 information\n");
 
797
                        return -1;
 
798
                }
 
799
                
 
800
                /* grrr, oh you could think on this earlier!!! */
 
801
                if (autolength > 0) {
 
802
                        //printf("tole je auto %d tole pa number %d\n", autolength, number);
 
803
                        packet[autolength] = (unsigned char)((number - (autolength + 2))/256);
 
804
                        packet[autolength+1] = (unsigned char)((number - (autolength + 2))%256);
 
805
                }       
 
806
                
 
807
                return 1;
 
808
        }
 
809
        else if (GTK_TOGGLE_BUTTON(ipv6)->active) {
 
810
                error("Not yet implemented");
 
811
                //printf("Not yet implemented\n");
 
812
                return 1;
 
813
        }
 
814
        else if (GTK_TOGGLE_BUTTON(arp)->active) {
 
815
                /* we need to check the auto button, if it is on we copy the values from the 
 
816
                 * arp filed entries in to the link layer source and destination mac entries 
 
817
                 * the good thing about this is that we add a feature that can help
 
818
                 * (or confuse?) the bad thing about this is that we can't do it once
 
819
                 * with the link layer and take whatever the next layer is*/
 
820
                if (GTK_TOGGLE_BUTTON(auto_bt)->active) {
 
821
                        GtkWidget *en_dst_mac, *en_src_mac, *L_dst, *L_src;
 
822
                        //gchar tmp_dst[18], tmp_src[18];
 
823
                        gchar *en_dst_t, *en_src_t;
 
824
 
 
825
                        en_dst_mac = lookup_widget(GTK_WIDGET(button), "A_targetmac");
 
826
                        en_src_mac = lookup_widget(GTK_WIDGET(button), "A_sendermac");
 
827
                        L_dst = lookup_widget(GTK_WIDGET(button), "L_dst_mac");
 
828
                        L_src = lookup_widget(GTK_WIDGET(button), "L_src_mac");
 
829
 
 
830
                        en_dst_t = (char *)gtk_entry_get_text(GTK_ENTRY(en_dst_mac));
 
831
                        en_src_t = (char *)gtk_entry_get_text(GTK_ENTRY(en_src_mac));
 
832
                        
 
833
                        /*chech if both addresses are ok */
 
834
                        if (check_mac_address(en_dst_t) == -1) {
 
835
                                //printf("Error: wrong mac entry in arp destination field, can't copy it\n");
 
836
                                error("Error: wrong mac entry in arp destination field, can't copy it");
 
837
                                return -1;
 
838
                        }
 
839
                         
 
840
                        /*chech if both addresses are ok */
 
841
                        if (check_mac_address(en_src_t) == -1) {
 
842
                                //printf("Error: wrong mac entry in arp source field, can't copy it\n");
 
843
                                error("Error: wrong mac entry in arp source field, can't copy it");
 
844
                                return -1;
 
845
                        }
 
846
 
 
847
                        /*insert it in the link layer field */
 
848
                        gtk_entry_set_text(GTK_ENTRY(L_dst), en_dst_t);
 
849
                        gtk_entry_set_text(GTK_ENTRY(L_src), en_src_t);
 
850
 
 
851
                }
 
852
         
 
853
                /* now we get the link layer info */
 
854
                if (link_level_get(button, user_data) == -1) {
 
855
                        //printf("Error: problem on link layer with arp packet\n");
 
856
                        return -1;
 
857
                }
 
858
 
 
859
                /* call the function that gets the arp protocol information */
 
860
                if (arp_get(button, user_data) == -1) {
 
861
                        //printf("Error: problem with arp information\n");
 
862
                        return -1;
 
863
                }
 
864
                        
 
865
                if (autolength > 0) {
 
866
                        //printf("tole je auto %d tole pa number %d\n", autolength, number);
 
867
                        packet[autolength] = (unsigned char)((number - (autolength + 2))/256);
 
868
                        packet[autolength+1] = (unsigned char)((number - (autolength + 2))%256);
 
869
                }       
 
870
                return 1;
 
871
        }
 
872
        
 
873
        else if (GTK_TOGGLE_BUTTON(usedef)->active) {
 
874
                /* if usedef is active we will manually get the link layer info */
 
875
                if (link_level_get(button, user_data) == -1) {
 
876
                        //printf("Error: problem on link layer\n");
 
877
                        return -1;
 
878
                }
 
879
                
 
880
                if((number == 14) || (number == 18))
 
881
                        max = 1500;
 
882
                else if ((number == 17) || (number == 21))
 
883
                        max = 1497;
 
884
                else if ((number == 22) || (number == 26))
 
885
                        max = 1492;
 
886
                else {
 
887
                        //printf("Error: failure with number in layer 2 user defined field\n");
 
888
                        error("Error: failure with number in layer 2 user defined payload");
 
889
                        return -1;
 
890
                }
 
891
 
 
892
                text_e = lookup_widget(GTK_WIDGET (button), "text1");
 
893
                GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_e));
 
894
                length = gtk_text_buffer_get_char_count(buffer);
 
895
                //text = (char *) malloc(length + 1);
 
896
                GtkTextIter start,end;
 
897
                //gtk_text_buffer_get_start_iter(buffer,start);
 
898
                //gtk_text_buffer_get_end_iter(buffer,end);
 
899
                gtk_text_buffer_get_bounds(buffer,&start,&end);
 
900
                text = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);      
 
901
                //text = gtk_editable_get_chars(GTK_EDITABLE(text_e), 0, -1);
 
902
                //g_free(start);
 
903
                //g_free(end);
 
904
        
 
905
                if (get_network_payload(button, user_data, length, max, text) == -1) {
 
906
                        //printf("Error: problem with payload on network layer\n");
 
907
                        g_free(text);
 
908
                        return -1;
 
909
                }
 
910
                else
 
911
                        g_free(text);
 
912
 
 
913
                if (autolength > 0) {
 
914
                        //printf("tole je auto %d tole pa number %d\n", autolength, number);
 
915
                        packet[autolength] = (unsigned char)((number - (autolength + 2))/256);
 
916
                        packet[autolength+1] = (unsigned char)((number - (autolength + 2))%256);
 
917
                }       
 
918
        }
 
919
        else {  /* none of above -> something is wrong! */
 
920
                //printf("Error: problem with network layer button\n");
 
921
                error("Error: problem with network layer button");
 
922
                return -1;
 
923
        }
 
924
                
 
925
        return 1;
 
926
}
 
927
 
 
928
 
 
929
/* let's parse the IPv4 protokol information */
 
930
int ipv4_get(GtkButton *button, gpointer user_data) {
 
931
        GtkWidget *version, *header_length, *tos, *total_length, *identification, *flags;
 
932
        GtkWidget *frag_offset, *ttl, *protocol, *header_cks, *header_cks_bt;
 
933
        GtkWidget *src_ip, *dst_ip, *options, *total_length_bt;
 
934
        GtkWidget *udp_bt, *tcp_bt, *icmp_bt, *igmp_bt, *usedef_bt, *pay_text_e;
 
935
        gchar *version_t, *header_length_t, *tos_t, *total_length_t, *identification_t, *flags_t;
 
936
        gchar *frag_offset_t, *ttl_t, *protocol_t, *header_cks_t;
 
937
        gchar *src_ip_t, *dst_ip_t, *options_t, *pay_text;
 
938
        int length_start, header_cks_start, cks_start, cks_stop;
 
939
        gchar tmp[4];
 
940
        int i, j, pay_length, pay_max;
 
941
        guint16 value, ipcksum;
 
942
        guint32 pseudo_header_sum;
 
943
        
 
944
        version = lookup_widget(GTK_WIDGET(button), "entry26");
 
945
        header_length = lookup_widget(GTK_WIDGET(button), "entry27");
 
946
        tos = lookup_widget(GTK_WIDGET(button), "entry28");
 
947
        total_length = lookup_widget(GTK_WIDGET(button), "entry29");
 
948
        total_length_bt = lookup_widget(GTK_WIDGET(button), "checkbutton21");
 
949
        identification = lookup_widget(GTK_WIDGET(button), "entry30");
 
950
        flags = lookup_widget(GTK_WIDGET(button), "entry31");
 
951
        frag_offset = lookup_widget(GTK_WIDGET(button), "entry32");
 
952
        ttl = lookup_widget(GTK_WIDGET(button), "entry44");
 
953
        protocol = lookup_widget(GTK_WIDGET(button), "entry34");
 
954
        header_cks = lookup_widget(GTK_WIDGET(button), "entry35");
 
955
        header_cks_bt = lookup_widget(GTK_WIDGET(button), "ip_header_cks_cbt");
 
956
        src_ip = lookup_widget(GTK_WIDGET(button), "entry38");
 
957
        dst_ip = lookup_widget(GTK_WIDGET(button), "entry37");
 
958
        options = lookup_widget(GTK_WIDGET(button), "entry39");
 
959
        udp_bt = lookup_widget(GTK_WIDGET(button), "udp_bt");
 
960
        tcp_bt = lookup_widget(GTK_WIDGET(button), "tcp_bt");
 
961
        icmp_bt = lookup_widget(GTK_WIDGET(button), "icmp_bt");
 
962
        igmp_bt = lookup_widget(GTK_WIDGET(button), "igmp_bt");
 
963
        usedef_bt = lookup_widget(GTK_WIDGET(button), "ip_user_data_bt");
 
964
        
 
965
        version_t = (char *)gtk_entry_get_text(GTK_ENTRY(version));
 
966
        header_length_t = (char *)gtk_entry_get_text(GTK_ENTRY(header_length));
 
967
        tos_t = (char *)gtk_entry_get_text(GTK_ENTRY(tos));
 
968
        total_length_t = (char *)gtk_entry_get_text(GTK_ENTRY(total_length));
 
969
        identification_t = (char *)gtk_entry_get_text(GTK_ENTRY(identification));
 
970
        flags_t = (char *)gtk_entry_get_text(GTK_ENTRY(flags));
 
971
        frag_offset_t = (char *)gtk_entry_get_text(GTK_ENTRY(frag_offset));
 
972
        ttl_t = (char *)gtk_entry_get_text(GTK_ENTRY(ttl));
 
973
        protocol_t = (char *)gtk_entry_get_text(GTK_ENTRY(protocol));
 
974
        header_cks_t = (char *)gtk_entry_get_text(GTK_ENTRY(header_cks));
 
975
        src_ip_t = (char *)gtk_entry_get_text(GTK_ENTRY(src_ip));
 
976
        dst_ip_t = (char *)gtk_entry_get_text(GTK_ENTRY(dst_ip));
 
977
        options_t = (char *)gtk_entry_get_text(GTK_ENTRY(options));
 
978
        
 
979
        /* we want to know where the ip header starts, to calculate the checksum later */
 
980
        cks_start = number;
 
981
 
 
982
        /* now we have all the widgets, so let start parsing them */
 
983
        /* starting with version */
 
984
        strncpy(&tmp[0], version_t, 1);
 
985
        strncpy(&tmp[1], header_length_t, 1);
 
986
        
 
987
        if (char2x(tmp) == -1) {
 
988
                //printf("Error: ipv4 version or header length field\n");
 
989
                error("Error: ipv4 version or header length field");
 
990
                return -1;
 
991
        }
 
992
        packet[number] = (unsigned char)char2x(tmp);
 
993
        number++;
 
994
        
 
995
        /* tos field */
 
996
        if (char2x(tos_t) == -1) {
 
997
                //printf("Error: ipv4 tos field\n");
 
998
                error("Error: ipv4 tos field");
 
999
                return -1;
 
1000
        }
 
1001
        packet[number] = (unsigned char)char2x(tos_t);
 
1002
        number++;
 
1003
 
 
1004
        /* total length */
 
1005
        /* if auto is on then we have to calculate this, but we can do this
 
1006
         * at the end, when we have the whole ip packet together. so if auto 
 
1007
         * is enabled we set the marking and recaltulate it in the end */
 
1008
        if (GTK_TOGGLE_BUTTON(total_length_bt)->active) {
 
1009
                length_start = number;
 
1010
                number++;
 
1011
                number++;
 
1012
        }
 
1013
        else {
 
1014
                length_start = 0; /* if length start is 0, then we leave it in the end */
 
1015
                if ( (atol(total_length_t) < 0) || (atol(total_length_t) > 65535) ) {
 
1016
                        //printf("Error: ipv4 total length range\n");
 
1017
                        error("Error: ipv4 total length range");
 
1018
                        return -1;
 
1019
                }
 
1020
 
 
1021
                /* there can be rubbish in this field */
 
1022
                if (check_digit(total_length_t, strlen(total_length_t), 
 
1023
                                        "Error: ipv4 total length field values") == -1)
 
1024
                                return -1;
 
1025
 
 
1026
                packet[number] = (char)(atol(total_length_t)/256);
 
1027
                number++;       
 
1028
                packet[number] = (char)(atol(total_length_t)%256);
 
1029
                number++;       
 
1030
        }
 
1031
        
 
1032
        /* identification */
 
1033
        if (char2x(identification_t) == -1) {
 
1034
                //printf("Error: ipv4 identification field\n");
 
1035
                error("Error: ipv4 identification field");
 
1036
                return -1;
 
1037
        }
 
1038
        packet[number] = (unsigned char)char2x(identification_t);
 
1039
        number++;
 
1040
        identification_t++; identification_t++;
 
1041
        if (char2x(identification_t) == -1) {
 
1042
                //printf("Error: ipv4 identification field\n");
 
1043
                error("Error: ipv4 identification field");
 
1044
                return -1;
 
1045
        }
 
1046
        packet[number] = (unsigned char)char2x(identification_t);
 
1047
        number++;
 
1048
 
 
1049
        /* flags and fragment offset */
 
1050
        if ( (atoi(flags_t) > 7) || (atoi(flags_t) < 0) ) {
 
1051
                //printf("Error: ipv4 flags field: the value can be beetwen 0 and 7\n");
 
1052
                error("Error: ipv4 flags field: the value can be beetwen 0 and 7");
 
1053
                return -1;
 
1054
        }
 
1055
                
 
1056
        if ( (atoi(frag_offset_t) > 8191) || (atoi(frag_offset_t) < 0) ) {
 
1057
                //printf("Error: ipv4 fragmentation offset field: (0 - 8191)\n");
 
1058
                error("Error: ipv4 fragmentation offset field: (0 - 8191)");
 
1059
                return -1;
 
1060
        }
 
1061
 
 
1062
        /* there can be rubbish in this field */
 
1063
        if (check_digit(flags_t, strlen(flags_t), "Error: ipv4 flags values") == -1)
 
1064
                                return -1;
 
1065
 
 
1066
        /* there can be rubbish in this field */
 
1067
        if (check_digit(frag_offset_t, strlen(frag_offset_t), 
 
1068
                                        "Error: ipv4 fragmentation offset field values ") == -1)
 
1069
                                return -1;
 
1070
 
 
1071
        /* this is the correct int value now 
 
1072
         * we need to store it as 2 byte hex value */
 
1073
        value = (atoi(flags_t)<<13   & 0xE000) | 
 
1074
                (atoi(frag_offset_t) & 0x1FFF) ;
 
1075
        
 
1076
        /* YYY what about big endian computers - hope it works */
 
1077
        value = htons(value);
 
1078
        memcpy(&packet[number], &value, 2);
 
1079
        number++;
 
1080
        number++;
 
1081
                
 
1082
        /* ttl value */
 
1083
        if ( (atoi(ttl_t) < 0) || (atoi(ttl_t) > 255) ) {
 
1084
                //printf("Error: ipv4 ttl range\n");
 
1085
                error("Error: ipv4 ttl range");
 
1086
                return -1;
 
1087
        }
 
1088
 
 
1089
        /* there can be rubbish in this field */
 
1090
        if (check_digit(ttl_t, strlen(ttl_t), "Error: ipv4 ttl field values") == -1)
 
1091
                                return -1;
 
1092
 
 
1093
        packet[number] = (char)(atoi(ttl_t));
 
1094
        number++;       
 
1095
 
 
1096
        /* protocol field */
 
1097
        if ( (atoi(protocol_t) < 0) || (atoi(protocol_t) > 255) ) {
 
1098
                //printf("Error: ipv4 protocol range\n");
 
1099
                error("Error: ipv4 protocol range");
 
1100
                return -1;
 
1101
        }
 
1102
 
 
1103
        /* there can be rubbish in this field */
 
1104
        if (check_digit(protocol_t, strlen(protocol_t), "Error: ipv4 protocol field values") == -1)
 
1105
                                return -1;
 
1106
 
 
1107
        packet[number] = (char)(atoi(protocol_t));
 
1108
        number++;       
 
1109
 
 
1110
        pseudo_header_sum = (guint32)(packet[number-1]);
 
1111
        
 
1112
        /* header checksum */
 
1113
        /* if auto is on then we have to calculate this, but we can do this
 
1114
         * at the end and recaltulate it for now we store the current number into
 
1115
         * another variable. we will calculate length in the end */
 
1116
        if (GTK_TOGGLE_BUTTON(header_cks_bt)->active) {
 
1117
                header_cks_start = number;
 
1118
                packet[number] = (unsigned char)0;
 
1119
                number++;
 
1120
                packet[number] = (unsigned char)0;
 
1121
                number++;
 
1122
        }
 
1123
        else {
 
1124
                /* if header_cks_start = 0, we leave it in the end */
 
1125
                header_cks_start = 0;
 
1126
                if (char2x(header_cks_t) == -1) {
 
1127
                        //printf("Error: ipv4 header checksum field\n");
 
1128
                        error("Error: ipv4 header checksum field");
 
1129
                        return -1;
 
1130
                }
 
1131
                packet[number] = (unsigned char)char2x(header_cks_t);
 
1132
                header_cks_t++; header_cks_t++; number++;
 
1133
                if (char2x(header_cks_t) == -1) {
 
1134
                        //printf("Error: ipv4 header checksum field\n");
 
1135
                        error("Error: ipv4 header checksum field");
 
1136
                        return -1;
 
1137
                }
 
1138
                packet[number] = (unsigned char)char2x(header_cks_t);
 
1139
                number++;
 
1140
        }
 
1141
        
 
1142
        /* source ip address */
 
1143
        ip_start = number;
 
1144
 
 
1145
        if (check_ip_address(src_ip_t) == -1) {
 
1146
                //printf("Error: Wrong source ipv4 address\n");
 
1147
                error("Error: Wrong source ipv4 address");
 
1148
                return -1;
 
1149
        }
 
1150
        
 
1151
        for (i=0; i<4; i++) {
 
1152
                for(j=0; j<4 && (*src_ip_t != '\0'); j++) {
 
1153
                        if ( ((int)*src_ip_t == '.') && (i<3) && (j>0) ) {
 
1154
                                src_ip_t++;
 
1155
                                break;
 
1156
                        }
 
1157
                        tmp[j] = *src_ip_t;
 
1158
                        src_ip_t++;
 
1159
                }
 
1160
                tmp[j] = '\0';
 
1161
                packet[number] = (unsigned char)(atoi(tmp));
 
1162
                number++;               
 
1163
        }
 
1164
 
 
1165
        /* destination ip address */
 
1166
        if (check_ip_address(dst_ip_t) == -1) {
 
1167
                //printf("Error: Wrong destination ipv4 address\n");
 
1168
                error("Error: Wrong destination ipv4 address");
 
1169
                return -1;
 
1170
        }
 
1171
        
 
1172
        for (i=0; i<4; i++) {
 
1173
                for(j=0; j<4 && (*dst_ip_t != '\0'); j++) {
 
1174
                        if ( ((int)*dst_ip_t == '.') && (i<3) && (j>0) ) {
 
1175
                                dst_ip_t++;
 
1176
                                break;
 
1177
                        }
 
1178
                        tmp[j] = *dst_ip_t;
 
1179
                        dst_ip_t++;
 
1180
                }
 
1181
                tmp[j] = '\0';
 
1182
                packet[number] = (unsigned char)(atoi(tmp));
 
1183
                number++;               
 
1184
        }
 
1185
 
 
1186
        /* this is checksum for protocol field plus IP source and destination 
 
1187
         * we need this later when we want to calculate the TCP or UDP checksum
 
1188
         * there we need this values so we pass them when calling the routine*/
 
1189
        pseudo_header_sum = pseudo_header_sum + get_checksum32(number-8, number-1);
 
1190
        
 
1191
        /* options? do allow then and how long can they be??? 
 
1192
         * ok we allow them and limit them to 40 bytes and the user should 
 
1193
         * care that the options length is always a multiple of 32 bits (4 bytes) */
 
1194
        if ( (strlen(options_t)%8) != 0) {
 
1195
                //printf("Error: Wrong ipv4 length of options field (length mod 8 must be 0)\n");
 
1196
                error("Error: Wrong ipv4 length of options field      \n(length mod 8 must be 0)");
 
1197
                return -1;
 
1198
        }
 
1199
        
 
1200
        if ( strlen(options_t) > 80) {
 
1201
                //printf("Error: ipv4 options field to long\n");
 
1202
                error("Error: ipv4 options field to long");
 
1203
                return -1;
 
1204
        }
 
1205
        
 
1206
        j = strlen(options_t)/2;
 
1207
        for (i=0; i<j; i++) {
 
1208
                if (char2x(options_t) == -1) {
 
1209
                        //printf("Error: ipv4 options field\n");
 
1210
                        error("Error: ipv4 options field");
 
1211
                        return -1;
 
1212
                }
 
1213
                packet[number] = (unsigned char)char2x(options_t);
 
1214
                number++; options_t++; options_t++;
 
1215
        }
 
1216
 
 
1217
        cks_stop = number;
 
1218
 
 
1219
        /* so we came to the end of ip header. what is next? */
 
1220
        /* tcp, udp, icmp or manually attached payload? */
 
1221
        if (GTK_TOGGLE_BUTTON(udp_bt)->active) {
 
1222
                if (udp_get(button, user_data, pseudo_header_sum) == -1) {
 
1223
                        //printf("Error: Problem with UDP information\n");
 
1224
                        return -1;
 
1225
                }
 
1226
        }
 
1227
        
 
1228
        else if (GTK_TOGGLE_BUTTON(tcp_bt)->active) {
 
1229
                if (tcp_get(button, user_data, pseudo_header_sum) == -1) {
 
1230
                        //printf("Error: Problem with TCP information\n");
 
1231
                        return -1;
 
1232
                }
 
1233
        }       
 
1234
        
 
1235
        else if (GTK_TOGGLE_BUTTON(icmp_bt)->active) {
 
1236
                if (icmp_get(button, user_data) == -1) {
 
1237
                        //printf("Error: Problem with ICMP information\n");
 
1238
                        return -1;
 
1239
                }
 
1240
        }       
 
1241
        
 
1242
        else if (GTK_TOGGLE_BUTTON(igmp_bt)->active) {
 
1243
                if (igmp_get(button, user_data) == -1) {
 
1244
                        //printf("Error: Problem with IGMP information\n");
 
1245
                        return -1;
 
1246
                }
 
1247
        }       
 
1248
 
 
1249
        else if (GTK_TOGGLE_BUTTON(usedef_bt)->active) {
 
1250
                        
 
1251
                pay_text_e = lookup_widget(GTK_WIDGET (button), "text2");
 
1252
                GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pay_text_e));
 
1253
                pay_length = gtk_text_buffer_get_char_count(buffer);    
 
1254
                GtkTextIter start,end;
 
1255
                //gtk_text_buffer_get_start_iter(buffer,start);
 
1256
                //gtk_text_buffer_get_end_iter(buffer,end);
 
1257
                gtk_text_buffer_get_bounds(buffer,&start,&end);
 
1258
                pay_text = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
 
1259
                //g_free(start);
 
1260
                //g_free(end);
 
1261
                //pay_text = (char *) malloc(pay_length + 1);
 
1262
                //pay_text = gtk_editable_get_chars(GTK_EDITABLE(pay_text_e), 0, -1);
 
1263
 
 
1264
                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
1265
                pay_max = 1514 - number;
 
1266
 
 
1267
                if (get_network_payload(button, user_data, pay_length, pay_max, pay_text) == -1) {
 
1268
                        //printf("Error: Problem with IPv4 payload\n");
 
1269
                        g_free(pay_text);
 
1270
                        return -1;
 
1271
                }
 
1272
                else
 
1273
                        g_free(pay_text);
 
1274
        }
 
1275
 
 
1276
        else {
 
1277
                //printf("Error: IPv4 zoombie error!!!\n");
 
1278
                error("Error: IPv4 zoombie error!!!");
 
1279
                return -1;
 
1280
        }
 
1281
                
 
1282
        
 
1283
        /* so we are back again to cumpute the length and checksum. so this is for length */
 
1284
        if (length_start > 0) {
 
1285
                packet[length_start] = (char)((number - length_start + 2)/256);
 
1286
                packet[length_start+1] = (char)((number - length_start + 2)%256);
 
1287
        }
 
1288
 
 
1289
        /* and this for checksum */
 
1290
        if (header_cks_start > 0) {
 
1291
                ipcksum = ((-1) - get_checksum16(cks_start, cks_stop) % 0x10000);
 
1292
                packet[header_cks_start] = (char)(ipcksum/256);
 
1293
                packet[header_cks_start+1] =  (char)(ipcksum%256);
 
1294
        }
 
1295
        
 
1296
        return 1;
 
1297
}
 
1298
        
 
1299
 
 
1300
int udp_get(GtkButton *button, gpointer user_data, guint32 pseudo_header_sum) 
 
1301
{
 
1302
        
 
1303
        GtkWidget *srcport, *dstport, *length, *length_bt, *checksum, *checksum_bt;
 
1304
        GtkWidget *payload_bt, *payload;
 
1305
 
 
1306
        gchar *srcport_t, *dstport_t, *length_t, *checksum_t, *payload_t;
 
1307
 
 
1308
        int length_start, checksum_start, cks_start, cks_stop, payload_length, odd=0;
 
1309
        guint32 udpcksum;
 
1310
        
 
1311
        srcport = lookup_widget(GTK_WIDGET(button), "entry56");
 
1312
        dstport = lookup_widget(GTK_WIDGET(button), "entry41");
 
1313
        length = lookup_widget(GTK_WIDGET(button), "entry42");
 
1314
        length_bt = lookup_widget(GTK_WIDGET(button), "checkbutton3");
 
1315
        checksum = lookup_widget(GTK_WIDGET(button), "entry43");
 
1316
        checksum_bt = lookup_widget(GTK_WIDGET(button), "checkbutton4");
 
1317
        payload = lookup_widget(GTK_WIDGET(button), "text3");
 
1318
        payload_bt = lookup_widget(GTK_WIDGET(button), "checkbutton5");
 
1319
        
 
1320
        srcport_t= (char *)gtk_entry_get_text(GTK_ENTRY(srcport));
 
1321
        dstport_t= (char *)gtk_entry_get_text(GTK_ENTRY(dstport));
 
1322
        length_t= (char *)gtk_entry_get_text(GTK_ENTRY(length));
 
1323
        checksum_t= (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
1324
        
 
1325
        cks_start = number;
 
1326
        
 
1327
        /* source port */
 
1328
        if ( (atoi(srcport_t) < 0) || (atoi(srcport_t) > 65535) ) {
 
1329
                //printf("Error: Udp source port range\n");
 
1330
                error("Error: Udp source port range");
 
1331
                return -1;
 
1332
        }
 
1333
 
 
1334
        /* there can be rubbish in this field */
 
1335
        if (check_digit(srcport_t, strlen(srcport_t), "Error: Udp srcport field values") == -1)
 
1336
                                return -1;
 
1337
 
 
1338
        packet[number] = (char)(atol(srcport_t)/256);
 
1339
        number++;       
 
1340
        packet[number] = (char)(atol(srcport_t)%256);
 
1341
        number++;       
 
1342
        
 
1343
        /* destination port */
 
1344
        if ( (atoi(dstport_t) < 0) || (atoi(dstport_t) > 65535) ) {
 
1345
                //printf("Error: Udp destination port range\n");
 
1346
                error("Error: Udp destination port range");
 
1347
                return -1;
 
1348
        }
 
1349
 
 
1350
        /* there can be rubbish in this field */
 
1351
        if (check_digit(dstport_t, strlen(dstport_t), "Error: Udp destination port field values") == -1)
 
1352
                                return -1;
 
1353
 
 
1354
        packet[number] = (char)(atol(dstport_t)/256);
 
1355
        number++;       
 
1356
        packet[number] = (char)(atol(dstport_t)%256);
 
1357
        number++;       
 
1358
        
 
1359
        /* udp length */
 
1360
        if (GTK_TOGGLE_BUTTON(length_bt)->active) {
 
1361
                length_start = number;
 
1362
                number++;
 
1363
                number++;
 
1364
        }
 
1365
        else {
 
1366
                /* if length_start = 0, we leave it in the end */
 
1367
                length_start = 0;
 
1368
                if ( (atoi(length_t) < 0) || (atoi(length_t) > 65535) ) {
 
1369
                        //printf("Error: Udp length range\n");
 
1370
                        error("Error: Udp length range");
 
1371
                        return -1;
 
1372
                }
 
1373
        
 
1374
                /* there can be rubbish in this field */
 
1375
                if (check_digit(length_t, strlen(length_t), "Error: Udp length field values") == -1)
 
1376
                                return -1;
 
1377
 
 
1378
                packet[number] = (char)(atol(length_t)/256);
 
1379
                number++;       
 
1380
                packet[number] = (char)(atol(length_t)%256);
 
1381
                number++;       
 
1382
        }
 
1383
        
 
1384
        /* udp checksum */
 
1385
        if (GTK_TOGGLE_BUTTON(checksum_bt)->active) {
 
1386
                checksum_start = number;
 
1387
                packet[number] = (unsigned char)0;
 
1388
                number++;
 
1389
                packet[number] = (unsigned char)0;
 
1390
                number++;
 
1391
        }
 
1392
        else {
 
1393
                /* if checksum_start = 0, we leave it in the end */
 
1394
                checksum_start = 0;
 
1395
        
 
1396
                if (char2x(checksum_t) == -1) {
 
1397
                        //printf("Error: udp checksum field\n");
 
1398
                        error("Error: udp checksum field");
 
1399
                        return -1;
 
1400
                }
 
1401
                packet[number] = (unsigned char)char2x(checksum_t);
 
1402
                checksum_t++; checksum_t++; number++;
 
1403
                if (char2x(checksum_t) == -1) {
 
1404
                        //printf("Error: udp checksum field\n");
 
1405
                        error("Error: udp checksum field");
 
1406
                        return -1;
 
1407
                }
 
1408
                packet[number] = (unsigned char)char2x(checksum_t);
 
1409
                number++;
 
1410
        }
 
1411
 
 
1412
        /* we need this one for knowing where the udp payload starts
 
1413
         * we need this one when sending the packets out and modifing some values */
 
1414
        udp_start = number;     
 
1415
                
 
1416
        /* udp payload */
 
1417
        /* so do we allow packet's longer than 1518 (1522) bytes or not ? Not.*/
 
1418
        if (GTK_TOGGLE_BUTTON(payload_bt)->active) {
 
1419
                
 
1420
                GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(payload));
 
1421
                payload_length = gtk_text_buffer_get_char_count(buffer);
 
1422
                GtkTextIter start,end;
 
1423
                gtk_text_buffer_get_bounds(buffer,&start,&end);
 
1424
                //gtk_text_buffer_get_start_iter(buffer,&start);
 
1425
                //gtk_text_buffer_get_end_iter(buffer,&end);
 
1426
                payload_t = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
 
1427
                //g_free(start);
 
1428
                //g_free(end);
 
1429
                //payload_t = (char *) malloc(payload_length + 1);
 
1430
                //payload_t = gtk_editable_get_chars(GTK_EDITABLE(payload),0,-1);
 
1431
                
 
1432
                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
1433
                if (get_network_payload(button, user_data, payload_length, 
 
1434
                                                1514-number, payload_t) == -1) {
 
1435
                        //printf("Error: Problem with udp payload\n");
 
1436
                        g_free(payload_t);
 
1437
                        return -1;
 
1438
                }
 
1439
                else
 
1440
                        g_free(payload_t);
 
1441
 
 
1442
                cks_stop = number;
 
1443
        }
 
1444
        else
 
1445
                cks_stop = number;
 
1446
 
 
1447
        /* it will be possible that we add some other protocols on top of udp
 
1448
         * they will follow here... */
 
1449
 
 
1450
        
 
1451
        /* we have to fill the corect udp length if auto was enabled
 
1452
         * we add 2 bytes for source port and 2 bytes for dest port 
 
1453
         * because length_start points at udp length field  */
 
1454
        if (length_start > 0) {
 
1455
                packet[length_start] = (char)((number - length_start + 4)/256);
 
1456
                packet[length_start+1] = (char)((number - length_start + 4)%256);
 
1457
        }
 
1458
 
 
1459
        /* and finally compute the udp checksum if auto was enabled */
 
1460
        if (checksum_start > 0) {
 
1461
                /* if the user manually inserts the length value what then??? */
 
1462
                /* we don't care it that means, if you manually insert the length
 
1463
                 * than the auto checksum button won't help you 
 
1464
                 * it would be better if the value would be correct either */
 
1465
                
 
1466
                /* this if for udp length  */
 
1467
                udpcksum = (guint32)(cks_stop - cks_start);
 
1468
                /* pseudo header (ip part) + udplength + nr of cicles over guint16 */
 
1469
                udpcksum = pseudo_header_sum + udpcksum;
 
1470
                /* if the length is odd we have to add a pad byte */
 
1471
                if( (cks_stop - cks_start)%2 != 0)
 
1472
                               odd = 1;
 
1473
                /* previos value + part from udp checksum */
 
1474
                udpcksum = udpcksum + get_checksum32(cks_start, cks_stop+odd);
 
1475
                while (udpcksum >> 16)
 
1476
                        udpcksum = (udpcksum & 0xFFFF)+ (udpcksum >> 16);
 
1477
                /* the one's complement */
 
1478
                udpcksum = (-1) - udpcksum;
 
1479
 
 
1480
                /* let's write it */
 
1481
                packet[checksum_start] = (char)(udpcksum/256);
 
1482
                packet[checksum_start+1] =  (char)(udpcksum%256);
 
1483
        }
 
1484
        return 1;
 
1485
}
 
1486
 
 
1487
        
 
1488
int tcp_get(GtkButton *button, gpointer user_data, guint32 pseudo_header_sum) {
 
1489
        
 
1490
        GtkWidget *srcport, *dstport, *sequence_number, *ack_number, *header_length;
 
1491
        GtkWidget *flag_cwr, *flag_ecn;
 
1492
        GtkWidget *flag_urg, *flag_ack, *flag_psh, *flag_rst, *flag_syn, *flag_fin;
 
1493
        GtkWidget *window_size, *checksum, *checksum_bt, *urgent_pointer, *options;
 
1494
        GtkWidget *payload_bt, *payload;
 
1495
 
 
1496
        gchar *srcport_t, *dstport_t, *sequence_number_t, *ack_number_t, *header_length_t;
 
1497
        gchar *window_size_t, *checksum_t, *urgent_pointer_t, *options_t, *payload_t;
 
1498
 
 
1499
        int checksum_start, cks_start, cks_stop, i, j, payload_length, odd=0;
 
1500
        guint32 tcpcksum;
 
1501
        guint32 seqnr, acknr;
 
1502
        int flag_value = 0;
 
1503
 
 
1504
        srcport = lookup_widget(GTK_WIDGET(button), "entry46");
 
1505
        dstport = lookup_widget(GTK_WIDGET(button), "entry47");
 
1506
        sequence_number = lookup_widget(GTK_WIDGET(button), "entry48");
 
1507
        ack_number = lookup_widget(GTK_WIDGET(button), "entry49");
 
1508
        header_length = lookup_widget(GTK_WIDGET(button), "entry50");
 
1509
        flag_cwr = lookup_widget(GTK_WIDGET(button), "checkbutton22");
 
1510
        flag_ecn = lookup_widget(GTK_WIDGET(button), "checkbutton23");
 
1511
        flag_urg = lookup_widget(GTK_WIDGET(button), "checkbutton7");
 
1512
        flag_ack = lookup_widget(GTK_WIDGET(button), "checkbutton8");
 
1513
        flag_psh = lookup_widget(GTK_WIDGET(button), "checkbutton9");
 
1514
        flag_rst = lookup_widget(GTK_WIDGET(button), "checkbutton10");
 
1515
        flag_syn = lookup_widget(GTK_WIDGET(button), "checkbutton11");
 
1516
        flag_fin = lookup_widget(GTK_WIDGET(button), "checkbutton12");
 
1517
        window_size = lookup_widget(GTK_WIDGET(button), "entry51");
 
1518
        checksum = lookup_widget(GTK_WIDGET(button), "entry52");
 
1519
        checksum_bt = lookup_widget(GTK_WIDGET(button), "checkbutton13");
 
1520
        urgent_pointer = lookup_widget(GTK_WIDGET(button), "entry53");
 
1521
        options = lookup_widget(GTK_WIDGET(button), "entry54");
 
1522
        payload_bt = lookup_widget(GTK_WIDGET(button), "checkbutton14");
 
1523
        payload = lookup_widget(GTK_WIDGET(button), "text4");
 
1524
        
 
1525
        srcport_t= (char *)gtk_entry_get_text(GTK_ENTRY(srcport));
 
1526
        dstport_t= (char *)gtk_entry_get_text(GTK_ENTRY(dstport));
 
1527
        sequence_number_t = (char *)gtk_entry_get_text(GTK_ENTRY(sequence_number));
 
1528
        ack_number_t = (char *)gtk_entry_get_text(GTK_ENTRY(ack_number));
 
1529
        header_length_t = (char *)gtk_entry_get_text(GTK_ENTRY(header_length));
 
1530
        window_size_t = (char *)gtk_entry_get_text(GTK_ENTRY(window_size));
 
1531
        checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
1532
        urgent_pointer_t = (char *)gtk_entry_get_text(GTK_ENTRY(urgent_pointer));
 
1533
        options_t = (char *)gtk_entry_get_text(GTK_ENTRY(options));
 
1534
        
 
1535
        cks_start = number;
 
1536
        
 
1537
        /* we need this one for knowing where the tcp part starts
 
1538
         * we need this one when sending the packets out and modifing some values */
 
1539
        tcp_start = number;     
 
1540
                
 
1541
        /* source port */
 
1542
        if ( (atoi(srcport_t) < 0) || (atoi(srcport_t) > 65535) ) {
 
1543
                //printf("Error: tcp source port range\n");
 
1544
                error("Error: tcp source port range");
 
1545
                return -1;
 
1546
        }
 
1547
 
 
1548
        /* there can be rubbish in this field */
 
1549
        if (check_digit(srcport_t, strlen(srcport_t), "Error: tcp srcport field values") == -1)
 
1550
                                return -1;
 
1551
 
 
1552
        packet[number] = (char)(atol(srcport_t)/256);
 
1553
        number++;
 
1554
        packet[number] = (char)(atol(srcport_t)%256);
 
1555
        number++;
 
1556
        
 
1557
        /* destination port */
 
1558
        if ( (atoi(dstport_t) < 0) || (atoi(dstport_t) > 65535) ) {
 
1559
                //printf("Error: tcp destination port range\n");
 
1560
                error("Error: tcp destination port range");
 
1561
                return -1;
 
1562
        }
 
1563
 
 
1564
        /* there can be rubbish in this field */
 
1565
        if (check_digit(dstport_t, strlen(dstport_t), "Error: tcp destination port field values") == -1)
 
1566
                                return -1;
 
1567
 
 
1568
        packet[number] = (char)(atol(dstport_t)/256);
 
1569
        number++;
 
1570
        packet[number] = (char)(atol(dstport_t)%256);
 
1571
        number++;
 
1572
 
 
1573
        /* sequence number */
 
1574
        if ( strtoull(sequence_number_t, (char **)NULL, 10) > 0xFFFFFFFF ) {
 
1575
                //printf("Error: tcp sequence number range\n");
 
1576
                error("Error: tcp sequence number range");
 
1577
                return -1;
 
1578
        }
 
1579
 
 
1580
        /* there can be rubbish in this field */
 
1581
        if (check_digit(sequence_number_t, strlen(sequence_number_t), 
 
1582
                                                "Error: tcp sequence number field values") == -1)
 
1583
                                return -1;
 
1584
 
 
1585
        seqnr = strtoul(sequence_number_t, (char **)NULL, 10);
 
1586
        packet[number] = (char)(seqnr/16777216);
 
1587
        number++;
 
1588
        packet[number] = (char)(seqnr/65536);
 
1589
        number++;
 
1590
        packet[number] = (char)(seqnr/256);
 
1591
        number++;
 
1592
        packet[number] = (char)(seqnr%256);
 
1593
        number++;
 
1594
 
 
1595
        /* acknowledgment number */
 
1596
        if ( strtoull(ack_number_t, (char **)NULL, 10) > 0xFFFFFFFF) {
 
1597
                //printf("Error: tcp ack number range\n");
 
1598
                error("Error: tcp ack number range");
 
1599
                return -1;
 
1600
        }
 
1601
 
 
1602
        /* there can be rubbish in this field */
 
1603
        if (check_digit(ack_number_t, strlen(ack_number_t), "Error: tcp ack number field values") == -1)
 
1604
                                return -1;
 
1605
 
 
1606
        acknr = strtoul(ack_number_t, (char **)NULL, 10);
 
1607
        packet[number] = (char)(acknr/16777216);
 
1608
        number++;
 
1609
        packet[number] = (char)(acknr/65536);
 
1610
        number++;
 
1611
        packet[number] = (char)(acknr/256);
 
1612
        number++;
 
1613
        packet[number] = (char)(acknr%256);
 
1614
        number++;
 
1615
 
 
1616
        /* header length */     
 
1617
        if ( (atoi(header_length_t) < 0) || (atoi(header_length_t) > 60) ) {
 
1618
                //printf("Error: tcp header_length range\n");
 
1619
                error("Error: tcp header_length range");
 
1620
                return -1;
 
1621
        }
 
1622
 
 
1623
        /* since we insert value as int, when dividing it with 4 there must remain 0 */
 
1624
        if ( atoi(header_length_t) % 4 !=  0) {
 
1625
                //printf("Error: tcp header_length range\n");
 
1626
                error("Error: Wrong tcp header length value          \n(length mod 4 must be 0)");
 
1627
                return -1;
 
1628
        }
 
1629
 
 
1630
        /* there can be rubbish in this field */
 
1631
        if (check_digit(header_length_t, strlen(header_length_t), 
 
1632
                                                "Error: tcp header_length field values") == -1)
 
1633
                                return -1;
 
1634
 
 
1635
        packet[number] = (char)((atoi(header_length_t)*4));
 
1636
        number++;       
 
1637
 
 
1638
        /* flags */
 
1639
        if (GTK_TOGGLE_BUTTON(flag_cwr)->active) {
 
1640
                flag_value = flag_value + 128;
 
1641
        }       
 
1642
        if (GTK_TOGGLE_BUTTON(flag_ecn)->active) {
 
1643
                flag_value = flag_value + 64;
 
1644
        }
 
1645
        if (GTK_TOGGLE_BUTTON(flag_urg)->active) {
 
1646
                flag_value = flag_value + 32;
 
1647
        }
 
1648
        if (GTK_TOGGLE_BUTTON(flag_ack)->active) {
 
1649
                flag_value = flag_value + 16;
 
1650
        }
 
1651
        if (GTK_TOGGLE_BUTTON(flag_psh)->active) {
 
1652
                flag_value = flag_value + 8;
 
1653
        }
 
1654
        if (GTK_TOGGLE_BUTTON(flag_rst)->active) {
 
1655
                flag_value = flag_value + 4;
 
1656
        }
 
1657
        if (GTK_TOGGLE_BUTTON(flag_syn)->active) {
 
1658
                flag_value = flag_value + 2;
 
1659
        }
 
1660
        if (GTK_TOGGLE_BUTTON(flag_fin)->active) {
 
1661
                flag_value = flag_value + 1;
 
1662
        }
 
1663
        packet[number] = (char)flag_value;
 
1664
        number++;       
 
1665
        
 
1666
        /* window size */       
 
1667
        if ( (atoi(window_size_t) < 0) || (atoi(window_size_t) > 65535) ) {
 
1668
                //printf("Error: tcp window size range\n");
 
1669
                error("Error: tcp window size range");
 
1670
                return -1;
 
1671
        }
 
1672
 
 
1673
        /* there can be rubbish in this field */
 
1674
        if (check_digit(window_size_t, strlen(window_size_t), "Error: tcp window size field values") == -1)
 
1675
                                return -1;
 
1676
 
 
1677
        packet[number] = (char)(atol(window_size_t)/256);
 
1678
        number++;       
 
1679
        packet[number] = (char)(atol(window_size_t)%256);
 
1680
        number++;       
 
1681
 
 
1682
        /* tcp checksum */
 
1683
        if (GTK_TOGGLE_BUTTON(checksum_bt)->active) {
 
1684
                checksum_start = number;
 
1685
                packet[number] = (unsigned char)0;
 
1686
                number++;
 
1687
                packet[number] = (unsigned char)0;
 
1688
                number++;
 
1689
        }
 
1690
        else {
 
1691
                /* if checksum_start = 0, we leave it in the end */
 
1692
                checksum_start = 0;
 
1693
        
 
1694
                if (char2x(checksum_t) == -1) {
 
1695
                        //printf("Error: tcp checksum field\n");
 
1696
                        error("Error: tcp checksum field");
 
1697
                        return -1;
 
1698
                }
 
1699
                packet[number] = (unsigned char)char2x(checksum_t);
 
1700
                checksum_t++; checksum_t++; number++;
 
1701
                if (char2x(checksum_t) == -1) {
 
1702
                        //printf("Error: tcp checksum field\n");
 
1703
                        error("Error: tcp checksum field");
 
1704
                        return -1;
 
1705
                }
 
1706
                packet[number] = (unsigned char)char2x(checksum_t);
 
1707
                number++;
 
1708
        }
 
1709
                
 
1710
        /* urgent pointer */    
 
1711
        if ( (atoi(urgent_pointer_t) < 0) || (atoi(urgent_pointer_t) > 65535) ) {
 
1712
                //printf("Error: tcp urgent pointer range\n");
 
1713
                error("Error: tcp urgent pointer range");
 
1714
                return -1;
 
1715
        }
 
1716
 
 
1717
        /* there can be rubbish in this field */
 
1718
        if (check_digit(urgent_pointer_t, strlen(urgent_pointer_t), 
 
1719
                                                "Error: tcp urgent pointer field values") == -1)
 
1720
                                return -1;
 
1721
 
 
1722
        packet[number] = (char)(atol(urgent_pointer_t)/256);
 
1723
        number++;       
 
1724
        packet[number] = (char)(atol(urgent_pointer_t)%256);
 
1725
        number++;       
 
1726
 
 
1727
        /* tcp options */
 
1728
        if ( (strlen(options_t)%8) != 0) {
 
1729
                //printf("Error: Wrong length of tcp options field (length mod 8 must be 0)\n");
 
1730
                error("Error: Wrong length of tcp options field      \n(length mod 8 must be 0)");
 
1731
                return -1;
 
1732
        }
 
1733
        
 
1734
        if ( strlen(options_t) > 80) {
 
1735
                //printf("Error: tcp options field to long\n");
 
1736
                error("Error: tcp options field to long");
 
1737
                return -1;
 
1738
        }
 
1739
        
 
1740
        j = strlen(options_t)/2;
 
1741
        for (i=0; i<j; i++) {
 
1742
                if (char2x(options_t) == -1) {
 
1743
                        //printf("Error: tcp options field\n");
 
1744
                        error("Error: tcp options field");
 
1745
                        return -1;
 
1746
                }
 
1747
                packet[number] = (unsigned char)char2x(options_t);
 
1748
                number++; options_t++; options_t++;
 
1749
        }
 
1750
 
 
1751
        /* tcp payload */       
 
1752
        if (GTK_TOGGLE_BUTTON(payload_bt)->active) {
 
1753
                
 
1754
                GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(payload));
 
1755
                payload_length = gtk_text_buffer_get_char_count(buffer);
 
1756
                GtkTextIter start,end;
 
1757
                //gtk_text_buffer_get_start_iter(buffer,start);
 
1758
                //gtk_text_buffer_get_end_iter(buffer,end);
 
1759
                gtk_text_buffer_get_bounds(buffer,&start,&end);
 
1760
                payload_t = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
 
1761
                //g_free(start);
 
1762
                //g_free(end);
 
1763
                //payload_t = (char *) malloc(payload_length + 1);
 
1764
                //payload_t = gtk_editable_get_chars(GTK_EDITABLE(payload),0,-1);
 
1765
                
 
1766
                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
1767
                if (get_network_payload(button, user_data, payload_length, 
 
1768
                                                1514-number, payload_t) == -1) {
 
1769
                        //printf("Error: Problem with tcp payload\n");
 
1770
                        g_free(payload_t);
 
1771
                        return -1;
 
1772
                }
 
1773
                else
 
1774
                        g_free(payload_t);
 
1775
 
 
1776
                cks_stop = number;
 
1777
        }
 
1778
        else
 
1779
                cks_stop = number;
 
1780
 
 
1781
        /* it will be possible that we add some other protocols on top of tcp
 
1782
         * they will follow here... */
 
1783
        
 
1784
 
 
1785
        /* and finally compute the tcp checksum if auto was enabled */
 
1786
        if (checksum_start > 0) {
 
1787
                
 
1788
                /* this if for length  */
 
1789
                tcpcksum = (guint32)(cks_stop - cks_start);
 
1790
                /* pseudo header (ip part) + tcplength + nr of cicles over guint16 */
 
1791
                tcpcksum = pseudo_header_sum + tcpcksum;
 
1792
                /* if length is odd we have to add a pad byte */
 
1793
                if( (cks_stop - cks_start)%2 != 0)
 
1794
                                odd = 1;
 
1795
                /* previos value + part from tcp checksum */
 
1796
                tcpcksum = tcpcksum + get_checksum32(cks_start, cks_stop+odd);
 
1797
                while (tcpcksum >> 16)
 
1798
                        tcpcksum = (tcpcksum & 0xFFFF) + (tcpcksum >> 16);
 
1799
                /* the one's complement */
 
1800
                tcpcksum = (-1) - tcpcksum;
 
1801
 
 
1802
                /* what about if the lenght is odd ??? 
 
1803
                 * we check this in get_checksum routine */
 
1804
                
 
1805
                /* let's write it */
 
1806
                packet[checksum_start] = (char)(tcpcksum/256);
 
1807
                packet[checksum_start+1] =  (char)(tcpcksum%256);
 
1808
        }
 
1809
        
 
1810
        return 1;
 
1811
}
 
1812
 
 
1813
int igmp_get(GtkButton *button, gpointer user_data) {
 
1814
        
 
1815
        GtkWidget *type, *menux, *menu, *menu_item; 
 
1816
 
 
1817
        GtkWidget *maxresptime, *checksum, *cks_bt, *groupaddress, *resv, *nosf, *sourceaddresses;
 
1818
 
 
1819
        gchar *type_t;
 
1820
        gchar *maxresptime_t, *checksum_t, *groupaddress_t;     
 
1821
        gchar *resv_t, *nosf_t, *sourceaddresses_t;     
 
1822
        
 
1823
        int igmp_start, igmp_stop, checksum_start, payload_length;
 
1824
        guint16 igmpcksum;
 
1825
        gchar tmp[4];
 
1826
        int i, j, menu_index;
 
1827
        
 
1828
        type = lookup_widget(GTK_WIDGET(button), "entry166");
 
1829
        type_t = (char *)gtk_entry_get_text(GTK_ENTRY(type));
 
1830
        
 
1831
        igmp_start = number;
 
1832
        
 
1833
        /* type */
 
1834
        if (char2x(type_t) == -1) {
 
1835
                //printf("Error: igmp type field\n");
 
1836
                error("Error: igmp type field");
 
1837
                return -1;
 
1838
        }
 
1839
        packet[number] = (unsigned char)char2x(type_t);
 
1840
        number++;
 
1841
 
 
1842
        maxresptime = lookup_widget(GTK_WIDGET(button), "entry167");
 
1843
        menux = lookup_widget(GTK_WIDGET(button), "optionmenu20");
 
1844
        checksum = lookup_widget(GTK_WIDGET(button), "entry168");
 
1845
        cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton41");
 
1846
 
 
1847
        maxresptime_t = (char *)gtk_entry_get_text(GTK_ENTRY(maxresptime));
 
1848
        checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
1849
        /*gcc warning otherwise*/
 
1850
        groupaddress = lookup_widget(GTK_WIDGET(button), "entry169");
 
1851
 
 
1852
        /* igmp max response time */
 
1853
        if (char2x(maxresptime_t) == -1) {
 
1854
                //printf("Error: igmp max response time\n");
 
1855
                error("Error: igmp max response time");
 
1856
                return -1;
 
1857
        }
 
1858
        packet[number] = (unsigned char)char2x(maxresptime_t);
 
1859
        number++;
 
1860
                        
 
1861
        /* checksum */  
 
1862
        if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
 
1863
                checksum_start = number;
 
1864
                packet[number] = (unsigned char)0;
 
1865
                number++;
 
1866
                packet[number] = (unsigned char)0;
 
1867
                number++;
 
1868
        }
 
1869
        else {
 
1870
                /* if checksum_start = 0, we leave it in the end */
 
1871
                checksum_start = 0;
 
1872
 
 
1873
                if (char2x(checksum_t) == -1) {
 
1874
                        //printf("Error: igmp reply checksum field\n");
 
1875
                        error("Error: igmp checksum field");
 
1876
                        return -1;
 
1877
                }
 
1878
                packet[number] = (unsigned char)char2x(checksum_t);
 
1879
                checksum_t++; checksum_t++; number++;
 
1880
                if (char2x(checksum_t) == -1) {
 
1881
                        //printf("Error: igmp reply checksum field\n");
 
1882
                        error("Error: igmp checksum field");
 
1883
                        return -1;
 
1884
                }
 
1885
                packet[number] = (unsigned char)char2x(checksum_t);
 
1886
                number++;
 
1887
        }
 
1888
                        
 
1889
        menux = lookup_widget(GTK_WIDGET(button), "optionmenu20");
 
1890
 
 
1891
        menu = GTK_OPTION_MENU(menux)->menu;
 
1892
        menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
1893
        menu_index = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
1894
 
 
1895
        /* IGMP V3 query */
 
1896
        if (menu_index == 1) {
 
1897
                /* group address */
 
1898
                groupaddress = lookup_widget(GTK_WIDGET(button), "entry169");
 
1899
                groupaddress_t = (char *)gtk_entry_get_text(GTK_ENTRY(groupaddress));
 
1900
                if (check_ip_address(groupaddress_t) == -1) {
 
1901
                        //printf("Error: Wrong igmp address\n");
 
1902
                        error("Error: Wrong igmp group address");
 
1903
                        return -1;
 
1904
                }
 
1905
                
 
1906
                for (i=0; i<4; i++) {
 
1907
                        for(j=0; j<4 && (*groupaddress_t != '\0'); j++) {
 
1908
                                if ( ((int)*groupaddress_t == '.') && (i<3) && (j>0) ) {
 
1909
                                        groupaddress_t++;
 
1910
                                        break;
 
1911
                                }
 
1912
                                tmp[j] = *groupaddress_t;
 
1913
                                groupaddress_t++;
 
1914
                        }
 
1915
                        tmp[j] = '\0';
 
1916
                        packet[number] = (unsigned char)(atoi(tmp));
 
1917
                        number++;               
 
1918
                }
 
1919
                        
 
1920
                resv = lookup_widget(GTK_WIDGET(button), "entry171");
 
1921
                nosf = lookup_widget(GTK_WIDGET(button), "entry172");
 
1922
                sourceaddresses = lookup_widget(GTK_WIDGET(button), "entry173");
 
1923
 
 
1924
                resv_t = (char *)gtk_entry_get_text(GTK_ENTRY(resv));
 
1925
                nosf_t = (char *)gtk_entry_get_text(GTK_ENTRY(nosf));
 
1926
                sourceaddresses_t = (char *)gtk_entry_get_text(GTK_ENTRY(sourceaddresses));
 
1927
 
 
1928
                /* Resv, S, QRV, QQIC IGMP V3 values */
 
1929
                if (char2x(resv_t) == -1) {
 
1930
                        //printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
 
1931
                        error("Error: Resv, S, QRV, QQIC IGMP V3 values");
 
1932
                        return -1;
 
1933
                }
 
1934
                packet[number] = (unsigned char)char2x(resv_t);
 
1935
                resv_t++; resv_t++; number++;
 
1936
                if (char2x(resv_t) == -1) {
 
1937
                        //printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
 
1938
                        error("Error: Resv, S, QRV, QQIC IGMP V3 values");
 
1939
                        return -1;
 
1940
                }
 
1941
                packet[number] = (unsigned char)char2x(resv_t);
 
1942
                number++;
 
1943
                
 
1944
                /* number of sources */
 
1945
                if (char2x(nosf_t) == -1) {
 
1946
                        //printf("Error: IGMP V3 number of sources\n");
 
1947
                        error("Error: IGMP V3 number of sources");
 
1948
                        return -1;
 
1949
                }
 
1950
                packet[number] = (unsigned char)char2x(nosf_t);
 
1951
                nosf_t++; nosf_t++; number++;
 
1952
                if (char2x(nosf_t) == -1) {
 
1953
                        //printf("Error: IGMP V3 number of sources\n");
 
1954
                        error("Error: IGMP V3 number of sources");
 
1955
                        return -1;
 
1956
                }
 
1957
                packet[number] = (unsigned char)char2x(nosf_t);
 
1958
                number++;
 
1959
                
 
1960
                /* source addresses */
 
1961
                payload_length = strlen(sourceaddresses_t);
 
1962
                
 
1963
                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
1964
                if (get_network_payload(button, user_data, payload_length, 
 
1965
                                        1514-number, sourceaddresses_t) == -1) {
 
1966
                        //printf("problem with igmp reply payload\n");
 
1967
                        return -1;
 
1968
                }
 
1969
                
 
1970
                igmp_stop = number;
 
1971
 
 
1972
                if (checksum_start > 0) {
 
1973
                        
 
1974
                        igmpcksum =  get_checksum16(igmp_start, igmp_stop); 
 
1975
                        /* the one's complement */
 
1976
                        igmpcksum = (-1) - igmpcksum;
 
1977
 
 
1978
                        /* let's write it */
 
1979
                        packet[checksum_start] = (char)(igmpcksum/256);
 
1980
                        packet[checksum_start+1] =  (char)(igmpcksum%256);
 
1981
                }
 
1982
        }
 
1983
 
 
1984
        /* IGMP V3 report */
 
1985
        else if (menu_index == 4) {
 
1986
                resv = lookup_widget(GTK_WIDGET(button), "entry176");
 
1987
                nosf = lookup_widget(GTK_WIDGET(button), "entry177");
 
1988
                sourceaddresses = lookup_widget(GTK_WIDGET(button), "entry178");
 
1989
 
 
1990
                resv_t = (char *)gtk_entry_get_text(GTK_ENTRY(resv));
 
1991
                nosf_t = (char *)gtk_entry_get_text(GTK_ENTRY(nosf));
 
1992
                sourceaddresses_t = (char *)gtk_entry_get_text(GTK_ENTRY(sourceaddresses));
 
1993
 
 
1994
                /* Resv values */
 
1995
                if (char2x(resv_t) == -1) {
 
1996
                        //printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
 
1997
                        error("Error: Reserved IGMP V3 report values");
 
1998
                        return -1;
 
1999
                }
 
2000
                packet[number] = (unsigned char)char2x(resv_t);
 
2001
                resv_t++; resv_t++; number++;
 
2002
                if (char2x(resv_t) == -1) {
 
2003
                        //printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
 
2004
                        error("Error: Reserved IGMP V3 report values");
 
2005
                        return -1;
 
2006
                }
 
2007
                packet[number] = (unsigned char)char2x(resv_t);
 
2008
                number++;
 
2009
                
 
2010
                /* number of group records */
 
2011
                if (char2x(nosf_t) == -1) {
 
2012
                        //printf("Error: IGMP V3 number of sources\n");
 
2013
                        error("Error: IGMP V3 report number of group records");
 
2014
                        return -1;
 
2015
                }
 
2016
                packet[number] = (unsigned char)char2x(nosf_t);
 
2017
                nosf_t++; nosf_t++; number++;
 
2018
                if (char2x(nosf_t) == -1) {
 
2019
                        //printf("Error: IGMP V3 number of sources\n");
 
2020
                        error("Error: IGMP V3 report number of group records");
 
2021
                        return -1;
 
2022
                }
 
2023
                packet[number] = (unsigned char)char2x(nosf_t);
 
2024
                number++;
 
2025
                
 
2026
                /* group records */
 
2027
                payload_length = strlen(sourceaddresses_t);
 
2028
                
 
2029
                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
2030
                if (get_network_payload(button, user_data, payload_length, 
 
2031
                                        1514-number, sourceaddresses_t) == -1) {
 
2032
                        //printf("problem with igmp reply payload\n");
 
2033
                        return -1;
 
2034
                }
 
2035
                
 
2036
                igmp_stop = number;
 
2037
 
 
2038
                if (checksum_start > 0) {
 
2039
                        
 
2040
                        igmpcksum =  get_checksum16(igmp_start, igmp_stop); 
 
2041
                        /* the one's complement */
 
2042
                        igmpcksum = (-1) - igmpcksum;
 
2043
 
 
2044
                        /* let's write it */
 
2045
                        packet[checksum_start] = (char)(igmpcksum/256);
 
2046
                        packet[checksum_start+1] =  (char)(igmpcksum%256);
 
2047
                }
 
2048
 
 
2049
 
 
2050
        }
 
2051
        /* for all the other types */
 
2052
        else    {
 
2053
                /* group address */
 
2054
                groupaddress = lookup_widget(GTK_WIDGET(button), "entry175");
 
2055
                groupaddress_t = (char *)gtk_entry_get_text(GTK_ENTRY(groupaddress));
 
2056
                if (check_ip_address(groupaddress_t) == -1) {
 
2057
                        //printf("Error: Wrong igmp address\n");
 
2058
                        error("Error: Wrong igmp group address");
 
2059
                        return -1;
 
2060
                }
 
2061
                
 
2062
                for (i=0; i<4; i++) {
 
2063
                        for(j=0; j<4 && (*groupaddress_t != '\0'); j++) {
 
2064
                                if ( ((int)*groupaddress_t == '.') && (i<3) && (j>0) ) {
 
2065
                                        groupaddress_t++;
 
2066
                                        break;
 
2067
                                }
 
2068
                                tmp[j] = *groupaddress_t;
 
2069
                                groupaddress_t++;
 
2070
                        }
 
2071
                        tmp[j] = '\0';
 
2072
                        packet[number] = (unsigned char)(atoi(tmp));
 
2073
                        number++;               
 
2074
                }
 
2075
 
 
2076
                igmp_stop = number;
 
2077
 
 
2078
                if (checksum_start > 0) {
 
2079
                        
 
2080
                        igmpcksum =  get_checksum16(igmp_start, igmp_stop); 
 
2081
                        /* the one's complement */
 
2082
                        igmpcksum = (-1) - igmpcksum;
 
2083
 
 
2084
                        /* let's write it */
 
2085
                        packet[checksum_start] = (char)(igmpcksum/256);
 
2086
                        packet[checksum_start+1] =  (char)(igmpcksum%256);
 
2087
                }
 
2088
        }       
 
2089
        
 
2090
        return 1;
 
2091
}
 
2092
        
 
2093
int icmp_get(GtkButton *button, gpointer user_data) {
 
2094
        
 
2095
        GtkWidget *type; 
 
2096
        GtkWidget *code, *checksum, *cks_bt, *identifier, *seq_nr, *unused;
 
2097
        GtkWidget *data_bt, *data;
 
2098
 
 
2099
        gchar *type_t;
 
2100
        gchar *code_t, *checksum_t, *identifier_t, *seq_nr_t, *data_t, *unused_t;       
 
2101
        
 
2102
        int icmp_start, icmp_stop, checksum_start, payload_length;
 
2103
        guint32 icmpcksum;
 
2104
        
 
2105
        type = lookup_widget(GTK_WIDGET(button), "entry57");
 
2106
        type_t = (char *)gtk_entry_get_text(GTK_ENTRY(type));
 
2107
        
 
2108
        icmp_start = number;
 
2109
        
 
2110
        /* type */
 
2111
        if (char2x(type_t) == -1) {
 
2112
                //printf("Error: icmp type field\n");
 
2113
                error("Error: icmp type field");
 
2114
                return -1;
 
2115
        }
 
2116
        packet[number] = (unsigned char)char2x(type_t);
 
2117
        number++;
 
2118
 
 
2119
        /* YYY hmmm, it would be better to be the type filed inserted as int
 
2120
         * not as hex, so we have to calculate it */
 
2121
        switch (atoi(type_t)) {
 
2122
                case 0: {
 
2123
                        //printf("ICMP echo reply\n");
 
2124
                        code = lookup_widget(GTK_WIDGET(button), "entry62");
 
2125
                        checksum = lookup_widget(GTK_WIDGET(button), "entry63");
 
2126
                        cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton16");
 
2127
                        identifier = lookup_widget(GTK_WIDGET(button), "entry64");
 
2128
                        seq_nr = lookup_widget(GTK_WIDGET(button), "entry65");
 
2129
                        data_bt = lookup_widget(GTK_WIDGET(button), "checkbutton17");
 
2130
                        data = lookup_widget(GTK_WIDGET(button), "entry66");
 
2131
 
 
2132
                        code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
 
2133
                        checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
2134
                        identifier_t = (char *)gtk_entry_get_text(GTK_ENTRY(identifier));
 
2135
                        seq_nr_t = (char *)gtk_entry_get_text(GTK_ENTRY(seq_nr));
 
2136
                        data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
 
2137
                        
 
2138
                        /* code */
 
2139
                        if (char2x(code_t) == -1) {
 
2140
                                //printf("Error: icmp reply code field\n");
 
2141
                                error("Error: icmp reply code field");
 
2142
                                return -1;
 
2143
                        }
 
2144
                        packet[number] = (unsigned char)char2x(code_t);
 
2145
                        number++;
 
2146
                        
 
2147
                        /* checksum */  
 
2148
                        if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
 
2149
                                checksum_start = number;
 
2150
                                packet[number] = (unsigned char)0;
 
2151
                                number++;
 
2152
                                packet[number] = (unsigned char)0;
 
2153
                                number++;
 
2154
                        }
 
2155
                        else {
 
2156
                        /* if checksum_start = 0, we leave it in the end */
 
2157
                                checksum_start = 0;
 
2158
        
 
2159
                                if (char2x(checksum_t) == -1) {
 
2160
                                        //printf("Error: icmp reply checksum field\n");
 
2161
                                        error("Error: icmp reply checksum field");
 
2162
                                        return -1;
 
2163
                                }
 
2164
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2165
                                checksum_t++; checksum_t++; number++;
 
2166
                                if (char2x(checksum_t) == -1) {
 
2167
                                        //printf("Error: icmp reply checksum field\n");
 
2168
                                        error("Error: icmp reply checksum field");
 
2169
                                        return -1;
 
2170
                                }
 
2171
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2172
                                number++;
 
2173
                        }
 
2174
                        
 
2175
                        /* identifier */
 
2176
                        if (char2x(identifier_t) == -1) {
 
2177
                                //printf("Error: icmp reply identifier field\n");
 
2178
                                error("Error: icmp reply identifier field");
 
2179
                                return -1;
 
2180
                        }
 
2181
                        packet[number] = (unsigned char)char2x(identifier_t);
 
2182
                        identifier_t++; identifier_t++; number++;
 
2183
                        if (char2x(identifier_t) == -1) {
 
2184
                                //printf("Error: icmp reply identifier field\n");
 
2185
                                error("Error: icmp reply identifier field");
 
2186
                                return -1;
 
2187
                        }
 
2188
                        packet[number] = (unsigned char)char2x(identifier_t);
 
2189
                        number++;
 
2190
                        
 
2191
                        /* sequence number */
 
2192
                        if (char2x(seq_nr_t) == -1) {
 
2193
                                //printf("Error: icmp reply identifier field\n");
 
2194
                                error("Error: icmp reply identifier field");
 
2195
                                return -1;
 
2196
                        }
 
2197
                        packet[number] = (unsigned char)char2x(seq_nr_t);
 
2198
                        seq_nr_t++; seq_nr_t++; number++;
 
2199
                        if (char2x(seq_nr_t) == -1) {
 
2200
                                //printf("Error: icmp reply identifier field\n");
 
2201
                                error("Error: icmp reply identifier field");
 
2202
                                return -1;
 
2203
                        }
 
2204
                        packet[number] = (unsigned char)char2x(seq_nr_t);
 
2205
                        number++;
 
2206
                        
 
2207
                        /* data */
 
2208
                        if (GTK_TOGGLE_BUTTON(data_bt)->active) {
 
2209
                                
 
2210
                                payload_length = strlen(data_t);
 
2211
                                
 
2212
                                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
2213
                                if (get_network_payload(button, user_data, payload_length, 
 
2214
                                                        1514-number, data_t) == -1) {
 
2215
                                        //printf("problem with icmp reply payload\n");
 
2216
                                        return -1;
 
2217
                                }
 
2218
 
 
2219
                                icmp_stop = number;
 
2220
                        }
 
2221
                        else
 
2222
                                icmp_stop = number;
 
2223
 
 
2224
                        if (checksum_start > 0) {
 
2225
                                
 
2226
                                icmpcksum =  get_checksum16(icmp_start, icmp_stop); 
 
2227
                                /* the one's complement */
 
2228
                                icmpcksum = (-1) - icmpcksum;
 
2229
 
 
2230
                                /* let's write it */
 
2231
                                packet[checksum_start] = (char)(icmpcksum/256);
 
2232
                                packet[checksum_start+1] =  (char)(icmpcksum%256);
 
2233
                        }
 
2234
                        break;
 
2235
                }
 
2236
                
 
2237
 
 
2238
                /* icmp echo request */
 
2239
                case 8: {
 
2240
                        //printf("ICMP echo request\n");
 
2241
                        code = lookup_widget(GTK_WIDGET(button), "entry74");
 
2242
                        checksum = lookup_widget(GTK_WIDGET(button), "entry77");
 
2243
                        cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton20");
 
2244
                        identifier = lookup_widget(GTK_WIDGET(button), "entry75");
 
2245
                        seq_nr = lookup_widget(GTK_WIDGET(button), "entry78");
 
2246
                        data_bt = lookup_widget(GTK_WIDGET(button), "checkbutton19");
 
2247
                        data = lookup_widget(GTK_WIDGET(button), "entry76");
 
2248
 
 
2249
                        code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
 
2250
                        checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
2251
                        identifier_t = (char *)gtk_entry_get_text(GTK_ENTRY(identifier));
 
2252
                        seq_nr_t = (char *)gtk_entry_get_text(GTK_ENTRY(seq_nr));
 
2253
                        data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
 
2254
                        
 
2255
                        /* code */
 
2256
                        if (char2x(code_t) == -1) {
 
2257
                                //printf("Error: icmp request code field\n");
 
2258
                                error("Error: icmp request code field");
 
2259
                                return -1;
 
2260
                        }
 
2261
                        packet[number] = (unsigned char)char2x(code_t);
 
2262
                        number++;
 
2263
                        
 
2264
                        /* checksum */  
 
2265
                        if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
 
2266
                                checksum_start = number;
 
2267
                                packet[number] = (unsigned char)0;
 
2268
                                number++;
 
2269
                                packet[number] = (unsigned char)0;
 
2270
                                number++;
 
2271
                        }
 
2272
                        else {
 
2273
                        /* if checksum_start = 0, we leave it in the end */
 
2274
                                checksum_start = 0;
 
2275
        
 
2276
                                if (char2x(checksum_t) == -1) {
 
2277
                                        //printf("Error: icmp request checksum field\n");
 
2278
                                        error("Error: icmp request checksum field");
 
2279
                                        return -1;
 
2280
                                }
 
2281
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2282
                                checksum_t++; checksum_t++; number++;
 
2283
                                if (char2x(checksum_t) == -1) {
 
2284
                                        //printf("Error: icmp request checksum field\n");
 
2285
                                        error("Error: icmp request checksum field");
 
2286
                                        return -1;
 
2287
                                }
 
2288
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2289
                                number++;
 
2290
                        }
 
2291
                        
 
2292
                        /* identifier */
 
2293
                        if (char2x(identifier_t) == -1) {
 
2294
                                //printf("Error: icmp request identifier field\n");
 
2295
                                error("Error: icmp request identifier field");
 
2296
                                return -1;
 
2297
                        }
 
2298
                        packet[number] = (unsigned char)char2x(identifier_t);
 
2299
                        identifier_t++; identifier_t++; number++;
 
2300
                        if (char2x(identifier_t) == -1) {
 
2301
                                //printf("Error: icmp request identifier field\n");
 
2302
                                error("Error: icmp request identifier field");
 
2303
                                return -1;
 
2304
                        }
 
2305
                        packet[number] = (unsigned char)char2x(identifier_t);
 
2306
                        number++;
 
2307
                        
 
2308
                        /* sequence number */
 
2309
                        if (char2x(seq_nr_t) == -1) {
 
2310
                                //printf("Error: icmp request identifier field\n");
 
2311
                                error("Error: icmp request identifier field");
 
2312
                                return -1;
 
2313
                        }
 
2314
                        packet[number] = (unsigned char)char2x(seq_nr_t);
 
2315
                        seq_nr_t++; seq_nr_t++; number++;
 
2316
                        if (char2x(seq_nr_t) == -1) {
 
2317
                                //printf("Error: icmp request identifier field\n");
 
2318
                                error("Error: icmp request identifier field");
 
2319
                                return -1;
 
2320
                        }
 
2321
                        packet[number] = (unsigned char)char2x(seq_nr_t);
 
2322
                        number++;
 
2323
                        
 
2324
                        /* data */
 
2325
                        if (GTK_TOGGLE_BUTTON(data_bt)->active) {
 
2326
                                
 
2327
                                payload_length = strlen(data_t);
 
2328
                                
 
2329
                                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
2330
                                if (get_network_payload(button, user_data, payload_length, 
 
2331
                                                        1514-number, data_t) == -1) {
 
2332
                                        //printf("Error: problem with icmp request payload\n");
 
2333
                                        return -1;
 
2334
                                }
 
2335
 
 
2336
                                icmp_stop = number;
 
2337
                        }
 
2338
                        else
 
2339
                                icmp_stop = number;
 
2340
 
 
2341
                        if (checksum_start > 0) {
 
2342
                                
 
2343
                                icmpcksum =  get_checksum16(icmp_start, icmp_stop); 
 
2344
                                /* the one's complement */
 
2345
                                icmpcksum = (-1) - icmpcksum;
 
2346
 
 
2347
                                /* let's write it */
 
2348
                                packet[checksum_start] = (char)(icmpcksum/256);
 
2349
                                packet[checksum_start+1] =  (char)(icmpcksum%256);
 
2350
                        }
 
2351
                        break;
 
2352
                }
 
2353
                
 
2354
                        
 
2355
                /* icmp destination unreacheable */
 
2356
                case 3: {
 
2357
                        //printf("ICMP destination unreacheable\n");
 
2358
                        code = lookup_widget(GTK_WIDGET(button), "entry58");
 
2359
                        checksum = lookup_widget(GTK_WIDGET(button), "entry59");
 
2360
                        cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton15");
 
2361
                        unused = lookup_widget(GTK_WIDGET(button), "entry60");
 
2362
                        data_bt = lookup_widget(GTK_WIDGET(button), "checkbutton24");
 
2363
                        data = lookup_widget(GTK_WIDGET(button), "entry61");
 
2364
 
 
2365
                        code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
 
2366
                        checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
2367
                        unused_t = (char *)gtk_entry_get_text(GTK_ENTRY(unused));
 
2368
                        data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
 
2369
                        
 
2370
                        /* code */
 
2371
                        if (char2x(code_t) == -1) {
 
2372
                                //printf("Error: icmp destination unreacheable code field\n");
 
2373
                                error("Error: icmp destination unreacheable code field");
 
2374
                                return -1;
 
2375
                        }
 
2376
                        packet[number] = (unsigned char)char2x(code_t);
 
2377
                        number++;
 
2378
                        
 
2379
                        /* checksum */  
 
2380
                        if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
 
2381
                                checksum_start = number;
 
2382
                                packet[number] = (unsigned char)0;
 
2383
                                number++;
 
2384
                                packet[number] = (unsigned char)0;
 
2385
                                number++;
 
2386
                        }
 
2387
                        else {
 
2388
                        /* if checksum_start = 0, we leave it in the end */
 
2389
                                checksum_start = 0;
 
2390
        
 
2391
                                if (char2x(checksum_t) == -1) {
 
2392
                                        //printf("Error: icmp destination unreacheable checksum field\n");
 
2393
                                        error("Error: icmp destination unreacheable checksum field");
 
2394
                                        return -1;
 
2395
                                }
 
2396
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2397
                                checksum_t++; checksum_t++; number++;
 
2398
                                if (char2x(checksum_t) == -1) {
 
2399
                                        //printf("Error: icmp destination unreacheable checksum field\n");
 
2400
                                        error("Error: icmp destination unreacheable checksum field");
 
2401
                                        return -1;
 
2402
                                }
 
2403
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2404
                                number++;
 
2405
                        }
 
2406
                        
 
2407
                        /* unused field */
 
2408
                        if (char2x(unused_t) == -1) {
 
2409
                                //printf("Error: icmp destination unreacheable unused field\n");
 
2410
                                error("Error: icmp destination unreacheable unused field");
 
2411
                                return -1;
 
2412
                        }
 
2413
                        packet[number] = (unsigned char)char2x(unused_t);
 
2414
                        unused_t++; unused_t++; number++;
 
2415
                        if (char2x(unused_t) == -1) {
 
2416
                                //printf("Error: icmp destination unreacheable unused field\n");
 
2417
                                error("Error: icmp destination unreacheable unused field");
 
2418
                                return -1;
 
2419
                        }
 
2420
                        packet[number] = (unsigned char)char2x(unused_t);
 
2421
                        unused_t++; unused_t++; number++;
 
2422
                        if (char2x(unused_t) == -1) {
 
2423
                                //printf("Error: icmp destination unreacheable unused field\n");
 
2424
                                error("Error: icmp destination unreacheable unused field");
 
2425
                                return -1;
 
2426
                        }
 
2427
                        packet[number] = (unsigned char)char2x(unused_t);
 
2428
                        unused_t++; unused_t++; number++;
 
2429
                        if (char2x(unused_t) == -1) {
 
2430
                                //printf("Error: icmp destination unreacheable unused field\n");
 
2431
                                error("Error: icmp destination unreacheable unused field");
 
2432
                                return -1;
 
2433
                        }
 
2434
                        packet[number] = (unsigned char)char2x(unused_t);
 
2435
                        number++;
 
2436
                        
 
2437
                        /* data */
 
2438
                        if (GTK_TOGGLE_BUTTON(data_bt)->active) {
 
2439
                                
 
2440
                                payload_length = strlen(data_t);
 
2441
                                
 
2442
                                /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
2443
                                if (get_network_payload(button, user_data, payload_length, 
 
2444
                                                        1514-number, data_t) == -1) {
 
2445
                                        //printf("Error: problem with icmp destination unreacheable payload\n");
 
2446
                                        return -1;
 
2447
                                }
 
2448
 
 
2449
                                icmp_stop = number;
 
2450
                        }
 
2451
                        else
 
2452
                                icmp_stop = number;
 
2453
 
 
2454
                        if (checksum_start > 0) {
 
2455
                                
 
2456
                                icmpcksum =  get_checksum16(icmp_start, icmp_stop); 
 
2457
                                /* the one's complement */
 
2458
                                icmpcksum = (-1) - icmpcksum;
 
2459
 
 
2460
                                /* let's write it */
 
2461
                                packet[checksum_start] = (char)(icmpcksum/256);
 
2462
                                packet[checksum_start+1] =  (char)(icmpcksum%256);
 
2463
                        }
 
2464
                        break;
 
2465
                }
 
2466
                        
 
2467
                default: {
 
2468
                        //printf("Other type of icmp message\n");
 
2469
                        code = lookup_widget(GTK_WIDGET(button), "entry157");
 
2470
                        checksum = lookup_widget(GTK_WIDGET(button), "entry158");
 
2471
                        cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton38");
 
2472
                        data = lookup_widget(GTK_WIDGET(button), "entry159");
 
2473
 
 
2474
                        code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
 
2475
                        checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
 
2476
                        data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
 
2477
                        
 
2478
                        /* code */
 
2479
                        if (char2x(code_t) == -1) {
 
2480
                                //printf("Error: icmp other code field\n");
 
2481
                                error("Error: icmp other code field");
 
2482
                                return -1;
 
2483
                        }
 
2484
                        packet[number] = (unsigned char)char2x(code_t);
 
2485
                        number++;
 
2486
                        
 
2487
                        /* checksum */  
 
2488
                        if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
 
2489
                                checksum_start = number;
 
2490
                                packet[number] = (unsigned char)0;
 
2491
                                number++;
 
2492
                                packet[number] = (unsigned char)0;
 
2493
                                number++;
 
2494
                        }
 
2495
                        /* if checksum_start = 0, we leave it in the end */
 
2496
                        else {
 
2497
                                checksum_start = 0;
 
2498
        
 
2499
                                if (char2x(checksum_t) == -1) {
 
2500
                                        //printf("Error: icmp destination unreacheable checksum field\n");
 
2501
                                        error("Error: icmp destination unreacheable checksum field");
 
2502
                                        return -1;
 
2503
                                }
 
2504
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2505
                                checksum_t++; checksum_t++; number++;
 
2506
                                if (char2x(checksum_t) == -1) {
 
2507
                                        //printf("Error: icmp destination unreacheable checksum field\n");
 
2508
                                        error("Error: icmp destination unreacheable checksum field");
 
2509
                                        return -1;
 
2510
                                }
 
2511
                                packet[number] = (unsigned char)char2x(checksum_t);
 
2512
                                number++;
 
2513
                        }
 
2514
                        
 
2515
                        /* data */
 
2516
                        payload_length = strlen(data_t);
 
2517
                                
 
2518
                        /* YYY 1514-number is not ok in case we use 802.1q!!! */
 
2519
                        if (get_network_payload(button, user_data, payload_length, 
 
2520
                                                1514-number, data_t) == -1) {
 
2521
                                //printf("Error: problem with icmp destination unreacheable payload\n");
 
2522
                                return -1;
 
2523
                        }
 
2524
 
 
2525
                        icmp_stop = number;
 
2526
 
 
2527
                        if (checksum_start > 0) {
 
2528
                                
 
2529
                                icmpcksum =  get_checksum16(icmp_start, icmp_stop); 
 
2530
                                /* the one's complement */
 
2531
                                icmpcksum = (-1) - icmpcksum;
 
2532
 
 
2533
                                /* let's write it */
 
2534
                                packet[checksum_start] = (char)(icmpcksum/256);
 
2535
                                packet[checksum_start+1] =  (char)(icmpcksum%256);
 
2536
                        }
 
2537
                }
 
2538
        }
 
2539
        return 1;
 
2540
}
 
2541
                
 
2542
/* we have to parse the arp protocol information */
 
2543
int arp_get(GtkButton *button, gpointer user_data)
 
2544
{
 
2545
        GtkWidget *hwtype, *prottype, *hwsize, *protsize;
 
2546
        GtkWidget *rbt10, *rbt11, *rbt17, *en81;
 
2547
        GtkWidget *sendermac, *senderip, *targetmac, *targetip;
 
2548
        gchar *hwtype_t, *prottype_t, *hwsize_t, *protsize_t, *en81_t;
 
2549
        gchar *sendermac_t, *senderip_t, *targetmac_t, *targetip_t;
 
2550
        int i, j;
 
2551
        gchar tmp[4];
 
2552
 
 
2553
        hwtype = lookup_widget(GTK_WIDGET(button), "A_hwtype"); 
 
2554
        prottype = lookup_widget(GTK_WIDGET(button), "A_prottype");     
 
2555
        hwsize = lookup_widget(GTK_WIDGET(button), "A_hwsize"); 
 
2556
        protsize = lookup_widget(GTK_WIDGET(button), "A_protsize");     
 
2557
        
 
2558
        rbt10 = lookup_widget(GTK_WIDGET(button), "radiobutton10");     
 
2559
        rbt11 = lookup_widget(GTK_WIDGET(button), "radiobutton11");     
 
2560
        rbt17 = lookup_widget(GTK_WIDGET(button), "radiobutton17");     
 
2561
        en81 = lookup_widget(GTK_WIDGET(button), "entry81");    
 
2562
 
 
2563
        sendermac = lookup_widget(GTK_WIDGET(button), "A_sendermac");   
 
2564
        senderip = lookup_widget(GTK_WIDGET(button), "A_senderip");     
 
2565
        targetmac = lookup_widget(GTK_WIDGET(button), "A_targetmac");   
 
2566
        targetip = lookup_widget(GTK_WIDGET(button), "A_targetip");     
 
2567
 
 
2568
        hwtype_t = (char *)gtk_entry_get_text(GTK_ENTRY(hwtype));
 
2569
        prottype_t = (char *)gtk_entry_get_text(GTK_ENTRY(prottype));
 
2570
        hwsize_t = (char *)gtk_entry_get_text(GTK_ENTRY(hwsize));
 
2571
        protsize_t = (char *)gtk_entry_get_text(GTK_ENTRY(protsize));
 
2572
        sendermac_t = (char *)gtk_entry_get_text(GTK_ENTRY(sendermac));
 
2573
        senderip_t = (char *)gtk_entry_get_text(GTK_ENTRY(senderip));
 
2574
        targetmac_t = (char *)gtk_entry_get_text(GTK_ENTRY(targetmac));
 
2575
        targetip_t = (char *)gtk_entry_get_text(GTK_ENTRY(targetip));
 
2576
        en81_t = (char *)gtk_entry_get_text(GTK_ENTRY(en81));
 
2577
        
 
2578
        /* now we have all the widget and we start parsing the info: we start with the hardware type */
 
2579
        if (char2x(hwtype_t) == -1) {
 
2580
                //printf("Error: hwtype field\n");
 
2581
                error("Error: hwtype field");
 
2582
                return -1;
 
2583
        }
 
2584
        packet[number] = (unsigned char)char2x(hwtype_t);
 
2585
        hwtype_t++; hwtype_t++; number++;
 
2586
        if (char2x(hwtype_t) == -1) {
 
2587
                //printf("Error: hwtype field\n");
 
2588
                error("Error: hwtype field");
 
2589
                return -1;
 
2590
        }
 
2591
        packet[number] = (unsigned char)char2x(hwtype_t);
 
2592
        number++;
 
2593
 
 
2594
        /* prottype */
 
2595
        if (char2x(prottype_t) == -1) {
 
2596
                //printf("Error: prottype field\n");
 
2597
                error("Error: prottype field");
 
2598
                return -1;
 
2599
        }
 
2600
        packet[number] = (unsigned char)char2x(prottype_t);
 
2601
        prottype_t++; prottype_t++; number++;
 
2602
        if (char2x(prottype_t) == -1) {
 
2603
                //printf("Error: prottype field\n");
 
2604
                error("Error: prottype field");
 
2605
                return -1;
 
2606
        }
 
2607
        packet[number] = (unsigned char)char2x(prottype_t);
 
2608
        number++;
 
2609
 
 
2610
        /* hwsize */
 
2611
        if (char2x(hwsize_t) == -1) {
 
2612
                //printf("Error: hwsize field\n");
 
2613
                error("Error: hwsize field");
 
2614
                return -1;
 
2615
        }
 
2616
        packet[number] = (unsigned char)char2x(hwsize_t);
 
2617
        number++;
 
2618
 
 
2619
        /* protsize */
 
2620
        if (char2x(protsize_t) == -1) {
 
2621
                //printf("Error: protsize field\n");
 
2622
                error("Error: protsize field");
 
2623
                return -1;
 
2624
        }
 
2625
        packet[number] = (unsigned char)char2x(protsize_t);
 
2626
        number++;
 
2627
 
 
2628
        /* which opcode */
 
2629
        if (GTK_TOGGLE_BUTTON(rbt10)->active) {
 
2630
                packet[number] = 0x00;
 
2631
                number++;
 
2632
                packet[number] = 0x01;
 
2633
                number++;
 
2634
                
 
2635
        }
 
2636
        else if (GTK_TOGGLE_BUTTON(rbt11)->active) { 
 
2637
                packet[number] = 0x00;
 
2638
                number++;
 
2639
                packet[number] = 0x02;
 
2640
                number++;
 
2641
        }
 
2642
        else if (GTK_TOGGLE_BUTTON(rbt17)->active) { 
 
2643
                if (char2x(en81_t) == -1) {
 
2644
                        //printf("Error: entry arp opcode\n");
 
2645
                        error("Error: entry arp opcode");
 
2646
                        return -1;
 
2647
                }
 
2648
                packet[number] = (unsigned char)char2x(en81_t);
 
2649
                en81_t++; en81_t++; number++;
 
2650
                if (char2x(en81_t) == -1) {
 
2651
                        //printf("Error: entry arp opcode\n");
 
2652
                        error("Error: entry arp opcode");
 
2653
                        return -1;
 
2654
                }
 
2655
                packet[number] = (unsigned char)char2x(en81_t);
 
2656
                number++;
 
2657
        }
 
2658
        else {
 
2659
                //printf("Error: Something is wrong with the arp opcode\n");
 
2660
                error("Error: Something is wrong with the arp opcode");
 
2661
                return -1;
 
2662
        }
 
2663
 
 
2664
        
 
2665
        /* and now the ip&mac values: check if addresses are ok */
 
2666
        eth_start = number;
 
2667
        
 
2668
        if (check_mac_address(sendermac_t) == -1) {
 
2669
                //printf("Error: Wrong mac entry in arp sender field, can't copy it\n");
 
2670
                error("Error: Wrong mac entry in arp sender field, can't copy it");
 
2671
                return -1;
 
2672
        }
 
2673
        if (check_mac_address(targetmac_t) == -1) {
 
2674
                //printf("Error: Wrong mac entry in arp target field, can't copy it\n");
 
2675
                error("Error: Wrong mac entry in arp target field, can't copy it");
 
2676
                return -1;
 
2677
        }
 
2678
        if (check_ip_address(senderip_t) == -1) {
 
2679
                //printf("Error: Wrong ip entry in arp sender field, can't copy it\n");
 
2680
                error("Error: Wrong ip entry in arp sender field, can't copy it");
 
2681
                return -1;
 
2682
        }
 
2683
        if (check_ip_address(targetip_t) == -1) {
 
2684
                //printf("Error: Wrong ip entry in arp target field, can't copy it\n");
 
2685
                error("Error: Wrong ip entry in arp target field, can't copy it");
 
2686
                return -1;
 
2687
        }
 
2688
        
 
2689
        /* if all addresses are ok, we copy them into packet: first sender mac */
 
2690
        for(i=0; i<6; i++) {
 
2691
                packet[number] = (unsigned char)char2x(sendermac_t);
 
2692
                sendermac_t = sendermac_t + 3; number++;
 
2693
        }
 
2694
        
 
2695
        /* sender ip */
 
2696
        for (i=0; i<4; i++) {
 
2697
                for(j=0; j<4 && (*senderip_t != '\0'); j++) {
 
2698
                        if ( ((int)*senderip_t == '.') && (i<3) && (j>0) ) {
 
2699
                                senderip_t++;
 
2700
                                break;
 
2701
                        }
 
2702
                        tmp[j] = *senderip_t;
 
2703
                        senderip_t++;
 
2704
                }
 
2705
                tmp[j] = '\0';
 
2706
                packet[number] = (unsigned char)(atoi(tmp));
 
2707
                number++;               
 
2708
        }
 
2709
        
 
2710
        /* target mac */
 
2711
        for(i=0; i<6; i++) {
 
2712
                packet[number] = (unsigned char)char2x(targetmac_t);
 
2713
                targetmac_t = targetmac_t + 3; number++;
 
2714
        }
 
2715
        
 
2716
        /* target ip */
 
2717
        for (i=0; i<4; i++) {
 
2718
                for(j=0; j<4 && (*targetip_t != '\0'); j++) {
 
2719
                        if ( ((int)*targetip_t == '.') && (i<3) && (j>0) ) {
 
2720
                                targetip_t++;
 
2721
                                break;
 
2722
                        }
 
2723
                        tmp[j] = *targetip_t;
 
2724
                        targetip_t++;
 
2725
                }
 
2726
                tmp[j] = '\0';
 
2727
                packet[number] = (unsigned char)(atoi(tmp));
 
2728
                number++;               
 
2729
        }
 
2730
        
 
2731
        return 1;
 
2732
}
 
2733
 
 
2734
 
 
2735
/* user choosed to manually attach payload, so here we are */
 
2736
int get_network_payload(GtkButton *button, gpointer user_data, int length, int max, gchar *entry)
 
2737
{
 
2738
        int i, stevec = 0;
 
2739
        gchar *ptr;
 
2740
 
 
2741
        /* firs we check if total length without spaces is an even number */
 
2742
        ptr = entry;
 
2743
        for (i=0; i < length; i++, ptr++) {
 
2744
                if (isspace(*ptr) != 0) { /* prazne znake ne upostevam */
 
2745
                        continue;
 
2746
                }       
 
2747
                stevec++;
 
2748
        }
 
2749
 
 
2750
        if ( stevec % 2 != 0) {
 
2751
                //printf("Error: Payload lengtht must be an even number\n");
 
2752
                error("Error: Payload lengtht must be an even number");
 
2753
                return -1;
 
2754
        }
 
2755
        
 
2756
        stevec = 1;
 
2757
 
 
2758
        for (i=0; i < length ; ) {
 
2759
                if (isspace(*entry) != 0) { /* prazne znake ne upostevam */
 
2760
                        entry++;
 
2761
                        i++;
 
2762
                        continue;
 
2763
                }       
 
2764
                if (stevec > max) {
 
2765
                        //printf("Error: Network layer payload lengtht to long\n");
 
2766
                        error("Error: Network layer payload lengtht to long");
 
2767
                        return -1;
 
2768
                }
 
2769
                if (char2x(entry) == -1) {
 
2770
                        //printf("Error: network layer payload\n");
 
2771
                        error("Error: network layer payload");
 
2772
                        return -1;
 
2773
                }
 
2774
                packet[number] = (unsigned char)char2x(entry);
 
2775
                number++; i++; i++; entry++; entry++; stevec++;;        
 
2776
        }
 
2777
        return 1;       
 
2778
}
 
2779
 
 
2780
 
 
2781
int link_level_get(GtkButton *button, gpointer user_data)
 
2782
{
 
2783
        GtkWidget *ver2_tbt, *_801q_cbt, *_8023_tbt;
 
2784
        GtkWidget *ethtype_e;
 
2785
        gchar *ethtype_t;
 
2786
 
 
2787
        ver2_tbt = lookup_widget(GTK_WIDGET (button), "bt_ver2");
 
2788
        _8023_tbt = lookup_widget(GTK_WIDGET (button), "bt_8023");
 
2789
        _801q_cbt = lookup_widget(GTK_WIDGET (button), "bt_8021q");
 
2790
 
 
2791
        /* always we need first the dest and source mac address */
 
2792
        if (get_mac_from_string(button) == -1) {
 
2793
                //printf("Error: mac address field\n");
 
2794
                error("Error: mac address field");
 
2795
                return -1;
 
2796
        }
 
2797
        number = 12;
 
2798
 
 
2799
        /* is 802.1q active - do we need to add 4 or 8 bytes? */
 
2800
        if (GTK_TOGGLE_BUTTON(_801q_cbt)->active) {
 
2801
                if (get_8021q(button) == -1) {
 
2802
                        //printf("Error: 802.1q field\n");
 
2803
                        return -1;
 
2804
                }
 
2805
        }
 
2806
        if (GTK_TOGGLE_BUTTON(_8023_tbt)->active) { /* uporabimo ethernet vezije 802.3 */
 
2807
                if (get_8023(button) == -1) {
 
2808
                        //printf("Error: 802.3 field");
 
2809
                        return -1;
 
2810
                }
 
2811
        }
 
2812
 
 
2813
        else if (GTK_TOGGLE_BUTTON(ver2_tbt)->active){ /* pol pa verzijo 2 */ 
 
2814
                autolength = 0;
 
2815
                ethtype_e = lookup_widget(GTK_WIDGET (button), "L_ethtype");
 
2816
                ethtype_t = (char *)gtk_entry_get_text(GTK_ENTRY(ethtype_e));
 
2817
                if (char2x(ethtype_t) == -1) {
 
2818
                        //printf("Error: ethernet type field\n");
 
2819
                        error("Error: ethernet type field");
 
2820
                        return -1;
 
2821
                }
 
2822
                packet[number] = (unsigned char)char2x(ethtype_t);
 
2823
                ethtype_t++; ethtype_t++; number++;
 
2824
                if (char2x(ethtype_t) == -1) {
 
2825
                        //printf("Error: ethernet type field\n");
 
2826
                        error("Error: ethernet type field");
 
2827
                        return -1;
 
2828
                }
 
2829
                packet[number] = (unsigned char)char2x(ethtype_t);
 
2830
                number++;
 
2831
        }
 
2832
        else {/* kva a je mogoce token ring??? */
 
2833
                //printf("Error: in ethernet field\n");
 
2834
                error("Error: in ethernet field");
 
2835
                return -1;
 
2836
        }
 
2837
 
 
2838
 
 
2839
        return 1;
 
2840
}
 
2841
 
 
2842
 
 
2843
/* if we are in the 802.3 ethernet version */
 
2844
int get_8023(GtkButton *button)
 
2845
{
 
2846
        GtkWidget *ethlength_e, *L8023llc_tbt, *L8023llcsnap_tbt, *Ldsap_e, *Lssap_e;
 
2847
        GtkWidget *Lctrl_e, *Loui_e, *Lpid_e, *autolength_bt;
 
2848
        gchar *Ldsap_t, *Lssap_t, *Lctrl_t, *Loui_t, *Lpid_t;
 
2849
        gchar *ethlength_t;
 
2850
 
 
2851
        /* do we need to calculate the length field or will be suplied manually */
 
2852
        autolength_bt = lookup_widget(GTK_WIDGET (button), "checkbutton2");
 
2853
        if (GTK_TOGGLE_BUTTON(autolength_bt)->active) {
 
2854
                autolength = number;
 
2855
                packet[number] = 0x0; number++; packet[number] = 0x0; number++;
 
2856
        }
 
2857
        else {
 
2858
                autolength = 0;
 
2859
                ethlength_e = lookup_widget(GTK_WIDGET (button), "entry5");
 
2860
                ethlength_t = (char *)gtk_entry_get_text(GTK_ENTRY(ethlength_e));
 
2861
                if (char2x(ethlength_t) == -1) {
 
2862
                        //printf("Error: 802.3 length field\n");
 
2863
                        error("Error: 802.3 length field");
 
2864
                        return -1;
 
2865
                }
 
2866
                packet[number] = (unsigned char)char2x(ethlength_t);
 
2867
                ethlength_t++; ethlength_t++; number++;
 
2868
                if (char2x(ethlength_t) == -1) {
 
2869
                        //printf("Error: 802.3 length field\n");
 
2870
                        error("Error: 802.3 length field");
 
2871
                        return -1;
 
2872
                }
 
2873
                packet[number] = (unsigned char)char2x(ethlength_t);
 
2874
                number++;
 
2875
        }
 
2876
        
 
2877
        L8023llc_tbt = lookup_widget(GTK_WIDGET (button), "L_8023_llc_tbt");
 
2878
        L8023llcsnap_tbt = lookup_widget(GTK_WIDGET (button), "L_8023_llcsnap_tbt");
 
2879
        Ldsap_e = lookup_widget(GTK_WIDGET (button), "L_dsap");
 
2880
        Lssap_e= lookup_widget(GTK_WIDGET (button), "L_ssap");
 
2881
        Lctrl_e= lookup_widget(GTK_WIDGET (button), "L_ctrl");
 
2882
 
 
2883
        Ldsap_t = (char *)gtk_entry_get_text(GTK_ENTRY(Ldsap_e));
 
2884
        if (char2x(Ldsap_t) == -1) {
 
2885
                //printf("Error: 802.3 ldsap field\n");
 
2886
                error("Error: 802.3 ldsap field");
 
2887
                       return -1;
 
2888
        }
 
2889
        packet[number] = (unsigned char)char2x(Ldsap_t);
 
2890
        number++;
 
2891
 
 
2892
        Lssap_t = (char *)gtk_entry_get_text(GTK_ENTRY(Lssap_e));
 
2893
        if (char2x(Lssap_t) == -1) {
 
2894
                //printf("Error: 802.3 lssap field\n");
 
2895
                error("Error: 802.3 lssap field");
 
2896
                       return -1;
 
2897
        }
 
2898
        packet[number] = (unsigned char)char2x(Lssap_t);
 
2899
        number++;
 
2900
 
 
2901
        Lctrl_t = (char *)gtk_entry_get_text(GTK_ENTRY(Lctrl_e));
 
2902
        if (char2x(Lctrl_t) == -1) {
 
2903
                //printf("Error: 802.3 Ctrl field\n");
 
2904
                error("Error: 802.3 Ctrl field");
 
2905
                       return -1;
 
2906
        }
 
2907
        packet[number] = (unsigned char)char2x(Lctrl_t);
 
2908
        number++;
 
2909
 
 
2910
        /* do we need snap encapsulation */ 
 
2911
        if (GTK_TOGGLE_BUTTON(L8023llcsnap_tbt)->active) {
 
2912
                Loui_e = lookup_widget(GTK_WIDGET (button), "L_oui");
 
2913
                Lpid_e = lookup_widget(GTK_WIDGET (button), "L_pid");
 
2914
                
 
2915
                Loui_t = (char *)gtk_entry_get_text(GTK_ENTRY(Loui_e));
 
2916
                if (char2x(Loui_t) == -1) {
 
2917
                        //printf("Error: 802.3 oui field\n");
 
2918
                        error("Error: 802.3 oui field");
 
2919
                        return -1;
 
2920
                }
 
2921
                packet[number] = (unsigned char)char2x(Loui_t);
 
2922
                number++; Loui_t++, Loui_t++;
 
2923
 
 
2924
                if (char2x(Loui_t) == -1) {
 
2925
                        //printf("Error: 802.3 oui field\n");
 
2926
                        error("Error: 802.3 oui field");
 
2927
                        return -1;
 
2928
                }
 
2929
                packet[number] = (unsigned char)char2x(Loui_t);
 
2930
                number++; Loui_t++, Loui_t++;
 
2931
 
 
2932
                if (char2x(Loui_t) == -1) {
 
2933
                        //printf("Error: 802.3 oui field\n");
 
2934
                        error("Error: 802.3 oui field");
 
2935
                        return -1;
 
2936
                }
 
2937
                packet[number] = (unsigned char)char2x(Loui_t);
 
2938
                number++; 
 
2939
 
 
2940
                Lpid_t = (char *)gtk_entry_get_text(GTK_ENTRY(Lpid_e));
 
2941
                if (char2x(Lpid_t) == -1) {
 
2942
                        //printf("Error: 802.3 snap pid field\n");
 
2943
                        error("Error: 802.3 snap pid field");
 
2944
                        return -1;
 
2945
                }
 
2946
                packet[number] = (unsigned char)char2x(Lpid_t);
 
2947
                number++; Lpid_t++; Lpid_t++;
 
2948
 
 
2949
                if (char2x(Lpid_t) == -1) {
 
2950
                        //printf("Error: 802.3 snap pid field\n");
 
2951
                        error("Error: 802.3 snap pid field");
 
2952
                        return -1;
 
2953
                }
 
2954
                packet[number] = (unsigned char)char2x(Lpid_t);
 
2955
                number++; 
 
2956
 
 
2957
                return 1;
 
2958
        }
 
2959
        else 
 
2960
                return 1;
 
2961
}
 
2962
 
 
2963
 
 
2964
/* function parses 802.1q field */
 
2965
int get_8021q(GtkButton *button)
 
2966
{
 
2967
        GtkWidget *vlan_e, *priority_m, *cfi1_rbt, *vlanid_e, *menu, *menu_item, *QinQ_bt, *QinQ, *QinQpvid;
 
2968
        gchar *vlan_t, *vlanid_t, *QinQ_t; 
 
2969
        gint menu_index, cfi =0;
 
2970
        char tmp[2];
 
2971
 
 
2972
        QinQ_bt = lookup_widget(GTK_WIDGET (button), "checkbutton40");
 
2973
 
 
2974
        /* what about QinQ field? */
 
2975
        if (GTK_TOGGLE_BUTTON(QinQ_bt)->active) {
 
2976
                QinQpvid = lookup_widget(GTK_WIDGET (button), "optionmenu21");
 
2977
                QinQ = lookup_widget(GTK_WIDGET (button), "entry165");
 
2978
                
 
2979
                menu = GTK_OPTION_MENU(QinQpvid)->menu;
 
2980
                menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
2981
                menu_index = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
2982
 
 
2983
                switch (menu_index) {
 
2984
                        case 0: {
 
2985
                                packet[number] = (unsigned char)char2x("81");
 
2986
                                number++;
 
2987
                                packet[number] = (unsigned char)char2x("00");
 
2988
                                number++;
 
2989
                                break;
 
2990
                        }
 
2991
                        case 1: {
 
2992
                                packet[number] = (unsigned char)char2x("91");
 
2993
                                number++;
 
2994
                                packet[number] = (unsigned char)char2x("00");
 
2995
                                number++;
 
2996
                                break;
 
2997
                        }
 
2998
                        case 2: {
 
2999
                                packet[number] = (unsigned char)char2x("92");
 
3000
                                number++;
 
3001
                                packet[number] = (unsigned char)char2x("00");
 
3002
                                number++;
 
3003
                                break;
 
3004
                        }
 
3005
                        case 3: {
 
3006
                                packet[number] = (unsigned char)char2x("88");
 
3007
                                number++;
 
3008
                                packet[number] = (unsigned char)char2x("a8");
 
3009
                                number++;
 
3010
                                break;
 
3011
                        }
 
3012
                }
 
3013
 
 
3014
                QinQ_t = (char *)gtk_entry_get_text(GTK_ENTRY(QinQ));
 
3015
                if (char2x(QinQ_t) == -1) {
 
3016
                        //printf("Error: VLAN QinQ type field\n");
 
3017
                        error("Error: VLAN QinQ field");
 
3018
                        return -1;
 
3019
                }
 
3020
                packet[number] = (unsigned char)char2x(QinQ_t);
 
3021
                QinQ_t++; QinQ_t++; number++;
 
3022
                if (char2x(QinQ_t) == -1) {
 
3023
                        //printf("Error: VLAN QinQ type field\n");
 
3024
                        error("Error: VLAN QinQ field");
 
3025
                        return -1;
 
3026
                }
 
3027
                packet[number] = (unsigned char)char2x(QinQ_t);
 
3028
                number++;
 
3029
 
 
3030
        }
 
3031
 
 
3032
        vlan_e = lookup_widget(GTK_WIDGET (button), "L_tag_id");
 
3033
        priority_m = lookup_widget(GTK_WIDGET (button), "L_optmenu2_bt");
 
3034
        cfi1_rbt = lookup_widget(GTK_WIDGET (button), "checkbutton39");
 
3035
        vlanid_e = lookup_widget(GTK_WIDGET (button), "L_vlan_id");
 
3036
        vlan_t = (char *)gtk_entry_get_text(GTK_ENTRY(vlan_e));
 
3037
        vlanid_t = (char *)gtk_entry_get_text(GTK_ENTRY(vlanid_e));
 
3038
                
 
3039
        /* first we chech the vlan protocol id */
 
3040
        if (char2x(vlan_t) == -1) {
 
3041
                //printf("Error: 802.1q type field\n");
 
3042
                error("Error: 802.1q type field");
 
3043
                return -1;
 
3044
        }
 
3045
        packet[number] = (unsigned char)char2x(vlan_t);
 
3046
        vlan_t++; vlan_t++; number++;
 
3047
        if (char2x(vlan_t) == -1) {
 
3048
                //printf("Error: 802.1q type field\n");
 
3049
                error("Error: 802.1q type field");
 
3050
                return -1;
 
3051
        }
 
3052
        packet[number] = (unsigned char)char2x(vlan_t);
 
3053
        number++;       
 
3054
        
 
3055
        /* next we need the priority */
 
3056
        menu = GTK_OPTION_MENU(priority_m)->menu;
 
3057
        menu_item = gtk_menu_get_active (GTK_MENU (menu));
 
3058
        menu_index = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
 
3059
 
 
3060
        /* what about CFI bit? */
 
3061
        if (GTK_TOGGLE_BUTTON(cfi1_rbt)->active)
 
3062
                cfi = 1;
 
3063
        else 
 
3064
                cfi = 0;
 
3065
 
 
3066
        /* in cfi we store the value of priority and cfi */
 
3067
        tmp[0] = (unsigned char)(menu_index * 2 + cfi);
 
3068
        snprintf(&(tmp[0]), 2, "%x", tmp[0]);
 
3069
 
 
3070
        /* we need the vlan id */
 
3071
        tmp[1] = *vlanid_t;
 
3072
 
 
3073
        if (char2x(tmp) == -1) {
 
3074
                //printf("Error: 802.1q: priority & cfi field & 1 byte vlan id\n");
 
3075
                error("Error: 802.1q: priority & cfi field & 1 byte vlan id");
 
3076
                return -1;
 
3077
        }
 
3078
        packet[number] = (unsigned char)char2x(tmp);
 
3079
 
 
3080
        vlanid_t++; number++;
 
3081
 
 
3082
        if (char2x(vlanid_t) == -1) {
 
3083
                //printf("Error: 802.1q vlanid \n");
 
3084
                error("Error: 802.1q vlanid ");
 
3085
                return -1;
 
3086
        }
 
3087
        packet[number] = (unsigned char)char2x(vlanid_t);
 
3088
        number++;
 
3089
 
 
3090
        return 1;
 
3091
}
 
3092
 
 
3093
 
 
3094
/* calculate the checksum 
 
3095
 * we pass the start and stop number in packet[] 
 
3096
 * where we won't to calculate the checksum */
 
3097
guint32 get_checksum32(int cks_start, int cks_stop) 
 
3098
 
 
3099
{
 
3100
        guint32 value;
 
3101
        long sum = 0;
 
3102
 
 
3103
        for (; cks_start<cks_stop; ) {
 
3104
                /* we take 16 bit word's -> 2 bytes */
 
3105
                value = (packet[cks_start]<<8) + packet[cks_start+1];
 
3106
                sum = sum + value;
 
3107
                /* for every cicle, this means where the sum exceeds 
 
3108
                 * the 16 bit unsigned max value (65536), you have to add 1
 
3109
                 * to the rest */
 
3110
                //sum = (sum % 0x10000) + (sum / 0x10000);
 
3111
                cks_start +=2; 
 
3112
        }
 
3113
        /* we don't do extract the sum from 0xFFFF (or -1), so you have to do
 
3114
         * this later */
 
3115
        //return (sum % 0x10000);
 
3116
        return sum;
 
3117
}
 
3118
        
 
3119
 
 
3120
guint16 get_checksum16(int cks_start, int cks_stop) 
 
3121
 
 
3122
{
 
3123
        guint16 value;
 
3124
        long sum = 0;
 
3125
 
 
3126
        for (; cks_start<cks_stop; ) {
 
3127
                /* we take 16 bit word's -> 2 bytes */
 
3128
                value = (packet[cks_start]<<8) + packet[cks_start+1];
 
3129
                sum = sum + value;
 
3130
                /* for every cicle, this means where the sum exceeds 
 
3131
                 * the 16 bit unsigned max value (65536), you have to add 1
 
3132
                 * to the rest */
 
3133
                sum = (sum % 0x10000) + (sum / 0x10000);
 
3134
                cks_start +=2; 
 
3135
        }
 
3136
        /* we don't do extract the sum from 0xFFFF (or -1), so you have to do
 
3137
         * this later */
 
3138
        return (sum % 0x10000);
 
3139
        //return sum;
 
3140
}
 
3141
/*check ip address */
 
3142
int check_ip_address(gchar *ptr)
 
3143
{
 
3144
        int i, j;
 
3145
        gchar tmp[4];
 
3146
        
 
3147
        for (i=0; i<4; i++) {
 
3148
                for(j=0; j<4 && (*ptr != '\0'); j++) {
 
3149
                        if ( ((int)*ptr == '.') && (i<3) && (j>0) ) {
 
3150
                                ptr++;
 
3151
                                break;
 
3152
                        }
 
3153
                        if ( (*ptr <48) || (*ptr>57) )
 
3154
                                return -1;
 
3155
                        else {
 
3156
                                tmp[j] = *ptr;
 
3157
                                ptr++;
 
3158
                        }
 
3159
                }
 
3160
                tmp[j] = '\0';
 
3161
                if ( (atoi(tmp) < 0) || (atoi(tmp) > 255) || (strlen(tmp)==0) || (strlen(tmp)>3) )
 
3162
                        return -1;
 
3163
        }
 
3164
        return 1;
 
3165
}
 
3166
 
 
3167
 
 
3168
/* check mac address */
 
3169
int check_mac_address(gchar *ptr)
 
3170
{
 
3171
        int i;
 
3172
 
 
3173
        if ( strlen(ptr) > 17)
 
3174
                return -1;
 
3175
        /* all mac addresses must be in full xx:xx:xx:xx:xx:xx format. f:... in not ok 0f:... works */
 
3176
        for(i=0; i<6; i++) {
 
3177
                if (char2x(ptr) == -1)
 
3178
                        return -1;
 
3179
                ptr = ptr+2;
 
3180
                if ( (*ptr != ':') && (i<5) )
 
3181
                        return -1;
 
3182
                else
 
3183
                        ptr++;
 
3184
        }
 
3185
        return 1;
 
3186
}
 
3187
 
 
3188
 
 
3189
/* function parses mac address */
 
3190
int get_mac_from_string(GtkButton *button)
 
3191
{
 
3192
        GtkWidget *dstmac_e, *srcmac_e;
 
3193
        gchar *dstmac_t, *srcmac_t;
 
3194
        int dst_length, src_length, i;
 
3195
 
 
3196
        dstmac_e = lookup_widget(GTK_WIDGET (button), "L_dst_mac");
 
3197
        srcmac_e = lookup_widget(GTK_WIDGET (button), "L_src_mac");
 
3198
        dstmac_t = (char *)gtk_entry_get_text(GTK_ENTRY(dstmac_e));
 
3199
        srcmac_t = (char *)gtk_entry_get_text(GTK_ENTRY(srcmac_e));
 
3200
        dst_length = strlen(dstmac_t);
 
3201
        src_length = strlen(srcmac_t);
 
3202
 
 
3203
        /* mac naslov mora viti v formatu xx:xx:xx:xx:xx:xx to pomeni 17 znakov skupaj! */
 
3204
        if ((src_length != 17) || (dst_length != 17))
 
3205
                return -1;
 
3206
        
 
3207
        /* first we store destination address into packet[] */
 
3208
        for(i=0; i<6; i++) {
 
3209
                if (char2x(dstmac_t) == -1)
 
3210
                        return -1;
 
3211
                packet[i] = (unsigned char)char2x(dstmac_t);
 
3212
                dstmac_t = dstmac_t + 2;
 
3213
                if ((i<5) && (*dstmac_t != ':'))
 
3214
                        return -1;
 
3215
                else if (i == 5)
 
3216
                        ;
 
3217
                else
 
3218
                        dstmac_t++;
 
3219
        }
 
3220
                 
 
3221
        /* source address into packet[] */
 
3222
        for(i=6; i<12; i++) {
 
3223
                if (char2x(srcmac_t) == -1)
 
3224
                        return -1;
 
3225
                packet[i] = (unsigned char)char2x(srcmac_t);
 
3226
                srcmac_t = srcmac_t + 2;
 
3227
                if ((i<5) && (*srcmac_t != ':'))
 
3228
                        return -1;
 
3229
                else if (i == 5)
 
3230
                        ;
 
3231
                else
 
3232
                        srcmac_t++;
 
3233
        }
 
3234
                 
 
3235
        return 1;
 
3236
}
 
3237
 
 
3238
 
 
3239
/* function takes pointer to char and converts two chars to hex and returns signed int. If you want to use te return value as char, you need to cast back to (unsigned char) */
 
3240
signed int char2x(char *p) 
 
3241
{
 
3242
    unsigned char x=0;
 
3243
 
 
3244
    if ( (*p >= '0') && (*p <= '9')) {
 
3245
        x = ((*p) - 48) * 16;
 
3246
    }
 
3247
    else if ((*p >= 'A') && (*p <= 'F')) {
 
3248
        x = ((*p) - 55) * 16;
 
3249
    }
 
3250
    else if ((*p >= 'a') && (*p <= 'f')) {
 
3251
        x = ((*p) - 87) * 16;
 
3252
    }
 
3253
    else {
 
3254
        return -1;
 
3255
    }
 
3256
    p++;
 
3257
    if ( (*p >= '0') && (*p <= '9')) {
 
3258
        x = x + ((*p) - 48);
 
3259
    }
 
3260
    else if ((*p >= 'A') && (*p <= 'F')) {
 
3261
        x = x + ((*p) - 55);
 
3262
    }
 
3263
    else if ((*p >= 'a') && (*p <= 'f')) {
 
3264
        x = x + ((*p) - 87);
 
3265
    }
 
3266
    else {
 
3267
        return -1;
 
3268
    }
 
3269
    return (int)x;
 
3270
}
 
3271
 
 
3272
 
 
3273
char c4(int value)
 
3274
{
 
3275
        switch(value) {
 
3276
                case 0: return '0';
 
3277
                case 1: return '1';
 
3278
                case 2: return '2';
 
3279
                case 3: return '3';
 
3280
                case 4: return '4';
 
3281
                case 5: return '5';
 
3282
                case 6: return '6';
 
3283
                case 7: return '7';
 
3284
                case 8: return '8';
 
3285
                case 9: return '9';
 
3286
                case 10: return 'A';
 
3287
                case 11: return 'B';
 
3288
                case 12: return 'C';
 
3289
                case 13: return 'D';
 
3290
                case 14: return 'E';
 
3291
                case 15: return 'F';
 
3292
                default: return '0';
 
3293
        }       
 
3294
}
 
3295
 
 
3296
 
 
3297
char *c8(char *s, unsigned char x) {
 
3298
        *s++ = c4(x>>4);
 
3299
        *s++ = c4(x & 0x0F);
 
3300
        *s = '\0';
 
3301
        return s;
 
3302
}
 
3303
 
 
3304
 
 
3305
int insert_frequency(int codec, int frequency, int length, GtkWidget *payload_entry, gint amp_index) 
 
3306
{
 
3307
        double  fs = 8000;      /* vzorcna frekvenca */
 
3308
        double amp;      /* amplituda */
 
3309
        double  ph = 0;         /* zacetna faza */
 
3310
        double delta_ph;
 
3311
        double sample;       /* 16 bit variable */
 
3312
        gchar entry_t[2*length+1];
 
3313
        gchar *ptr;
 
3314
 
 
3315
        ptr = entry_t;
 
3316
        delta_ph = 2* M_PI *frequency/fs;
 
3317
 
 
3318
        /* the amp values are: low - 5000, mid - 15000, max - 30000 */
 
3319
        amp = 5000 + amp_index * 7500 + amp_index * amp_index * 2500;
 
3320
 
 
3321
        while(length) {
 
3322
                sample = amp*sin(ph);
 
3323
                ph = ph + delta_ph;
 
3324
                while (ph > (2*M_PI)) {
 
3325
                    ph = ph - (2*M_PI);
 
3326
                }
 
3327
        
 
3328
                if (codec == 1) 
 
3329
                        c8(ptr, linear2alaw((gint16)sample));
 
3330
                else
 
3331
                        c8(ptr, linear2ulaw((gint16)sample));
 
3332
                ptr++;
 
3333
                ptr++;
 
3334
 
 
3335
                length--;
 
3336
        }
 
3337
 
 
3338
        *ptr = '\0';
 
3339
 
 
3340
        gtk_entry_set_text(GTK_ENTRY(payload_entry), entry_t);
 
3341
 
 
3342
        return 1;
 
3343
}
 
3344
 
 
3345
 
 
3346
/* Following three routines are from Sun Microsystems, Inc. */
 
3347
unsigned char linear2alaw(int pcm_val)  /* 2's complement (16-bit range) */
 
3348
{
 
3349
        static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF};
 
3350
        int             mask; 
 
3351
        int             seg;  
 
3352
        unsigned char   aval;
 
3353
 
 
3354
        pcm_val = pcm_val >> 3;
 
3355
 
 
3356
        if (pcm_val >= 0) {
 
3357
                mask = 0xD5;            /* sign (7th) bit = 1 */
 
3358
        } else {
 
3359
                mask = 0x55;            /* sign bit = 0 */
 
3360
                pcm_val = -pcm_val - 1;
 
3361
        }
 
3362
 
 
3363
        /* Convert the scaled magnitude to segment number. */
 
3364
        seg = search(pcm_val, seg_aend, 8);
 
3365
 
 
3366
        /* Combine the sign, segment, and quantization bits. */
 
3367
 
 
3368
        if (seg >= 8)           /* out of range, return maximum value. */
 
3369
                return (unsigned char) (0x7F ^ mask);
 
3370
        else {
 
3371
                aval = (unsigned char) seg << 4;
 
3372
                if (seg < 2)
 
3373
                        aval |= (pcm_val >> 1) & 0xf;
 
3374
                else
 
3375
                        aval |= (pcm_val >> seg) & 0xf;
 
3376
                return (aval ^ mask);
 
3377
        }
 
3378
}
 
3379
 
 
3380
 
 
3381
unsigned char linear2ulaw(short pcm_val)  /* 2's complement (16-bit range) */
 
3382
{
 
3383
        static short seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
 
3384
        short           mask;
 
3385
        short           seg;
 
3386
        unsigned char   uval;
 
3387
 
 
3388
        /* Get the sign and the magnitude of the value. */
 
3389
        pcm_val = pcm_val >> 2;
 
3390
        if (pcm_val < 0) {
 
3391
                pcm_val = -pcm_val;
 
3392
                mask = 0x7F;
 
3393
        } else {
 
3394
                mask = 0xFF;
 
3395
        }
 
3396
        if ( pcm_val > 8159 ) pcm_val = 8159;           /* clip the magnitude */
 
3397
        pcm_val += (0x84 >> 2);
 
3398
 
 
3399
        /* Convert the scaled magnitude to segment number. */
 
3400
        seg = search(pcm_val, seg_uend, 8);
 
3401
 
 
3402
        /*
 
3403
         * Combine the sign, segment, quantization bits;
 
3404
         * and complement the code word.
 
3405
         */
 
3406
        if (seg >= 8)           /* out of range, return maximum value. */
 
3407
                return (unsigned char) (0x7F ^ mask);
 
3408
        else {
 
3409
                uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
 
3410
                return (uval ^ mask);
 
3411
        }
 
3412
 
 
3413
}
 
3414
 
 
3415
 
 
3416
short search(int val, short *table, int size)
 
3417
{
 
3418
        int i;
 
3419
 
 
3420
        for (i = 0; i < size; i++) {
 
3421
                if (val <= *table++)
 
3422
                        return (i);
 
3423
        }
 
3424
        return (size);
 
3425
}
 
3426
 
 
3427
 
 
3428
int check_digit(char *field, int length, char *text)
 
3429
{
 
3430
        int i;
 
3431
        
 
3432
        /* we check if the field contains only numbers and is not empty */
 
3433
        if (length == 0) {
 
3434
                //printf("%s\n", text);
 
3435
                error(text);
 
3436
                return -1;
 
3437
        }
 
3438
 
 
3439
        for(i=0; i < length; i++, field++) {
 
3440
                if (isdigit(*field) == 0) {
 
3441
                        //printf("%s\n", text);
 
3442
                        error(text);
 
3443
                        return -1;
 
3444
                }       
 
3445
        }
 
3446
        return 1;
 
3447
}       
 
3448
 
 
3449
 
 
3450
int check_hex(char *field, int length, char *text)
 
3451
{
 
3452
        int i;
 
3453
 
 
3454
        for(i=0; i < length; i++, field++) {
 
3455
                if (isxdigit(*field) == 0) {
 
3456
                        //printf("%s\n", text);
 
3457
                        error(text);
 
3458
                        return -1;
 
3459
                }       
 
3460
        }
 
3461
        return 1;
 
3462
}
 
3463
 
 
3464
 
 
3465
/* what format do we allow for packet files */
 
3466
/* YYY add to ignore the comments  */
 
3467
int check_if_file_is_packet(FILE *file_p)
 
3468
{
 
3469
        int c, i=0;     
 
3470
        gboolean first = 1;
 
3471
 
 
3472
        while ( (c = fgetc( file_p )) != EOF ) {
 
3473
                if (first ==1) {
 
3474
                        if (isspace(c) != 0) 
 
3475
                                continue;
 
3476
                        if (c == 35) {
 
3477
                                while ( getc(file_p) != 10);
 
3478
                                continue;
 
3479
                        }
 
3480
                }
 
3481
                if (isxdigit(c) == 0) {
 
3482
                        //printf("Error: File does not contain a valid packet!\n");
 
3483
                        error("Error: File does not contain a valid packet");
 
3484
                        return -1;
 
3485
                }
 
3486
 
 
3487
                if (first == 1)
 
3488
                        first = 0;
 
3489
                else
 
3490
                        first = 1;
 
3491
                i++;
 
3492
        }
 
3493
        
 
3494
        /* 1514 or 1518, how to enable the vlan checking */
 
3495
        if ( (i%2 != 0) || (i > 3536) ) {
 
3496
                //printf("Error: File length is not ok\n");
 
3497
                error("Error: File length is not ok");
 
3498
                return -1;
 
3499
        }
 
3500
 
 
3501
        return i;
 
3502
}
 
3503
 
 
3504
 
 
3505
void statusbar_text(GtkButton *button, char *text) {
 
3506
 
 
3507
        GtkWidget *statusbar;
 
3508
        gint context_id;
 
3509
        char buff[101];
 
3510
 
 
3511
        statusbar = lookup_widget(GTK_WIDGET (button), "statusbar1");
 
3512
        context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "Statusbar example");
 
3513
 
 
3514
        snprintf(buff, strlen(text)+1, text );
 
3515
        gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
 
3516
 
 
3517
}
 
3518
 
 
3519
void gen_crc32_table()
 
3520
{
 
3521
        unsigned long crc, poly;
 
3522
        int i, j;
 
3523
 
 
3524
        poly = 0xEDB88320L;
 
3525
        for (i = 0; i < 256; i++) {
 
3526
                crc = i;
 
3527
                for (j = 8; j > 0; j--) {
 
3528
                        if (crc & 1)
 
3529
                                crc = (crc >> 1) ^ poly;
 
3530
                        else
 
3531
                                crc >>= 1;
 
3532
                }
 
3533
                crc32_table[i] = crc;
 
3534
        }
 
3535
}
 
3536
 
 
3537
 
 
3538
unsigned long get_crc32(unsigned char *p, int len)
 
3539
{
 
3540
        register unsigned long crc;
 
3541
        int i;
 
3542
        if (!crc32_table_init) {
 
3543
                gen_crc32_table();
 
3544
                crc32_table_init = 1;
 
3545
        }
 
3546
        
 
3547
        crc = 0xFFFFFFFF;
 
3548
        for (i=0; i<len; i++) {
 
3549
                crc = (crc>>8) ^ crc32_table[ (crc ^ *p++) & 0xFF ];
 
3550
        }
 
3551
        crc = crc^0xFFFFFFFF;
 
3552
        /* big endian to little endian */
 
3553
        crc = ((crc >> 24) & 0x000000FF) ^
 
3554
              ((crc >>  8) & 0x0000FF00) ^
 
3555
              ((crc <<  8) & 0x00FF0000) ^
 
3556
              ((crc << 24) & 0xFF000000);
 
3557
        return crc;
 
3558
}
 
3559
 
 
3560
 
 
3561