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

« back to all changes in this revision

Viewing changes to src/loadpacket.c

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * packETH - ethernet packet generator
 
3
 * By Miha Jemec <m.jemec@iskratel.si>
 
4
 * Copyright 2003 Miha Jemec, Iskratel
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 *
 
20
 * 
 
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;
 
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[10];
 
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 */
 
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
        if (i == 33024) {
 
607
                if (remain < 4) {
 
608
                        error("Can't load packet: Ethernet VLAN field is not long enough!");
 
609
                        return -1;
 
610
                }
 
611
                remain = remain -4;
 
612
 
 
613
                ptrf = ptrf + 4;
 
614
                *ptrt++ = '0';
 
615
                *ptrt-- = *ptrf++;
 
616
                i = char2x(ptrt);
 
617
 
 
618
                if (whocalled == 1) {
 
619
                        w3 = lookup_widget(GTK_WIDGET(button), "L_optmenu2_bt");
 
620
                        w4 = lookup_widget(GTK_WIDGET(button), "checkbutton39");
 
621
                        w5 = lookup_widget(GTK_WIDGET(button), "checkbutton40");
 
622
                        w6 = lookup_widget(GTK_WIDGET(button), "L_vlan_id");
 
623
                        w7 = lookup_widget(GTK_WIDGET(button), "entry165");
 
624
                
 
625
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
626
                        gtk_widget_set_sensitive (w2, TRUE);
 
627
                
 
628
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w3), (i>>1));
 
629
 
 
630
                        if ( (i%2) == 0)
 
631
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w4), FALSE);
 
632
                        else
 
633
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w4), TRUE);
 
634
 
 
635
                        inspar(button, "L_vlan_id", ptrf, 3);
 
636
                }
 
637
                else
 
638
                        ptrf = ptrf+3;
 
639
 
 
640
                i = char2x(ptrf)*256 + char2x(ptrf+2);
 
641
 
 
642
                /* we support now vlan stacking, in case the protocol in once again 0x8100
 
643
                 * we just add next 4 bytes in a QinQ field and switch the toggle button */
 
644
                if (i == 33024) {
 
645
                        if (remain < 4) {
 
646
                                error("Can't load packet: QinQ VLAN field is not long enough!");
 
647
                                return -1;
 
648
                        }
 
649
                        remain = remain -4;
 
650
 
 
651
                        ptrf = ptrf + 4;
 
652
                        
 
653
                        if (whocalled == 1) {
 
654
                                gtk_widget_set_sensitive (w7, TRUE);
 
655
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w5), TRUE);
 
656
 
 
657
                                inspar(button, "entry165", ptrf, 4);
 
658
                        }
 
659
                        else
 
660
                                ptrf = ptrf +3;
 
661
 
 
662
                        i = char2x(ptrf)*256 + char2x(ptrf+2);
 
663
                }       
 
664
                else {
 
665
                        if (whocalled == 1) {
 
666
                                gtk_widget_set_sensitive (w7, FALSE);
 
667
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w5), FALSE);
 
668
                        }       
 
669
                }
 
670
        }
 
671
        else {
 
672
                if (whocalled == 1) {
 
673
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
674
                        gtk_widget_set_sensitive (w2, FALSE);
 
675
                }       
 
676
        }
 
677
        
 
678
        /* c will tell us which ethernet type we have */
 
679
        c = i;
 
680
 
 
681
        /* ok, from now one, we split the dissection in different routines, depending on what values */
 
682
        /* now if length is <= 1500, we have 802.3 ethernet and this value means length of ethernet packet */
 
683
        if (i <= 1500) 
 
684
                next_prot = ethernet_8023(button, whocalled);
 
685
        /* Values between 1500 and 1536 are forbidden */
 
686
        else if ( (i>1500) && (i<1536) ) {
 
687
                error("Can't load packet: Wrong ethernet length/type field");
 
688
                return -1;
 
689
        }
 
690
        /* if i >= 1536 - ethernet ver II */
 
691
        else
 
692
                next_prot = ethernet_verII(button, whocalled);
 
693
 
 
694
 
 
695
        /* ok, so we have dissected the ethernet layer and now move on two the next layer.
 
696
         * if the ethernet dissector returns -1, this means an error and we quit
 
697
         * otherwise, the return value can be 2048 == 0x0800 and this means the ipv4
 
698
         * so we try to dissect ipv4 header. in case it is ok, we activate the ippkt_radibt
 
699
         * and this one then calls the callback which fills in ethernet ver II type field
 
700
         * and PID field in 802.3 LLC/SNAP field. It is the same for arp packets
 
701
         * for other packets we will try to open the userdefined window */
 
702
 
 
703
        /* we got an error? */
 
704
        if (next_prot == -1) 
 
705
                return -1;
 
706
 
 
707
        /* ipv4 */
 
708
        else if (next_prot == 2048) {
 
709
                /* ok, ipv4 should follow, so we call the routine for parsing ipv4 header. */
 
710
                next_prot = ipv4_header(button, whocalled, clptr);
 
711
                if (next_prot == -1)
 
712
                        return -1;
 
713
 
 
714
                /* if the return value from parsing ipv4 header was != 0, then the header parameters 
 
715
                 * are ok and we can open ipv4 notebook page activate toggle button (button calls 
 
716
                 * the callback then!!! */
 
717
                w1 = lookup_widget(GTK_WIDGET(button), "ippkt_radibt");
 
718
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
719
 
 
720
                /* here we do the further parsing: tcp, udp, icmp, ...*/
 
721
                if (next_prot == 1) {
 
722
                        /* try to parse icmp header */
 
723
                        next_prot = icmp_header(button, whocalled);
 
724
                        /* not ok, return an error */
 
725
                        if (next_prot == -1)
 
726
                                return -1;
 
727
                        /* ok, lets activate the icmp notebook */
 
728
                        else {
 
729
                                w1 = lookup_widget(GTK_WIDGET(button), "icmp_bt");
 
730
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
731
                        }
 
732
                }
 
733
                else if (next_prot == 2) {
 
734
                        /* try to parse igmp header */
 
735
                        next_prot = igmp_header(button, whocalled);
 
736
                        /* not ok, return an error */
 
737
                        if (next_prot == -1)
 
738
                                return -1;
 
739
                        /* ok, lets activate the igmp notebook */
 
740
                        else {
 
741
                                w1 = lookup_widget(GTK_WIDGET(button), "igmp_bt");
 
742
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
743
                        }
 
744
                }
 
745
                else if (next_prot == 6) {
 
746
                        /* try to parse tcp header */
 
747
                        next_prot = tcp_header(button, whocalled);
 
748
                        /* not ok, return an error */
 
749
                        if (next_prot == -1)
 
750
                                return -1;
 
751
                        /* ok, lets activate the tcp notebook */
 
752
                        else {
 
753
                                w1 = lookup_widget(GTK_WIDGET(button), "tcp_bt");
 
754
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
755
                        }
 
756
 
 
757
                        /* protocols on top of tcp would follow here */
 
758
 
 
759
                }       
 
760
                else if (next_prot == 17) {
 
761
                        /* try to parse udp header */
 
762
                        next_prot = udp_header(button, whocalled);
 
763
                        /* not ok, return an error */
 
764
                        if (next_prot == -1)
 
765
                                return -1;
 
766
                        /* ok, lets activate the udp notebook */
 
767
                        else {
 
768
                                w1 = lookup_widget(GTK_WIDGET(button), "udp_bt");
 
769
                                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
770
                        }
 
771
 
 
772
                        /* protocols on top of udp would follow here */
 
773
                }       
 
774
                /* protocol we do not support yet; user defined window */
 
775
                else {
 
776
                        next_prot = usedef_insert(button, "text2", whocalled);
 
777
                        w1 = lookup_widget(GTK_WIDGET(button), "ip_user_data_bt");
 
778
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
779
                }
 
780
        }
 
781
        /*arp */
 
782
        else if (next_prot == 2054) {
 
783
                /* ok, arp header follows */
 
784
                next_prot = arp_header(button, whocalled);
 
785
                if (next_prot == -1)
 
786
                        return -1;
 
787
 
 
788
                w1 = lookup_widget(GTK_WIDGET(button), "arppkt_radiobt");
 
789
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
790
        }
 
791
 
 
792
        /* when ipv6 will be added, activate ipv6 button instead of userdef button */
 
793
        else if (next_prot == 34525) {
 
794
                //w1 = lookup_widget(GTK_WIDGET(button), "IPv6_rdbt");
 
795
                //gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
796
                w1 = lookup_widget(GTK_WIDGET(button), "usedef2_radibt");
 
797
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
798
 
 
799
                ptrf = ptrf - 4;
 
800
                inspar(button, "L_ethtype", ptrf, 4);
 
801
                
 
802
                /* -2 means only llc without snap was used, so we don't insert the value in pid field */
 
803
                if (next_prot != -2) {
 
804
                        ptrf = ptrf - 4;
 
805
                        inspar(button, "L_pid", ptrf, 4);
 
806
                }
 
807
        }
 
808
        /* anything else - user defined */
 
809
        else {
 
810
                /* setting "usedef2_radibt" toggle button to true will call the callback which will clear 
 
811
                eth II type field and 802.3 pid field, so we have to fill this later */
 
812
                w1 = lookup_widget(GTK_WIDGET(button), "usedef2_radibt");
 
813
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
814
                
 
815
                /* we still have c to distinguish between ver II and 802.3 */
 
816
                /* ver II */
 
817
                if (c >= 1536) {
 
818
                        ptrf = ptrf - 4;
 
819
                        inspar(button, "L_ethtype", ptrf, 4);
 
820
                }
 
821
                /* 802.3 and with LLC SNAP */
 
822
                else if (next_prot != -2) {
 
823
                        ptrf = ptrf - 4;
 
824
                        inspar(button, "L_pid", ptrf, 4);
 
825
                }               
 
826
 
 
827
                next_prot = usedef_insert(button, "text1", whocalled);
 
828
        }
 
829
 
 
830
        return 1;
 
831
}
 
832
 
 
833
 
 
834
int arp_header(GtkButton *button, int whocalled) {
 
835
 
 
836
        char tmp[5];
 
837
        int x;
 
838
 
 
839
        if (whocalled==2) {
 
840
                protokol = ARP;
 
841
                return 1;
 
842
        }
 
843
 
 
844
        /* arp header length == 28; but packet can be longer, f.e. to satisfy the min packet length */
 
845
        if (remain < 28) {
 
846
                error("Can't load packet: Packet length shorter than ARP header length!");
 
847
                return -1;
 
848
        }
 
849
 
 
850
        remain = remain - 28;
 
851
 
 
852
        /* hardware type */
 
853
        inspar(button, "A_hwtype", ptrf, 4);
 
854
 
 
855
        /* protocol type */
 
856
        inspar(button, "A_prottype", ptrf, 4);
 
857
 
 
858
        /* hardware size */
 
859
        inspar(button, "A_hwsize", ptrf, 2);
 
860
 
 
861
        /* protocol size */
 
862
        inspar(button, "A_protsize", ptrf, 2);
 
863
 
 
864
        /* opcode is next */
 
865
        if ( (*ptrf == '0') && (*(ptrf+1) == '0') && (*(ptrf+2) == '0') && (*(ptrf+3) == '1') ) {
 
866
                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton10");
 
867
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
868
                ptrf = ptrf + 4;
 
869
        }
 
870
        else if ( (*ptrf == '0') && (*(ptrf+1) == '0') && (*(ptrf+2) == '0') && (*(ptrf+3) == '2') ) {
 
871
                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton11");
 
872
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
873
                ptrf = ptrf + 4;
 
874
        }
 
875
        else {
 
876
                w1 = lookup_widget(GTK_WIDGET(button), "radiobutton17");
 
877
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
878
                inspar(button, "entry81", ptrf, 4);
 
879
        }
 
880
 
 
881
        /* sender mac */
 
882
        ptrt = temp;
 
883
        w1 = lookup_widget(GTK_WIDGET(button), "A_sendermac");
 
884
        for (i=1; i<=18; i++, ptrt++) {
 
885
                if (i%3 == 0) 
 
886
                        *ptrt = ':';
 
887
                else {
 
888
                        *ptrt = *ptrf++;
 
889
                }
 
890
        }
 
891
        *ptrt = '\0';
 
892
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
893
 
 
894
        /* sender ip */
 
895
        ptrt = temp;
 
896
        memset(temp, 0, 20);
 
897
        w1 = lookup_widget(GTK_WIDGET(button), "A_senderip");
 
898
        for (i=1; i<=12; i++, ptrt++) {
 
899
                if (i%3 == 0) {                 
 
900
                        x = char2x(tmp);
 
901
                        if (i==12)
 
902
                                snprintf(tmp, 4, "%d", x);              
 
903
                        else
 
904
                                snprintf(tmp, 5, "%d.", x);             
 
905
                        strcat(temp, tmp);
 
906
                }
 
907
                else {
 
908
                        tmp[(i-1)%3] = *ptrf++;
 
909
                }
 
910
        }
 
911
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
912
 
 
913
        /* target mac */
 
914
        ptrt = temp;
 
915
        w1 = lookup_widget(GTK_WIDGET(button), "A_targetmac");
 
916
        for (i=1; i<=18; i++, ptrt++) {
 
917
                if (i%3 == 0) 
 
918
                        *ptrt = ':';
 
919
                else {
 
920
                        *ptrt = *ptrf++;
 
921
                }
 
922
        }
 
923
        *ptrt = '\0';
 
924
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
925
 
 
926
        /* target ip */
 
927
        ptrt = temp;
 
928
        memset(temp, 0, 20);
 
929
        w1 = lookup_widget(GTK_WIDGET(button), "A_targetip");
 
930
        for (i=1; i<=12; i++, ptrt++) {
 
931
                if (i%3 == 0) {                 
 
932
                        x = char2x(tmp);
 
933
                        if (i==12)
 
934
                                snprintf(tmp, 4, "%d", x);              
 
935
                        else
 
936
                                snprintf(tmp, 5, "%d.", x);             
 
937
                        strcat(temp, tmp);
 
938
                }
 
939
                else {
 
940
                        tmp[(i-1)%3] = *ptrf++;
 
941
                }
 
942
        }
 
943
        gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
944
 
 
945
        return 1;
 
946
 
 
947
}
 
948
 
 
949
 
 
950
int igmp_header(GtkButton *button, int whocalled) {
 
951
 
 
952
        int x, x1;
 
953
        char tmp[4];
 
954
 
 
955
        if (whocalled==2) {
 
956
                protokol = IGMP;
 
957
                return 1;
 
958
        }
 
959
 
 
960
        /* well normal igmp type should have at least 8 bytes, so this is min for us */
 
961
        if (remain < 8) {
 
962
                error("Can't load packet: Packet length shorter than IGMP header length!");
 
963
                return -1;
 
964
        }
 
965
 
 
966
        remain = remain -8;
 
967
 
 
968
        /* igmp type */
 
969
        x = char2x(ptrf);
 
970
        /* insert version */
 
971
        inspar(button, "entry166", ptrf, 2);
 
972
 
 
973
        w1 = lookup_widget(GTK_WIDGET(button), "optionmenu20");
 
974
        w2 = lookup_widget(GTK_WIDGET(button), "notebook8");
 
975
        if (x == 17) {
 
976
                if (remain > 4) {
 
977
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 1);
 
978
                        gtk_notebook_set_page(GTK_NOTEBOOK(w2), 1);
 
979
                        }
 
980
                else    {
 
981
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 0);
 
982
                        gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
983
                        }
 
984
        }
 
985
        else if (x == 18) {
 
986
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 2);
 
987
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
988
                }
 
989
        else if (x == 22) {
 
990
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 3);
 
991
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
992
                }
 
993
        else if (x == 34) {
 
994
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 4);
 
995
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 2);
 
996
                }
 
997
        else if (x == 23) {
 
998
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 5);
 
999
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1000
                }
 
1001
        else    {
 
1002
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 6);
 
1003
                gtk_notebook_set_page(GTK_NOTEBOOK(w2), 0);
 
1004
                }
 
1005
 
 
1006
        inspar(button, "entry167", ptrf, 2);
 
1007
 
 
1008
        /* set checksum button on auto */
 
1009
        w2 = lookup_widget(GTK_WIDGET(button), "checkbutton41");
 
1010
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1011
        ptrf = ptrf + 4;
 
1012
 
 
1013
 
 
1014
        if ( (x == 17) && (remain>4) ) { /* IGMP V3 query */
 
1015
                /*insert group ip */
 
1016
                ptrt = temp;
 
1017
                memset(temp, 0, 20);
 
1018
                w1 = lookup_widget(GTK_WIDGET(button), "entry169");
 
1019
                for (i=1; i<=12; i++, ptrt++) {
 
1020
                        if (i%3 == 0) {                 
 
1021
                                x = char2x(tmp);
 
1022
                                if (i==12)
 
1023
                                        snprintf(tmp, 4, "%d", x);              
 
1024
                                else
 
1025
                                        snprintf(tmp, 5, "%d.", x);             
 
1026
                                strcat(temp, tmp);
 
1027
                        }
 
1028
                        else {
 
1029
                                tmp[(i-1)%3] = *ptrf++;
 
1030
                        }
 
1031
                }
 
1032
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1033
 
 
1034
                inspar(button, "entry171", ptrf, 4);
 
1035
                x1 = (int)retint2(ptrf, 4);
 
1036
                inspar(button, "entry172", ptrf, 4);
 
1037
                /*#inspar(button, "entry173", ptrf, x1);*/
 
1038
                inspar(button, "entry173", ptrf, remain);
 
1039
                
 
1040
        }
 
1041
        else if (x==22) { /*IGMP V3 report */
 
1042
                inspar(button, "entry176", ptrf, 4);
 
1043
                x1 = (int)retint2(ptrf, 4);
 
1044
                inspar(button, "entry177", ptrf, 4);
 
1045
                inspar(button, "entry178", ptrf, x1);
 
1046
                
 
1047
        }
 
1048
        else { /*all the other versions */
 
1049
                /*insert group ip */
 
1050
                ptrt = temp;
 
1051
                memset(temp, 0, 20);
 
1052
                w1 = lookup_widget(GTK_WIDGET(button), "entry175");
 
1053
                for (i=1; i<=12; i++, ptrt++) {
 
1054
                        if (i%3 == 0) {                 
 
1055
                                x = char2x(tmp);
 
1056
                                if (i==12)
 
1057
                                        snprintf(tmp, 4, "%d", x);              
 
1058
                                else
 
1059
                                        snprintf(tmp, 5, "%d.", x);             
 
1060
                                strcat(temp, tmp);
 
1061
                        }
 
1062
                        else {
 
1063
                                tmp[(i-1)%3] = *ptrf++;
 
1064
                        }
 
1065
                }
 
1066
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1067
                        
 
1068
        
 
1069
        }       
 
1070
 
 
1071
        return 1;
 
1072
}
 
1073
 
 
1074
int icmp_header(GtkButton *button, int whocalled) {
 
1075
 
 
1076
        int x;
 
1077
 
 
1078
        if (whocalled==2) {
 
1079
                protokol = ICMP;
 
1080
                return 1;
 
1081
        }
 
1082
 
 
1083
        /* well normal icmp type should have at least 8 bytes, so this is min for us */
 
1084
        if (remain < 8) {
 
1085
                error("Can't load packet: Packet length shorter than ICMP header length!");
 
1086
                return -1;
 
1087
        }
 
1088
 
 
1089
        remain = remain -8;
 
1090
 
 
1091
        /* icmp type */
 
1092
        x = char2x(ptrf);
 
1093
        /* insert version */
 
1094
        inspar(button, "entry57", ptrf, 2);
 
1095
 
 
1096
        w1 = lookup_widget(GTK_WIDGET(button), "optionmenu4");
 
1097
        if (x == 0)
 
1098
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 0);
 
1099
        else if (x == 3) 
 
1100
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 1);
 
1101
        else if (x == 8) 
 
1102
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 2);
 
1103
        else
 
1104
                gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 3);
 
1105
 
 
1106
 
 
1107
        if (x == 0) { /* echo reply */
 
1108
                /* insert code, checksum, identifier and seq number and data if there is some */
 
1109
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1110
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 0);
 
1111
                inspar(button, "entry62", ptrf, 2);
 
1112
                //inspar(button, "entry63", ptrf, 4);
 
1113
                ptrf = ptrf + 4;
 
1114
                inspar(button, "entry64", ptrf, 4);
 
1115
                inspar(button, "entry65", ptrf, 4);
 
1116
                /* set checksum button on auto */
 
1117
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton16");
 
1118
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1119
 
 
1120
                inspar(button, "entry66", ptrf, remain * 2);
 
1121
                if (remain > 0) {
 
1122
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton17");
 
1123
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1124
                }
 
1125
                else {
 
1126
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton17");
 
1127
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1128
                }
 
1129
                
 
1130
        }
 
1131
        else if (x == 3) { /* destination unreacheable */
 
1132
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1133
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 2);
 
1134
                /* which code? */
 
1135
                x = char2x(ptrf);
 
1136
                /* insert code */
 
1137
                inspar(button, "entry58", ptrf, 2);
 
1138
 
 
1139
                w1 = lookup_widget(GTK_WIDGET(button), "optionmenu5");
 
1140
                if ( (x >= 0) && (x <= 15) )
 
1141
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), x);
 
1142
                else
 
1143
                        gtk_option_menu_set_history (GTK_OPTION_MENU (w1), 16);
 
1144
                
 
1145
                /* insert code, checksum, identifier and seq number and data if there is some */
 
1146
                //inspar(button, "entry59", ptrf, 4);
 
1147
                ptrf = ptrf + 4;
 
1148
                inspar(button, "entry60", ptrf, 8);
 
1149
                /* set checksum button on auto */
 
1150
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton15");
 
1151
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1152
 
 
1153
                inspar(button, "entry61", ptrf, remain * 2);
 
1154
                if (remain > 0) {
 
1155
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton24");
 
1156
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1157
                }
 
1158
                else {
 
1159
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton24");
 
1160
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1161
                }
 
1162
        }
 
1163
        else if (x == 8) { /* echo request */
 
1164
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1165
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 5);
 
1166
                /* insert code, checksum, identifier and seq number and data if there is some */
 
1167
                inspar(button, "entry74", ptrf, 2);
 
1168
                //inspar(button, "entry77", ptrf, 4);
 
1169
                ptrf = ptrf + 4;
 
1170
                inspar(button, "entry75", ptrf, 4);
 
1171
                inspar(button, "entry78", ptrf, 4);
 
1172
                /* set checksum button on auto */
 
1173
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton20");
 
1174
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1175
 
 
1176
                inspar(button, "entry76", ptrf, remain * 2);
 
1177
                if (remain > 0) {
 
1178
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton19");
 
1179
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1180
                }
 
1181
                else {
 
1182
                        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton19");
 
1183
                        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1184
                }
 
1185
 
 
1186
        }
 
1187
        else { /* all the rest */
 
1188
                w1 = lookup_widget(GTK_WIDGET(button), "notebook5");
 
1189
                gtk_notebook_set_page(GTK_NOTEBOOK(w1), 1);
 
1190
                /* insert code, checksum and data if there is some */
 
1191
                inspar(button, "entry157", ptrf, 2);
 
1192
                //inspar(button, "entry158", ptrf, 4);
 
1193
                ptrf = ptrf + 4;
 
1194
                /* set checksum button on auto */
 
1195
                w2 = lookup_widget(GTK_WIDGET(button), "checkbutton38");
 
1196
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1197
 
 
1198
                inspar(button, "entry159", ptrf, (remain + 4) * 2);
 
1199
 
 
1200
        }
 
1201
 
 
1202
        return 1;
 
1203
}
 
1204
 
 
1205
 
 
1206
int usedef_insert(GtkButton *button, char *entry, int whocalled) {
 
1207
 
 
1208
        int i, j;
 
1209
        char tmp[4600];
 
1210
 
 
1211
        if (whocalled == 1)
 
1212
                return 1;
 
1213
 
 
1214
        /* get access to buffer of the text field */
 
1215
        w2 = lookup_widget(GTK_WIDGET(button), entry);
 
1216
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w2));
 
1217
 
 
1218
        /* copy data to tmp field */
 
1219
        for (i=0, j=1; (i < (remain * 3) ); i++, j++) {
 
1220
                tmp[i] = *ptrf++; i++;
 
1221
                tmp[i] = *ptrf++; i++;
 
1222
                /* we allow only 16 bytes in each row - looks nicer */
 
1223
                if ((j % 16) == 0 && (j > 1)) {
 
1224
                        tmp[i]='\n';
 
1225
                        j = 0;
 
1226
                }
 
1227
                else
 
1228
                        tmp[i] = ' ';
 
1229
        }
 
1230
        tmp[i] = '\0';
 
1231
 
 
1232
        /* insert the text in the text field */
 
1233
        gtk_text_buffer_set_text(buffer,tmp,-1);
 
1234
 
 
1235
        return 1;
 
1236
 
 
1237
 
 
1238
}
 
1239
 
 
1240
 
 
1241
int tcp_header(GtkButton *button, int whocalled) {
 
1242
 
 
1243
        int x, i, j;
 
1244
        char tmp[4600], tmp2[3], ch;
 
1245
 
 
1246
        if (whocalled==2) {
 
1247
                protokol = TCP;
 
1248
                return 1;
 
1249
        }
 
1250
 
 
1251
 
 
1252
        /* for standard header this is minimum length */
 
1253
        if (remain < 20) {
 
1254
                error("Can't load packet: Packet length shorter than TCP header length!");
 
1255
                return -1;
 
1256
        }
 
1257
 
 
1258
        /* ok, packet is long enough to fill in the standard header, but what is the header length?
 
1259
         * we insert this later but need now to see that the packet is long enough */
 
1260
        x = retint(ptrf+24);
 
1261
        if ( (x * 4) > remain ) {
 
1262
                error("Can't load packet:\nPacket lenght shorter than TCP header length!");
 
1263
                return -1;
 
1264
        }
 
1265
        if ( x < 5 ) {
 
1266
                error("Can't load packet:\nTCP header length shorter than 20 bytes!");
 
1267
                return -1;
 
1268
        }
 
1269
 
 
1270
        /* source port */
 
1271
        insint(button, "entry46", ptrf, 4);
 
1272
        
 
1273
        /* destination port */
 
1274
        insint(button, "entry47", ptrf, 4);
 
1275
 
 
1276
        /* sequence number */
 
1277
        insint(button, "entry48", ptrf, 8);
 
1278
 
 
1279
        /* acknowledgement number */
 
1280
        insint(button, "entry49", ptrf, 8);
 
1281
        
 
1282
        /* now we insert value for length */
 
1283
        snprintf(tmp2, 3, "%d", x*4);
 
1284
        w1 = lookup_widget(GTK_WIDGET(button), "entry50");
 
1285
        gtk_entry_set_text(GTK_ENTRY(w1), tmp2);
 
1286
 
 
1287
        /* increase by one for length and for another one for 4 bits that are reserved */
 
1288
        ptrf = ptrf + 2;
 
1289
 
 
1290
        /* flags; next byte */  
 
1291
        ch = char2x(ptrf) % 0x0100;
 
1292
 
 
1293
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton22");
 
1294
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x80) > 0 ? TRUE : FALSE);
 
1295
        
 
1296
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton23");
 
1297
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x40) > 0 ? TRUE : FALSE);
 
1298
        
 
1299
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton7");
 
1300
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x20) > 0 ? TRUE : FALSE);
 
1301
        
 
1302
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton8");
 
1303
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x10) > 0 ? TRUE : FALSE);
 
1304
        
 
1305
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton9");
 
1306
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x08) > 0 ? TRUE : FALSE);
 
1307
        
 
1308
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton10");
 
1309
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x04) > 0 ? TRUE : FALSE);
 
1310
        
 
1311
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton11");
 
1312
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x02) > 0 ? TRUE : FALSE);
 
1313
        
 
1314
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton12");
 
1315
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), (ch & 0x01) > 0 ? TRUE : FALSE);
 
1316
        
 
1317
        ptrf = ptrf + 2;
 
1318
 
 
1319
        /* window size */
 
1320
        insint(button, "entry51", ptrf, 4);
 
1321
        
 
1322
        /* checksum */
 
1323
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton13");
 
1324
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1325
        //inspar(button, "entry52", ptrf, 4);
 
1326
        ptrf = ptrf + 4;
 
1327
 
 
1328
        /* window size */
 
1329
        insint(button, "entry53", ptrf, 4);
 
1330
        
 
1331
        /* any options ? */
 
1332
        /* - 20 for standard header */
 
1333
        inspar(button, "entry54", ptrf, ( (x*4) - 20) * 2);
 
1334
 
 
1335
        remain = remain - x*4;
 
1336
 
 
1337
        /* get access to buffer of the text field */
 
1338
        w2 = lookup_widget(GTK_WIDGET(button), "text4");
 
1339
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w2));
 
1340
 
 
1341
        if (remain > 0) {
 
1342
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton14");
 
1343
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1344
        
 
1345
                /* copy data to tmp field */
 
1346
                for (i=0, j=1; (i < (remain * 3) ); i++, j++) {
 
1347
                        tmp[i] = *ptrf++; i++;
 
1348
                        tmp[i] = *ptrf++; i++;
 
1349
                        /* we allow only 16 bytes in each row - looks nicer */
 
1350
                        if ((j % 16) == 0 && (j > 1)) {
 
1351
                                tmp[i]='\n';
 
1352
                                j = 0;
 
1353
                        }
 
1354
                        else
 
1355
                                tmp[i] = ' ';
 
1356
                }
 
1357
                tmp[i] = '\0';
 
1358
 
 
1359
                /* insert the text in the text field */
 
1360
                gtk_text_buffer_set_text(buffer,tmp,-1);
 
1361
        }
 
1362
        else {  
 
1363
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton14");
 
1364
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1365
        }
 
1366
 
 
1367
        /* since tcp does not have any protocol field we could return destination port value which 
 
1368
         * usually describes next layer protocol; currently we return 1 */
 
1369
 
 
1370
        return 1;
 
1371
}
 
1372
 
 
1373
int udp_header(GtkButton *button, int whocalled) {
 
1374
 
 
1375
        int i, j;
 
1376
        char tmp[4600];
 
1377
 
 
1378
        if (whocalled==2) {
 
1379
                protokol = UDP;
 
1380
                return 1;
 
1381
        }
 
1382
 
 
1383
        /* for standard header this is minimum length */
 
1384
        if (remain < 8) {
 
1385
                error("Can't load packet: Packet length shorter than UDP header length!");
 
1386
                return -1;
 
1387
        }
 
1388
 
 
1389
        remain = remain - 8;
 
1390
 
 
1391
        /* source port */
 
1392
        insint(button, "entry56", ptrf, 4);
 
1393
        
 
1394
        /* destination port */
 
1395
        insint(button, "entry41", ptrf, 4);
 
1396
 
 
1397
        /* length */
 
1398
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton3");
 
1399
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1400
        //insint(button, "entry42", ptrf, 4);
 
1401
        ptrf = ptrf + 4;
 
1402
 
 
1403
        /* checksum */
 
1404
        w1 = lookup_widget(GTK_WIDGET(button), "checkbutton4");
 
1405
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1406
        //inspar(button, "entry43", "", 4);
 
1407
        ptrf = ptrf + 4;
 
1408
 
 
1409
        /* get access to buffer of the text field */
 
1410
        w2 = lookup_widget(GTK_WIDGET(button), "text3");
 
1411
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w2));
 
1412
 
 
1413
        if (remain > 0) {
 
1414
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton5");
 
1415
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1416
        
 
1417
                /* copy data to tmp field */
 
1418
                for (i=0, j=1; (i < (remain * 3) ); i++, j++) {
 
1419
                        tmp[i] = *ptrf++; i++;
 
1420
                        tmp[i] = *ptrf++; i++;
 
1421
                        /* we allow only 16 bytes in each row - looks nicer */
 
1422
                        if ((j % 16) == 0 && (j > 1)) {
 
1423
                                tmp[i]='\n';
 
1424
                                j = 0;
 
1425
                        }
 
1426
                        else
 
1427
                                tmp[i] = ' ';
 
1428
                }
 
1429
                tmp[i] = '\0';
 
1430
 
 
1431
                /* insert the text in the text field */
 
1432
                gtk_text_buffer_set_text(buffer,tmp,-1);
 
1433
        }
 
1434
        else {  
 
1435
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton5");
 
1436
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), FALSE);
 
1437
        }
 
1438
 
 
1439
        /* since udp does not have any protocol field we could return destination port value which 
 
1440
         * usually describes next layer protocol; currently we return 1 */
 
1441
        return 1;
 
1442
}
 
1443
 
 
1444
 
 
1445
int ipv4_header(GtkButton *button, int whocalled, struct clist_hdr *clptr ) {
 
1446
 
 
1447
        char tmp[5];
 
1448
        int x, header_l, prot;
 
1449
 
 
1450
        if (whocalled==2) {
 
1451
                protokol = IPv4;
 
1452
        }
 
1453
 
 
1454
        /* for standard header this is minimum length */
 
1455
        if (remain < 20) {
 
1456
                error("Can't load packet: IPv4 header field is not long enough!");
 
1457
                return -1;
 
1458
        }
 
1459
 
 
1460
        /* first comes version but we will first check the length and then insert version */
 
1461
        ptrf++;
 
1462
 
 
1463
        /* check the header length */
 
1464
        /* we don't need to check the return value here, it is already done when reading from file */
 
1465
        header_l = retint(ptrf);
 
1466
        /* header length is the number of 32-bit words in the header, including any options. 
 
1467
         * Since this is a 4-bit field, it limits the header to 60 bytes. So the remaining length 
 
1468
         * should be at least that long or we exit here */
 
1469
        if ( (header_l * 4) < 20 ) {
 
1470
                error("Can't load packet:\nIPv4 header length shorter than 20 bytes!");
 
1471
                return -1;
 
1472
        }
 
1473
        if ( (header_l * 4) > remain ) {
 
1474
                error("Can't load packet:\nPacket lenght shorter than IPv4 header length!");
 
1475
                return -1;
 
1476
        }
 
1477
        ptrf--;
 
1478
 
 
1479
        if (whocalled==1) {
 
1480
                /* insert version */
 
1481
                inspar(button, "entry26", ptrf, 1);
 
1482
 
 
1483
                /* insert header length */
 
1484
                inspar(button, "entry27", ptrf, 1);
 
1485
 
 
1486
                /* insert tos */
 
1487
                inspar(button, "entry28", ptrf, 2);
 
1488
 
 
1489
                /* insert total length */
 
1490
                w1 = lookup_widget(GTK_WIDGET(button), "checkbutton21");
 
1491
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1492
                //insint(button, "entry29", ptrf, 4);
 
1493
        }
 
1494
        else
 
1495
                ptrf = ptrf+4;
 
1496
 
 
1497
        ptrf = ptrf + 4;
 
1498
 
 
1499
        if (whocalled==1) {
 
1500
                /* insert identification */
 
1501
                inspar(button, "entry30", ptrf, 4);
 
1502
        }
 
1503
        else
 
1504
                ptrf = ptrf+4;
 
1505
 
 
1506
        /* insert flags */
 
1507
        *tmp = 0x30; /* 0x30 == 0 */
 
1508
        *(tmp+1) = *ptrf;
 
1509
        x = char2x(tmp);
 
1510
        x = x >> 1; /* use only first 3 bits */
 
1511
        
 
1512
        if (whocalled==1) {
 
1513
                w1 = lookup_widget(GTK_WIDGET(button), "entry31");
 
1514
                snprintf(tmp, 4, "%d", x);
 
1515
                gtk_entry_set_text(GTK_ENTRY(w1), tmp);
 
1516
        }
 
1517
 
 
1518
        /* insert fragment offset */
 
1519
        *tmp = 0x30; /* 0x30 == 0 */
 
1520
        *(tmp+1) = *ptrf;
 
1521
        x = (char2x(tmp)%2); /* need only last bit */
 
1522
        if (x == 0)
 
1523
                *tmp = 0x30;
 
1524
        else
 
1525
                *tmp = 0x31;
 
1526
        strncpy(tmp+1, ptrf+1, 3);
 
1527
 
 
1528
        if (whocalled==1) {
 
1529
                insint(button, "entry32", tmp, 4);
 
1530
        
 
1531
                /* insert ttl */
 
1532
                insint(button, "entry44", ptrf, 2);
 
1533
        }
 
1534
        else
 
1535
                ptrf = ptrf+6;
 
1536
 
 
1537
        prot = char2x(ptrf);
 
1538
 
 
1539
        if (whocalled==1) {
 
1540
                /* insert protocol */
 
1541
                insint(button, "entry34", ptrf, 2);
 
1542
 
 
1543
                /* insert header checksum */
 
1544
                w1 = lookup_widget(GTK_WIDGET(button), "ip_header_cks_cbt");
 
1545
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1546
                //inspar(button, "entry35", ptrf, 4);
 
1547
        }
 
1548
        else
 
1549
                ptrf = ptrf+2;
 
1550
 
 
1551
        ptrf = ptrf + 4;
 
1552
 
 
1553
        /*insert source ip */
 
1554
        ptrt = temp;
 
1555
        memset(temp, 0, 20);
 
1556
        if (whocalled==1)
 
1557
                w1 = lookup_widget(GTK_WIDGET(button), "entry38");
 
1558
        for (i=1; i<=12; i++, ptrt++) {
 
1559
                if (i%3 == 0) {                 
 
1560
                        x = char2x(tmp);
 
1561
                        if (i==12)
 
1562
                                snprintf(tmp, 4, "%d", x);              
 
1563
                        else
 
1564
                                snprintf(tmp, 5, "%d.", x);             
 
1565
                        strcat(temp, tmp);
 
1566
                }
 
1567
                else {
 
1568
                        tmp[(i-1)%3] = *ptrf++;
 
1569
                }
 
1570
        }
 
1571
 
 
1572
        if (whocalled==1)
 
1573
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1574
        else
 
1575
                memcpy(clptr->src, temp, 20);
 
1576
 
 
1577
        /*insert destination ip */
 
1578
        ptrt = temp;
 
1579
        memset(temp, 0, 20);
 
1580
        if (whocalled==1)
 
1581
                w1 = lookup_widget(GTK_WIDGET(button), "entry37");
 
1582
        for (i=1; i<=12; i++, ptrt++) {
 
1583
                if (i%3 == 0) {                 
 
1584
                        x = char2x(tmp);
 
1585
                        if (i==12)
 
1586
                                snprintf(tmp, 4, "%d", x);              
 
1587
                        else
 
1588
                                snprintf(tmp, 5, "%d.", x);             
 
1589
                        strcat(temp, tmp);
 
1590
                }
 
1591
                else {
 
1592
                        tmp[(i-1)%3] = *ptrf++;
 
1593
                }
 
1594
        }
 
1595
        if (whocalled==1)
 
1596
                gtk_entry_set_text(GTK_ENTRY(w1), temp);
 
1597
        else
 
1598
                memcpy(clptr->dst, temp, 20);
 
1599
 
 
1600
        /* insert ipv4 options 
 
1601
         * header_l * 4 == total header length, - 20 for standard header == options length in bytes*/
 
1602
        if (whocalled==1)
 
1603
                inspar(button, "entry39", ptrf, ( (header_l*4) - 20) * 2);
 
1604
 
 
1605
        remain = remain - (header_l * 4);
 
1606
        
 
1607
        return prot;
 
1608
}
 
1609
 
 
1610
 
 
1611
int ethernet_8023(GtkButton *button, int whocalled) {
 
1612
 
 
1613
        int dsap, lsap, ctrl;
 
1614
        long pid;
 
1615
 
 
1616
        if (whocalled==2) {
 
1617
                protokol = ETH_802_3;
 
1618
                return 1;
 
1619
        }
 
1620
 
 
1621
        if (remain < 6) {
 
1622
                error("Can't load packet: Ethernet 802.3 LLC field is not long enough!");
 
1623
                return -1;
 
1624
        }
 
1625
        remain = remain - 3;
 
1626
 
 
1627
        w1 = lookup_widget(GTK_WIDGET(button), "bt_8023");
 
1628
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1629
 
 
1630
        //w2 = lookup_widget(GTK_WIDGET(button), "frame7");
 
1631
        //gtk_widget_set_sensitive (w2, TRUE);
 
1632
        //gtk_notebook_set_page(GTK_NOTEBOOK(w3), 1);
 
1633
 
 
1634
        w1 = lookup_widget(GTK_WIDGET(button), "entry5");
 
1635
        //inspar(button, "entry5", ptrf, 4);
 
1636
        ptrf = ptrf + 4;
 
1637
        gtk_widget_set_sensitive (w1, FALSE);
 
1638
 
 
1639
        w2 = lookup_widget(GTK_WIDGET(button), "checkbutton2");
 
1640
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w2), TRUE);
 
1641
        
 
1642
        /*now the LLC / LLC-SNAP part */
 
1643
        /* we decode only RFC 1042 format, that means the following value:
 
1644
                dsap == ssap == 0xAA
 
1645
                ctrl == 0x03
 
1646
                OUI  == 0x000000 
 
1647
        */
 
1648
        dsap = char2x(ptrf);    
 
1649
        inspar(button, "L_dsap", ptrf, 2);
 
1650
        lsap = char2x(ptrf);    
 
1651
        inspar(button, "L_ssap", ptrf, 2);
 
1652
        ctrl = char2x(ptrf);    
 
1653
        inspar(button, "L_ctrl", ptrf, 2);
 
1654
 
 
1655
        /* in case dsap != ssap != 0xAA or ctrl != 0x03 or remain length < 5 bytes, we have only 
 
1656
         * LLC without SNAP and we return value for user defined next layer */
 
1657
        if ( (dsap != 170 ) || (lsap != 170) || (ctrl != 3) || (remain < 5) ) {
 
1658
                w1 = lookup_widget(GTK_WIDGET(button), "L_8023_llc_tbt");
 
1659
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1660
                w1 = lookup_widget(GTK_WIDGET(button), "L_oui");
 
1661
                w2 = lookup_widget(GTK_WIDGET(button), "L_pid");
 
1662
                gtk_widget_set_sensitive (w1, FALSE);
 
1663
                gtk_widget_set_sensitive (w2, FALSE);
 
1664
                /* this means we insert all the data as user defined field */
 
1665
                return -2;
 
1666
        }
 
1667
        /* in this case everything is ok but oui in not 0 */
 
1668
        /*         <--------------this is oui--------------------->   */        
 
1669
        else if ( (char2x(ptrf) + char2x(ptrf+2) + char2x(ptrf+4) != 0 ) ) {
 
1670
                w1 = lookup_widget(GTK_WIDGET(button), "L_8023_llc_tbt");
 
1671
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1672
                w1 = lookup_widget(GTK_WIDGET(button), "L_oui");
 
1673
                w2 = lookup_widget(GTK_WIDGET(button), "L_pid");
 
1674
                gtk_widget_set_sensitive (w1, FALSE);
 
1675
                gtk_widget_set_sensitive (w2, FALSE);
 
1676
                /* this means we insert all the data as user defined field */
 
1677
                return -2;
 
1678
        }
 
1679
        
 
1680
        /* substract 3 for oui and 2 for pid */
 
1681
        remain = remain - 5;
 
1682
 
 
1683
        /* ok, so we have dsap and ssap == 0xAA, Ctlr == 0x03, OUI == 0x0 and lenght is long enough */
 
1684
        /* set llc-snap button */
 
1685
        w1 = lookup_widget(GTK_WIDGET(button), "L_8023_llcsnap_tbt");
 
1686
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1687
 
 
1688
        /* insert 0x00 into oui field */
 
1689
        inspar(button, "L_oui", ptrf, 6);
 
1690
        pid = char2x(ptrf)*256 + char2x(ptrf+2);
 
1691
 
 
1692
        ptrf = ptrf + 4;
 
1693
 
 
1694
        return pid;
 
1695
}
 
1696
 
 
1697
 
 
1698
int ethernet_verII(GtkButton *button, int whocalled) {
 
1699
 
 
1700
        int pid;
 
1701
        
 
1702
        if (whocalled==2) 
 
1703
                protokol = ETH_II;
 
1704
 
 
1705
        if (whocalled==1) {
 
1706
                w1 = lookup_widget(GTK_WIDGET(button), "bt_ver2");
 
1707
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w1), TRUE);
 
1708
        }
 
1709
 
 
1710
        pid = char2x(ptrf)*256 + char2x(ptrf+2);
 
1711
 
 
1712
        ptrf = ptrf + 4;
 
1713
 
 
1714
        return pid;
 
1715
}
 
1716
 
 
1717
 
 
1718
/* this one inserts (length) characters from (char *from) into entry named (char *entry). It adds \0 
 
1719
at the end and in moves pointer ptrf (this one points the the next "data" to be inserted) by length */
 
1720
void inspar(GtkButton *button, char *entry, char *from, int length) {
 
1721
 
 
1722
        GtkWidget *widg;
 
1723
        //char tmp[81];
 
1724
        char *ptr;
 
1725
 
 
1726
        ptr = malloc(length * sizeof(char) + 1);        
 
1727
 
 
1728
        widg = lookup_widget(GTK_WIDGET(button), entry);
 
1729
 
 
1730
        strncpy(ptr, from, length);
 
1731
        ptr[length] = '\0';
 
1732
        gtk_entry_set_text(GTK_ENTRY(widg), ptr);
 
1733
        ptrf = ptrf + length;
 
1734
 
 
1735
        free(ptr);
 
1736
 
1737
 
 
1738
 
 
1739
/* this one reads (length) characters strating at (char *from), converts them to int and inserts them 
 
1740
into field (*entry) as integer. f.e: 0x56 == (int)86 => writes into (*entry) 86 
 
1741
note that max size for length is 10!!! when calling this routine */
 
1742
void insint(GtkButton *button, char *entry, char *from, int length) {
 
1743
 
 
1744
        GtkWidget *widg;
 
1745
        char tmp[11];
 
1746
        unsigned long value = 0;
 
1747
        int i;
 
1748
        unsigned char x = 0;
 
1749
 
 
1750
        widg = lookup_widget(GTK_WIDGET(button), entry);
 
1751
 
 
1752
        for (i = 0; i < length; i++) {
 
1753
                if ( (*from >= '0') && (*from <= '9')) 
 
1754
                        x = ((*from) - 48);
 
1755
                else if ((*from >= 'A') && (*from <= 'F')) 
 
1756
                        x = ((*from) - 55);
 
1757
                else if ((*from >= 'a') && (*from <= 'f')) 
 
1758
                        x = ((*from) - 87);
 
1759
                
 
1760
                value = value + ((int)x) * ((unsigned long)1 << (4*(length-1-i)) );
 
1761
                from++;
 
1762
        }
 
1763
 
 
1764
        ptrf = ptrf + length;
 
1765
 
 
1766
        snprintf(tmp, 11, "%lu", value);
 
1767
        gtk_entry_set_text(GTK_ENTRY(widg), tmp);
 
1768
 
1769
 
 
1770
 
 
1771
/* from a character return int */
 
1772
signed int retint(char *ch) {
 
1773
 
 
1774
        unsigned char x;
 
1775
 
 
1776
        if ( (*ch >= '0') && (*ch <= '9')) 
 
1777
                x = ((*ch) - 48);
 
1778
        else if ((*ch >= 'A') && (*ch <= 'F')) 
 
1779
                x = ((*ch) - 55);
 
1780
        else if ((*ch >= 'a') && (*ch <= 'f')) 
 
1781
                x = ((*ch) - 87);
 
1782
        else 
 
1783
                return -1;
 
1784
        
 
1785
        return (int)x;
 
1786
        
 
1787
}
 
1788
 
 
1789
 
 
1790
/* this one reads (length) characters strating at (*from), and returns integer (max 10 char length) */
 
1791
unsigned long retint2(char *from, int length) {
 
1792
 
 
1793
        unsigned long value = 0;
 
1794
        int i;
 
1795
        unsigned char x = 0;
 
1796
 
 
1797
        for (i = 0; i < length; i++) {
 
1798
                if ( (*from >= '0') && (*from <= '9')) 
 
1799
                        x = ((*from) - 48);
 
1800
                else if ((*from >= 'A') && (*from <= 'F')) 
 
1801
                        x = ((*from) - 55);
 
1802
                else if ((*from >= 'a') && (*from <= 'f')) 
 
1803
                        x = ((*from) - 87);
 
1804
                
 
1805
                value = value + ((int)x) * ((unsigned long)1 << (4*(length-1-i)) );
 
1806
                from++;
 
1807
        }
 
1808
 
 
1809
        return value;
 
1810
 
1811
 
 
1812
 
 
1813
/* i have newer really understood the endians... help appreciated... 
 
1814
 * this routines just converts the contents of a char field of size 8 chars 
 
1815
 * it works also, if destination and source are the same field*/
 
1816
 
 
1817
void convert8field(char *to, char *from) {
 
1818
 
 
1819
        char f1[8];
 
1820
 
 
1821
        /* we copy first the source contents */
 
1822
        memcpy(f1, from, 8);
 
1823
 
 
1824
        memcpy(to+0, f1+6,1);
 
1825
        memcpy(to+1, f1+7,1);
 
1826
        memcpy(to+2, f1+4,1);
 
1827
        memcpy(to+3, f1+5,1);
 
1828
        memcpy(to+4, f1+2,1);
 
1829
        memcpy(to+5, f1+3,1);
 
1830
        memcpy(to+6, f1+0,1);
 
1831
        memcpy(to+7, f1+1,1);
 
1832
}