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

« back to all changes in this revision

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