2
* packETH - ethernet packet generator
3
* By Miha Jemec <m.jemec@iskratel.si>
4
* Copyright 2003 Miha Jemec, Iskratel
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.
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.
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.
20
* function.c - all routines except callbacks and routines for sending
33
#include <sys/timeb.h>
38
#include <netpacket/packet.h>
39
#include <net/ethernet.h>
40
#include <netinet/in.h>
41
#include <sys/ioctl.h>
44
#include "callbacks.h"
47
#include "function_send.h"
50
/* some global variables:
51
* packet [] - the packet contents
52
* number - packet length
54
unsigned char packet[1522];
61
gboolean stop_flag = 0;
62
extern char iftext[20];
63
static unsigned long crc32_table[256];
64
int crc32_table_init = 0;
66
/* structure that holds parameters for generator */
83
int udpstart; //udp payload start
84
int tcpstart; //tcp header start
85
int ipstart; //ip source address start
86
int ethstart; //start of address position in arp header
95
unsigned char pkttable[10][1518];
96
/* partable columns mean: [0] - is there a packet or not (1)/(0), [1] - length of packet, [2] -
97
number of packets [3] - gap between, [4] - gap to the next sequence, [5] - enable(1) / disable(0) */
98
long int partable[10][6];
101
/* be carefull with number. when you build a packet you should make number++ after the last
102
* copied value in the packet[] field since you start with number = 0, but when you call
103
* packet_go_on_the_link() you pass that number as the number of packets which is one
104
* more than in the last packet[number] = ... line */
107
/* send button was pressed */
108
int send_packet(GtkButton *button, gpointer user_data)
110
GtkWidget *statusbar, *notebk, *abstime, *reltime, *en5, *en6, *ntbk7;
111
GtkWidget *en1, *en2, *en3, *en4, *ckbt1, *ckbt2, *xoptm, *yoptm, *xmenu_item, *ymenu_item;
112
GtkWidget *optm1, *optm2, *optm3, *menu, *xmenu, *ymenu, *menu_item, *stopbt, *toolbar;
113
GtkWidget *button1, *button2, *button3, *button4, *button5, *button6;
116
gchar *en1_t, *en2_t, *en3_t, *en4_t, *en5_t, *en6_t;
117
gint context_id, page, cpage;
118
char buff[100], buf2[80];
126
statusbar = lookup_widget(GTK_WIDGET (button), "statusbar1");
127
context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "Statusbar example");
129
notebk = lookup_widget(GTK_WIDGET (button), "notebook1");
131
/* now we have to decide what happens when the send button is pressed */
132
page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebk));
134
if ( page == 0 ) { /* so we have the build notebook open, it means we send only one packet */
136
if (make_packet(button, user_data) == -1) {
137
//printf("problems with making packet!\n");
138
snprintf(buff, 100, " Problems with making packet!");
139
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
143
/* YYY if the built packet is shorter then 60 bytes, we add padding zero bytes
144
* to fill up the length till 60 (min ethrenet frame length). This bytes will be
145
* added anyway by the device driver, so we do this just to prevent misunderstanding:
146
* till now if your packet was 20 bytes long, then it was also said -> 20 bytes
147
* sent on eth0... but actually 60 bytes (+CRC) were sent */
149
memset(&packet[number], 0x00, ( 60 - number ) );
153
/* thats how the packet looks like */
154
//for (i = 0; i < number; i++)
155
// printf("%x ", packet[i]);
156
//printf("\nnumber je %d\n", number);
158
/* let's send the packet */
159
c = packet_go_on_the_link(packet, number);
162
//printf("problems with sending\n");
163
snprintf(buff, 100, " Problems with sending!");
164
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
171
strftime(buf2,80, "%H:%M:%S", ptr);
172
snprintf(buff, 100, " %s -----> %d bytes sent on %s", buf2, c, iftext);
173
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
179
/* is it the generator that sends the build packets? */
180
else if (page == 1) {
181
if (make_packet(button, user_data) == -1) {
182
//printf("problems with making packet!\n");
183
snprintf(buff, 100, " Problems with making packet!");
184
gtk_statusbar_push(GTK_STATUSBAR(statusbar),
185
GPOINTER_TO_INT(context_id), buff);
189
abstime = lookup_widget(GTK_WIDGET (button), "radiobutton34");
190
reltime = lookup_widget(GTK_WIDGET (button), "radiobutton35");
191
button1 = lookup_widget(GTK_WIDGET (button), "Build_button");
192
button2 = lookup_widget(GTK_WIDGET (button), "Gen_button");
193
button3 = lookup_widget(GTK_WIDGET (button), "Genp");
194
button4 = lookup_widget(GTK_WIDGET (button), "Interface_button");
195
button5 = lookup_widget(GTK_WIDGET (button), "Send_button");
196
button6 = lookup_widget(GTK_WIDGET (button), "Gensbt");
197
stopbt = lookup_widget(GTK_WIDGET (button), "Stop_button");
198
en1 = lookup_widget(GTK_WIDGET (button), "entry109");
199
en2 = lookup_widget(GTK_WIDGET (button), "entry110");
200
ckbt1 = lookup_widget(GTK_WIDGET(button), "checkbutton35");
201
ckbt2 = lookup_widget(GTK_WIDGET(button), "checkbutton37");
202
ntbk7 = lookup_widget(GTK_WIDGET(button), "notebook7");
204
en1_t = (char *)gtk_entry_get_text(GTK_ENTRY(en1));
205
en2_t = (char *)gtk_entry_get_text(GTK_ENTRY(en2));
207
/* do we have to adjust any parameters while sending? */
209
cpage = gtk_notebook_get_current_page(GTK_NOTEBOOK(ntbk7));
211
/* depending on what "packet type" is currently open we take some action */
215
optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu9");
216
menu = GTK_OPTION_MENU(optm1)->menu;
217
menu_item = gtk_menu_get_active (GTK_MENU (menu));
218
params1.inc = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
222
/* ip frame with user def option or icmp option */
224
optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu16");
225
menu = GTK_OPTION_MENU(optm1)->menu;
226
menu_item = gtk_menu_get_active (GTK_MENU (menu));
227
params1.inc = 10 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
233
optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu17");
234
menu = GTK_OPTION_MENU(optm1)->menu;
235
menu_item = gtk_menu_get_active (GTK_MENU (menu));
236
params1.inc = 20 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
242
optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu18");
243
menu = GTK_OPTION_MENU(optm1)->menu;
244
menu_item = gtk_menu_get_active (GTK_MENU (menu));
245
params1.inc = 30 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
251
optm1 = lookup_widget(GTK_WIDGET (button), "optionmenu19");
252
menu = GTK_OPTION_MENU(optm1)->menu;
253
menu_item = gtk_menu_get_active (GTK_MENU (menu));
254
params1.inc = 40 + g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
260
/* changing mac address */
261
if ((params1.inc%10) == 1) {
263
error("Error: Packets is not long enough to change MAC address");
268
/* changing ip source address */
269
else if ((params1.inc == 12) || (params1.inc == 32) || (params1.inc == 42)) {
270
if (number < ip_start + 1 + 4) {
271
error("Error: Packet is not long enough to change IP values");
276
/* changing ip and mac source address */
277
else if ((params1.inc == 13) || (params1.inc == 33) || (params1.inc == 43)) {
278
if (number < ip_start + 1 + 4) {
279
error("Error: Packet is not long enough to change MAC & IP values");
285
else if (params1.inc == 22) {
286
if (number < eth_start + 1 + 6 + 4) {
287
error("Error: Packet is not long enough to change ARP values");
292
/* ip source and tcp port values */
293
else if (params1.inc == 34) {
294
if (number < tcp_start + 2) {
295
error("Error: Packet isn't long enough to change TCP port");
300
/* increase udp payload by one */
301
else if (params1.inc == 44) {
302
if (number < udp_start + 1) {
303
error("Error: Packet is not long enough to increase UDP payload");
309
else if ( (params1.inc >= 45) && (params1.inc <=47) ) {
310
if (number < udp_start + 1 + 12) {
311
error("Error: Packet is not long enough to increase RTP values");
314
params1.inc = params1.inc - 38;
316
/* changing byte x */
317
/* YYY this part of code is a mess (it's friday 15.30 now))*/
318
else if ((params1.inc == 2) || (params1.inc == 14) || (params1.inc == 23) ||
319
(params1.inc == 35) || (params1.inc == 48)) {
321
/* offset x field, is it ok */
322
en5 = lookup_widget(GTK_WIDGET (button), "entry160");
323
en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
324
if ( (strtol(en5_t, (char **)NULL, 10) == 0) ||
325
(number < strtol(en5_t, (char **)NULL, 10)) ) {
326
error("Error: Wrong byte x offset!");
329
length = strlen(en5_t);
330
for(m=0; m<length; m++) {
331
if (isdigit(*(en5_t+m)) == 0) {
332
error("Error: Wrong byte x entry!");
336
params1.xbyte = strtol(en5_t, (char **)NULL, 10);
338
/* option menu button for x byte */
339
xoptm = lookup_widget(GTK_WIDGET (button), "optionmenu14");
340
xmenu = GTK_OPTION_MENU(xoptm)->menu;
341
xmenu_item = gtk_menu_get_active (GTK_MENU (xmenu));
342
params1.xchange = g_list_index (GTK_MENU_SHELL (xmenu)->children, xmenu_item);
343
memcpy(params1.xstart, &packet[params1.xbyte-1], 4);
345
if ((params1.xchange==1) || (params1.xchange==2)) {
346
/* range button for x byte */
347
en5 = lookup_widget(GTK_WIDGET (button), "entry161");
348
en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
349
if ( (strtol(en5_t, (char **)NULL, 10) == 0) ) {
350
// || (4294967294 < strtol(en5_t, (char **)NULL, 10)) ) {
351
error("Error: Wrong byte x range!");
354
length = strlen(en5_t);
355
for(m=0; m<length; m++) {
356
if (isdigit(*(en5_t+m)) == 0) {
357
error("Error: Wrong byte x range entry!");
361
params1.xrange = strtol(en5_t, (char **)NULL, 10);
366
/* changing byte x and y */
367
else if ((params1.inc == 3) || (params1.inc == 15) || (params1.inc == 24) ||
368
(params1.inc == 36) || (params1.inc == 49)) {
369
en5 = lookup_widget(GTK_WIDGET (button), "entry160");
370
en6 = lookup_widget(GTK_WIDGET (button), "entry162");
371
en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
372
en6_t = (char *)gtk_entry_get_text(GTK_ENTRY(en6));
373
if ( (number < strtol(en5_t, (char **)NULL, 10)) ||
374
(number < strtol(en6_t, (char **)NULL, 10)) ) {
375
error("Error: Packets is not long enough to change specified byte");
378
length = strlen(en5_t);
379
for(m=0; m<length; m++) {
380
if (isdigit(*(en5_t+m)) == 0) {
381
error("Error: Wrong byte x entry!");
385
length = strlen(en6_t);
386
for(m=0; m<length; m++) {
387
if (isdigit(*(en6_t+m)) == 0) {
388
error("Error: Wrong byte y entry!");
392
params1.xbyte = strtol(en5_t, (char **)NULL, 10);
393
xoptm = lookup_widget(GTK_WIDGET (button), "optionmenu14");
394
xmenu = GTK_OPTION_MENU(xoptm)->menu;
395
xmenu_item = gtk_menu_get_active (GTK_MENU (xmenu));
396
memcpy(params1.xstart, &packet[params1.xbyte-1], 4);
397
params1.xchange = g_list_index (GTK_MENU_SHELL (xmenu)->children, xmenu_item);
398
params1.ybyte = strtol(en6_t, (char **)NULL, 10);
399
yoptm = lookup_widget(GTK_WIDGET (button), "optionmenu15");
400
ymenu = GTK_OPTION_MENU(yoptm)->menu;
401
ymenu_item = gtk_menu_get_active (GTK_MENU (ymenu));
402
memcpy(params1.ystart, &packet[params1.ybyte-1], 4);
403
params1.ychange = g_list_index (GTK_MENU_SHELL (ymenu)->children, ymenu_item);
405
if ((params1.xchange==1) || (params1.xchange==2)) {
406
en5 = lookup_widget(GTK_WIDGET (button), "entry161");
407
en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
408
if ( (strtol(en5_t, (char **)NULL, 10) == 0) ) {
409
// || (4294967295 < strtol(en5_t, (char **)NULL, 10)) ) {
410
error("Error: Wrong byte x range!");
413
length = strlen(en5_t);
414
for(m=0; m<length; m++) {
415
if (isdigit(*(en5_t+m)) == 0) {
416
error("Error: Wrong byte x range entry!");
420
params1.xrange = strtol(en5_t, (char **)NULL, 10);
423
if ((params1.ychange==1) || (params1.ychange==2)) {
424
en5 = lookup_widget(GTK_WIDGET (button), "entry163");
425
en5_t = (char *)gtk_entry_get_text(GTK_ENTRY(en5));
426
if ( (strtol(en5_t, (char **)NULL, 10) == 0) ) {
427
// || (4294967295 < strtol(en5_t, (char **)NULL, 10)) ) {
428
error("Error: Wrong byte y range!");
431
length = strlen(en5_t);
432
for(m=0; m<length; m++) {
433
if (isdigit(*(en5_t+m)) == 0) {
434
error("Error: Wrong byte y range entry!");
438
params1.yrange = strtol(en5_t, (char **)NULL, 10);
444
/* if the togglebutton is active, just keep on sending till stop is pressed */
445
if (GTK_TOGGLE_BUTTON(ckbt1)->active) {
449
/* there can be rubbish in this field */
450
if (check_digit(en1_t, strlen(en1_t), "Error: Number of packets to send field") == -1)
453
params1.count = strtol(en1_t, (char **)NULL, 10);
454
/* we allow to send 9999999 max */
455
if ( (params1.count > 9999999) || (params1.count < 1) ) {
456
//printf("Error: Packets send number value\n");
457
error("Error: Packets send number value (1-9999999)");
462
if (GTK_TOGGLE_BUTTON(ckbt2)->active) {
466
/* there can be rubbish in this field */
467
if (check_digit(en2_t, strlen(en2_t), "Error: Delay between packets field") == -1)
470
params1.del = strtol(en2_t, (char **)NULL, 10);
471
/* max delay 999,999999 s */
472
if ( (params1.del > 999999999) || (params1.del < 1) ) {
473
//printf("Error: Delay between packets value\n");
474
error("Error: Delay between packets value (1-999999999)");
479
/* YYY if the built packet is shorter then 60 bytes, we add padding zero bytes
480
* to fill up the length till 60 (min ethrenet frame length). This bytes will be
481
* added anyway by the device driver, so we do this just to prevent misunderstanding:
482
* till now if your packet was 20 bytes long, then it was also said -> 20 bytes
483
* sent on eth0... but actually 60 bytes (+CRC) were sent */
485
memset(&packet[number], 0x00, ( 60 - number ) );
486
/* there were problems with sendbuilt function, if the packet was shorter then
487
* 60 bytes. Checksum was wrong calculated */
491
params1.button = statusbar;
492
params1.context_id = context_id;
493
params1.button1 = button1;
494
params1.button2 = button2;
495
params1.button3 = button3;
496
params1.button4 = button4;
497
params1.button5 = button5;
498
params1.button6 = button6;
499
params1.stopbt = stopbt;
500
params1.udpstart = udp_start;
501
params1.tcpstart = tcp_start;
502
params1.ipstart = ip_start;
503
params1.ethstart = eth_start;
505
if (GTK_TOGGLE_BUTTON(reltime)->active)
506
params1.timeflag = 1;
508
params1.timeflag = 0;
510
//gtk_widget_set_sensitive (button1, FALSE);
511
//gtk_widget_set_sensitive (button2, FALSE);
512
//gtk_widget_set_sensitive (button3, FALSE);
513
//gtk_widget_set_sensitive (button4, FALSE);
514
//gtk_widget_set_sensitive (button5, FALSE);
515
//gtk_widget_set_sensitive (stopbt, TRUE);
517
snprintf(buff, 100, " Starting generator...");
518
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
520
pthread_create(&thread_id, NULL, &sendbuilt, ¶ms1);
524
/* is it the generator that sends different sequences? */
525
else if (page == 2) {
528
int j = 0, sum = 0, sum1 = 0;
530
abstime = lookup_widget(GTK_WIDGET (button), "radiobutton36");
531
reltime = lookup_widget(GTK_WIDGET (button), "radiobutton37");
532
toolbar = lookup_widget(GTK_WIDGET (button), "toolbar1");
533
stopbt = lookup_widget(GTK_WIDGET (button), "Stop_button");
534
optm1 = lookup_widget(GTK_WIDGET (button), "checkbutton36");
535
optm2 = lookup_widget(GTK_WIDGET (button), "entry151");
536
optm3 = lookup_widget(GTK_WIDGET (button), "entry152");
537
button1 = lookup_widget(GTK_WIDGET (button), "Build_button");
538
button2 = lookup_widget(GTK_WIDGET (button), "Gen_button");
539
button3 = lookup_widget(GTK_WIDGET (button), "Genp");
540
button4 = lookup_widget(GTK_WIDGET (button), "Interface_button");
541
button5 = lookup_widget(GTK_WIDGET (button), "Send_button");
542
button6 = lookup_widget(GTK_WIDGET (button), "Gensbt");
544
en1_t = (char *)gtk_entry_get_text(GTK_ENTRY(optm2));
545
en2_t = (char *)gtk_entry_get_text(GTK_ENTRY(optm3));
547
if (GTK_TOGGLE_BUTTON(reltime)->active)
548
params1.timeflag = 1;
550
params1.timeflag = 0;
552
/* if the togglebutton is active, just keep on sending till stop is pressed */
553
if (GTK_TOGGLE_BUTTON(optm1)->active) {
557
/* there can be rubbish in this field */
558
if (check_digit(en1_t, strlen(en1_t),
559
"Error: Number of sequences to send field") == -1)
562
params1.count = strtol(en1_t, (char **)NULL, 10);
563
/* we allow to send 9999999 max */
564
if ( (params1.count > 9999999) || (params1.count < 1) ) {
565
//printf("Error: Number of sequences to send field\n");
566
error("Error: Number of sequences to send field (1 - 9999999)");
571
/* if the above number is 1, we don't need to check for this delay */
572
if (params1.count == 1)
575
/* there can be rubbish in this field */
576
if (check_digit(en2_t, strlen(en2_t), "Error: Delay between sequences field") == -1)
579
params1.del = strtol(en2_t, (char **)NULL, 10);
580
/* max delay 999,999999 s */
581
if ( (params1.del > 999999999) || (params1.del < 0) ) {
582
//printf("Error: Delay between sequences field\n");
583
error("Error: Delay between sequences field (0 - 999999999)");
588
/* we fill in a table with the parameters */
589
for (i=0; i<10; i++) {
591
/* name of the packet and packet contents */
592
snprintf(buff4, 100, "entry%d", 111+i);
593
en1 = lookup_widget(GTK_WIDGET (button), buff4);
594
en1_t = (char *)gtk_entry_get_text(GTK_ENTRY(en1));
596
/* enable or disable */
597
snprintf(buff4, 100, "checkbutton%d", 25+i);
598
ckbt1 = lookup_widget(GTK_WIDGET(button), buff4);
599
if (GTK_TOGGLE_BUTTON(ckbt1)->active) {
600
params1.partable[i][5] = 0;
604
params1.partable[i][5] = 1;
606
/* if there is no name, skip it */
607
if ( strlen(en1_t) == 0 ) {
608
params1.partable[i][0] = 0;
612
params1.partable[i][0] = 1;
614
/* open file for reading */
615
if ( (file_p = fopen(en1_t, "r")) == NULL) {
616
snprintf(buff4, 100, "Error: Can not open file for reading:%s", en1_t);
617
//printf("Error: Can not open file for reading %s\n", en1_t);
622
/* we have to read the packet contents stored in a file */
625
struct pcaprec_hdr ph;
629
/* first we read the pcap file header */
630
freads = fread(pkt_temp, sizeof(fh), 1, file_p);
635
memcpy(&fh, pkt_temp, 24);
637
/* if magic number in NOK, exit */
638
if (fh.magic != PCAP_MAGIC)
641
/* next the pcap packet header */
642
freads = fread(pkt_temp, sizeof(ph), 1, file_p);
648
/* copy the 16 bytes into ph structure */
649
memcpy(&ph, pkt_temp, 16);
651
/* and the packet itself, but only up to the capture length */
652
freads = fread(¶ms1.pkttable[i][0], ph.incl_len, 1, file_p);
659
params1.partable[i][1] = ph.incl_len;
662
/* number of packets to send */
663
snprintf(buff4, 100, "entry%d", 121+i);
664
en2 = lookup_widget(GTK_WIDGET (button), buff4);
665
en2_t = (char *)gtk_entry_get_text(GTK_ENTRY(en2));
666
snprintf(buff4, 100, "Error: Number of packets field in row %d", i+1);
667
if (check_digit(en2_t,strlen(en2_t), buff4) == -1)
670
params1.partable[i][2] = strtol(en2_t, (char **)NULL, 10);
671
/* we allow to send 9999999 max */
672
if ( (params1.partable[i][2] > 9999999) || (params1.partable[i][2] < 0) ) {
673
snprintf(buff4, 100, "Error: number of packets value in row %d", i+1);
674
//printf("Error: number of packets value in row %d\n", i+1);
679
/* delay between packets */
680
snprintf(buff4, 100, "entry%d", 131+i);
681
en3 = lookup_widget(GTK_WIDGET (button), buff4);
682
en3_t = (char *)gtk_entry_get_text(GTK_ENTRY(en3));
683
snprintf(buff4, 100, "Error: Delay between packets field in row %d", i+1);
684
if (check_digit(en3_t,strlen(en3_t), buff4) == -1)
687
params1.partable[i][3] = strtol(en3_t, (char **)NULL, 10);
688
/* max delay 999,999999 s */
689
if ( (params1.partable[i][3] > 999999999) || (params1.partable[i][3] < 0) ) {
690
snprintf(buff4, 100, "Error: delay between value in row %d", i+1);
691
//printf("Error: delay between value in row %d\n", i+1);
696
/* delay to next sequence */
697
snprintf(buff4, 100, "entry%d", 141+i);
698
en4 = lookup_widget(GTK_WIDGET (button), buff4);
699
en4_t = (char *)gtk_entry_get_text(GTK_ENTRY(en4));
700
snprintf(buff4, 100, "Error: Delay to next value in row %d", i+1);
701
if (check_digit(en4_t,strlen(en4_t), buff4) == -1)
704
params1.partable[i][4] = strtol(en4_t, (char **)NULL, 10);
705
/* max delay 999,999999 s */
706
if ( (params1.partable[i][4] > 999999999) || (params1.partable[i][4] < 0) ) {
707
snprintf(buff4, 100, "Error: delay to next value in row %d", i+1);
708
//printf("Error: delay to next value in row %d\n", i+1);
715
/* if all the fields are empty or disabled we return immediattely */
716
for (j=0; j<10; j++) {
717
sum = sum + params1.partable[j][0];
718
sum1 = sum1 + params1.partable[j][5];
721
if ( (sum ==0 ) || (sum1 == 0) ) {
722
snprintf(buff, 100, " Nothing to send...");
723
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
727
params1.button = statusbar;
728
params1.context_id = context_id;
729
params1.toolbar = toolbar;
730
params1.stopbt = stopbt;
731
params1.button1 = button1;
732
params1.button2 = button2;
733
params1.button3 = button3;
734
params1.button4 = button4;
735
params1.button5 = button5;
736
params1.button6 = button6;
738
snprintf(buff, 100, " Starting sequence generator...");
739
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
741
pthread_create(&thread_id, NULL, &sendsequence, ¶ms1);
745
/* is it the generator that uses the kernel module? */
746
else if (page == 3) {
754
int make_packet(GtkButton *button, gpointer user_data)
756
GtkWidget *ipv4, *ipv6, *arp, *usedef;
757
GtkWidget *auto_bt, *text_e;
761
/* first we fill packet field with 0 */
762
memset(packet, 0x00, 1522);
764
/* YYY what about auto selection for link layer is on?
765
* in case of saving packet we don't get here and it is ok
766
* in case of user defined payload we automatically disable this feature and it is ok
767
* so what about arp, ipv4 and ipv6?
768
* in case of an arp packet we accept the auto get mac option and it means that
769
* we take the source and destination mac address from the arp protokol field
770
* in case of an ipv4 packet this means that we don't open the raw socket but
771
* do all the sending on ip socket which helps us getting the mac address */
773
auto_bt = lookup_widget(GTK_WIDGET (button), "auto_get_mac_cbt");
774
ipv4 = lookup_widget(GTK_WIDGET (button), "ippkt_radibt");
775
ipv6 = lookup_widget(GTK_WIDGET (button), "IPv6_rdbt");
776
arp = lookup_widget(GTK_WIDGET (button), "arppkt_radiobt");
777
usedef = lookup_widget(GTK_WIDGET (button), "usedef2_radibt");
779
/* what about next layer: ipv4, ipv6, arp or manually attached payload? */
780
if (GTK_TOGGLE_BUTTON(ipv4)->active) {
781
/* YYY - to be done */
782
if (GTK_TOGGLE_BUTTON(auto_bt)->active) {
783
error("Not yet implemented");
784
//printf("Not yet implemented\n");
788
/* now we get the link layer info */
789
else if (link_level_get(button, user_data) == -1) {
790
//printf("Error: problem on link layer with IPv4 packet\n");
794
/* call the function that gets the ipv4 protocol information */
795
if (ipv4_get(button, user_data) == -1) {
796
//printf("Error: problem with IPv4 information\n");
800
/* grrr, oh you could think on this earlier!!! */
801
if (autolength > 0) {
802
//printf("tole je auto %d tole pa number %d\n", autolength, number);
803
packet[autolength] = (unsigned char)((number - (autolength + 2))/256);
804
packet[autolength+1] = (unsigned char)((number - (autolength + 2))%256);
809
else if (GTK_TOGGLE_BUTTON(ipv6)->active) {
810
error("Not yet implemented");
811
//printf("Not yet implemented\n");
814
else if (GTK_TOGGLE_BUTTON(arp)->active) {
815
/* we need to check the auto button, if it is on we copy the values from the
816
* arp filed entries in to the link layer source and destination mac entries
817
* the good thing about this is that we add a feature that can help
818
* (or confuse?) the bad thing about this is that we can't do it once
819
* with the link layer and take whatever the next layer is*/
820
if (GTK_TOGGLE_BUTTON(auto_bt)->active) {
821
GtkWidget *en_dst_mac, *en_src_mac, *L_dst, *L_src;
822
//gchar tmp_dst[18], tmp_src[18];
823
gchar *en_dst_t, *en_src_t;
825
en_dst_mac = lookup_widget(GTK_WIDGET(button), "A_targetmac");
826
en_src_mac = lookup_widget(GTK_WIDGET(button), "A_sendermac");
827
L_dst = lookup_widget(GTK_WIDGET(button), "L_dst_mac");
828
L_src = lookup_widget(GTK_WIDGET(button), "L_src_mac");
830
en_dst_t = (char *)gtk_entry_get_text(GTK_ENTRY(en_dst_mac));
831
en_src_t = (char *)gtk_entry_get_text(GTK_ENTRY(en_src_mac));
833
/*chech if both addresses are ok */
834
if (check_mac_address(en_dst_t) == -1) {
835
//printf("Error: wrong mac entry in arp destination field, can't copy it\n");
836
error("Error: wrong mac entry in arp destination field, can't copy it");
840
/*chech if both addresses are ok */
841
if (check_mac_address(en_src_t) == -1) {
842
//printf("Error: wrong mac entry in arp source field, can't copy it\n");
843
error("Error: wrong mac entry in arp source field, can't copy it");
847
/*insert it in the link layer field */
848
gtk_entry_set_text(GTK_ENTRY(L_dst), en_dst_t);
849
gtk_entry_set_text(GTK_ENTRY(L_src), en_src_t);
853
/* now we get the link layer info */
854
if (link_level_get(button, user_data) == -1) {
855
//printf("Error: problem on link layer with arp packet\n");
859
/* call the function that gets the arp protocol information */
860
if (arp_get(button, user_data) == -1) {
861
//printf("Error: problem with arp information\n");
865
if (autolength > 0) {
866
//printf("tole je auto %d tole pa number %d\n", autolength, number);
867
packet[autolength] = (unsigned char)((number - (autolength + 2))/256);
868
packet[autolength+1] = (unsigned char)((number - (autolength + 2))%256);
873
else if (GTK_TOGGLE_BUTTON(usedef)->active) {
874
/* if usedef is active we will manually get the link layer info */
875
if (link_level_get(button, user_data) == -1) {
876
//printf("Error: problem on link layer\n");
880
if((number == 14) || (number == 18))
882
else if ((number == 17) || (number == 21))
884
else if ((number == 22) || (number == 26))
887
//printf("Error: failure with number in layer 2 user defined field\n");
888
error("Error: failure with number in layer 2 user defined payload");
892
text_e = lookup_widget(GTK_WIDGET (button), "text1");
893
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_e));
894
length = gtk_text_buffer_get_char_count(buffer);
895
//text = (char *) malloc(length + 1);
896
GtkTextIter start,end;
897
//gtk_text_buffer_get_start_iter(buffer,start);
898
//gtk_text_buffer_get_end_iter(buffer,end);
899
gtk_text_buffer_get_bounds(buffer,&start,&end);
900
text = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
901
//text = gtk_editable_get_chars(GTK_EDITABLE(text_e), 0, -1);
905
if (get_network_payload(button, user_data, length, max, text) == -1) {
906
//printf("Error: problem with payload on network layer\n");
913
if (autolength > 0) {
914
//printf("tole je auto %d tole pa number %d\n", autolength, number);
915
packet[autolength] = (unsigned char)((number - (autolength + 2))/256);
916
packet[autolength+1] = (unsigned char)((number - (autolength + 2))%256);
919
else { /* none of above -> something is wrong! */
920
//printf("Error: problem with network layer button\n");
921
error("Error: problem with network layer button");
929
/* let's parse the IPv4 protokol information */
930
int ipv4_get(GtkButton *button, gpointer user_data) {
931
GtkWidget *version, *header_length, *tos, *total_length, *identification, *flags;
932
GtkWidget *frag_offset, *ttl, *protocol, *header_cks, *header_cks_bt;
933
GtkWidget *src_ip, *dst_ip, *options, *total_length_bt;
934
GtkWidget *udp_bt, *tcp_bt, *icmp_bt, *igmp_bt, *usedef_bt, *pay_text_e;
935
gchar *version_t, *header_length_t, *tos_t, *total_length_t, *identification_t, *flags_t;
936
gchar *frag_offset_t, *ttl_t, *protocol_t, *header_cks_t;
937
gchar *src_ip_t, *dst_ip_t, *options_t, *pay_text;
938
int length_start, header_cks_start, cks_start, cks_stop;
940
int i, j, pay_length, pay_max;
941
guint16 value, ipcksum;
942
guint32 pseudo_header_sum;
944
version = lookup_widget(GTK_WIDGET(button), "entry26");
945
header_length = lookup_widget(GTK_WIDGET(button), "entry27");
946
tos = lookup_widget(GTK_WIDGET(button), "entry28");
947
total_length = lookup_widget(GTK_WIDGET(button), "entry29");
948
total_length_bt = lookup_widget(GTK_WIDGET(button), "checkbutton21");
949
identification = lookup_widget(GTK_WIDGET(button), "entry30");
950
flags = lookup_widget(GTK_WIDGET(button), "entry31");
951
frag_offset = lookup_widget(GTK_WIDGET(button), "entry32");
952
ttl = lookup_widget(GTK_WIDGET(button), "entry44");
953
protocol = lookup_widget(GTK_WIDGET(button), "entry34");
954
header_cks = lookup_widget(GTK_WIDGET(button), "entry35");
955
header_cks_bt = lookup_widget(GTK_WIDGET(button), "ip_header_cks_cbt");
956
src_ip = lookup_widget(GTK_WIDGET(button), "entry38");
957
dst_ip = lookup_widget(GTK_WIDGET(button), "entry37");
958
options = lookup_widget(GTK_WIDGET(button), "entry39");
959
udp_bt = lookup_widget(GTK_WIDGET(button), "udp_bt");
960
tcp_bt = lookup_widget(GTK_WIDGET(button), "tcp_bt");
961
icmp_bt = lookup_widget(GTK_WIDGET(button), "icmp_bt");
962
igmp_bt = lookup_widget(GTK_WIDGET(button), "igmp_bt");
963
usedef_bt = lookup_widget(GTK_WIDGET(button), "ip_user_data_bt");
965
version_t = (char *)gtk_entry_get_text(GTK_ENTRY(version));
966
header_length_t = (char *)gtk_entry_get_text(GTK_ENTRY(header_length));
967
tos_t = (char *)gtk_entry_get_text(GTK_ENTRY(tos));
968
total_length_t = (char *)gtk_entry_get_text(GTK_ENTRY(total_length));
969
identification_t = (char *)gtk_entry_get_text(GTK_ENTRY(identification));
970
flags_t = (char *)gtk_entry_get_text(GTK_ENTRY(flags));
971
frag_offset_t = (char *)gtk_entry_get_text(GTK_ENTRY(frag_offset));
972
ttl_t = (char *)gtk_entry_get_text(GTK_ENTRY(ttl));
973
protocol_t = (char *)gtk_entry_get_text(GTK_ENTRY(protocol));
974
header_cks_t = (char *)gtk_entry_get_text(GTK_ENTRY(header_cks));
975
src_ip_t = (char *)gtk_entry_get_text(GTK_ENTRY(src_ip));
976
dst_ip_t = (char *)gtk_entry_get_text(GTK_ENTRY(dst_ip));
977
options_t = (char *)gtk_entry_get_text(GTK_ENTRY(options));
979
/* we want to know where the ip header starts, to calculate the checksum later */
982
/* now we have all the widgets, so let start parsing them */
983
/* starting with version */
984
strncpy(&tmp[0], version_t, 1);
985
strncpy(&tmp[1], header_length_t, 1);
987
if (char2x(tmp) == -1) {
988
//printf("Error: ipv4 version or header length field\n");
989
error("Error: ipv4 version or header length field");
992
packet[number] = (unsigned char)char2x(tmp);
996
if (char2x(tos_t) == -1) {
997
//printf("Error: ipv4 tos field\n");
998
error("Error: ipv4 tos field");
1001
packet[number] = (unsigned char)char2x(tos_t);
1005
/* if auto is on then we have to calculate this, but we can do this
1006
* at the end, when we have the whole ip packet together. so if auto
1007
* is enabled we set the marking and recaltulate it in the end */
1008
if (GTK_TOGGLE_BUTTON(total_length_bt)->active) {
1009
length_start = number;
1014
length_start = 0; /* if length start is 0, then we leave it in the end */
1015
if ( (atol(total_length_t) < 0) || (atol(total_length_t) > 65535) ) {
1016
//printf("Error: ipv4 total length range\n");
1017
error("Error: ipv4 total length range");
1021
/* there can be rubbish in this field */
1022
if (check_digit(total_length_t, strlen(total_length_t),
1023
"Error: ipv4 total length field values") == -1)
1026
packet[number] = (char)(atol(total_length_t)/256);
1028
packet[number] = (char)(atol(total_length_t)%256);
1032
/* identification */
1033
if (char2x(identification_t) == -1) {
1034
//printf("Error: ipv4 identification field\n");
1035
error("Error: ipv4 identification field");
1038
packet[number] = (unsigned char)char2x(identification_t);
1040
identification_t++; identification_t++;
1041
if (char2x(identification_t) == -1) {
1042
//printf("Error: ipv4 identification field\n");
1043
error("Error: ipv4 identification field");
1046
packet[number] = (unsigned char)char2x(identification_t);
1049
/* flags and fragment offset */
1050
if ( (atoi(flags_t) > 7) || (atoi(flags_t) < 0) ) {
1051
//printf("Error: ipv4 flags field: the value can be beetwen 0 and 7\n");
1052
error("Error: ipv4 flags field: the value can be beetwen 0 and 7");
1056
if ( (atoi(frag_offset_t) > 8191) || (atoi(frag_offset_t) < 0) ) {
1057
//printf("Error: ipv4 fragmentation offset field: (0 - 8191)\n");
1058
error("Error: ipv4 fragmentation offset field: (0 - 8191)");
1062
/* there can be rubbish in this field */
1063
if (check_digit(flags_t, strlen(flags_t), "Error: ipv4 flags values") == -1)
1066
/* there can be rubbish in this field */
1067
if (check_digit(frag_offset_t, strlen(frag_offset_t),
1068
"Error: ipv4 fragmentation offset field values ") == -1)
1071
/* this is the correct int value now
1072
* we need to store it as 2 byte hex value */
1073
value = (atoi(flags_t)<<13 & 0xE000) |
1074
(atoi(frag_offset_t) & 0x1FFF) ;
1076
/* YYY what about big endian computers - hope it works */
1077
value = htons(value);
1078
memcpy(&packet[number], &value, 2);
1083
if ( (atoi(ttl_t) < 0) || (atoi(ttl_t) > 255) ) {
1084
//printf("Error: ipv4 ttl range\n");
1085
error("Error: ipv4 ttl range");
1089
/* there can be rubbish in this field */
1090
if (check_digit(ttl_t, strlen(ttl_t), "Error: ipv4 ttl field values") == -1)
1093
packet[number] = (char)(atoi(ttl_t));
1096
/* protocol field */
1097
if ( (atoi(protocol_t) < 0) || (atoi(protocol_t) > 255) ) {
1098
//printf("Error: ipv4 protocol range\n");
1099
error("Error: ipv4 protocol range");
1103
/* there can be rubbish in this field */
1104
if (check_digit(protocol_t, strlen(protocol_t), "Error: ipv4 protocol field values") == -1)
1107
packet[number] = (char)(atoi(protocol_t));
1110
pseudo_header_sum = (guint32)(packet[number-1]);
1112
/* header checksum */
1113
/* if auto is on then we have to calculate this, but we can do this
1114
* at the end and recaltulate it for now we store the current number into
1115
* another variable. we will calculate length in the end */
1116
if (GTK_TOGGLE_BUTTON(header_cks_bt)->active) {
1117
header_cks_start = number;
1118
packet[number] = (unsigned char)0;
1120
packet[number] = (unsigned char)0;
1124
/* if header_cks_start = 0, we leave it in the end */
1125
header_cks_start = 0;
1126
if (char2x(header_cks_t) == -1) {
1127
//printf("Error: ipv4 header checksum field\n");
1128
error("Error: ipv4 header checksum field");
1131
packet[number] = (unsigned char)char2x(header_cks_t);
1132
header_cks_t++; header_cks_t++; number++;
1133
if (char2x(header_cks_t) == -1) {
1134
//printf("Error: ipv4 header checksum field\n");
1135
error("Error: ipv4 header checksum field");
1138
packet[number] = (unsigned char)char2x(header_cks_t);
1142
/* source ip address */
1145
if (check_ip_address(src_ip_t) == -1) {
1146
//printf("Error: Wrong source ipv4 address\n");
1147
error("Error: Wrong source ipv4 address");
1151
for (i=0; i<4; i++) {
1152
for(j=0; j<4 && (*src_ip_t != '\0'); j++) {
1153
if ( ((int)*src_ip_t == '.') && (i<3) && (j>0) ) {
1161
packet[number] = (unsigned char)(atoi(tmp));
1165
/* destination ip address */
1166
if (check_ip_address(dst_ip_t) == -1) {
1167
//printf("Error: Wrong destination ipv4 address\n");
1168
error("Error: Wrong destination ipv4 address");
1172
for (i=0; i<4; i++) {
1173
for(j=0; j<4 && (*dst_ip_t != '\0'); j++) {
1174
if ( ((int)*dst_ip_t == '.') && (i<3) && (j>0) ) {
1182
packet[number] = (unsigned char)(atoi(tmp));
1186
/* this is checksum for protocol field plus IP source and destination
1187
* we need this later when we want to calculate the TCP or UDP checksum
1188
* there we need this values so we pass them when calling the routine*/
1189
pseudo_header_sum = pseudo_header_sum + get_checksum32(number-8, number-1);
1191
/* options? do allow then and how long can they be???
1192
* ok we allow them and limit them to 40 bytes and the user should
1193
* care that the options length is always a multiple of 32 bits (4 bytes) */
1194
if ( (strlen(options_t)%8) != 0) {
1195
//printf("Error: Wrong ipv4 length of options field (length mod 8 must be 0)\n");
1196
error("Error: Wrong ipv4 length of options field \n(length mod 8 must be 0)");
1200
if ( strlen(options_t) > 80) {
1201
//printf("Error: ipv4 options field to long\n");
1202
error("Error: ipv4 options field to long");
1206
j = strlen(options_t)/2;
1207
for (i=0; i<j; i++) {
1208
if (char2x(options_t) == -1) {
1209
//printf("Error: ipv4 options field\n");
1210
error("Error: ipv4 options field");
1213
packet[number] = (unsigned char)char2x(options_t);
1214
number++; options_t++; options_t++;
1219
/* so we came to the end of ip header. what is next? */
1220
/* tcp, udp, icmp or manually attached payload? */
1221
if (GTK_TOGGLE_BUTTON(udp_bt)->active) {
1222
if (udp_get(button, user_data, pseudo_header_sum) == -1) {
1223
//printf("Error: Problem with UDP information\n");
1228
else if (GTK_TOGGLE_BUTTON(tcp_bt)->active) {
1229
if (tcp_get(button, user_data, pseudo_header_sum) == -1) {
1230
//printf("Error: Problem with TCP information\n");
1235
else if (GTK_TOGGLE_BUTTON(icmp_bt)->active) {
1236
if (icmp_get(button, user_data) == -1) {
1237
//printf("Error: Problem with ICMP information\n");
1242
else if (GTK_TOGGLE_BUTTON(igmp_bt)->active) {
1243
if (igmp_get(button, user_data) == -1) {
1244
//printf("Error: Problem with IGMP information\n");
1249
else if (GTK_TOGGLE_BUTTON(usedef_bt)->active) {
1251
pay_text_e = lookup_widget(GTK_WIDGET (button), "text2");
1252
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pay_text_e));
1253
pay_length = gtk_text_buffer_get_char_count(buffer);
1254
GtkTextIter start,end;
1255
//gtk_text_buffer_get_start_iter(buffer,start);
1256
//gtk_text_buffer_get_end_iter(buffer,end);
1257
gtk_text_buffer_get_bounds(buffer,&start,&end);
1258
pay_text = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
1261
//pay_text = (char *) malloc(pay_length + 1);
1262
//pay_text = gtk_editable_get_chars(GTK_EDITABLE(pay_text_e), 0, -1);
1264
/* YYY 1514-number is not ok in case we use 802.1q!!! */
1265
pay_max = 1514 - number;
1267
if (get_network_payload(button, user_data, pay_length, pay_max, pay_text) == -1) {
1268
//printf("Error: Problem with IPv4 payload\n");
1277
//printf("Error: IPv4 zoombie error!!!\n");
1278
error("Error: IPv4 zoombie error!!!");
1283
/* so we are back again to cumpute the length and checksum. so this is for length */
1284
if (length_start > 0) {
1285
packet[length_start] = (char)((number - length_start + 2)/256);
1286
packet[length_start+1] = (char)((number - length_start + 2)%256);
1289
/* and this for checksum */
1290
if (header_cks_start > 0) {
1291
ipcksum = ((-1) - get_checksum16(cks_start, cks_stop) % 0x10000);
1292
packet[header_cks_start] = (char)(ipcksum/256);
1293
packet[header_cks_start+1] = (char)(ipcksum%256);
1300
int udp_get(GtkButton *button, gpointer user_data, guint32 pseudo_header_sum)
1303
GtkWidget *srcport, *dstport, *length, *length_bt, *checksum, *checksum_bt;
1304
GtkWidget *payload_bt, *payload;
1306
gchar *srcport_t, *dstport_t, *length_t, *checksum_t, *payload_t;
1308
int length_start, checksum_start, cks_start, cks_stop, payload_length, odd=0;
1311
srcport = lookup_widget(GTK_WIDGET(button), "entry56");
1312
dstport = lookup_widget(GTK_WIDGET(button), "entry41");
1313
length = lookup_widget(GTK_WIDGET(button), "entry42");
1314
length_bt = lookup_widget(GTK_WIDGET(button), "checkbutton3");
1315
checksum = lookup_widget(GTK_WIDGET(button), "entry43");
1316
checksum_bt = lookup_widget(GTK_WIDGET(button), "checkbutton4");
1317
payload = lookup_widget(GTK_WIDGET(button), "text3");
1318
payload_bt = lookup_widget(GTK_WIDGET(button), "checkbutton5");
1320
srcport_t= (char *)gtk_entry_get_text(GTK_ENTRY(srcport));
1321
dstport_t= (char *)gtk_entry_get_text(GTK_ENTRY(dstport));
1322
length_t= (char *)gtk_entry_get_text(GTK_ENTRY(length));
1323
checksum_t= (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
1328
if ( (atoi(srcport_t) < 0) || (atoi(srcport_t) > 65535) ) {
1329
//printf("Error: Udp source port range\n");
1330
error("Error: Udp source port range");
1334
/* there can be rubbish in this field */
1335
if (check_digit(srcport_t, strlen(srcport_t), "Error: Udp srcport field values") == -1)
1338
packet[number] = (char)(atol(srcport_t)/256);
1340
packet[number] = (char)(atol(srcport_t)%256);
1343
/* destination port */
1344
if ( (atoi(dstport_t) < 0) || (atoi(dstport_t) > 65535) ) {
1345
//printf("Error: Udp destination port range\n");
1346
error("Error: Udp destination port range");
1350
/* there can be rubbish in this field */
1351
if (check_digit(dstport_t, strlen(dstport_t), "Error: Udp destination port field values") == -1)
1354
packet[number] = (char)(atol(dstport_t)/256);
1356
packet[number] = (char)(atol(dstport_t)%256);
1360
if (GTK_TOGGLE_BUTTON(length_bt)->active) {
1361
length_start = number;
1366
/* if length_start = 0, we leave it in the end */
1368
if ( (atoi(length_t) < 0) || (atoi(length_t) > 65535) ) {
1369
//printf("Error: Udp length range\n");
1370
error("Error: Udp length range");
1374
/* there can be rubbish in this field */
1375
if (check_digit(length_t, strlen(length_t), "Error: Udp length field values") == -1)
1378
packet[number] = (char)(atol(length_t)/256);
1380
packet[number] = (char)(atol(length_t)%256);
1385
if (GTK_TOGGLE_BUTTON(checksum_bt)->active) {
1386
checksum_start = number;
1387
packet[number] = (unsigned char)0;
1389
packet[number] = (unsigned char)0;
1393
/* if checksum_start = 0, we leave it in the end */
1396
if (char2x(checksum_t) == -1) {
1397
//printf("Error: udp checksum field\n");
1398
error("Error: udp checksum field");
1401
packet[number] = (unsigned char)char2x(checksum_t);
1402
checksum_t++; checksum_t++; number++;
1403
if (char2x(checksum_t) == -1) {
1404
//printf("Error: udp checksum field\n");
1405
error("Error: udp checksum field");
1408
packet[number] = (unsigned char)char2x(checksum_t);
1412
/* we need this one for knowing where the udp payload starts
1413
* we need this one when sending the packets out and modifing some values */
1417
/* so do we allow packet's longer than 1518 (1522) bytes or not ? Not.*/
1418
if (GTK_TOGGLE_BUTTON(payload_bt)->active) {
1420
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(payload));
1421
payload_length = gtk_text_buffer_get_char_count(buffer);
1422
GtkTextIter start,end;
1423
gtk_text_buffer_get_bounds(buffer,&start,&end);
1424
//gtk_text_buffer_get_start_iter(buffer,&start);
1425
//gtk_text_buffer_get_end_iter(buffer,&end);
1426
payload_t = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
1429
//payload_t = (char *) malloc(payload_length + 1);
1430
//payload_t = gtk_editable_get_chars(GTK_EDITABLE(payload),0,-1);
1432
/* YYY 1514-number is not ok in case we use 802.1q!!! */
1433
if (get_network_payload(button, user_data, payload_length,
1434
1514-number, payload_t) == -1) {
1435
//printf("Error: Problem with udp payload\n");
1447
/* it will be possible that we add some other protocols on top of udp
1448
* they will follow here... */
1451
/* we have to fill the corect udp length if auto was enabled
1452
* we add 2 bytes for source port and 2 bytes for dest port
1453
* because length_start points at udp length field */
1454
if (length_start > 0) {
1455
packet[length_start] = (char)((number - length_start + 4)/256);
1456
packet[length_start+1] = (char)((number - length_start + 4)%256);
1459
/* and finally compute the udp checksum if auto was enabled */
1460
if (checksum_start > 0) {
1461
/* if the user manually inserts the length value what then??? */
1462
/* we don't care it that means, if you manually insert the length
1463
* than the auto checksum button won't help you
1464
* it would be better if the value would be correct either */
1466
/* this if for udp length */
1467
udpcksum = (guint32)(cks_stop - cks_start);
1468
/* pseudo header (ip part) + udplength + nr of cicles over guint16 */
1469
udpcksum = pseudo_header_sum + udpcksum;
1470
/* if the length is odd we have to add a pad byte */
1471
if( (cks_stop - cks_start)%2 != 0)
1473
/* previos value + part from udp checksum */
1474
udpcksum = udpcksum + get_checksum32(cks_start, cks_stop+odd);
1475
while (udpcksum >> 16)
1476
udpcksum = (udpcksum & 0xFFFF)+ (udpcksum >> 16);
1477
/* the one's complement */
1478
udpcksum = (-1) - udpcksum;
1480
/* let's write it */
1481
packet[checksum_start] = (char)(udpcksum/256);
1482
packet[checksum_start+1] = (char)(udpcksum%256);
1488
int tcp_get(GtkButton *button, gpointer user_data, guint32 pseudo_header_sum) {
1490
GtkWidget *srcport, *dstport, *sequence_number, *ack_number, *header_length;
1491
GtkWidget *flag_cwr, *flag_ecn;
1492
GtkWidget *flag_urg, *flag_ack, *flag_psh, *flag_rst, *flag_syn, *flag_fin;
1493
GtkWidget *window_size, *checksum, *checksum_bt, *urgent_pointer, *options;
1494
GtkWidget *payload_bt, *payload;
1496
gchar *srcport_t, *dstport_t, *sequence_number_t, *ack_number_t, *header_length_t;
1497
gchar *window_size_t, *checksum_t, *urgent_pointer_t, *options_t, *payload_t;
1499
int checksum_start, cks_start, cks_stop, i, j, payload_length, odd=0;
1501
guint32 seqnr, acknr;
1504
srcport = lookup_widget(GTK_WIDGET(button), "entry46");
1505
dstport = lookup_widget(GTK_WIDGET(button), "entry47");
1506
sequence_number = lookup_widget(GTK_WIDGET(button), "entry48");
1507
ack_number = lookup_widget(GTK_WIDGET(button), "entry49");
1508
header_length = lookup_widget(GTK_WIDGET(button), "entry50");
1509
flag_cwr = lookup_widget(GTK_WIDGET(button), "checkbutton22");
1510
flag_ecn = lookup_widget(GTK_WIDGET(button), "checkbutton23");
1511
flag_urg = lookup_widget(GTK_WIDGET(button), "checkbutton7");
1512
flag_ack = lookup_widget(GTK_WIDGET(button), "checkbutton8");
1513
flag_psh = lookup_widget(GTK_WIDGET(button), "checkbutton9");
1514
flag_rst = lookup_widget(GTK_WIDGET(button), "checkbutton10");
1515
flag_syn = lookup_widget(GTK_WIDGET(button), "checkbutton11");
1516
flag_fin = lookup_widget(GTK_WIDGET(button), "checkbutton12");
1517
window_size = lookup_widget(GTK_WIDGET(button), "entry51");
1518
checksum = lookup_widget(GTK_WIDGET(button), "entry52");
1519
checksum_bt = lookup_widget(GTK_WIDGET(button), "checkbutton13");
1520
urgent_pointer = lookup_widget(GTK_WIDGET(button), "entry53");
1521
options = lookup_widget(GTK_WIDGET(button), "entry54");
1522
payload_bt = lookup_widget(GTK_WIDGET(button), "checkbutton14");
1523
payload = lookup_widget(GTK_WIDGET(button), "text4");
1525
srcport_t= (char *)gtk_entry_get_text(GTK_ENTRY(srcport));
1526
dstport_t= (char *)gtk_entry_get_text(GTK_ENTRY(dstport));
1527
sequence_number_t = (char *)gtk_entry_get_text(GTK_ENTRY(sequence_number));
1528
ack_number_t = (char *)gtk_entry_get_text(GTK_ENTRY(ack_number));
1529
header_length_t = (char *)gtk_entry_get_text(GTK_ENTRY(header_length));
1530
window_size_t = (char *)gtk_entry_get_text(GTK_ENTRY(window_size));
1531
checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
1532
urgent_pointer_t = (char *)gtk_entry_get_text(GTK_ENTRY(urgent_pointer));
1533
options_t = (char *)gtk_entry_get_text(GTK_ENTRY(options));
1537
/* we need this one for knowing where the tcp part starts
1538
* we need this one when sending the packets out and modifing some values */
1542
if ( (atoi(srcport_t) < 0) || (atoi(srcport_t) > 65535) ) {
1543
//printf("Error: tcp source port range\n");
1544
error("Error: tcp source port range");
1548
/* there can be rubbish in this field */
1549
if (check_digit(srcport_t, strlen(srcport_t), "Error: tcp srcport field values") == -1)
1552
packet[number] = (char)(atol(srcport_t)/256);
1554
packet[number] = (char)(atol(srcport_t)%256);
1557
/* destination port */
1558
if ( (atoi(dstport_t) < 0) || (atoi(dstport_t) > 65535) ) {
1559
//printf("Error: tcp destination port range\n");
1560
error("Error: tcp destination port range");
1564
/* there can be rubbish in this field */
1565
if (check_digit(dstport_t, strlen(dstport_t), "Error: tcp destination port field values") == -1)
1568
packet[number] = (char)(atol(dstport_t)/256);
1570
packet[number] = (char)(atol(dstport_t)%256);
1573
/* sequence number */
1574
if ( strtoull(sequence_number_t, (char **)NULL, 10) > 0xFFFFFFFF ) {
1575
//printf("Error: tcp sequence number range\n");
1576
error("Error: tcp sequence number range");
1580
/* there can be rubbish in this field */
1581
if (check_digit(sequence_number_t, strlen(sequence_number_t),
1582
"Error: tcp sequence number field values") == -1)
1585
seqnr = strtoul(sequence_number_t, (char **)NULL, 10);
1586
packet[number] = (char)(seqnr/16777216);
1588
packet[number] = (char)(seqnr/65536);
1590
packet[number] = (char)(seqnr/256);
1592
packet[number] = (char)(seqnr%256);
1595
/* acknowledgment number */
1596
if ( strtoull(ack_number_t, (char **)NULL, 10) > 0xFFFFFFFF) {
1597
//printf("Error: tcp ack number range\n");
1598
error("Error: tcp ack number range");
1602
/* there can be rubbish in this field */
1603
if (check_digit(ack_number_t, strlen(ack_number_t), "Error: tcp ack number field values") == -1)
1606
acknr = strtoul(ack_number_t, (char **)NULL, 10);
1607
packet[number] = (char)(acknr/16777216);
1609
packet[number] = (char)(acknr/65536);
1611
packet[number] = (char)(acknr/256);
1613
packet[number] = (char)(acknr%256);
1617
if ( (atoi(header_length_t) < 0) || (atoi(header_length_t) > 60) ) {
1618
//printf("Error: tcp header_length range\n");
1619
error("Error: tcp header_length range");
1623
/* since we insert value as int, when dividing it with 4 there must remain 0 */
1624
if ( atoi(header_length_t) % 4 != 0) {
1625
//printf("Error: tcp header_length range\n");
1626
error("Error: Wrong tcp header length value \n(length mod 4 must be 0)");
1630
/* there can be rubbish in this field */
1631
if (check_digit(header_length_t, strlen(header_length_t),
1632
"Error: tcp header_length field values") == -1)
1635
packet[number] = (char)((atoi(header_length_t)*4));
1639
if (GTK_TOGGLE_BUTTON(flag_cwr)->active) {
1640
flag_value = flag_value + 128;
1642
if (GTK_TOGGLE_BUTTON(flag_ecn)->active) {
1643
flag_value = flag_value + 64;
1645
if (GTK_TOGGLE_BUTTON(flag_urg)->active) {
1646
flag_value = flag_value + 32;
1648
if (GTK_TOGGLE_BUTTON(flag_ack)->active) {
1649
flag_value = flag_value + 16;
1651
if (GTK_TOGGLE_BUTTON(flag_psh)->active) {
1652
flag_value = flag_value + 8;
1654
if (GTK_TOGGLE_BUTTON(flag_rst)->active) {
1655
flag_value = flag_value + 4;
1657
if (GTK_TOGGLE_BUTTON(flag_syn)->active) {
1658
flag_value = flag_value + 2;
1660
if (GTK_TOGGLE_BUTTON(flag_fin)->active) {
1661
flag_value = flag_value + 1;
1663
packet[number] = (char)flag_value;
1667
if ( (atoi(window_size_t) < 0) || (atoi(window_size_t) > 65535) ) {
1668
//printf("Error: tcp window size range\n");
1669
error("Error: tcp window size range");
1673
/* there can be rubbish in this field */
1674
if (check_digit(window_size_t, strlen(window_size_t), "Error: tcp window size field values") == -1)
1677
packet[number] = (char)(atol(window_size_t)/256);
1679
packet[number] = (char)(atol(window_size_t)%256);
1683
if (GTK_TOGGLE_BUTTON(checksum_bt)->active) {
1684
checksum_start = number;
1685
packet[number] = (unsigned char)0;
1687
packet[number] = (unsigned char)0;
1691
/* if checksum_start = 0, we leave it in the end */
1694
if (char2x(checksum_t) == -1) {
1695
//printf("Error: tcp checksum field\n");
1696
error("Error: tcp checksum field");
1699
packet[number] = (unsigned char)char2x(checksum_t);
1700
checksum_t++; checksum_t++; number++;
1701
if (char2x(checksum_t) == -1) {
1702
//printf("Error: tcp checksum field\n");
1703
error("Error: tcp checksum field");
1706
packet[number] = (unsigned char)char2x(checksum_t);
1710
/* urgent pointer */
1711
if ( (atoi(urgent_pointer_t) < 0) || (atoi(urgent_pointer_t) > 65535) ) {
1712
//printf("Error: tcp urgent pointer range\n");
1713
error("Error: tcp urgent pointer range");
1717
/* there can be rubbish in this field */
1718
if (check_digit(urgent_pointer_t, strlen(urgent_pointer_t),
1719
"Error: tcp urgent pointer field values") == -1)
1722
packet[number] = (char)(atol(urgent_pointer_t)/256);
1724
packet[number] = (char)(atol(urgent_pointer_t)%256);
1728
if ( (strlen(options_t)%8) != 0) {
1729
//printf("Error: Wrong length of tcp options field (length mod 8 must be 0)\n");
1730
error("Error: Wrong length of tcp options field \n(length mod 8 must be 0)");
1734
if ( strlen(options_t) > 80) {
1735
//printf("Error: tcp options field to long\n");
1736
error("Error: tcp options field to long");
1740
j = strlen(options_t)/2;
1741
for (i=0; i<j; i++) {
1742
if (char2x(options_t) == -1) {
1743
//printf("Error: tcp options field\n");
1744
error("Error: tcp options field");
1747
packet[number] = (unsigned char)char2x(options_t);
1748
number++; options_t++; options_t++;
1752
if (GTK_TOGGLE_BUTTON(payload_bt)->active) {
1754
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(payload));
1755
payload_length = gtk_text_buffer_get_char_count(buffer);
1756
GtkTextIter start,end;
1757
//gtk_text_buffer_get_start_iter(buffer,start);
1758
//gtk_text_buffer_get_end_iter(buffer,end);
1759
gtk_text_buffer_get_bounds(buffer,&start,&end);
1760
payload_t = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
1763
//payload_t = (char *) malloc(payload_length + 1);
1764
//payload_t = gtk_editable_get_chars(GTK_EDITABLE(payload),0,-1);
1766
/* YYY 1514-number is not ok in case we use 802.1q!!! */
1767
if (get_network_payload(button, user_data, payload_length,
1768
1514-number, payload_t) == -1) {
1769
//printf("Error: Problem with tcp payload\n");
1781
/* it will be possible that we add some other protocols on top of tcp
1782
* they will follow here... */
1785
/* and finally compute the tcp checksum if auto was enabled */
1786
if (checksum_start > 0) {
1788
/* this if for length */
1789
tcpcksum = (guint32)(cks_stop - cks_start);
1790
/* pseudo header (ip part) + tcplength + nr of cicles over guint16 */
1791
tcpcksum = pseudo_header_sum + tcpcksum;
1792
/* if length is odd we have to add a pad byte */
1793
if( (cks_stop - cks_start)%2 != 0)
1795
/* previos value + part from tcp checksum */
1796
tcpcksum = tcpcksum + get_checksum32(cks_start, cks_stop+odd);
1797
while (tcpcksum >> 16)
1798
tcpcksum = (tcpcksum & 0xFFFF) + (tcpcksum >> 16);
1799
/* the one's complement */
1800
tcpcksum = (-1) - tcpcksum;
1802
/* what about if the lenght is odd ???
1803
* we check this in get_checksum routine */
1805
/* let's write it */
1806
packet[checksum_start] = (char)(tcpcksum/256);
1807
packet[checksum_start+1] = (char)(tcpcksum%256);
1813
int igmp_get(GtkButton *button, gpointer user_data) {
1815
GtkWidget *type, *menux, *menu, *menu_item;
1817
GtkWidget *maxresptime, *checksum, *cks_bt, *groupaddress, *resv, *nosf, *sourceaddresses;
1820
gchar *maxresptime_t, *checksum_t, *groupaddress_t;
1821
gchar *resv_t, *nosf_t, *sourceaddresses_t;
1823
int igmp_start, igmp_stop, checksum_start, payload_length;
1826
int i, j, menu_index;
1828
type = lookup_widget(GTK_WIDGET(button), "entry166");
1829
type_t = (char *)gtk_entry_get_text(GTK_ENTRY(type));
1831
igmp_start = number;
1834
if (char2x(type_t) == -1) {
1835
//printf("Error: igmp type field\n");
1836
error("Error: igmp type field");
1839
packet[number] = (unsigned char)char2x(type_t);
1842
maxresptime = lookup_widget(GTK_WIDGET(button), "entry167");
1843
menux = lookup_widget(GTK_WIDGET(button), "optionmenu20");
1844
checksum = lookup_widget(GTK_WIDGET(button), "entry168");
1845
cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton41");
1847
maxresptime_t = (char *)gtk_entry_get_text(GTK_ENTRY(maxresptime));
1848
checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
1849
/*gcc warning otherwise*/
1850
groupaddress = lookup_widget(GTK_WIDGET(button), "entry169");
1852
/* igmp max response time */
1853
if (char2x(maxresptime_t) == -1) {
1854
//printf("Error: igmp max response time\n");
1855
error("Error: igmp max response time");
1858
packet[number] = (unsigned char)char2x(maxresptime_t);
1862
if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
1863
checksum_start = number;
1864
packet[number] = (unsigned char)0;
1866
packet[number] = (unsigned char)0;
1870
/* if checksum_start = 0, we leave it in the end */
1873
if (char2x(checksum_t) == -1) {
1874
//printf("Error: igmp reply checksum field\n");
1875
error("Error: igmp checksum field");
1878
packet[number] = (unsigned char)char2x(checksum_t);
1879
checksum_t++; checksum_t++; number++;
1880
if (char2x(checksum_t) == -1) {
1881
//printf("Error: igmp reply checksum field\n");
1882
error("Error: igmp checksum field");
1885
packet[number] = (unsigned char)char2x(checksum_t);
1889
menux = lookup_widget(GTK_WIDGET(button), "optionmenu20");
1891
menu = GTK_OPTION_MENU(menux)->menu;
1892
menu_item = gtk_menu_get_active (GTK_MENU (menu));
1893
menu_index = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
1896
if (menu_index == 1) {
1898
groupaddress = lookup_widget(GTK_WIDGET(button), "entry169");
1899
groupaddress_t = (char *)gtk_entry_get_text(GTK_ENTRY(groupaddress));
1900
if (check_ip_address(groupaddress_t) == -1) {
1901
//printf("Error: Wrong igmp address\n");
1902
error("Error: Wrong igmp group address");
1906
for (i=0; i<4; i++) {
1907
for(j=0; j<4 && (*groupaddress_t != '\0'); j++) {
1908
if ( ((int)*groupaddress_t == '.') && (i<3) && (j>0) ) {
1912
tmp[j] = *groupaddress_t;
1916
packet[number] = (unsigned char)(atoi(tmp));
1920
resv = lookup_widget(GTK_WIDGET(button), "entry171");
1921
nosf = lookup_widget(GTK_WIDGET(button), "entry172");
1922
sourceaddresses = lookup_widget(GTK_WIDGET(button), "entry173");
1924
resv_t = (char *)gtk_entry_get_text(GTK_ENTRY(resv));
1925
nosf_t = (char *)gtk_entry_get_text(GTK_ENTRY(nosf));
1926
sourceaddresses_t = (char *)gtk_entry_get_text(GTK_ENTRY(sourceaddresses));
1928
/* Resv, S, QRV, QQIC IGMP V3 values */
1929
if (char2x(resv_t) == -1) {
1930
//printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
1931
error("Error: Resv, S, QRV, QQIC IGMP V3 values");
1934
packet[number] = (unsigned char)char2x(resv_t);
1935
resv_t++; resv_t++; number++;
1936
if (char2x(resv_t) == -1) {
1937
//printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
1938
error("Error: Resv, S, QRV, QQIC IGMP V3 values");
1941
packet[number] = (unsigned char)char2x(resv_t);
1944
/* number of sources */
1945
if (char2x(nosf_t) == -1) {
1946
//printf("Error: IGMP V3 number of sources\n");
1947
error("Error: IGMP V3 number of sources");
1950
packet[number] = (unsigned char)char2x(nosf_t);
1951
nosf_t++; nosf_t++; number++;
1952
if (char2x(nosf_t) == -1) {
1953
//printf("Error: IGMP V3 number of sources\n");
1954
error("Error: IGMP V3 number of sources");
1957
packet[number] = (unsigned char)char2x(nosf_t);
1960
/* source addresses */
1961
payload_length = strlen(sourceaddresses_t);
1963
/* YYY 1514-number is not ok in case we use 802.1q!!! */
1964
if (get_network_payload(button, user_data, payload_length,
1965
1514-number, sourceaddresses_t) == -1) {
1966
//printf("problem with igmp reply payload\n");
1972
if (checksum_start > 0) {
1974
igmpcksum = get_checksum16(igmp_start, igmp_stop);
1975
/* the one's complement */
1976
igmpcksum = (-1) - igmpcksum;
1978
/* let's write it */
1979
packet[checksum_start] = (char)(igmpcksum/256);
1980
packet[checksum_start+1] = (char)(igmpcksum%256);
1984
/* IGMP V3 report */
1985
else if (menu_index == 4) {
1986
resv = lookup_widget(GTK_WIDGET(button), "entry176");
1987
nosf = lookup_widget(GTK_WIDGET(button), "entry177");
1988
sourceaddresses = lookup_widget(GTK_WIDGET(button), "entry178");
1990
resv_t = (char *)gtk_entry_get_text(GTK_ENTRY(resv));
1991
nosf_t = (char *)gtk_entry_get_text(GTK_ENTRY(nosf));
1992
sourceaddresses_t = (char *)gtk_entry_get_text(GTK_ENTRY(sourceaddresses));
1995
if (char2x(resv_t) == -1) {
1996
//printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
1997
error("Error: Reserved IGMP V3 report values");
2000
packet[number] = (unsigned char)char2x(resv_t);
2001
resv_t++; resv_t++; number++;
2002
if (char2x(resv_t) == -1) {
2003
//printf("Error: Resv, S, QRV, QQIC IGMP V3 values\n");
2004
error("Error: Reserved IGMP V3 report values");
2007
packet[number] = (unsigned char)char2x(resv_t);
2010
/* number of group records */
2011
if (char2x(nosf_t) == -1) {
2012
//printf("Error: IGMP V3 number of sources\n");
2013
error("Error: IGMP V3 report number of group records");
2016
packet[number] = (unsigned char)char2x(nosf_t);
2017
nosf_t++; nosf_t++; number++;
2018
if (char2x(nosf_t) == -1) {
2019
//printf("Error: IGMP V3 number of sources\n");
2020
error("Error: IGMP V3 report number of group records");
2023
packet[number] = (unsigned char)char2x(nosf_t);
2027
payload_length = strlen(sourceaddresses_t);
2029
/* YYY 1514-number is not ok in case we use 802.1q!!! */
2030
if (get_network_payload(button, user_data, payload_length,
2031
1514-number, sourceaddresses_t) == -1) {
2032
//printf("problem with igmp reply payload\n");
2038
if (checksum_start > 0) {
2040
igmpcksum = get_checksum16(igmp_start, igmp_stop);
2041
/* the one's complement */
2042
igmpcksum = (-1) - igmpcksum;
2044
/* let's write it */
2045
packet[checksum_start] = (char)(igmpcksum/256);
2046
packet[checksum_start+1] = (char)(igmpcksum%256);
2051
/* for all the other types */
2054
groupaddress = lookup_widget(GTK_WIDGET(button), "entry175");
2055
groupaddress_t = (char *)gtk_entry_get_text(GTK_ENTRY(groupaddress));
2056
if (check_ip_address(groupaddress_t) == -1) {
2057
//printf("Error: Wrong igmp address\n");
2058
error("Error: Wrong igmp group address");
2062
for (i=0; i<4; i++) {
2063
for(j=0; j<4 && (*groupaddress_t != '\0'); j++) {
2064
if ( ((int)*groupaddress_t == '.') && (i<3) && (j>0) ) {
2068
tmp[j] = *groupaddress_t;
2072
packet[number] = (unsigned char)(atoi(tmp));
2078
if (checksum_start > 0) {
2080
igmpcksum = get_checksum16(igmp_start, igmp_stop);
2081
/* the one's complement */
2082
igmpcksum = (-1) - igmpcksum;
2084
/* let's write it */
2085
packet[checksum_start] = (char)(igmpcksum/256);
2086
packet[checksum_start+1] = (char)(igmpcksum%256);
2093
int icmp_get(GtkButton *button, gpointer user_data) {
2096
GtkWidget *code, *checksum, *cks_bt, *identifier, *seq_nr, *unused;
2097
GtkWidget *data_bt, *data;
2100
gchar *code_t, *checksum_t, *identifier_t, *seq_nr_t, *data_t, *unused_t;
2102
int icmp_start, icmp_stop, checksum_start, payload_length;
2105
type = lookup_widget(GTK_WIDGET(button), "entry57");
2106
type_t = (char *)gtk_entry_get_text(GTK_ENTRY(type));
2108
icmp_start = number;
2111
if (char2x(type_t) == -1) {
2112
//printf("Error: icmp type field\n");
2113
error("Error: icmp type field");
2116
packet[number] = (unsigned char)char2x(type_t);
2119
/* YYY hmmm, it would be better to be the type filed inserted as int
2120
* not as hex, so we have to calculate it */
2121
switch (atoi(type_t)) {
2123
//printf("ICMP echo reply\n");
2124
code = lookup_widget(GTK_WIDGET(button), "entry62");
2125
checksum = lookup_widget(GTK_WIDGET(button), "entry63");
2126
cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton16");
2127
identifier = lookup_widget(GTK_WIDGET(button), "entry64");
2128
seq_nr = lookup_widget(GTK_WIDGET(button), "entry65");
2129
data_bt = lookup_widget(GTK_WIDGET(button), "checkbutton17");
2130
data = lookup_widget(GTK_WIDGET(button), "entry66");
2132
code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
2133
checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
2134
identifier_t = (char *)gtk_entry_get_text(GTK_ENTRY(identifier));
2135
seq_nr_t = (char *)gtk_entry_get_text(GTK_ENTRY(seq_nr));
2136
data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
2139
if (char2x(code_t) == -1) {
2140
//printf("Error: icmp reply code field\n");
2141
error("Error: icmp reply code field");
2144
packet[number] = (unsigned char)char2x(code_t);
2148
if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
2149
checksum_start = number;
2150
packet[number] = (unsigned char)0;
2152
packet[number] = (unsigned char)0;
2156
/* if checksum_start = 0, we leave it in the end */
2159
if (char2x(checksum_t) == -1) {
2160
//printf("Error: icmp reply checksum field\n");
2161
error("Error: icmp reply checksum field");
2164
packet[number] = (unsigned char)char2x(checksum_t);
2165
checksum_t++; checksum_t++; number++;
2166
if (char2x(checksum_t) == -1) {
2167
//printf("Error: icmp reply checksum field\n");
2168
error("Error: icmp reply checksum field");
2171
packet[number] = (unsigned char)char2x(checksum_t);
2176
if (char2x(identifier_t) == -1) {
2177
//printf("Error: icmp reply identifier field\n");
2178
error("Error: icmp reply identifier field");
2181
packet[number] = (unsigned char)char2x(identifier_t);
2182
identifier_t++; identifier_t++; number++;
2183
if (char2x(identifier_t) == -1) {
2184
//printf("Error: icmp reply identifier field\n");
2185
error("Error: icmp reply identifier field");
2188
packet[number] = (unsigned char)char2x(identifier_t);
2191
/* sequence number */
2192
if (char2x(seq_nr_t) == -1) {
2193
//printf("Error: icmp reply identifier field\n");
2194
error("Error: icmp reply identifier field");
2197
packet[number] = (unsigned char)char2x(seq_nr_t);
2198
seq_nr_t++; seq_nr_t++; number++;
2199
if (char2x(seq_nr_t) == -1) {
2200
//printf("Error: icmp reply identifier field\n");
2201
error("Error: icmp reply identifier field");
2204
packet[number] = (unsigned char)char2x(seq_nr_t);
2208
if (GTK_TOGGLE_BUTTON(data_bt)->active) {
2210
payload_length = strlen(data_t);
2212
/* YYY 1514-number is not ok in case we use 802.1q!!! */
2213
if (get_network_payload(button, user_data, payload_length,
2214
1514-number, data_t) == -1) {
2215
//printf("problem with icmp reply payload\n");
2224
if (checksum_start > 0) {
2226
icmpcksum = get_checksum16(icmp_start, icmp_stop);
2227
/* the one's complement */
2228
icmpcksum = (-1) - icmpcksum;
2230
/* let's write it */
2231
packet[checksum_start] = (char)(icmpcksum/256);
2232
packet[checksum_start+1] = (char)(icmpcksum%256);
2238
/* icmp echo request */
2240
//printf("ICMP echo request\n");
2241
code = lookup_widget(GTK_WIDGET(button), "entry74");
2242
checksum = lookup_widget(GTK_WIDGET(button), "entry77");
2243
cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton20");
2244
identifier = lookup_widget(GTK_WIDGET(button), "entry75");
2245
seq_nr = lookup_widget(GTK_WIDGET(button), "entry78");
2246
data_bt = lookup_widget(GTK_WIDGET(button), "checkbutton19");
2247
data = lookup_widget(GTK_WIDGET(button), "entry76");
2249
code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
2250
checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
2251
identifier_t = (char *)gtk_entry_get_text(GTK_ENTRY(identifier));
2252
seq_nr_t = (char *)gtk_entry_get_text(GTK_ENTRY(seq_nr));
2253
data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
2256
if (char2x(code_t) == -1) {
2257
//printf("Error: icmp request code field\n");
2258
error("Error: icmp request code field");
2261
packet[number] = (unsigned char)char2x(code_t);
2265
if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
2266
checksum_start = number;
2267
packet[number] = (unsigned char)0;
2269
packet[number] = (unsigned char)0;
2273
/* if checksum_start = 0, we leave it in the end */
2276
if (char2x(checksum_t) == -1) {
2277
//printf("Error: icmp request checksum field\n");
2278
error("Error: icmp request checksum field");
2281
packet[number] = (unsigned char)char2x(checksum_t);
2282
checksum_t++; checksum_t++; number++;
2283
if (char2x(checksum_t) == -1) {
2284
//printf("Error: icmp request checksum field\n");
2285
error("Error: icmp request checksum field");
2288
packet[number] = (unsigned char)char2x(checksum_t);
2293
if (char2x(identifier_t) == -1) {
2294
//printf("Error: icmp request identifier field\n");
2295
error("Error: icmp request identifier field");
2298
packet[number] = (unsigned char)char2x(identifier_t);
2299
identifier_t++; identifier_t++; number++;
2300
if (char2x(identifier_t) == -1) {
2301
//printf("Error: icmp request identifier field\n");
2302
error("Error: icmp request identifier field");
2305
packet[number] = (unsigned char)char2x(identifier_t);
2308
/* sequence number */
2309
if (char2x(seq_nr_t) == -1) {
2310
//printf("Error: icmp request identifier field\n");
2311
error("Error: icmp request identifier field");
2314
packet[number] = (unsigned char)char2x(seq_nr_t);
2315
seq_nr_t++; seq_nr_t++; number++;
2316
if (char2x(seq_nr_t) == -1) {
2317
//printf("Error: icmp request identifier field\n");
2318
error("Error: icmp request identifier field");
2321
packet[number] = (unsigned char)char2x(seq_nr_t);
2325
if (GTK_TOGGLE_BUTTON(data_bt)->active) {
2327
payload_length = strlen(data_t);
2329
/* YYY 1514-number is not ok in case we use 802.1q!!! */
2330
if (get_network_payload(button, user_data, payload_length,
2331
1514-number, data_t) == -1) {
2332
//printf("Error: problem with icmp request payload\n");
2341
if (checksum_start > 0) {
2343
icmpcksum = get_checksum16(icmp_start, icmp_stop);
2344
/* the one's complement */
2345
icmpcksum = (-1) - icmpcksum;
2347
/* let's write it */
2348
packet[checksum_start] = (char)(icmpcksum/256);
2349
packet[checksum_start+1] = (char)(icmpcksum%256);
2355
/* icmp destination unreacheable */
2357
//printf("ICMP destination unreacheable\n");
2358
code = lookup_widget(GTK_WIDGET(button), "entry58");
2359
checksum = lookup_widget(GTK_WIDGET(button), "entry59");
2360
cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton15");
2361
unused = lookup_widget(GTK_WIDGET(button), "entry60");
2362
data_bt = lookup_widget(GTK_WIDGET(button), "checkbutton24");
2363
data = lookup_widget(GTK_WIDGET(button), "entry61");
2365
code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
2366
checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
2367
unused_t = (char *)gtk_entry_get_text(GTK_ENTRY(unused));
2368
data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
2371
if (char2x(code_t) == -1) {
2372
//printf("Error: icmp destination unreacheable code field\n");
2373
error("Error: icmp destination unreacheable code field");
2376
packet[number] = (unsigned char)char2x(code_t);
2380
if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
2381
checksum_start = number;
2382
packet[number] = (unsigned char)0;
2384
packet[number] = (unsigned char)0;
2388
/* if checksum_start = 0, we leave it in the end */
2391
if (char2x(checksum_t) == -1) {
2392
//printf("Error: icmp destination unreacheable checksum field\n");
2393
error("Error: icmp destination unreacheable checksum field");
2396
packet[number] = (unsigned char)char2x(checksum_t);
2397
checksum_t++; checksum_t++; number++;
2398
if (char2x(checksum_t) == -1) {
2399
//printf("Error: icmp destination unreacheable checksum field\n");
2400
error("Error: icmp destination unreacheable checksum field");
2403
packet[number] = (unsigned char)char2x(checksum_t);
2408
if (char2x(unused_t) == -1) {
2409
//printf("Error: icmp destination unreacheable unused field\n");
2410
error("Error: icmp destination unreacheable unused field");
2413
packet[number] = (unsigned char)char2x(unused_t);
2414
unused_t++; unused_t++; number++;
2415
if (char2x(unused_t) == -1) {
2416
//printf("Error: icmp destination unreacheable unused field\n");
2417
error("Error: icmp destination unreacheable unused field");
2420
packet[number] = (unsigned char)char2x(unused_t);
2421
unused_t++; unused_t++; number++;
2422
if (char2x(unused_t) == -1) {
2423
//printf("Error: icmp destination unreacheable unused field\n");
2424
error("Error: icmp destination unreacheable unused field");
2427
packet[number] = (unsigned char)char2x(unused_t);
2428
unused_t++; unused_t++; number++;
2429
if (char2x(unused_t) == -1) {
2430
//printf("Error: icmp destination unreacheable unused field\n");
2431
error("Error: icmp destination unreacheable unused field");
2434
packet[number] = (unsigned char)char2x(unused_t);
2438
if (GTK_TOGGLE_BUTTON(data_bt)->active) {
2440
payload_length = strlen(data_t);
2442
/* YYY 1514-number is not ok in case we use 802.1q!!! */
2443
if (get_network_payload(button, user_data, payload_length,
2444
1514-number, data_t) == -1) {
2445
//printf("Error: problem with icmp destination unreacheable payload\n");
2454
if (checksum_start > 0) {
2456
icmpcksum = get_checksum16(icmp_start, icmp_stop);
2457
/* the one's complement */
2458
icmpcksum = (-1) - icmpcksum;
2460
/* let's write it */
2461
packet[checksum_start] = (char)(icmpcksum/256);
2462
packet[checksum_start+1] = (char)(icmpcksum%256);
2468
//printf("Other type of icmp message\n");
2469
code = lookup_widget(GTK_WIDGET(button), "entry157");
2470
checksum = lookup_widget(GTK_WIDGET(button), "entry158");
2471
cks_bt = lookup_widget(GTK_WIDGET(button), "checkbutton38");
2472
data = lookup_widget(GTK_WIDGET(button), "entry159");
2474
code_t = (char *)gtk_entry_get_text(GTK_ENTRY(code));
2475
checksum_t = (char *)gtk_entry_get_text(GTK_ENTRY(checksum));
2476
data_t = (char *)gtk_entry_get_text(GTK_ENTRY(data));
2479
if (char2x(code_t) == -1) {
2480
//printf("Error: icmp other code field\n");
2481
error("Error: icmp other code field");
2484
packet[number] = (unsigned char)char2x(code_t);
2488
if (GTK_TOGGLE_BUTTON(cks_bt)->active) {
2489
checksum_start = number;
2490
packet[number] = (unsigned char)0;
2492
packet[number] = (unsigned char)0;
2495
/* if checksum_start = 0, we leave it in the end */
2499
if (char2x(checksum_t) == -1) {
2500
//printf("Error: icmp destination unreacheable checksum field\n");
2501
error("Error: icmp destination unreacheable checksum field");
2504
packet[number] = (unsigned char)char2x(checksum_t);
2505
checksum_t++; checksum_t++; number++;
2506
if (char2x(checksum_t) == -1) {
2507
//printf("Error: icmp destination unreacheable checksum field\n");
2508
error("Error: icmp destination unreacheable checksum field");
2511
packet[number] = (unsigned char)char2x(checksum_t);
2516
payload_length = strlen(data_t);
2518
/* YYY 1514-number is not ok in case we use 802.1q!!! */
2519
if (get_network_payload(button, user_data, payload_length,
2520
1514-number, data_t) == -1) {
2521
//printf("Error: problem with icmp destination unreacheable payload\n");
2527
if (checksum_start > 0) {
2529
icmpcksum = get_checksum16(icmp_start, icmp_stop);
2530
/* the one's complement */
2531
icmpcksum = (-1) - icmpcksum;
2533
/* let's write it */
2534
packet[checksum_start] = (char)(icmpcksum/256);
2535
packet[checksum_start+1] = (char)(icmpcksum%256);
2542
/* we have to parse the arp protocol information */
2543
int arp_get(GtkButton *button, gpointer user_data)
2545
GtkWidget *hwtype, *prottype, *hwsize, *protsize;
2546
GtkWidget *rbt10, *rbt11, *rbt17, *en81;
2547
GtkWidget *sendermac, *senderip, *targetmac, *targetip;
2548
gchar *hwtype_t, *prottype_t, *hwsize_t, *protsize_t, *en81_t;
2549
gchar *sendermac_t, *senderip_t, *targetmac_t, *targetip_t;
2553
hwtype = lookup_widget(GTK_WIDGET(button), "A_hwtype");
2554
prottype = lookup_widget(GTK_WIDGET(button), "A_prottype");
2555
hwsize = lookup_widget(GTK_WIDGET(button), "A_hwsize");
2556
protsize = lookup_widget(GTK_WIDGET(button), "A_protsize");
2558
rbt10 = lookup_widget(GTK_WIDGET(button), "radiobutton10");
2559
rbt11 = lookup_widget(GTK_WIDGET(button), "radiobutton11");
2560
rbt17 = lookup_widget(GTK_WIDGET(button), "radiobutton17");
2561
en81 = lookup_widget(GTK_WIDGET(button), "entry81");
2563
sendermac = lookup_widget(GTK_WIDGET(button), "A_sendermac");
2564
senderip = lookup_widget(GTK_WIDGET(button), "A_senderip");
2565
targetmac = lookup_widget(GTK_WIDGET(button), "A_targetmac");
2566
targetip = lookup_widget(GTK_WIDGET(button), "A_targetip");
2568
hwtype_t = (char *)gtk_entry_get_text(GTK_ENTRY(hwtype));
2569
prottype_t = (char *)gtk_entry_get_text(GTK_ENTRY(prottype));
2570
hwsize_t = (char *)gtk_entry_get_text(GTK_ENTRY(hwsize));
2571
protsize_t = (char *)gtk_entry_get_text(GTK_ENTRY(protsize));
2572
sendermac_t = (char *)gtk_entry_get_text(GTK_ENTRY(sendermac));
2573
senderip_t = (char *)gtk_entry_get_text(GTK_ENTRY(senderip));
2574
targetmac_t = (char *)gtk_entry_get_text(GTK_ENTRY(targetmac));
2575
targetip_t = (char *)gtk_entry_get_text(GTK_ENTRY(targetip));
2576
en81_t = (char *)gtk_entry_get_text(GTK_ENTRY(en81));
2578
/* now we have all the widget and we start parsing the info: we start with the hardware type */
2579
if (char2x(hwtype_t) == -1) {
2580
//printf("Error: hwtype field\n");
2581
error("Error: hwtype field");
2584
packet[number] = (unsigned char)char2x(hwtype_t);
2585
hwtype_t++; hwtype_t++; number++;
2586
if (char2x(hwtype_t) == -1) {
2587
//printf("Error: hwtype field\n");
2588
error("Error: hwtype field");
2591
packet[number] = (unsigned char)char2x(hwtype_t);
2595
if (char2x(prottype_t) == -1) {
2596
//printf("Error: prottype field\n");
2597
error("Error: prottype field");
2600
packet[number] = (unsigned char)char2x(prottype_t);
2601
prottype_t++; prottype_t++; number++;
2602
if (char2x(prottype_t) == -1) {
2603
//printf("Error: prottype field\n");
2604
error("Error: prottype field");
2607
packet[number] = (unsigned char)char2x(prottype_t);
2611
if (char2x(hwsize_t) == -1) {
2612
//printf("Error: hwsize field\n");
2613
error("Error: hwsize field");
2616
packet[number] = (unsigned char)char2x(hwsize_t);
2620
if (char2x(protsize_t) == -1) {
2621
//printf("Error: protsize field\n");
2622
error("Error: protsize field");
2625
packet[number] = (unsigned char)char2x(protsize_t);
2629
if (GTK_TOGGLE_BUTTON(rbt10)->active) {
2630
packet[number] = 0x00;
2632
packet[number] = 0x01;
2636
else if (GTK_TOGGLE_BUTTON(rbt11)->active) {
2637
packet[number] = 0x00;
2639
packet[number] = 0x02;
2642
else if (GTK_TOGGLE_BUTTON(rbt17)->active) {
2643
if (char2x(en81_t) == -1) {
2644
//printf("Error: entry arp opcode\n");
2645
error("Error: entry arp opcode");
2648
packet[number] = (unsigned char)char2x(en81_t);
2649
en81_t++; en81_t++; number++;
2650
if (char2x(en81_t) == -1) {
2651
//printf("Error: entry arp opcode\n");
2652
error("Error: entry arp opcode");
2655
packet[number] = (unsigned char)char2x(en81_t);
2659
//printf("Error: Something is wrong with the arp opcode\n");
2660
error("Error: Something is wrong with the arp opcode");
2665
/* and now the ip&mac values: check if addresses are ok */
2668
if (check_mac_address(sendermac_t) == -1) {
2669
//printf("Error: Wrong mac entry in arp sender field, can't copy it\n");
2670
error("Error: Wrong mac entry in arp sender field, can't copy it");
2673
if (check_mac_address(targetmac_t) == -1) {
2674
//printf("Error: Wrong mac entry in arp target field, can't copy it\n");
2675
error("Error: Wrong mac entry in arp target field, can't copy it");
2678
if (check_ip_address(senderip_t) == -1) {
2679
//printf("Error: Wrong ip entry in arp sender field, can't copy it\n");
2680
error("Error: Wrong ip entry in arp sender field, can't copy it");
2683
if (check_ip_address(targetip_t) == -1) {
2684
//printf("Error: Wrong ip entry in arp target field, can't copy it\n");
2685
error("Error: Wrong ip entry in arp target field, can't copy it");
2689
/* if all addresses are ok, we copy them into packet: first sender mac */
2690
for(i=0; i<6; i++) {
2691
packet[number] = (unsigned char)char2x(sendermac_t);
2692
sendermac_t = sendermac_t + 3; number++;
2696
for (i=0; i<4; i++) {
2697
for(j=0; j<4 && (*senderip_t != '\0'); j++) {
2698
if ( ((int)*senderip_t == '.') && (i<3) && (j>0) ) {
2702
tmp[j] = *senderip_t;
2706
packet[number] = (unsigned char)(atoi(tmp));
2711
for(i=0; i<6; i++) {
2712
packet[number] = (unsigned char)char2x(targetmac_t);
2713
targetmac_t = targetmac_t + 3; number++;
2717
for (i=0; i<4; i++) {
2718
for(j=0; j<4 && (*targetip_t != '\0'); j++) {
2719
if ( ((int)*targetip_t == '.') && (i<3) && (j>0) ) {
2723
tmp[j] = *targetip_t;
2727
packet[number] = (unsigned char)(atoi(tmp));
2735
/* user choosed to manually attach payload, so here we are */
2736
int get_network_payload(GtkButton *button, gpointer user_data, int length, int max, gchar *entry)
2741
/* firs we check if total length without spaces is an even number */
2743
for (i=0; i < length; i++, ptr++) {
2744
if (isspace(*ptr) != 0) { /* prazne znake ne upostevam */
2750
if ( stevec % 2 != 0) {
2751
//printf("Error: Payload lengtht must be an even number\n");
2752
error("Error: Payload lengtht must be an even number");
2758
for (i=0; i < length ; ) {
2759
if (isspace(*entry) != 0) { /* prazne znake ne upostevam */
2765
//printf("Error: Network layer payload lengtht to long\n");
2766
error("Error: Network layer payload lengtht to long");
2769
if (char2x(entry) == -1) {
2770
//printf("Error: network layer payload\n");
2771
error("Error: network layer payload");
2774
packet[number] = (unsigned char)char2x(entry);
2775
number++; i++; i++; entry++; entry++; stevec++;;
2781
int link_level_get(GtkButton *button, gpointer user_data)
2783
GtkWidget *ver2_tbt, *_801q_cbt, *_8023_tbt;
2784
GtkWidget *ethtype_e;
2787
ver2_tbt = lookup_widget(GTK_WIDGET (button), "bt_ver2");
2788
_8023_tbt = lookup_widget(GTK_WIDGET (button), "bt_8023");
2789
_801q_cbt = lookup_widget(GTK_WIDGET (button), "bt_8021q");
2791
/* always we need first the dest and source mac address */
2792
if (get_mac_from_string(button) == -1) {
2793
//printf("Error: mac address field\n");
2794
error("Error: mac address field");
2799
/* is 802.1q active - do we need to add 4 or 8 bytes? */
2800
if (GTK_TOGGLE_BUTTON(_801q_cbt)->active) {
2801
if (get_8021q(button) == -1) {
2802
//printf("Error: 802.1q field\n");
2806
if (GTK_TOGGLE_BUTTON(_8023_tbt)->active) { /* uporabimo ethernet vezije 802.3 */
2807
if (get_8023(button) == -1) {
2808
//printf("Error: 802.3 field");
2813
else if (GTK_TOGGLE_BUTTON(ver2_tbt)->active){ /* pol pa verzijo 2 */
2815
ethtype_e = lookup_widget(GTK_WIDGET (button), "L_ethtype");
2816
ethtype_t = (char *)gtk_entry_get_text(GTK_ENTRY(ethtype_e));
2817
if (char2x(ethtype_t) == -1) {
2818
//printf("Error: ethernet type field\n");
2819
error("Error: ethernet type field");
2822
packet[number] = (unsigned char)char2x(ethtype_t);
2823
ethtype_t++; ethtype_t++; number++;
2824
if (char2x(ethtype_t) == -1) {
2825
//printf("Error: ethernet type field\n");
2826
error("Error: ethernet type field");
2829
packet[number] = (unsigned char)char2x(ethtype_t);
2832
else {/* kva a je mogoce token ring??? */
2833
//printf("Error: in ethernet field\n");
2834
error("Error: in ethernet field");
2843
/* if we are in the 802.3 ethernet version */
2844
int get_8023(GtkButton *button)
2846
GtkWidget *ethlength_e, *L8023llc_tbt, *L8023llcsnap_tbt, *Ldsap_e, *Lssap_e;
2847
GtkWidget *Lctrl_e, *Loui_e, *Lpid_e, *autolength_bt;
2848
gchar *Ldsap_t, *Lssap_t, *Lctrl_t, *Loui_t, *Lpid_t;
2851
/* do we need to calculate the length field or will be suplied manually */
2852
autolength_bt = lookup_widget(GTK_WIDGET (button), "checkbutton2");
2853
if (GTK_TOGGLE_BUTTON(autolength_bt)->active) {
2854
autolength = number;
2855
packet[number] = 0x0; number++; packet[number] = 0x0; number++;
2859
ethlength_e = lookup_widget(GTK_WIDGET (button), "entry5");
2860
ethlength_t = (char *)gtk_entry_get_text(GTK_ENTRY(ethlength_e));
2861
if (char2x(ethlength_t) == -1) {
2862
//printf("Error: 802.3 length field\n");
2863
error("Error: 802.3 length field");
2866
packet[number] = (unsigned char)char2x(ethlength_t);
2867
ethlength_t++; ethlength_t++; number++;
2868
if (char2x(ethlength_t) == -1) {
2869
//printf("Error: 802.3 length field\n");
2870
error("Error: 802.3 length field");
2873
packet[number] = (unsigned char)char2x(ethlength_t);
2877
L8023llc_tbt = lookup_widget(GTK_WIDGET (button), "L_8023_llc_tbt");
2878
L8023llcsnap_tbt = lookup_widget(GTK_WIDGET (button), "L_8023_llcsnap_tbt");
2879
Ldsap_e = lookup_widget(GTK_WIDGET (button), "L_dsap");
2880
Lssap_e= lookup_widget(GTK_WIDGET (button), "L_ssap");
2881
Lctrl_e= lookup_widget(GTK_WIDGET (button), "L_ctrl");
2883
Ldsap_t = (char *)gtk_entry_get_text(GTK_ENTRY(Ldsap_e));
2884
if (char2x(Ldsap_t) == -1) {
2885
//printf("Error: 802.3 ldsap field\n");
2886
error("Error: 802.3 ldsap field");
2889
packet[number] = (unsigned char)char2x(Ldsap_t);
2892
Lssap_t = (char *)gtk_entry_get_text(GTK_ENTRY(Lssap_e));
2893
if (char2x(Lssap_t) == -1) {
2894
//printf("Error: 802.3 lssap field\n");
2895
error("Error: 802.3 lssap field");
2898
packet[number] = (unsigned char)char2x(Lssap_t);
2901
Lctrl_t = (char *)gtk_entry_get_text(GTK_ENTRY(Lctrl_e));
2902
if (char2x(Lctrl_t) == -1) {
2903
//printf("Error: 802.3 Ctrl field\n");
2904
error("Error: 802.3 Ctrl field");
2907
packet[number] = (unsigned char)char2x(Lctrl_t);
2910
/* do we need snap encapsulation */
2911
if (GTK_TOGGLE_BUTTON(L8023llcsnap_tbt)->active) {
2912
Loui_e = lookup_widget(GTK_WIDGET (button), "L_oui");
2913
Lpid_e = lookup_widget(GTK_WIDGET (button), "L_pid");
2915
Loui_t = (char *)gtk_entry_get_text(GTK_ENTRY(Loui_e));
2916
if (char2x(Loui_t) == -1) {
2917
//printf("Error: 802.3 oui field\n");
2918
error("Error: 802.3 oui field");
2921
packet[number] = (unsigned char)char2x(Loui_t);
2922
number++; Loui_t++, Loui_t++;
2924
if (char2x(Loui_t) == -1) {
2925
//printf("Error: 802.3 oui field\n");
2926
error("Error: 802.3 oui field");
2929
packet[number] = (unsigned char)char2x(Loui_t);
2930
number++; Loui_t++, Loui_t++;
2932
if (char2x(Loui_t) == -1) {
2933
//printf("Error: 802.3 oui field\n");
2934
error("Error: 802.3 oui field");
2937
packet[number] = (unsigned char)char2x(Loui_t);
2940
Lpid_t = (char *)gtk_entry_get_text(GTK_ENTRY(Lpid_e));
2941
if (char2x(Lpid_t) == -1) {
2942
//printf("Error: 802.3 snap pid field\n");
2943
error("Error: 802.3 snap pid field");
2946
packet[number] = (unsigned char)char2x(Lpid_t);
2947
number++; Lpid_t++; Lpid_t++;
2949
if (char2x(Lpid_t) == -1) {
2950
//printf("Error: 802.3 snap pid field\n");
2951
error("Error: 802.3 snap pid field");
2954
packet[number] = (unsigned char)char2x(Lpid_t);
2964
/* function parses 802.1q field */
2965
int get_8021q(GtkButton *button)
2967
GtkWidget *vlan_e, *priority_m, *cfi1_rbt, *vlanid_e, *menu, *menu_item, *QinQ_bt, *QinQ, *QinQpvid;
2968
gchar *vlan_t, *vlanid_t, *QinQ_t;
2969
gint menu_index, cfi =0;
2972
QinQ_bt = lookup_widget(GTK_WIDGET (button), "checkbutton40");
2974
/* what about QinQ field? */
2975
if (GTK_TOGGLE_BUTTON(QinQ_bt)->active) {
2976
QinQpvid = lookup_widget(GTK_WIDGET (button), "optionmenu21");
2977
QinQ = lookup_widget(GTK_WIDGET (button), "entry165");
2979
menu = GTK_OPTION_MENU(QinQpvid)->menu;
2980
menu_item = gtk_menu_get_active (GTK_MENU (menu));
2981
menu_index = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
2983
switch (menu_index) {
2985
packet[number] = (unsigned char)char2x("81");
2987
packet[number] = (unsigned char)char2x("00");
2992
packet[number] = (unsigned char)char2x("91");
2994
packet[number] = (unsigned char)char2x("00");
2999
packet[number] = (unsigned char)char2x("92");
3001
packet[number] = (unsigned char)char2x("00");
3006
packet[number] = (unsigned char)char2x("88");
3008
packet[number] = (unsigned char)char2x("a8");
3014
QinQ_t = (char *)gtk_entry_get_text(GTK_ENTRY(QinQ));
3015
if (char2x(QinQ_t) == -1) {
3016
//printf("Error: VLAN QinQ type field\n");
3017
error("Error: VLAN QinQ field");
3020
packet[number] = (unsigned char)char2x(QinQ_t);
3021
QinQ_t++; QinQ_t++; number++;
3022
if (char2x(QinQ_t) == -1) {
3023
//printf("Error: VLAN QinQ type field\n");
3024
error("Error: VLAN QinQ field");
3027
packet[number] = (unsigned char)char2x(QinQ_t);
3032
vlan_e = lookup_widget(GTK_WIDGET (button), "L_tag_id");
3033
priority_m = lookup_widget(GTK_WIDGET (button), "L_optmenu2_bt");
3034
cfi1_rbt = lookup_widget(GTK_WIDGET (button), "checkbutton39");
3035
vlanid_e = lookup_widget(GTK_WIDGET (button), "L_vlan_id");
3036
vlan_t = (char *)gtk_entry_get_text(GTK_ENTRY(vlan_e));
3037
vlanid_t = (char *)gtk_entry_get_text(GTK_ENTRY(vlanid_e));
3039
/* first we chech the vlan protocol id */
3040
if (char2x(vlan_t) == -1) {
3041
//printf("Error: 802.1q type field\n");
3042
error("Error: 802.1q type field");
3045
packet[number] = (unsigned char)char2x(vlan_t);
3046
vlan_t++; vlan_t++; number++;
3047
if (char2x(vlan_t) == -1) {
3048
//printf("Error: 802.1q type field\n");
3049
error("Error: 802.1q type field");
3052
packet[number] = (unsigned char)char2x(vlan_t);
3055
/* next we need the priority */
3056
menu = GTK_OPTION_MENU(priority_m)->menu;
3057
menu_item = gtk_menu_get_active (GTK_MENU (menu));
3058
menu_index = g_list_index (GTK_MENU_SHELL (menu)->children, menu_item);
3060
/* what about CFI bit? */
3061
if (GTK_TOGGLE_BUTTON(cfi1_rbt)->active)
3066
/* in cfi we store the value of priority and cfi */
3067
tmp[0] = (unsigned char)(menu_index * 2 + cfi);
3068
snprintf(&(tmp[0]), 2, "%x", tmp[0]);
3070
/* we need the vlan id */
3073
if (char2x(tmp) == -1) {
3074
//printf("Error: 802.1q: priority & cfi field & 1 byte vlan id\n");
3075
error("Error: 802.1q: priority & cfi field & 1 byte vlan id");
3078
packet[number] = (unsigned char)char2x(tmp);
3080
vlanid_t++; number++;
3082
if (char2x(vlanid_t) == -1) {
3083
//printf("Error: 802.1q vlanid \n");
3084
error("Error: 802.1q vlanid ");
3087
packet[number] = (unsigned char)char2x(vlanid_t);
3094
/* calculate the checksum
3095
* we pass the start and stop number in packet[]
3096
* where we won't to calculate the checksum */
3097
guint32 get_checksum32(int cks_start, int cks_stop)
3103
for (; cks_start<cks_stop; ) {
3104
/* we take 16 bit word's -> 2 bytes */
3105
value = (packet[cks_start]<<8) + packet[cks_start+1];
3107
/* for every cicle, this means where the sum exceeds
3108
* the 16 bit unsigned max value (65536), you have to add 1
3110
//sum = (sum % 0x10000) + (sum / 0x10000);
3113
/* we don't do extract the sum from 0xFFFF (or -1), so you have to do
3115
//return (sum % 0x10000);
3120
guint16 get_checksum16(int cks_start, int cks_stop)
3126
for (; cks_start<cks_stop; ) {
3127
/* we take 16 bit word's -> 2 bytes */
3128
value = (packet[cks_start]<<8) + packet[cks_start+1];
3130
/* for every cicle, this means where the sum exceeds
3131
* the 16 bit unsigned max value (65536), you have to add 1
3133
sum = (sum % 0x10000) + (sum / 0x10000);
3136
/* we don't do extract the sum from 0xFFFF (or -1), so you have to do
3138
return (sum % 0x10000);
3141
/*check ip address */
3142
int check_ip_address(gchar *ptr)
3147
for (i=0; i<4; i++) {
3148
for(j=0; j<4 && (*ptr != '\0'); j++) {
3149
if ( ((int)*ptr == '.') && (i<3) && (j>0) ) {
3153
if ( (*ptr <48) || (*ptr>57) )
3161
if ( (atoi(tmp) < 0) || (atoi(tmp) > 255) || (strlen(tmp)==0) || (strlen(tmp)>3) )
3168
/* check mac address */
3169
int check_mac_address(gchar *ptr)
3173
if ( strlen(ptr) > 17)
3175
/* all mac addresses must be in full xx:xx:xx:xx:xx:xx format. f:... in not ok 0f:... works */
3176
for(i=0; i<6; i++) {
3177
if (char2x(ptr) == -1)
3180
if ( (*ptr != ':') && (i<5) )
3189
/* function parses mac address */
3190
int get_mac_from_string(GtkButton *button)
3192
GtkWidget *dstmac_e, *srcmac_e;
3193
gchar *dstmac_t, *srcmac_t;
3194
int dst_length, src_length, i;
3196
dstmac_e = lookup_widget(GTK_WIDGET (button), "L_dst_mac");
3197
srcmac_e = lookup_widget(GTK_WIDGET (button), "L_src_mac");
3198
dstmac_t = (char *)gtk_entry_get_text(GTK_ENTRY(dstmac_e));
3199
srcmac_t = (char *)gtk_entry_get_text(GTK_ENTRY(srcmac_e));
3200
dst_length = strlen(dstmac_t);
3201
src_length = strlen(srcmac_t);
3203
/* mac naslov mora viti v formatu xx:xx:xx:xx:xx:xx to pomeni 17 znakov skupaj! */
3204
if ((src_length != 17) || (dst_length != 17))
3207
/* first we store destination address into packet[] */
3208
for(i=0; i<6; i++) {
3209
if (char2x(dstmac_t) == -1)
3211
packet[i] = (unsigned char)char2x(dstmac_t);
3212
dstmac_t = dstmac_t + 2;
3213
if ((i<5) && (*dstmac_t != ':'))
3221
/* source address into packet[] */
3222
for(i=6; i<12; i++) {
3223
if (char2x(srcmac_t) == -1)
3225
packet[i] = (unsigned char)char2x(srcmac_t);
3226
srcmac_t = srcmac_t + 2;
3227
if ((i<5) && (*srcmac_t != ':'))
3239
/* function takes pointer to char and converts two chars to hex and returns signed int. If you want to use te return value as char, you need to cast back to (unsigned char) */
3240
signed int char2x(char *p)
3244
if ( (*p >= '0') && (*p <= '9')) {
3245
x = ((*p) - 48) * 16;
3247
else if ((*p >= 'A') && (*p <= 'F')) {
3248
x = ((*p) - 55) * 16;
3250
else if ((*p >= 'a') && (*p <= 'f')) {
3251
x = ((*p) - 87) * 16;
3257
if ( (*p >= '0') && (*p <= '9')) {
3258
x = x + ((*p) - 48);
3260
else if ((*p >= 'A') && (*p <= 'F')) {
3261
x = x + ((*p) - 55);
3263
else if ((*p >= 'a') && (*p <= 'f')) {
3264
x = x + ((*p) - 87);
3286
case 10: return 'A';
3287
case 11: return 'B';
3288
case 12: return 'C';
3289
case 13: return 'D';
3290
case 14: return 'E';
3291
case 15: return 'F';
3292
default: return '0';
3297
char *c8(char *s, unsigned char x) {
3299
*s++ = c4(x & 0x0F);
3305
int insert_frequency(int codec, int frequency, int length, GtkWidget *payload_entry, gint amp_index)
3307
double fs = 8000; /* vzorcna frekvenca */
3308
double amp; /* amplituda */
3309
double ph = 0; /* zacetna faza */
3311
double sample; /* 16 bit variable */
3312
gchar entry_t[2*length+1];
3316
delta_ph = 2* M_PI *frequency/fs;
3318
/* the amp values are: low - 5000, mid - 15000, max - 30000 */
3319
amp = 5000 + amp_index * 7500 + amp_index * amp_index * 2500;
3322
sample = amp*sin(ph);
3324
while (ph > (2*M_PI)) {
3329
c8(ptr, linear2alaw((gint16)sample));
3331
c8(ptr, linear2ulaw((gint16)sample));
3340
gtk_entry_set_text(GTK_ENTRY(payload_entry), entry_t);
3346
/* Following three routines are from Sun Microsystems, Inc. */
3347
unsigned char linear2alaw(int pcm_val) /* 2's complement (16-bit range) */
3349
static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF};
3354
pcm_val = pcm_val >> 3;
3357
mask = 0xD5; /* sign (7th) bit = 1 */
3359
mask = 0x55; /* sign bit = 0 */
3360
pcm_val = -pcm_val - 1;
3363
/* Convert the scaled magnitude to segment number. */
3364
seg = search(pcm_val, seg_aend, 8);
3366
/* Combine the sign, segment, and quantization bits. */
3368
if (seg >= 8) /* out of range, return maximum value. */
3369
return (unsigned char) (0x7F ^ mask);
3371
aval = (unsigned char) seg << 4;
3373
aval |= (pcm_val >> 1) & 0xf;
3375
aval |= (pcm_val >> seg) & 0xf;
3376
return (aval ^ mask);
3381
unsigned char linear2ulaw(short pcm_val) /* 2's complement (16-bit range) */
3383
static short seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
3388
/* Get the sign and the magnitude of the value. */
3389
pcm_val = pcm_val >> 2;
3396
if ( pcm_val > 8159 ) pcm_val = 8159; /* clip the magnitude */
3397
pcm_val += (0x84 >> 2);
3399
/* Convert the scaled magnitude to segment number. */
3400
seg = search(pcm_val, seg_uend, 8);
3403
* Combine the sign, segment, quantization bits;
3404
* and complement the code word.
3406
if (seg >= 8) /* out of range, return maximum value. */
3407
return (unsigned char) (0x7F ^ mask);
3409
uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
3410
return (uval ^ mask);
3416
short search(int val, short *table, int size)
3420
for (i = 0; i < size; i++) {
3421
if (val <= *table++)
3428
int check_digit(char *field, int length, char *text)
3432
/* we check if the field contains only numbers and is not empty */
3434
//printf("%s\n", text);
3439
for(i=0; i < length; i++, field++) {
3440
if (isdigit(*field) == 0) {
3441
//printf("%s\n", text);
3450
int check_hex(char *field, int length, char *text)
3454
for(i=0; i < length; i++, field++) {
3455
if (isxdigit(*field) == 0) {
3456
//printf("%s\n", text);
3465
/* what format do we allow for packet files */
3466
/* YYY add to ignore the comments */
3467
int check_if_file_is_packet(FILE *file_p)
3472
while ( (c = fgetc( file_p )) != EOF ) {
3474
if (isspace(c) != 0)
3477
while ( getc(file_p) != 10);
3481
if (isxdigit(c) == 0) {
3482
//printf("Error: File does not contain a valid packet!\n");
3483
error("Error: File does not contain a valid packet");
3494
/* 1514 or 1518, how to enable the vlan checking */
3495
if ( (i%2 != 0) || (i > 3536) ) {
3496
//printf("Error: File length is not ok\n");
3497
error("Error: File length is not ok");
3505
void statusbar_text(GtkButton *button, char *text) {
3507
GtkWidget *statusbar;
3511
statusbar = lookup_widget(GTK_WIDGET (button), "statusbar1");
3512
context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "Statusbar example");
3514
snprintf(buff, strlen(text)+1, text );
3515
gtk_statusbar_push(GTK_STATUSBAR(statusbar), GPOINTER_TO_INT(context_id), buff);
3519
void gen_crc32_table()
3521
unsigned long crc, poly;
3525
for (i = 0; i < 256; i++) {
3527
for (j = 8; j > 0; j--) {
3529
crc = (crc >> 1) ^ poly;
3533
crc32_table[i] = crc;
3538
unsigned long get_crc32(unsigned char *p, int len)
3540
register unsigned long crc;
3542
if (!crc32_table_init) {
3544
crc32_table_init = 1;
3548
for (i=0; i<len; i++) {
3549
crc = (crc>>8) ^ crc32_table[ (crc ^ *p++) & 0xFF ];
3551
crc = crc^0xFFFFFFFF;
3552
/* big endian to little endian */
3553
crc = ((crc >> 24) & 0x000000FF) ^
3554
((crc >> 8) & 0x0000FF00) ^
3555
((crc << 8) & 0x00FF0000) ^
3556
((crc << 24) & 0xFF000000);