~ubuntu-branches/ubuntu/wily/packeth/wily

« back to all changes in this revision

Viewing changes to src/loadpacket.c

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-03-04 21:32:00 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100304213200-dnrtvqlt9c5nont4
Tags: 1.6.5-1
* New upstream version
* debian/control:
  - updated my email address
  - DMUA removed
  - bumped Standards-Version to 3.8.4 (no changes needed)
  - bumped debhelper dependency to >= 7.0.50~, to use
    overridden rules
  - removed Build-Dependency on quilt
* debian/rules:
  - use dh7
  - rename /usr/bin/packETH to /usr/bin/packeth
* debian/watch updated
* debian/source/format: use 3.0 (quilt)
* debian/patches/:
  - fix_pixmaps.patch removed, fixed upstream
  - fix_warnings.patch updated, and renamed to 01-fix_warnings.patch
  - fix_Makefile.patch removed, fixed in debian/rules
  - 02-fix_typo.patch added
* debian/examples removed, no more available upstream
* debian/packeth.1 manually rewritten, without using docbook sources
* debian/copyright: point to the non-symlink license

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * packETH - ethernet packet generator
 
3
 * By Miha Jemec <jemcek@gmail.com>
 
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
 * 
 
21
 */
 
22
#include <gtk/gtk.h>
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <math.h>
 
27
#include "loadpacket.h"
 
28
#include <ctype.h>
 
29
#include "support.h"
 
30
#include "function.h"
 
31
#include "callbacks.h"
 
32
//#include "libnet/libnet-headers.h"
 
33
//#include "headers.h"
 
34
 
 
35
static GtkWidget *w1, *w2, *w3, *w4, *w5, *w6, *w7, *w8;
 
36
char field[3100];
 
37
char temp[20];
 
38
char temp6[40];
 
39
char *ptrf;
 
40
char *ptrt;
 
41
int next_prot;
 
42
long i;
 
43
int remain;
 
44
protocol_type protokol;
 
45
 
 
46
/* this one loads the parameters from file into notebook2 (Gen-b page) */
 
47
int load_gen_b_data(GtkButton *button, FILE *file_p) {
 
48
                                
 
49
        long int buff4[5];
 
50
        char buff[10];
 
51
        char buffc[11][200];
 
52
        int c, j, k;
 
53
 
 
54
        w1 = lookup_widget(GTK_WIDGET (button), "radiobutton35");
 
55
        w2 = lookup_widget(GTK_WIDGET (button), "radiobutton34");
 
56
        w3 = lookup_widget(GTK_WIDGET (button), "optionmenu9");
 
57
        w4 = lookup_widget(GTK_WIDGET (button), "entry109");
 
58
        w5 = lookup_widget(GTK_WIDGET (button), "entry110");
 
59
        w6 = lookup_widget(GTK_WIDGET(button), "checkbutton35");
 
60
        w7 = lookup_widget(GTK_WIDGET(button), "checkbutton37");
 
61
        /* we read the file ohh python, where are you... */
 
62
        k = 0;
 
63
        j = 0;
 
64
        /* rules for config files:
 
65
         * - comments start with #
 
66
         * - there can be spaces and newlines
 
67
         * - only digits and - are acceptable characters
 
68
         * - ...
 
69
         */
 
70
        /* we have to limit the lines we read paramters from */
 
71
        while ( (c = fgetc( file_p )) != EOF ) {
 
72
                /* all the comment lines, starting with # , no limit for comment lines*/
 
73
                if ( (j==0) && (c == 35)) {
 
74
                        /* ok, read till the end of line */
 
75
                        while ( getc(file_p) != 10);
 
76
                        continue;
 
77
                }
 
78
 
 
79
                /* let's limit the size */
 
80
                if ( (j > 9) || (k > 2) )
 
81
                        return -1;
 
82
 
 
83
                /* ok, it is not a comment line so the info: only digits and minus sign are acceptable */
 
84
                if ( (isdigit(c) != 0) || (c == 45) ) { 
 
85
                        buff[j] = c;
 
86
                        j++;
 
87
                        buff[j] = '\0';
 
88
                }
 
89
                /* no digit is it a newline? */
 
90
                else if (c==10) {
 
91
                        if (j==0)
 
92
                                continue;
 
93
                        if (strlen(buff) == 0)
 
94
                                *buff = 0;
 
95
                        buff4[k] = strtol(buff, (char **)NULL, 10);
 
96
                        j = 0;
 
97
                        strncpy(&buffc[k][j], buff, 9);
 
98
                        k++;
 
99
                }
 
100
                /* not, ok this is an error */
 
101
                else {
 
102
                        return -1;
 
103
                }
 
104
        }
 
105
 
 
106
        if (buff4[0] == 1)
 
107
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w2), 1);
 
108
        else if (buff4[0] == 0)
 
109
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w1), 1);
 
110
        else {
 
111
                return -1;
 
112
        }
 
113
 
 
114
        /* adjusting parameters...
 
115
        if ( (buff4[1] >= 0) && (buff4[1] <= 4) )
 
116
                gtk_option_menu_set_history (GTK_OPTION_MENU (w3), buff4[1]);
 
117
        else {
 
118
                return -1;
 
119
        } */
 
120
        
 
121
        if (buff4[1] == - 3) {
 
122
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w6), 1);
 
123
                gtk_entry_set_text(GTK_ENTRY(w4), "");
 
124
                gtk_widget_set_sensitive (w4, FALSE);
 
125
        }
 
126
        else if ( (buff4[1] > 0) && (buff4[1] <= 9999999) ) {
 
127
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w6), 0);
 
128
                gtk_widget_set_sensitive (w4, TRUE);
 
129
                gtk_entry_set_text(GTK_ENTRY(w4), &buffc[1][0]);
 
130
        }
 
131
        else {
 
132
                return -1;
 
133
        }
 
134
 
 
135
        if (buff4[2] == 0) {
 
136
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w7), 1);
 
137
                gtk_entry_set_text(GTK_ENTRY(w5), "");
 
138
                gtk_widget_set_sensitive (w5, FALSE);
 
139
        }
 
140
        else if ( (buff4[2] >= 1) && (buff4[2] <= 999999999)  ) {
 
141
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w7), 0);
 
142
                gtk_widget_set_sensitive (w5, TRUE);
 
143
                gtk_entry_set_text(GTK_ENTRY(w5), &buffc[2][0]);
 
144
        }
 
145
        else {
 
146
                return -1;
 
147
        }
 
148
        
 
149
        return 1;
 
150
}
 
151
 
 
152
 
 
153
 
 
154
/* this one loads the parameters from file into notebook2 (Gen-s page) */
 
155
int load_gen_s_data(GtkButton *button, FILE *file_p) {
 
156
 
 
157
        long int buff4[5];
 
158
        char buff[100];
 
159
        char buffc[11][200];
 
160
        int c, j, k;
 
161
        char *ptr = NULL, *ptr2 = NULL;
 
162
        long int cc;
 
163
 
 
164
 
 
165
        k = 0;
 
166
        j = 0;
 
167
        /* rules for config files:
 
168
         * - comments start with #
 
169
         * - there can be spaces and newlines
 
170
         * - only digits and - are acceptable characters
 
171
         * - ...
 
172
         */
 
173
        while ( (c = fgetc( file_p )) != EOF ) {
 
174
                /* all the comment lines, starting with # */
 
175
                if ( (j==0) && (c == 35)) {
 
176
                        while ( getc(file_p) != 10);
 
177
                        continue;
 
178
                }
 
179
                /* all blank lines */
 
180
                if ( (j==0) && (c == 10))
 
181
                        continue;
 
182
                /* read the whole lines */
 
183
                if ((isascii(c) != 0) && (j<200) && (c!=10) && (k<11)) {
 
184
                        buffc[k][j] = c;
 
185
                        j++;
 
186
                        buffc[k][j] = '\0';
 
187
                }
 
188
                /* , or \n mean end of string */
 
189
                else if (c==10) {
 
190
                        j=0;
 
191
                        k++;
 
192
                }
 
193
                else {
 
194
                        return -1;
 
195
                     }
 
196
        }
 
197
 
 
198
        w1 = lookup_widget(GTK_WIDGET (button), "radiobutton36");
 
199
        w2 = lookup_widget(GTK_WIDGET (button), "radiobutton37");
 
200
        w4 = lookup_widget(GTK_WIDGET (button), "entry151");
 
201
        w5 = lookup_widget(GTK_WIDGET (button), "entry152");
 
202
        w6 = lookup_widget(GTK_WIDGET(button), "checkbutton36");
 
203
 
 
204
        /* first line should have three parameters */
 
205
        /* first is absolute or relative delay, allowed values 0 and 1 */
 
206
        if (strncmp(&buffc[0][0], "1", 1) == 0)
 
207
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w1), 1);
 
208
        else if (strncmp(&buffc[0][0], "0", 1) == 0)
 
209
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w2), 1);
 
210
        else {
 
211
                return -1;
 
212
        }
 
213
 
 
214
        /* second is number of packets: -3 means infinite, or 1 till 9999999) */
 
215
        if (strncmp(&buffc[0][2], "-3", 2) == 0) {
 
216
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w6), 1);
 
217
                gtk_entry_set_text(GTK_ENTRY(w4), "");
 
218
                gtk_widget_set_sensitive (w4, FALSE);
 
219
                ptr = &buffc[0][4];
 
220
        }
 
221
        else {
 
222
                if ( (ptr = strchr(&buffc[0][2], 44)) == NULL) {
 
223
                        return -1;
 
224
                }
 
225
                *ptr = '\0';
 
226
                buff4[0] = strtol(&buffc[0][2], (char **)NULL, 10);
 
227
 
 
228
                if ( (buff4[0] >= 0) && (buff4[0] <= 9999999) ) {
 
229
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w6), 0);
 
230
                        gtk_widget_set_sensitive (w4, TRUE);
 
231
                        gtk_entry_set_text(GTK_ENTRY(w4), &buffc[0][2]);
 
232
                }
 
233
                else {
 
234
                        return -1;
 
235
                }
 
236
        }
 
237
 
 
238
        /* last parameter is delay between sequences */
 
239
        buff4[0] = strtol(ptr+1, (char **)NULL, 10);
 
240
 
 
241
        if ( (buff4[0] >= 0) && (buff4[0] <= 999999999) ) {
 
242
                gtk_entry_set_text(GTK_ENTRY(w5), ptr+1);
 
243
        }
 
244
        else {
 
245
                return -1;
 
246
        }
 
247
 
 
248
        /* we have to clean everything */
 
249
        for (j = 0; j < 10; j++) {
 
250
                snprintf(buff, 10, "entry%d", 111+j);
 
251
                w2 = lookup_widget(GTK_WIDGET (button), buff);
 
252
                gtk_entry_set_text(GTK_ENTRY(w2), "");
 
253
                snprintf(buff, 100, "entry%d", 121+j);
 
254
                w3 = lookup_widget(GTK_WIDGET (button), buff);
 
255
                gtk_entry_set_text(GTK_ENTRY(w3), "");
 
256
                snprintf(buff, 100, "entry%d", 131+j);
 
257
                w3 = lookup_widget(GTK_WIDGET (button), buff);
 
258
                gtk_entry_set_text(GTK_ENTRY(w3), "");
 
259
                snprintf(buff, 100, "entry%d", 141+j);
 
260
                w3 = lookup_widget(GTK_WIDGET (button), buff);
 
261
                gtk_entry_set_text(GTK_ENTRY(w3), "");
 
262
                snprintf(buff, 100, "checkbutton%d", 25+j);
 
263
                w2 = lookup_widget(GTK_WIDGET(button), buff);
 
264
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w2), 0);
 
265
        }
 
266
 
 
267
        /* and now all the rest */
 
268
        for (j = 1; j < k; j++) {
 
269
                /* first is packet name */
 
270
                if ( (ptr2 = strchr(&buffc[j][0], 44)) == NULL)
 
271
                        continue;
 
272
                *ptr2 = '\0';
 
273
                if ( (strlen(&buffc[j][0]) > 0 ) && (strlen(&buffc[j][0]) < 70) ) {
 
274
                        snprintf(buff, 10, "entry%d", 110+j);
 
275
                        w2 = lookup_widget(GTK_WIDGET (button), buff);
 
276
                        gtk_entry_set_text(GTK_ENTRY(w2), &buffc[j][0]);
 
277
                }
 
278
                else {
 
279
                        return -1;
 
280
                }
 
281
                /* number of packets */
 
282
                ptr = ptr2; ptr++;
 
283
                if ( (ptr2 = strchr(ptr, 44)) == NULL) {
 
284
                        return -1;
 
285
                }
 
286
                *ptr2 = '\0';
 
287
                cc = strtol(ptr, (char **)NULL, 10);
 
288
                if ( (cc < 0) || (cc > 9999999) ) {
 
289
                        return -1;
 
290
                }
 
291
                snprintf(buff, 100, "entry%d", 120+j);
 
292
                w3 = lookup_widget(GTK_WIDGET (button), buff);
 
293
                gtk_entry_set_text(GTK_ENTRY(w3), ptr);
 
294
 
 
295
                /* delay between packets */
 
296
                ptr = ptr2; ptr++;
 
297
                if ( (ptr2 = strchr(ptr, 44)) == NULL) {
 
298
                        return -1;
 
299
                }
 
300
                *ptr2 = '\0';
 
301
                cc = strtol(ptr, (char **)NULL, 10);
 
302
                if ( (cc < 0) || (cc > 999999999) ) {
 
303
                        return -1;
 
304
                }
 
305
                snprintf(buff, 100, "entry%d", 130+j);
 
306
                w3 = lookup_widget(GTK_WIDGET (button), buff);
 
307
                gtk_entry_set_text(GTK_ENTRY(w3), ptr);
 
308
 
 
309
                /* delay to next */
 
310
                ptr = ptr2; ptr++;
 
311
                if ( (ptr2 = strchr(ptr, 44)) == NULL) {
 
312
                        return -1;
 
313
                }
 
314
                *ptr2 = '\0';
 
315
                cc = strtol(ptr, (char **)NULL, 10);
 
316
                if ( (cc < 0) || (cc > 999999999) ) {
 
317
                        return -1;
 
318
                }
 
319
                snprintf(buff, 100, "entry%d", 140+j);
 
320
                w3 = lookup_widget(GTK_WIDGET (button), buff);
 
321
                gtk_entry_set_text(GTK_ENTRY(w3), ptr);
 
322
 
 
323
                /* enable or disable */
 
324
                ptr = ptr2; ptr++;
 
325
                snprintf(buff, 100, "checkbutton%d", 24+j);
 
326
                w2 = lookup_widget(GTK_WIDGET(button), buff);
 
327
                if (*ptr == '1')
 
328
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w2), 0);
 
329
                else if (*ptr == '0')
 
330
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w2), 1);
 
331
                else {
 
332
                      return -1;
 
333
                }
 
334
 
 
335
        }
 
336
 
 
337
        return 1;
 
338
 
 
339
}
 
340
 
 
341
/* opens the pcap file. 
 
342
 * if we call this function from the builder window only one - first, packet will be read 
 
343
 * from the Genp window, packets will be loaded untill EOF is reached */
 
344
int load_data(GtkButton *button, FILE *file_p, int whocalled, int howmanypackets) {
 
345
        
 
346
        struct pcap_hdr fh;
 
347
        struct pcaprec_hdr ph;
 
348
        struct clist_hdr clh;
 
349
        int j, ji, freads;
 
350
        guint32 secu = 0, secb = 0;
 
351
        guint32 usecu = 0, usecb = 0;
 
352
        double timediff = 0;
 
353
        double timebeg= 0;
 
354
        char pkt_temp[3100];
 
355
        GtkWidget *clis;
 
356
 
 
357
        /* first we read the pcap file header */
 
358
        freads = fread(pkt_temp, sizeof(fh), 1, file_p);
 
359
        /* if EOF, exit */
 
360
        if (freads == 0)
 
361
                return 1;
 
362
 
 
363
        memcpy(&fh, pkt_temp, 24);
 
364
 
 
365
        /* if magic number in NOK, exit */
 
366
        if (fh.magic != PCAP_MAGIC) 
 
367
                return -1;
 
368
 
 
369
        /* hm, I  forgot a little bit, but I assume 1 means builder here */
 
370
        if (whocalled == 1) {
 
371
                
 
372
                /* next the  pcap packet header */
 
373
                freads = fread(pkt_temp, sizeof(ph), 1, file_p);
 
374
                
 
375
                /* if EOF, exit */
 
376
                if (freads == 0)
 
377
                        return 1;
 
378
 
 
379
                /* copy the 16 bytes into ph structure */
 
380
                memcpy(&ph, pkt_temp, 16);
 
381
 
 
382
                /* and the packet itself, but only up to the capture length */
 
383
                freads = fread(pkt_temp+sizeof(ph), ph.incl_len, 1, file_p);
 
384
 
 
385
                /* if EOF, exit */
 
386
                if (freads == 0)
 
387
                        return 1;
 
388
 
 
389
                /* convert the packet information from int to hex */
 
390
                for(ji=0; ji<(sizeof(ph)+ph.incl_len); ji++)
 
391
                        c8(&field[2*ji], *(pkt_temp+ji)); 
 
392
                field[2*ji+2] = '\0';
 
393
                
 
394
                load_packet_disector(button, field, 1, &clh, ph.incl_len);                      
 
395
 
 
396
                return 1; 
 
397
        }
 
398
 
 
399
        else { 
 
400
        
 
401
                clis = lookup_widget(GTK_WIDGET (button), "clist2");
 
402
                gtk_clist_clear(GTK_CLIST(clis));
 
403
        
 
404
                for (j=0; j<howmanypackets; j++) {
 
405
        
 
406
                        /* next the  pcap packet header */
 
407
                        fread(pkt_temp, sizeof(ph), 1, file_p);
 
408
 
 
409
                        /* if EOF, exit */
 
410
                        if (freads == 0)
 
411
                                return 1;
 
412
 
 
413
                        /* copy the 16 bytes into ph structure */
 
414
                        memcpy(&ph, pkt_temp, 16);
 
415
 
 
416
                        /* and the packet itself, but only up to the capture length */
 
417
                        freads = fread(pkt_temp+sizeof(ph), ph.incl_len, 1, file_p);
 
418
 
 
419
                        /* if EOF, exit */
 
420
                        if (freads == 0)
 
421
                                return 1;
 
422
 
 
423
                        /* convert the packet information from int to hex */
 
424
                        for(ji=0; ji<(sizeof(ph)+ph.incl_len); ji++)
 
425
                                c8(&field[2*ji], *(pkt_temp+ji)); 
 
426
                        field[2*ji+2] = '\0';
 
427
                
 
428
                        /* we have to dissect the packet to get information for the list */
 
429
                        load_packet_disector(button, field, 2, &clh, ph.incl_len);                      
 
430
                        //printf("tukaj3 %s\n", field);
 
431
                        
 
432
                        /* calculate the time information */
 
433
                        if (j==0) { 
 
434
                                timediff = 0;
 
435
                                timebeg = 0;
 
436
                                secb = ph.ts_sec;
 
437
                                usecb = ph.ts_usec;
 
438
                        }
 
439
                        else { 
 
440
                                timediff = ((double)((double)ph.ts_sec - (double)secu)*1000000 + 
 
441
                                                                (double)((double)ph.ts_usec - (double)usecu)) / 1000000;
 
442
                                timebeg = ((double)((double)ph.ts_sec - (double)secb)*1000000 + 
 
443
                                                                (double)((double)ph.ts_usec - (double)usecb)) / 1000000;
 
444
                        }
 
445
 
 
446
                        //printf("tukaj2 %s\n", field);
 
447
 
 
448
                        secu = ph.ts_sec;
 
449
                        usecu = ph.ts_usec;
 
450
 
 
451
                        /* insert a new row into clist */
 
452
                        load_gen_p_data(button, clis, field, &ph, j+1, &clh, timediff, timebeg);
 
453
                }
 
454
                if (j == howmanypackets) 
 
455
                        error("Only first 1000 packets loaded!\nTo change this modify #define on top of callbacks.c");
 
456
        }
 
457
 
 
458
        return 1;
 
459
 
 
460
}
 
461
        
 
462
 
 
463
/* this one loads the parameters from file into notebook2 (Genp page) */
 
464
int load_gen_p_data(GtkButton *button, GtkWidget *clis, char *fieldek, struct pcaprec_hdr *ph2, 
 
465
                                        int pkt_nr, struct clist_hdr *clptr, double timediff, double timebeg) {
 
466
 
 
467
        gchar *datap[8];
 
468
        gchar fieldp[7][41];
 
469
        gchar field_p[1][3100];
 
470
 
 
471
        datap[0]=&fieldp[0][0];
 
472
        datap[1]=&fieldp[1][0];
 
473
        datap[2]=&fieldp[2][0];
 
474
        datap[3]=&fieldp[3][0];
 
475
        datap[4]=&fieldp[4][0];
 
476
        datap[5]=&fieldp[5][0];
 
477
        datap[6]=&fieldp[6][0];
 
478
        datap[7]=&field_p[0][0];
 
479
 
 
480
        //printf("TUKAJ1:%s\n", fieldek);
 
481
        g_snprintf(fieldp[0], 20, "%d", pkt_nr);
 
482
        g_snprintf(fieldp[1], 20, "%f", timebeg);
 
483
        g_snprintf(fieldp[2], 20, "%f", timediff);
 
484
 
 
485
        if ( (*ph2).incl_len == (*ph2).orig_len )
 
486
                g_snprintf(fieldp[3], 20, "%d", (*ph2).incl_len);
 
487
        else
 
488
                g_snprintf(fieldp[3], 20, "%d !", (*ph2).incl_len);
 
489
 
 
490
        g_snprintf(fieldp[4], 40, "%s", clptr->src);
 
491
        g_snprintf(fieldp[5], 40, "%s", clptr->dst);
 
492
 
 
493
        switch (protokol) {
 
494
                case ETH_II: {
 
495
                        g_snprintf(fieldp[6], 20, "Ethernet II");
 
496
                        break;
 
497
                }
 
498
                case ETH_802_3: {
 
499
                        g_snprintf(fieldp[6], 20, "Ethernet 802.3");
 
500
                        break;
 
501
                }
 
502
                case ARP: {
 
503
                        g_snprintf(fieldp[6], 20, "ARP");
 
504
                        break;
 
505
                }
 
506
                case IPv4: {
 
507
                        g_snprintf(fieldp[6], 20, "IPv4");
 
508
                        break;
 
509
                }
 
510
                case IPv6: {
 
511
                        g_snprintf(fieldp[6], 20, "IPv6");
 
512
                        break;
 
513
                }
 
514
                case TCP: {
 
515
                        g_snprintf(fieldp[6], 20, "TCP");
 
516
                        break;
 
517
                }
 
518
                case UDP: {
 
519
                        g_snprintf(fieldp[6], 20, "UDP");
 
520
                        break;
 
521
                }
 
522
                case IGMP: {
 
523
                        g_snprintf(fieldp[6], 20, "IGMP");
 
524
                        break;
 
525
                }
 
526
                case ICMP: {
 
527
                        g_snprintf(fieldp[6], 20, "ICMP");
 
528
                        break;
 
529
                }
 
530
        }
 
531
 
 
532
        g_snprintf(field_p[0], 2*(32+(*ph2).incl_len), "%s", fieldek);
 
533
                
 
534
        gtk_clist_append(GTK_CLIST(clis), datap);
 
535
        
 
536
        return 1;
 
537
 
 
538
}
 
539
 
 
540
/* this routine was changed. it loads the packet into notebook2 (Builder page) or it checks the file containing the packets
 
541
 * for loading into Genp window
 
542
 * if who called = 1 - we load the contents into builder field
 
543
 * if who called = 2 - we load the contenst into Genp window but we need the information for filling in the clist 
 
544
 */
 
545
int load_packet_disector(GtkButton *button, char *fieldek, int whocalled, struct clist_hdr *clptr, int dolpaketa) {
 
546
 
 
547
        int c;
 
548
 
 
549
        ptrf = fieldek;
 
550
        ptrt = temp;
 
551
 
 
552
        //printf("\n:\n%s\n", fieldek);
 
553
        //convert8field(ptrt, ptrf);    insint(button, "entry179", ptrt, 8); ptrt = temp; ptrf = ptrf-8;
 
554
        //convert8field(ptrt, ptrf+8);  insint(button, "entry180", ptrt, 8); ptrt = temp; ptrf = ptrf-8;
 
555
        //convert8field(ptrt, ptrf+16); insint(button, "entry181", ptrt, 8); ptrt = temp; ptrf = ptrf-8;
 
556
        //convert8field(ptrt, ptrf+24); insint(button, "entry182", ptrt, 8); ptrt = temp; ptrf = ptrf-8;
 
557
        remain = dolpaketa;
 
558
        ptrf = ptrf + 32;
 
559
 
 
560
        /* what is the shortest length we still allow?
 
561
         * we don't care if the packet is shorter than actually allowed to go on ethernet
 
562
         * maybe the user just wanted to save packet even if it is to short, so why not load it?
 
563
         * what we do demand is, that every layer must be completed 
 
564
         * ok, here at least 14 bytes: 6 dest mac, 6 source mac and 2 for type or length*/
 
565
        if (remain < 14) {
 
566
                error("Can't load packet: Ethernet header is not long enough!");
 
567
                return -1;
 
568
        }
 
569
 
 
570
        /* first there is destination mac */
 
571
        w1 = lookup_widget(GTK_WIDGET(button), "L_dst_mac");
 
572
        for (i=1; i<=18; i++, ptrt++) {
 
573
                if (i%3 == 0) 
 
574
                        *ptrt = ':';
 
575
                else {
 
576
                        *ptrt = *ptrf++;
 
577
                }
 
578
        }
 
579
        *(ptrt-1) = '\0';
 
580
 
 
581
        if (whocalled == 1)
 
582
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
583
        else 
 
584
                memcpy(clptr->dst, temp, 20);
 
585
 
 
586
        /* and source mac */
 
587
        ptrt = temp;
 
588
        w2 = lookup_widget(GTK_WIDGET(button), "L_src_mac");
 
589
        for (i=1; i<=18; i++, ptrt++) {
 
590
                if (i%3 == 0) 
 
591
                        *ptrt = ':';
 
592
                else {
 
593
                        *ptrt = *ptrf++;
 
594
                }
 
595
        }
 
596
        *(ptrt-1) = '\0';
 
597
 
 
598
        if (whocalled == 1)
 
599
                gtk_entry_set_text(GTK_ENTRY(w2), temp);
 
600
        else 
 
601
                memcpy(clptr->src, temp, 20);
 
602
 
 
603
        /* next there is type or length field or 802.1q or QinQ! */
 
604
        i = char2x(ptrf)*256 + char2x(ptrf+2);
 
605
 
 
606
        remain = remain - 14;
 
607
 
 
608
        /* in case of a vlan tag 0x8100 == 33024) */
 
609
        w1 = lookup_widget(GTK_WIDGET(button), "bt_8021q");
 
610
        w2 = lookup_widget(GTK_WIDGET(button), "frame6");
 
611
 
 
612
        if ((i == 33024) || (i==34984) || (i==37120) || (i==37376)) {
 
613
                w3 = lookup_widget(GTK_WIDGET(button), "L_optmenu2_bt");
 
614
                w4 = lookup_widget(GTK_WIDGET(button), "checkbutton39");
 
615
                w5 = lookup_widget(GTK_WIDGET(button), "checkbutton40");
 
616
                w6 = lookup_widget(GTK_WIDGET(button), "L_vlan_id");
 
617
                w7 = lookup_widget(GTK_WIDGET(button), "entry165");
 
618
                w8 = lookup_widget(GTK_WIDGET(button), "optionmenu21");
 
619
                
 
620
                //if we have 8100 after the next 4 bytes we do QinQ     
 
621
                if ( ((char2x(ptrf+8)*256 + char2x(ptrf+10))==33024) && remain>=8) 
 
622
                {
 
623
                        ptrf = ptrf + 4;
 
624
                        
 
625
                        if (whocalled == 1) {
 
626
                                gtk_widget_set_sensitive (w7, TRUE);
 
627
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w5), TRUE);
 
628
                                if (i==33024)
 
629
                                        gtk_option_menu_set_history (GTK_OPTION_MENU (w8), 0);
 
630
                                else if (i==34984)
 
631
                                        gtk_option_menu_set_history (GTK_OPTION_MENU (w8), 3);
 
632
                                else if (i==37120)
 
633
                                        gtk_option_menu_set_history (GTK_OPTION_MENU (w8), 1);
 
634
                                else if (i==37376)
 
635
                                        gtk_option_menu_set_history (GTK_OPTION_MENU (w8), 2);
 
636
 
 
637
                                inspar(button, "entry165", ptrf, 4);
 
638
                        }
 
639
                        else
 
640
                                ptrf = ptrf +3;
 
641
 
 
642
                        i = char2x(ptrf)*256 + char2x(ptrf+2);
 
643
                }       
 
644
                else {
 
645
                        if (whocalled == 1) {
 
646
                                gtk_widget_set_sensitive (w7, FALSE);
 
647
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w5), FALSE);
 
648
                        }       
 
649
 
 
650
                }
 
651
 
 
652
                if (remain < 4) {
 
653
                        error("Can't load packet: Ethernet VLAN field is not long enough!");
 
654
                        return -1;
 
655
                }
 
656
                remain = remain -4;
 
657
 
 
658
                ptrf = ptrf + 4;
 
659
                *ptrt++ = '0';
 
660
                *ptrt-- = *ptrf++;
 
661
                i = char2x(ptrt);
 
662
 
 
663
                if (whocalled == 1) {
 
664
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
665
                        gtk_widget_set_sensitive (w2, TRUE);
 
666
                
 
667
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w3), (i>>1));
 
668
 
 
669
                        if ( (i%2) == 0)
 
670
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w4), FALSE);
 
671
                        else
 
672
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w4), TRUE);
 
673
 
 
674
                        inspar(button, "L_vlan_id", ptrf, 3);
 
675
                }
 
676
                else
 
677
                        ptrf = ptrf+3;
 
678
 
 
679
                i = char2x(ptrf)*256 + char2x(ptrf+2);
 
680
 
 
681
        }
 
682
        else {
 
683
                if (whocalled == 1) {
 
684
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
685
 
 
686
                        gtk_widget_set_sensitive (w2, FALSE);
 
687
                }       
 
688
        }
 
689
        
 
690
        /* c will tell us which ethernet type we have */
 
691
        c = i;
 
692
 
 
693
        /* ok, from now one, we split the dissection in different routines, depending on what values */
 
694
        /* now if length is <= 1500, we have 802.3 ethernet and this value means length of ethernet packet */
 
695
        if (i <= 1500) 
 
696
                next_prot = ethernet_8023(button, whocalled);
 
697
        /* Values between 1500 and 1536 are forbidden */
 
698
        else if ( (i>1500) && (i<1536) ) {
 
699
                error("Can't load packet: Wrong ethernet length/type field");
 
700
                return -1;
 
701
        }
 
702
        /* if i >= 1536 - ethernet ver II */
 
703
        else
 
704
                next_prot = ethernet_verII(button, whocalled);
 
705
 
 
706
 
 
707
        /* ok, so we have dissected the ethernet layer and now move on two the next layer.
 
708
         * if the ethernet dissector returns -1, this means an error and we quit
 
709
         * otherwise, the return value can be 2048 == 0x0800 and this means the ipv4
 
710
         * so we try to dissect ipv4 header. in case it is ok, we activate the ippkt_radibt
 
711
         * and this one then calls the callback which fills in ethernet ver II type field
 
712
         * and PID field in 802.3 LLC/SNAP field. It is the same for arp packets
 
713
         * for other packets we will try to open the userdefined window */
 
714
 
 
715
        /* we got an error? */
 
716
        if (next_prot == -1) 
 
717
                return -1;
 
718
 
 
719
        /* ipv4 */
 
720
        else if (next_prot == 2048) {
 
721
                /* ok, ipv4 should follow, so we call the routine for parsing ipv4 header. */
 
722
                next_prot = ipv4_header(button, whocalled, clptr);
 
723
                if (next_prot == -1)
 
724
                        return -1;
 
725
 
 
726
                /* if the return value from parsing ipv4 header was != 0, then the header parameters 
 
727
                 * are ok and we can open ipv4 notebook page activate toggle button (button calls 
 
728
                 * the callback then!!! */
 
729
                w1 = lookup_widget(GTK_WIDGET(button), "ippkt_radibt");
 
730
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
731
 
 
732
                /* here we do the further parsing: tcp, udp, icmp, ...*/
 
733
                if (next_prot == 1) {
 
734
                        /* try to parse icmp header */
 
735
                        next_prot = icmp_header(button, whocalled);
 
736
                        /* not ok, return an error */
 
737
                        if (next_prot == -1)
 
738
                                return -1;
 
739
                        /* ok, lets activate the icmp notebook */
 
740
                        else {
 
741
                                w1 = lookup_widget(GTK_WIDGET(button), "icmp_bt");
 
742
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
743
                        }
 
744
                }
 
745
                else if (next_prot == 2) {
 
746
                        /* try to parse igmp header */
 
747
                        next_prot = igmp_header(button, whocalled);
 
748
                        /* not ok, return an error */
 
749
                        if (next_prot == -1)
 
750
                                return -1;
 
751
                        /* ok, lets activate the igmp notebook */
 
752
                        else {
 
753
                                w1 = lookup_widget(GTK_WIDGET(button), "igmp_bt");
 
754
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
755
                        }
 
756
                }
 
757
                else if (next_prot == 6) {
 
758
                        /* try to parse tcp header */
 
759
                        next_prot = tcp_header(button, whocalled);
 
760
                        /* not ok, return an error */
 
761
                        if (next_prot == -1)
 
762
                                return -1;
 
763
                        /* ok, lets activate the tcp notebook */
 
764
                        else {
 
765
                                w1 = lookup_widget(GTK_WIDGET(button), "tcp_bt");
 
766
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
767
                        }
 
768
 
 
769
                        /* protocols on top of tcp would follow here */
 
770
 
 
771
                }       
 
772
                else if (next_prot == 17) {
 
773
                        /* try to parse udp header */
 
774
                        next_prot = udp_header(button, whocalled);
 
775
                        /* not ok, return an error */
 
776
                        if (next_prot == -1)
 
777
                                return -1;
 
778
                        /* ok, lets activate the udp notebook */
 
779
                        else {
 
780
                                w1 = lookup_widget(GTK_WIDGET(button), "udp_bt");
 
781
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
782
                        }
 
783
 
 
784
                        /* protocols on top of udp would follow here */
 
785
                }       
 
786
                /* protocol we do not support yet; user defined window */
 
787
                else {
 
788
                        next_prot = usedef_insert(button, "text2", whocalled);
 
789
                        w1 = lookup_widget(GTK_WIDGET(button), "ip_user_data_bt");
 
790
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
791
                }
 
792
        }
 
793
        /*arp */
 
794
        else if (next_prot == 2054) {
 
795
                /* ok, arp header follows */
 
796
                next_prot = arp_header(button, whocalled);
 
797
                if (next_prot == -1)
 
798
                        return -1;
 
799
 
 
800
                w1 = lookup_widget(GTK_WIDGET(button), "arppkt_radiobt");
 
801
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
802
        }
 
803
 
 
804
        /* when ipv6 will be added, activate ipv6 button instead of userdef button */
 
805
        else if (next_prot == 34525) {
 
806
 
 
807
                /* ok, ipv6 should follow, so we call the routine for parsing ipv6 header. */
 
808
                next_prot = ipv6_header(button, whocalled, clptr);
 
809
                if (next_prot == -1)
 
810
                        return -1;
 
811
 
 
812
                w1 = lookup_widget(GTK_WIDGET(button), "IPv6_rdbt");
 
813
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
814
 
 
815
                /* here we do the further parsing: tcp, udp, icmp, ...*/
 
816
                if (next_prot == 1) {
 
817
                        /* try to parse icmpv6 header */
 
818
                        next_prot = icmpv6_header(button, whocalled);
 
819
                        /* not ok, return an error */
 
820
                        if (next_prot == -1)
 
821
                                return -1;
 
822
                        /* ok, lets activate the icmpv6 notebook */
 
823
                        else {
 
824
                                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton69");
 
825
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
826
                        }
 
827
                }
 
828
                else if (next_prot == 6) {
 
829
                        /* try to parse tcp header */
 
830
                        next_prot = tcp_header(button, whocalled);
 
831
                        /* not ok, return an error */
 
832
                        if (next_prot == -1)
 
833
                                return -1;
 
834
                        /* ok, lets activate the tcp notebook */
 
835
                        else {
 
836
                                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton68");
 
837
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
838
                        }
 
839
                }
 
840
                else if (next_prot == 17) {
 
841
                        /* try to parse udp header */
 
842
                        next_prot = udp_header(button, whocalled);
 
843
                        /* not ok, return an error */
 
844
                        if (next_prot == -1)
 
845
                                return -1;
 
846
                        /* ok, lets activate the udp notebook */
 
847
                        else {
 
848
                                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton67");
 
849
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
850
                        }
 
851
                }
 
852
                /* protocol we do not support yet; user defined window */
 
853
                else {
 
854
                        next_prot = usedef_insert(button, "text2", whocalled);
 
855
                        w1 = lookup_widget(GTK_WIDGET(button), "radiobutton71");
 
856
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
857
                }
 
858
 
 
859
        }
 
860
        /* anything else - user defined */
 
861
        else {
 
862
                /* setting "usedef2_radibt" toggle button to true will call the callback which will clear 
 
863
                eth II type field and 802.3 pid field, so we have to fill this later */
 
864
                w1 = lookup_widget(GTK_WIDGET(button), "usedef2_radibt");
 
865
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
866
                
 
867
                /* we still have c to distinguish between ver II and 802.3 */
 
868
                /* ver II */
 
869
                if (c >= 1536) {
 
870
                        ptrf = ptrf - 4;
 
871
                        inspar(button, "L_ethtype", ptrf, 4);
 
872
                }
 
873
                /* 802.3 and with LLC SNAP */
 
874
                else if (next_prot != -2) {
 
875
                        ptrf = ptrf - 4;
 
876
                        inspar(button, "L_pid", ptrf, 4);
 
877
                }               
 
878
 
 
879
                next_prot = usedef_insert(button, "text1", whocalled);
 
880
        }
 
881
 
 
882
        return 1;
 
883
}
 
884
 
 
885
 
 
886
int arp_header(GtkButton *button, int whocalled) {
 
887
 
 
888
        char tmp[5];
 
889
        int x;
 
890
 
 
891
        if (whocalled==2) {
 
892
                protokol = ARP;
 
893
                return 1;
 
894
        }
 
895
 
 
896
        /* arp header length == 28; but packet can be longer, f.e. to satisfy the min packet length */
 
897
        if (remain < 28) {
 
898
                error("Can't load packet: Packet length shorter than ARP header length!");
 
899
                return -1;
 
900
        }
 
901
 
 
902
        remain = remain - 28;
 
903
 
 
904
        /* hardware type */
 
905
        inspar(button, "A_hwtype", ptrf, 4);
 
906
 
 
907
        /* protocol type */
 
908
        inspar(button, "A_prottype", ptrf, 4);
 
909
 
 
910
        /* hardware size */
 
911
        inspar(button, "A_hwsize", ptrf, 2);
 
912
 
 
913
        /* protocol size */
 
914
        inspar(button, "A_protsize", ptrf, 2);
 
915
 
 
916
        /* opcode is next */
 
917
        if ( (*ptrf == '0') && (*(ptrf+1) == '0') && (*(ptrf+2) == '0') && (*(ptrf+3) == '1') ) {
 
918
                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton10");
 
919
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
920
                ptrf = ptrf + 4;
 
921
        }
 
922
        else if ( (*ptrf == '0') && (*(ptrf+1) == '0') && (*(ptrf+2) == '0') && (*(ptrf+3) == '2') ) {
 
923
                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton11");
 
924
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
925
                ptrf = ptrf + 4;
 
926
        }
 
927
        else {
 
928
                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton17");
 
929
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
930
                inspar(button, "entry81", ptrf, 4);
 
931
        }
 
932
 
 
933
        /* sender mac */
 
934
        ptrt = temp;
 
935
        w1 = lookup_widget(GTK_WIDGET(button), "A_sendermac");
 
936
        for (i=1; i<=18; i++, ptrt++) {
 
937
                if (i%3 == 0) 
 
938
                        *ptrt = ':';
 
939
                else {
 
940
                        *ptrt = *ptrf++;
 
941
                }
 
942
        }
 
943
        *ptrt = '\0';
 
944
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
945
 
 
946
        /* sender ip */
 
947
        ptrt = temp;
 
948
        memset(temp, 0, 20);
 
949
        w1 = lookup_widget(GTK_WIDGET(button), "A_senderip");
 
950
        for (i=1; i<=12; i++, ptrt++) {
 
951
                if (i%3 == 0) {                 
 
952
                        x = char2x(tmp);
 
953
                        if (i==12)
 
954
                                snprintf(tmp, 4, "%d", x);              
 
955
                        else
 
956
                                snprintf(tmp, 5, "%d.", x);             
 
957
                        strcat(temp, tmp);
 
958
                }
 
959
                else {
 
960
                        tmp[(i-1)%3] = *ptrf++;
 
961
                }
 
962
        }
 
963
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
964
 
 
965
        /* target mac */
 
966
        ptrt = temp;
 
967
        w1 = lookup_widget(GTK_WIDGET(button), "A_targetmac");
 
968
        for (i=1; i<=18; i++, ptrt++) {
 
969
                if (i%3 == 0) 
 
970
                        *ptrt = ':';
 
971
                else {
 
972
                        *ptrt = *ptrf++;
 
973
                }
 
974
        }
 
975
        *ptrt = '\0';
 
976
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
977
 
 
978
        /* target ip */
 
979
        ptrt = temp;
 
980
        memset(temp, 0, 20);
 
981
        w1 = lookup_widget(GTK_WIDGET(button), "A_targetip");
 
982
        for (i=1; i<=12; i++, ptrt++) {
 
983
                if (i%3 == 0) {                 
 
984
                        x = char2x(tmp);
 
985
                        if (i==12)
 
986
                                snprintf(tmp, 4, "%d", x);              
 
987
                        else
 
988
                                snprintf(tmp, 5, "%d.", x);             
 
989
                        strcat(temp, tmp);
 
990
                }
 
991
                else {
 
992
                        tmp[(i-1)%3] = *ptrf++;
 
993
                }
 
994
        }
 
995
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
996
 
 
997
        return 1;
 
998
 
 
999
}
 
1000
 
 
1001
 
 
1002
int igmp_header(GtkButton *button, int whocalled) {
 
1003
 
 
1004
        int x, x1;
 
1005
        char tmp[5];
 
1006
 
 
1007
        if (whocalled==2) {
 
1008
                protokol = IGMP;
 
1009
                return 1;
 
1010
        }
 
1011
 
 
1012
        /* well normal igmp type should have at least 8 bytes, so this is min for us */
 
1013
        if (remain < 8) {
 
1014
                error("Can't load packet: Packet length shorter than IGMP header length!");
 
1015
                return -1;
 
1016
        }
 
1017
 
 
1018
        remain = remain -8;
 
1019
 
 
1020
        /* igmp type */
 
1021
        x = char2x(ptrf);
 
1022
        /* insert version */
 
1023
        inspar(button, "entry166", ptrf, 2);
 
1024
 
 
1025
        w1 = lookup_widget(GTK_WIDGET(button), "optionmenu20");
 
1026
        w2 = lookup_widget(GTK_WIDGET(button), "notebook8");
 
1027
        if (x == 17) {
 
1028
                if (remain > 4) {
 
1029
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 1);
 
1030
                        gtk_notebook_set_page(GTK_NOTEBOOK(w2), 1);
 
1031
                        }
 
1032
                else    {
 
1033
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 0);
 
1034
                        gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1035
                        }
 
1036
        }
 
1037
        else if (x == 18) {
 
1038
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 2);
 
1039
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1040
                }
 
1041
        else if (x == 22) {
 
1042
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 3);
 
1043
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1044
                }
 
1045
        else if (x == 34) {
 
1046
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 4);
 
1047
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 2);
 
1048
                }
 
1049
        else if (x == 23) {
 
1050
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 5);
 
1051
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1052
                }
 
1053
        else    {
 
1054
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 6);
 
1055
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1056
                }
 
1057
 
 
1058
        inspar(button, "entry167", ptrf, 2);
 
1059
 
 
1060
        /* set checksum button on auto */
 
1061
        w2 = lookup_widget(GTK_WIDGET(button), "checkbutton41");
 
1062
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1063
        ptrf = ptrf + 4;
 
1064
 
 
1065
 
 
1066
        if ( (x == 17) && (remain>4) ) { /* IGMP V3 query */
 
1067
                /*insert group ip */
 
1068
                ptrt = temp;
 
1069
                memset(temp, 0, 20);
 
1070
                w1 = lookup_widget(GTK_WIDGET(button), "entry169");
 
1071
                for (i=1; i<=12; i++, ptrt++) {
 
1072
                        if (i%3 == 0) {                 
 
1073
                                x = char2x(tmp);
 
1074
                                if (i==12)
 
1075
                                        snprintf(tmp, 4, "%d", x);              
 
1076
                                else
 
1077
                                        snprintf(tmp, 5, "%d.", x);             
 
1078
                                strcat(temp, tmp);
 
1079
                        }
 
1080
                        else {
 
1081
                                tmp[(i-1)%3] = *ptrf++;
 
1082
                        }
 
1083
                }
 
1084
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1085
 
 
1086
                inspar(button, "entry171", ptrf, 4);
 
1087
                x1 = (int)retint2(ptrf, 4);
 
1088
                inspar(button, "entry172", ptrf, 4);
 
1089
                /*#inspar(button, "entry173", ptrf, x1);*/
 
1090
                inspar(button, "entry173", ptrf, remain);
 
1091
                
 
1092
        }
 
1093
        else if (x==22) { /*IGMP V3 report */
 
1094
                inspar(button, "entry176", ptrf, 4);
 
1095
                x1 = (int)retint2(ptrf, 4);
 
1096
                inspar(button, "entry177", ptrf, 4);
 
1097
                inspar(button, "entry178", ptrf, x1);
 
1098
                
 
1099
        }
 
1100
        else { /*all the other versions */
 
1101
                /*insert group ip */
 
1102
                ptrt = temp;
 
1103
                memset(temp, 0, 20);
 
1104
                w1 = lookup_widget(GTK_WIDGET(button), "entry175");
 
1105
                for (i=1; i<=12; i++, ptrt++) {
 
1106
                        if (i%3 == 0) {                 
 
1107
                                x = char2x(tmp);
 
1108
                                if (i==12)
 
1109
                                        snprintf(tmp, 4, "%d", x);              
 
1110
                                else
 
1111
                                        snprintf(tmp, 5, "%d.", x);             
 
1112
                                strcat(temp, tmp);
 
1113
                        }
 
1114
                        else {
 
1115
                                tmp[(i-1)%3] = *ptrf++;
 
1116
                        }
 
1117
                }
 
1118
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1119
                        
 
1120
        
 
1121
        }       
 
1122
 
 
1123
        return 1;
 
1124
}
 
1125
 
 
1126
int icmp_header(GtkButton *button, int whocalled) {
 
1127
 
 
1128
        int x;
 
1129
 
 
1130
        if (whocalled==2) {
 
1131
                protokol = ICMP;
 
1132
                return 1;
 
1133
        }
 
1134
 
 
1135
        /* well normal icmp type should have at least 8 bytes, so this is min for us */
 
1136
        if (remain < 8) {
 
1137
                error("Can't load packet: Packet length shorter than ICMP header length!");
 
1138
                return -1;
 
1139
        }
 
1140
 
 
1141
        remain = remain -8;
 
1142
 
 
1143
        /* icmp type */
 
1144
        x = char2x(ptrf);
 
1145
        /* insert version */
 
1146
        inspar(button, "entry57", ptrf, 2);
 
1147
 
 
1148
        w1 = lookup_widget(GTK_WIDGET(button), "optionmenu4");
 
1149
        if (x == 0)
 
1150
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 0);
 
1151
        else if (x == 3) 
 
1152
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 1);
 
1153
        else if (x == 8) 
 
1154
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 2);
 
1155
        else
 
1156
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 3);
 
1157
 
 
1158
 
 
1159
        if (x == 0) { /* echo reply */
 
1160
                /* insert code, checksum, identifier and seq number and data if there is some */
 
1161
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1162
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 0);
 
1163
                inspar(button, "entry62", ptrf, 2);
 
1164
                //inspar(button, "entry63", ptrf, 4);
 
1165
                ptrf = ptrf + 4;
 
1166
                inspar(button, "entry64", ptrf, 4);
 
1167
                inspar(button, "entry65", ptrf, 4);
 
1168
                /* set checksum button on auto */
 
1169
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton16");
 
1170
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1171
 
 
1172
                inspar(button, "entry66", ptrf, remain * 2);
 
1173
                if (remain > 0) {
 
1174
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton17");
 
1175
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1176
                }
 
1177
                else {
 
1178
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton17");
 
1179
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1180
                }
 
1181
                
 
1182
        }
 
1183
        else if (x == 3) { /* destination unreacheable */
 
1184
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1185
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 2);
 
1186
                /* which code? */
 
1187
                x = char2x(ptrf);
 
1188
                /* insert code */
 
1189
                inspar(button, "entry58", ptrf, 2);
 
1190
 
 
1191
                w1 = lookup_widget(GTK_WIDGET(button), "optionmenu5");
 
1192
                if ( (x >= 0) && (x <= 15) )
 
1193
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), x);
 
1194
                else
 
1195
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 16);
 
1196
                
 
1197
                /* insert code, checksum, identifier and seq number and data if there is some */
 
1198
                //inspar(button, "entry59", ptrf, 4);
 
1199
                ptrf = ptrf + 4;
 
1200
                inspar(button, "entry60", ptrf, 8);
 
1201
                /* set checksum button on auto */
 
1202
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton15");
 
1203
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1204
 
 
1205
                inspar(button, "entry61", ptrf, remain * 2);
 
1206
                if (remain > 0) {
 
1207
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton24");
 
1208
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1209
                }
 
1210
                else {
 
1211
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton24");
 
1212
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1213
                }
 
1214
        }
 
1215
        else if (x == 8) { /* echo request */
 
1216
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1217
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 5);
 
1218
                /* insert code, checksum, identifier and seq number and data if there is some */
 
1219
                inspar(button, "entry74", ptrf, 2);
 
1220
                //inspar(button, "entry77", ptrf, 4);
 
1221
                ptrf = ptrf + 4;
 
1222
                inspar(button, "entry75", ptrf, 4);
 
1223
                inspar(button, "entry78", ptrf, 4);
 
1224
                /* set checksum button on auto */
 
1225
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton20");
 
1226
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1227
 
 
1228
                inspar(button, "entry76", ptrf, remain * 2);
 
1229
                if (remain > 0) {
 
1230
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton19");
 
1231
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1232
                }
 
1233
                else {
 
1234
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton19");
 
1235
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1236
                }
 
1237
 
 
1238
        }
 
1239
        else { /* all the rest */
 
1240
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1241
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 1);
 
1242
                /* insert code, checksum and data if there is some */
 
1243
                inspar(button, "entry157", ptrf, 2);
 
1244
                //inspar(button, "entry158", ptrf, 4);
 
1245
                ptrf = ptrf + 4;
 
1246
                /* set checksum button on auto */
 
1247
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton38");
 
1248
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1249
 
 
1250
                inspar(button, "entry159", ptrf, (remain + 4) * 2);
 
1251
 
 
1252
        }
 
1253
 
 
1254
        return 1;
 
1255
}
 
1256
 
 
1257
 
 
1258
int usedef_insert(GtkButton *button, char *entry, int whocalled) {
 
1259
 
 
1260
        int i, j;
 
1261
        char tmp[4600];
 
1262
 
 
1263
        if (whocalled == 1)
 
1264
                return 1;
 
1265
 
 
1266
        /* get access to buffer of the text field */
 
1267
        w2 = lookup_widget(GTK_WIDGET(button), entry);
 
1268
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w2));
 
1269
 
 
1270
        /* copy data to tmp field */
 
1271
        for (i=0, j=1; (i < (remain * 3) ); i++, j++) {
 
1272
                tmp[i] = *ptrf++; i++;
 
1273
                tmp[i] = *ptrf++; i++;
 
1274
                /* we allow only 16 bytes in each row - looks nicer */
 
1275
                if ((j % 16) == 0 && (j > 1)) {
 
1276
                        tmp[i]='\n';
 
1277
                        j = 0;
 
1278
                }
 
1279
                else
 
1280
                        tmp[i] = ' ';
 
1281
        }
 
1282
        tmp[i] = '\0';
 
1283
 
 
1284
        /* insert the text in the text field */
 
1285
        gtk_text_buffer_set_text(buffer,tmp,-1);
 
1286
 
 
1287
        return 1;
 
1288
 
 
1289
 
 
1290
}
 
1291
 
 
1292
 
 
1293
int tcp_header(GtkButton *button, int whocalled) {
 
1294
 
 
1295
        int x, i, j;
 
1296
        char tmp[4600], tmp2[3], ch;
 
1297
 
 
1298
        if (whocalled==2) {
 
1299
                protokol = TCP;
 
1300
                return 1;
 
1301
        }
 
1302
 
 
1303
 
 
1304
        /* for standard header this is minimum length */
 
1305
        if (remain < 20) {
 
1306
                error("Can't load packet: Packet length shorter than TCP header length!");
 
1307
                return -1;
 
1308
        }
 
1309
 
 
1310
        /* ok, packet is long enough to fill in the standard header, but what is the header length?
 
1311
         * we insert this later but need now to see that the packet is long enough */
 
1312
        x = retint(ptrf+24);
 
1313
        if ( (x * 4) > remain ) {
 
1314
                error("Can't load packet:\nPacket length shorter than TCP header length!");
 
1315
                return -1;
 
1316
        }
 
1317
        if ( x < 5 ) {
 
1318
                error("Can't load packet:\nTCP header length shorter than 20 bytes!");
 
1319
                return -1;
 
1320
        }
 
1321
 
 
1322
        /* source port */
 
1323
        insint(button, "entry46", ptrf, 4);
 
1324
        
 
1325
        /* destination port */
 
1326
        insint(button, "entry47", ptrf, 4);
 
1327
 
 
1328
        /* sequence number */
 
1329
        insint(button, "entry48", ptrf, 8);
 
1330
 
 
1331
        /* acknowledgement number */
 
1332
        insint(button, "entry49", ptrf, 8);
 
1333
        
 
1334
        /* now we insert value for length */
 
1335
        snprintf(tmp2, 3, "%d", x*4);
 
1336
        w1 = lookup_widget(GTK_WIDGET(button), "entry50");
 
1337
        gtk_entry_set_text(GTK_ENTRY(w1), tmp2);
 
1338
 
 
1339
        /* increase by one for length and for another one for 4 bits that are reserved */
 
1340
        ptrf = ptrf + 2;
 
1341
 
 
1342
        /* flags; next byte */  
 
1343
        ch = char2x(ptrf) % 0x0100;
 
1344
 
 
1345
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton22");
 
1346
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x80) > 0 ? TRUE : FALSE);
 
1347
        
 
1348
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton23");
 
1349
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x40) > 0 ? TRUE : FALSE);
 
1350
        
 
1351
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton7");
 
1352
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x20) > 0 ? TRUE : FALSE);
 
1353
        
 
1354
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton8");
 
1355
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x10) > 0 ? TRUE : FALSE);
 
1356
        
 
1357
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton9");
 
1358
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x08) > 0 ? TRUE : FALSE);
 
1359
        
 
1360
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton10");
 
1361
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x04) > 0 ? TRUE : FALSE);
 
1362
        
 
1363
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton11");
 
1364
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x02) > 0 ? TRUE : FALSE);
 
1365
        
 
1366
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton12");
 
1367
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x01) > 0 ? TRUE : FALSE);
 
1368
        
 
1369
        ptrf = ptrf + 2;
 
1370
 
 
1371
        /* window size */
 
1372
        insint(button, "entry51", ptrf, 4);
 
1373
        
 
1374
        /* checksum */
 
1375
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton13");
 
1376
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1377
        //inspar(button, "entry52", ptrf, 4);
 
1378
        ptrf = ptrf + 4;
 
1379
 
 
1380
        /* window size */
 
1381
        insint(button, "entry53", ptrf, 4);
 
1382
        
 
1383
        /* any options ? */
 
1384
        /* - 20 for standard header */
 
1385
        inspar(button, "entry54", ptrf, ( (x*4) - 20) * 2);
 
1386
 
 
1387
        remain = remain - x*4;
 
1388
 
 
1389
        /* get access to buffer of the text field */
 
1390
        w2 = lookup_widget(GTK_WIDGET(button), "text4");
 
1391
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w2));
 
1392
 
 
1393
        if (remain > 0) {
 
1394
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton14");
 
1395
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1396
        
 
1397
                /* copy data to tmp field */
 
1398
                for (i=0, j=1; (i < (remain * 3) ); i++, j++) {
 
1399
                        tmp[i] = *ptrf++; i++;
 
1400
                        tmp[i] = *ptrf++; i++;
 
1401
                        /* we allow only 16 bytes in each row - looks nicer */
 
1402
                        if ((j % 16) == 0 && (j > 1)) {
 
1403
                                tmp[i]='\n';
 
1404
                                j = 0;
 
1405
                        }
 
1406
                        else
 
1407
                                tmp[i] = ' ';
 
1408
                }
 
1409
                tmp[i] = '\0';
 
1410
 
 
1411
                /* insert the text in the text field */
 
1412
                gtk_text_buffer_set_text(buffer,tmp,-1);
 
1413
        }
 
1414
        else {  
 
1415
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton14");
 
1416
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1417
        }
 
1418
 
 
1419
        /* since tcp does not have any protocol field we could return destination port value which 
 
1420
         * usually describes next layer protocol; currently we return 1 */
 
1421
 
 
1422
        return 1;
 
1423
}
 
1424
 
 
1425
int udp_header(GtkButton *button, int whocalled) {
 
1426
 
 
1427
        int i, j;
 
1428
        char tmp[4600];
 
1429
 
 
1430
        if (whocalled==2) {
 
1431
                protokol = UDP;
 
1432
                return 1;
 
1433
        }
 
1434
 
 
1435
        /* for standard header this is minimum length */
 
1436
        if (remain < 8) {
 
1437
                error("Can't load packet: Packet length shorter than UDP header length!");
 
1438
                return -1;
 
1439
        }
 
1440
 
 
1441
        remain = remain - 8;
 
1442
 
 
1443
        /* source port */
 
1444
        insint(button, "entry56", ptrf, 4);
 
1445
        
 
1446
        /* destination port */
 
1447
        insint(button, "entry41", ptrf, 4);
 
1448
 
 
1449
        /* length */
 
1450
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton3");
 
1451
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1452
        //insint(button, "entry42", ptrf, 4);
 
1453
        ptrf = ptrf + 4;
 
1454
 
 
1455
        /* checksum */
 
1456
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton4");
 
1457
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1458
        //inspar(button, "entry43", "", 4);
 
1459
        ptrf = ptrf + 4;
 
1460
 
 
1461
        /* get access to buffer of the text field */
 
1462
        w2 = lookup_widget(GTK_WIDGET(button), "text3");
 
1463
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w2));
 
1464
 
 
1465
        if (remain > 0) {
 
1466
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton5");
 
1467
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1468
        
 
1469
                /* copy data to tmp field */
 
1470
                for (i=0, j=1; (i < (remain * 3) ); i++, j++) {
 
1471
                        tmp[i] = *ptrf++; i++;
 
1472
                        tmp[i] = *ptrf++; i++;
 
1473
                        /* we allow only 16 bytes in each row - looks nicer */
 
1474
                        if ((j % 16) == 0 && (j > 1)) {
 
1475
                                tmp[i]='\n';
 
1476
                                j = 0;
 
1477
                        }
 
1478
                        else
 
1479
                                tmp[i] = ' ';
 
1480
                }
 
1481
                tmp[i] = '\0';
 
1482
 
 
1483
                /* insert the text in the text field */
 
1484
                gtk_text_buffer_set_text(buffer,tmp,-1);
 
1485
        }
 
1486
        else {  
 
1487
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton5");
 
1488
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1489
        }
 
1490
 
 
1491
        /* since udp does not have any protocol field we could return destination port value which 
 
1492
         * usually describes next layer protocol; currently we return 1 */
 
1493
        return 1;
 
1494
}
 
1495
 
 
1496
 
 
1497
int ipv4_header(GtkButton *button, int whocalled, struct clist_hdr *clptr ) {
 
1498
 
 
1499
        char tmp[5];
 
1500
        int x, header_l, prot;
 
1501
 
 
1502
        if (whocalled==2) {
 
1503
                protokol = IPv4;
 
1504
        }
 
1505
 
 
1506
        /* for standard header this is minimum length */
 
1507
        if (remain < 20) {
 
1508
                error("Can't load packet: IPv4 header field is not long enough!");
 
1509
                return -1;
 
1510
        }
 
1511
 
 
1512
        /* first comes version but we will first check the length and then insert version */
 
1513
        ptrf++;
 
1514
 
 
1515
        /* check the header length */
 
1516
        /* we don't need to check the return value here, it is already done when reading from file */
 
1517
        header_l = retint(ptrf);
 
1518
        /* header length is the number of 32-bit words in the header, including any options. 
 
1519
         * Since this is a 4-bit field, it limits the header to 60 bytes. So the remaining length 
 
1520
         * should be at least that long or we exit here */
 
1521
        if ( (header_l * 4) < 20 ) {
 
1522
                error("Can't load packet:\nIPv4 header length shorter than 20 bytes!");
 
1523
                return -1;
 
1524
        }
 
1525
        if ( (header_l * 4) > remain ) {
 
1526
                error("Can't load packet:\nPacket length shorter than IPv4 header length!");
 
1527
                return -1;
 
1528
        }
 
1529
        ptrf--;
 
1530
 
 
1531
        if (whocalled==1) {
 
1532
                /* insert version */
 
1533
                inspar(button, "entry26", ptrf, 1);
 
1534
 
 
1535
                /* insert header length */
 
1536
                inspar(button, "entry27", ptrf, 1);
 
1537
 
 
1538
                /* insert tos */
 
1539
                inspar(button, "entry28", ptrf, 2);
 
1540
 
 
1541
                /* insert total length */
 
1542
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton21");
 
1543
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1544
                //insint(button, "entry29", ptrf, 4);
 
1545
        }
 
1546
        else
 
1547
                ptrf = ptrf+4;
 
1548
 
 
1549
        ptrf = ptrf + 4;
 
1550
 
 
1551
        if (whocalled==1) {
 
1552
                /* insert identification */
 
1553
                inspar(button, "entry30", ptrf, 4);
 
1554
        }
 
1555
        else
 
1556
                ptrf = ptrf+4;
 
1557
 
 
1558
        /* insert flags */
 
1559
        *tmp = 0x30; /* 0x30 == 0 */
 
1560
        *(tmp+1) = *ptrf;
 
1561
        x = char2x(tmp);
 
1562
        x = x >> 1; /* use only first 3 bits */
 
1563
        
 
1564
        if (whocalled==1) {
 
1565
                w1 = lookup_widget(GTK_WIDGET(button), "entry31");
 
1566
                snprintf(tmp, 4, "%d", x);
 
1567
                gtk_entry_set_text(GTK_ENTRY(w1), tmp);
 
1568
        }
 
1569
 
 
1570
        /* insert fragment offset */
 
1571
        *tmp = 0x30; /* 0x30 == 0 */
 
1572
        *(tmp+1) = *ptrf;
 
1573
        x = (char2x(tmp)%2); /* need only last bit */
 
1574
        if (x == 0)
 
1575
                *tmp = 0x30;
 
1576
        else
 
1577
                *tmp = 0x31;
 
1578
        strncpy(tmp+1, ptrf+1, 3);
 
1579
 
 
1580
        if (whocalled==1) {
 
1581
                insint(button, "entry32", tmp, 4);
 
1582
        
 
1583
                /* insert ttl */
 
1584
                insint(button, "entry44", ptrf, 2);
 
1585
        }
 
1586
        else
 
1587
                ptrf = ptrf+6;
 
1588
 
 
1589
        prot = char2x(ptrf);
 
1590
 
 
1591
        if (whocalled==1) {
 
1592
                /* insert protocol */
 
1593
                insint(button, "entry34", ptrf, 2);
 
1594
 
 
1595
                /* insert header checksum */
 
1596
                w1 = lookup_widget(GTK_WIDGET(button), "ip_header_cks_cbt");
 
1597
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1598
                //inspar(button, "entry35", ptrf, 4);
 
1599
        }
 
1600
        else
 
1601
                ptrf = ptrf+2;
 
1602
 
 
1603
        ptrf = ptrf + 4;
 
1604
 
 
1605
        /*insert source ip */
 
1606
        ptrt = temp;
 
1607
        memset(temp, 0, 20);
 
1608
        if (whocalled==1)
 
1609
                w1 = lookup_widget(GTK_WIDGET(button), "entry38");
 
1610
        for (i=1; i<=12; i++, ptrt++) {
 
1611
                if (i%3 == 0) {                 
 
1612
                        x = char2x(tmp);
 
1613
                        if (i==12)
 
1614
                                snprintf(tmp, 4, "%d", x);              
 
1615
                        else
 
1616
                                snprintf(tmp, 5, "%d.", x);             
 
1617
                        strcat(temp, tmp);
 
1618
                }
 
1619
                else {
 
1620
                        tmp[(i-1)%3] = *ptrf++;
 
1621
                }
 
1622
        }
 
1623
 
 
1624
        if (whocalled==1)
 
1625
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1626
        else
 
1627
                memcpy(clptr->src, temp, 20);
 
1628
 
 
1629
        /*insert destination ip */
 
1630
        ptrt = temp;
 
1631
        memset(temp, 0, 20);
 
1632
        if (whocalled==1)
 
1633
                w1 = lookup_widget(GTK_WIDGET(button), "entry37");
 
1634
        for (i=1; i<=12; i++, ptrt++) {
 
1635
                if (i%3 == 0) {                 
 
1636
                        x = char2x(tmp);
 
1637
                        if (i==12)
 
1638
                                snprintf(tmp, 4, "%d", x);              
 
1639
                        else
 
1640
                                snprintf(tmp, 5, "%d.", x);             
 
1641
                        strcat(temp, tmp);
 
1642
                }
 
1643
                else {
 
1644
                        tmp[(i-1)%3] = *ptrf++;
 
1645
                }
 
1646
        }
 
1647
        if (whocalled==1)
 
1648
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1649
        else
 
1650
                memcpy(clptr->dst, temp, 20);
 
1651
 
 
1652
        /* insert ipv4 options 
 
1653
         * header_l * 4 == total header length, - 20 for standard header == options length in bytes*/
 
1654
        if (whocalled==1)
 
1655
                inspar(button, "entry39", ptrf, ( (header_l*4) - 20) * 2);
 
1656
 
 
1657
        remain = remain - (header_l * 4);
 
1658
        
 
1659
        return prot;
 
1660
}
 
1661
 
 
1662
 
 
1663
int ethernet_8023(GtkButton *button, int whocalled) {
 
1664
 
 
1665
        int dsap, lsap, ctrl;
 
1666
        long pid;
 
1667
 
 
1668
        if (whocalled==2) {
 
1669
                protokol = ETH_802_3;
 
1670
                return 1;
 
1671
        }
 
1672
 
 
1673
        if (remain < 6) {
 
1674
                error("Can't load packet: Ethernet 802.3 LLC field is not long enough!");
 
1675
                return -1;
 
1676
        }
 
1677
        remain = remain - 3;
 
1678
 
 
1679
        w1 = lookup_widget(GTK_WIDGET(button), "bt_8023");
 
1680
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1681
 
 
1682
        //w2 = lookup_widget(GTK_WIDGET(button), "frame7");
 
1683
        //gtk_widget_set_sensitive (w2, TRUE);
 
1684
        //gtk_notebook_set_page(GTK_NOTEBOOK(w3), 1);
 
1685
 
 
1686
        w1 = lookup_widget(GTK_WIDGET(button), "entry5");
 
1687
        //inspar(button, "entry5", ptrf, 4);
 
1688
        ptrf = ptrf + 4;
 
1689
        gtk_widget_set_sensitive (w1, FALSE);
 
1690
 
 
1691
        w2 = lookup_widget(GTK_WIDGET(button), "checkbutton2");
 
1692
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1693
        
 
1694
        /*now the LLC / LLC-SNAP part */
 
1695
        /* we decode only RFC 1042 format, that means the following value:
 
1696
                dsap == ssap == 0xAA
 
1697
                ctrl == 0x03
 
1698
                OUI  == 0x000000 
 
1699
        */
 
1700
        dsap = char2x(ptrf);    
 
1701
        inspar(button, "L_dsap", ptrf, 2);
 
1702
        lsap = char2x(ptrf);    
 
1703
        inspar(button, "L_ssap", ptrf, 2);
 
1704
        ctrl = char2x(ptrf);    
 
1705
        inspar(button, "L_ctrl", ptrf, 2);
 
1706
 
 
1707
        /* in case dsap != ssap != 0xAA or ctrl != 0x03 or remain length < 5 bytes, we have only 
 
1708
         * LLC without SNAP and we return value for user defined next layer */
 
1709
        if ( (dsap != 170 ) || (lsap != 170) || (ctrl != 3) || (remain < 5) ) {
 
1710
                w1 = lookup_widget(GTK_WIDGET(button), "L_8023_llc_tbt");
 
1711
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1712
                w1 = lookup_widget(GTK_WIDGET(button), "L_oui");
 
1713
                w2 = lookup_widget(GTK_WIDGET(button), "L_pid");
 
1714
                gtk_widget_set_sensitive (w1, FALSE);
 
1715
                gtk_widget_set_sensitive (w2, FALSE);
 
1716
                /* this means we insert all the data as user defined field */
 
1717
                return -2;
 
1718
        }
 
1719
        /* in this case everything is ok but oui in not 0 */
 
1720
        /*         <--------------this is oui--------------------->   */        
 
1721
        else if ( (char2x(ptrf) + char2x(ptrf+2) + char2x(ptrf+4) != 0 ) ) {
 
1722
                w1 = lookup_widget(GTK_WIDGET(button), "L_8023_llc_tbt");
 
1723
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1724
                w1 = lookup_widget(GTK_WIDGET(button), "L_oui");
 
1725
                w2 = lookup_widget(GTK_WIDGET(button), "L_pid");
 
1726
                gtk_widget_set_sensitive (w1, FALSE);
 
1727
                gtk_widget_set_sensitive (w2, FALSE);
 
1728
                /* this means we insert all the data as user defined field */
 
1729
                return -2;
 
1730
        }
 
1731
        
 
1732
        /* substract 3 for oui and 2 for pid */
 
1733
        remain = remain - 5;
 
1734
 
 
1735
        /* ok, so we have dsap and ssap == 0xAA, Ctlr == 0x03, OUI == 0x0 and length is long enough */
 
1736
        /* set llc-snap button */
 
1737
        w1 = lookup_widget(GTK_WIDGET(button), "L_8023_llcsnap_tbt");
 
1738
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1739
 
 
1740
        /* insert 0x00 into oui field */
 
1741
        inspar(button, "L_oui", ptrf, 6);
 
1742
        pid = char2x(ptrf)*256 + char2x(ptrf+2);
 
1743
 
 
1744
        ptrf = ptrf + 4;
 
1745
 
 
1746
        return pid;
 
1747
}
 
1748
 
 
1749
 
 
1750
int ethernet_verII(GtkButton *button, int whocalled) {
 
1751
 
 
1752
        int pid;
 
1753
        
 
1754
        if (whocalled==2) 
 
1755
                protokol = ETH_II;
 
1756
 
 
1757
        if (whocalled==1) {
 
1758
                w1 = lookup_widget(GTK_WIDGET(button), "bt_ver2");
 
1759
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1760
        }
 
1761
 
 
1762
        pid = char2x(ptrf)*256 + char2x(ptrf+2);
 
1763
 
 
1764
        ptrf = ptrf + 4;
 
1765
 
 
1766
        return pid;
 
1767
}
 
1768
 
 
1769
 
 
1770
/* this one inserts (length) characters from (char *from) into entry named (char *entry). It adds \0 
 
1771
at the end and in moves pointer ptrf (this one points the the next "data" to be inserted) by length */
 
1772
void inspar(GtkButton *button, char *entry, char *from, int length) {
 
1773
 
 
1774
        GtkWidget *widg;
 
1775
        //char tmp[81];
 
1776
        char *ptr;
 
1777
 
 
1778
        ptr = malloc(length * sizeof(char) + 1);        
 
1779
 
 
1780
        widg = lookup_widget(GTK_WIDGET(button), entry);
 
1781
 
 
1782
        strncpy(ptr, from, length);
 
1783
        ptr[length] = '\0';
 
1784
        gtk_entry_set_text(GTK_ENTRY(widg), ptr);
 
1785
        ptrf = ptrf + length;
 
1786
 
 
1787
        free(ptr);
 
1788
 
1789
 
 
1790
 
 
1791
/* this one reads (length) characters strating at (char *from), converts them to int and inserts them 
 
1792
into field (*entry) as integer. f.e: 0x56 == (int)86 => writes into (*entry) 86 
 
1793
note that max size for length is 10!!! when calling this routine */
 
1794
void insint(GtkButton *button, char *entry, char *from, int length) {
 
1795
 
 
1796
        GtkWidget *widg;
 
1797
        char tmp[11];
 
1798
        unsigned long value = 0;
 
1799
        int i;
 
1800
        unsigned char x = 0;
 
1801
 
 
1802
        widg = lookup_widget(GTK_WIDGET(button), entry);
 
1803
 
 
1804
        for (i = 0; i < length; i++) {
 
1805
                if ( (*from >= '0') && (*from <= '9')) 
 
1806
                        x = ((*from) - 48);
 
1807
                else if ((*from >= 'A') && (*from <= 'F')) 
 
1808
                        x = ((*from) - 55);
 
1809
                else if ((*from >= 'a') && (*from <= 'f')) 
 
1810
                        x = ((*from) - 87);
 
1811
                
 
1812
                value = value + ((int)x) * ((unsigned long)1 << (4*(length-1-i)) );
 
1813
                from++;
 
1814
        }
 
1815
 
 
1816
        ptrf = ptrf + length;
 
1817
 
 
1818
        snprintf(tmp, 11, "%lu", value);
 
1819
        gtk_entry_set_text(GTK_ENTRY(widg), tmp);
 
1820
 
1821
 
 
1822
 
 
1823
/* from a character return int */
 
1824
signed int retint(char *ch) {
 
1825
 
 
1826
        unsigned char x;
 
1827
 
 
1828
        if ( (*ch >= '0') && (*ch <= '9')) 
 
1829
                x = ((*ch) - 48);
 
1830
        else if ((*ch >= 'A') && (*ch <= 'F')) 
 
1831
                x = ((*ch) - 55);
 
1832
        else if ((*ch >= 'a') && (*ch <= 'f')) 
 
1833
                x = ((*ch) - 87);
 
1834
        else 
 
1835
                return -1;
 
1836
        
 
1837
        return (int)x;
 
1838
        
 
1839
}
 
1840
 
 
1841
 
 
1842
/* this one reads (length) characters strating at (*from), and returns integer (max 10 char length) */
 
1843
unsigned long retint2(char *from, int length) {
 
1844
 
 
1845
        unsigned long value = 0;
 
1846
        int i;
 
1847
        unsigned char x = 0;
 
1848
 
 
1849
        for (i = 0; i < length; i++) {
 
1850
                if ( (*from >= '0') && (*from <= '9')) 
 
1851
                        x = ((*from) - 48);
 
1852
                else if ((*from >= 'A') && (*from <= 'F')) 
 
1853
                        x = ((*from) - 55);
 
1854
                else if ((*from >= 'a') && (*from <= 'f')) 
 
1855
                        x = ((*from) - 87);
 
1856
                
 
1857
                value = value + ((int)x) * ((unsigned long)1 << (4*(length-1-i)) );
 
1858
                from++;
 
1859
        }
 
1860
 
 
1861
        return value;
 
1862
 
1863
 
 
1864
 
 
1865
/* i have newer really understood the endians... help appreciated... 
 
1866
 * this routines just converts the contents of a char field of size 8 chars 
 
1867
 * it works also, if destination and source are the same field*/
 
1868
 
 
1869
void convert8field(char *to, char *from) {
 
1870
 
 
1871
        char f1[8];
 
1872
 
 
1873
        /* we copy first the source contents */
 
1874
        memcpy(f1, from, 8);
 
1875
 
 
1876
        memcpy(to+0, f1+6,1);
 
1877
        memcpy(to+1, f1+7,1);
 
1878
        memcpy(to+2, f1+4,1);
 
1879
        memcpy(to+3, f1+5,1);
 
1880
        memcpy(to+4, f1+2,1);
 
1881
        memcpy(to+5, f1+3,1);
 
1882
        memcpy(to+6, f1+0,1);
 
1883
        memcpy(to+7, f1+1,1);
 
1884
}
 
1885
 
 
1886
int ipv6_header(GtkButton *button, int whocalled, struct clist_hdr *clptr ) {
 
1887
 
 
1888
        char tmp[5];
 
1889
        int x, prot, header_l;
 
1890
 
 
1891
        if (whocalled==2) {
 
1892
                protokol = IPv6;
 
1893
        }
 
1894
 
 
1895
        /* for standard header this is minimum length */
 
1896
        if (remain < 40) {
 
1897
                error("Can't load packet: IPv6 header field is not long enough!");
 
1898
                return -1;
 
1899
        }
 
1900
 
 
1901
        if (whocalled==1) {
 
1902
                /* insert version */
 
1903
                inspar(button, "entry195", ptrf, 1);
 
1904
 
 
1905
                /* insert traffic class */
 
1906
                inspar(button, "entry196", ptrf, 2);
 
1907
 
 
1908
                /* insert tos */
 
1909
                inspar(button, "entry197", ptrf, 5);
 
1910
 
 
1911
                /* insert total length */
 
1912
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton43");
 
1913
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1914
                //insint(button, "entry29", ptrf, 4);
 
1915
        }
 
1916
        else
 
1917
                ptrf = ptrf + 8;
 
1918
 
 
1919
        ptrf = ptrf + 4;
 
1920
 
 
1921
 
 
1922
        prot = char2x(ptrf);
 
1923
 
 
1924
        if (whocalled==1) {
 
1925
                /* insert next header */
 
1926
                inspar(button, "entry199", ptrf, 2);
 
1927
        }
 
1928
        else
 
1929
                ptrf = ptrf+2;
 
1930
 
 
1931
         if (whocalled==1) {
 
1932
                /* insert hop limit */
 
1933
                insint(button, "entry200", ptrf, 2);
 
1934
        }
 
1935
        else
 
1936
                ptrf = ptrf+2;
 
1937
 
 
1938
        /*insert source ip */
 
1939
        ptrt = temp6;
 
1940
        memset(temp6, 0, 40);
 
1941
 
 
1942
        if (whocalled==1)
 
1943
                w1 = lookup_widget(GTK_WIDGET(button), "entry201");
 
1944
 
 
1945
        for (i=1; i<8; i++) {
 
1946
                strncpy(ptrt, ptrf, 4);
 
1947
                ptrt = ptrt + 4;
 
1948
                strcat(ptrt, ":");
 
1949
                ptrt++;
 
1950
                ptrf = ptrf + 4;
 
1951
        }
 
1952
        strncpy(ptrt, ptrf, 4);
 
1953
        ptrf = ptrf + 4;
 
1954
 
 
1955
        if (whocalled==1)
 
1956
                gtk_entry_set_text(GTK_ENTRY(w1), temp6);
 
1957
        else
 
1958
                memcpy(clptr->src, temp6, 40);
 
1959
 
 
1960
        /*insert destination ip */
 
1961
        ptrt = temp6;
 
1962
        memset(temp6, 0, 40);
 
1963
        if (whocalled==1)
 
1964
                w1 = lookup_widget(GTK_WIDGET(button), "entry202");
 
1965
 
 
1966
        for (i=1; i<8; i++) {
 
1967
                strncpy(ptrt, ptrf, 4);
 
1968
                ptrt = ptrt + 4;
 
1969
                strcat(ptrt, ":");
 
1970
                ptrt++;
 
1971
                ptrf = ptrf + 4;
 
1972
        }
 
1973
        strncpy(ptrt, ptrf, 4);
 
1974
        ptrf = ptrf + 4;
 
1975
 
 
1976
        if (whocalled==1)
 
1977
                gtk_entry_set_text(GTK_ENTRY(w1), temp6);
 
1978
        else
 
1979
                memcpy(clptr->dst, temp6, 40);
 
1980
 
 
1981
        //extension header
 
1982
        while ( (prot==0) || (prot==43) || (prot==44) || (prot==51) || (prot==50) || (prot==60) ) {
 
1983
                prot = char2x(ptrf);
 
1984
                header_l = retint2(ptrf+2, 2);
 
1985
                inspar(button, "entry203", ptrf, header_l);
 
1986
 
 
1987
        }
 
1988
 
 
1989
        return prot;
 
1990
}
 
1991
 
 
1992
int icmpv6_header(GtkButton *button, int whocalled) {
 
1993
 
 
1994
        return 1;
 
1995
 
 
1996
}
 
1997