~ubuntu-branches/ubuntu/quantal/packeth/quantal

« back to all changes in this revision

Viewing changes to src/loadpacket.c

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2009-10-31 21:27:00 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091031212700-x2vkemah0nhdn57t
Tags: 1.6.3-1
* New upstream version
* debian/rules rewritten to adapt to new source layout
  - debian/manpages added
  - debian/examples added
  - debian/dirs added
* debian/control:
  - bumped quilt Build-Depends to >= 0.46-7~ (to use --with quilt in
    dh7)
  - Standards-Version bumped to 3.8.3 (no changes needed)
* debian/patches/* refreshed to cleanly apply

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