-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipv4lib.c
More file actions
926 lines (846 loc) · 25.2 KB
/
ipv4lib.c
File metadata and controls
926 lines (846 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//--------------------------------
//
// Set of functions and routines
// for packet inpection/disection
//
// implimentation
//
//-------------------------------
#include "ipv4lib.h"
//-----------------------------------------------------
// Initialization
//-----------------------------------------------------
Packet_Meta create_packet_meta(){
Packet_Meta pm = (Packet_Meta)calloc(1, sizeof(struct packet_meta_s));
if(pm)
return pm;
else{
fprintf(stderr, "Calloc failed at creating Packet Metadata\n");
return 0;
}
}
IPv4_Header create_ip_header(){
IPv4_Header ih = (IPv4_Header)calloc(1, sizeof(struct ipv4_header_s));
if(ih){
ih->source_ip = (uint8_t*)calloc(4,sizeof(uint8_t));
ih->destination_ip = (uint8_t*)calloc(4,sizeof(uint8_t));
return ih;
}
else{
fprintf(stderr, "Calloc failed at creating IP header\n");
return 0;
}
}
Ethernet_Header create_eII_header(){
Ethernet_Header eh = \
(Ethernet_Header)calloc(1, sizeof(struct ethernet_header_s));
if(eh){
eh->destination = (uint8_t*)calloc(6, sizeof(uint8_t));
eh->source = (uint8_t*)calloc(6, sizeof(uint8_t));
return eh;
}
else{
fprintf(stderr, "Calloc failed at creating Eth header\n");
return 0;
}
}
TCP_Header create_tcp_header(){
TCP_Header th = (TCP_Header)calloc(1, sizeof(struct tcp_header_s));
if(th)
return th;
else{
fprintf(stderr, "Calloc failed at creating tcp header\n");
return 0;
}
}
UDP_Header create_udp_header(){
UDP_Header uh = (UDP_Header)calloc(1, sizeof(struct udp_header_s));
if(uh)
return uh;
else{
fprintf(stderr, "Calloc failed at creating udp header\n");
return 0;
}
}
Packet create_packet(){
Packet p = (Packet)calloc(1, sizeof(struct packet_s));
if(p){
p->eh = create_eII_header();
p->ih = create_ip_header();
p->th = NULL; // will be created based on the ipv4 protocol
p->uh = NULL;
p->payload = 0;
return p;
}
else{
fprintf(stderr, "Calloc failed at creating Packet\n");
return 0;
}
}
int init_md_f(Packet_Meta pm, char* fn, int eth, int fcs, int pre, \
unsigned int bc, unsigned int ps){
if(pm){
if(fn[0]=='0'){
pm->packet = stdin;
}
else{
pm->packet = fopen(fn, "r");
if(!pm->packet){
fprintf(stderr,"Could not open file\n");
return 0;
}
}
pm->ethernet_flag|=eth;
pm->fcs_active|=fcs;
pm->pre_del|=pre;
pm->byte_count = bc;
pm->payload_size = ps;
return 1;
}
fprintf(stderr,"Packet meta struct is Null\n");
return 0;
}
int init_md_s(Packet_Meta pm, int eth, int fcs, int pre, \
unsigned int bc, unsigned int ps){
if(pm){
//create the socket
pm->socket = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
if(pm->socket<0){
fprintf(stderr,"Socket Error\nDid you run as root?\n");
return 0;
}
pm->packet_buffer = (uint8_t*)malloc(65536);
pm->pbp = 0;
pm->ethernet_flag|=eth;
pm->fcs_active|=fcs;
pm->pre_del|=pre;
pm->byte_count = bc;
pm->payload_size = ps;
return 1;
}
fprintf(stderr,"Packet meta struct is Null\n");
return 0;
}
//-----------------------------------------------------
// Packet Emission
//-----------------------------------------------------
int write_to_packet_buffer(Packet_Meta pm, Packet p){
if(!pm || !p){
fprintf(stderr,"Packet Meta or Packet is null in write_to_packet_buffer");
return 0;
}
if(!pm->packet_buffer){
pm->packet_buffer = (uint8_t*)calloc(MAX_IPV4,sizeof(uint8_t));
}
memset(pm->packet_buffer,0,MAX_IPV4);
pm->pbp = 0;
if(p->eh){
printf("Writing EH\n");
Ethernet_Header eh = p->eh;
memcpy(pm->packet_buffer+pm->pbp, eh->destination, 6);
pm->pbp+=6;
memcpy(pm->packet_buffer+pm->pbp, eh->source, 6);
pm->pbp+=6;
memcpy(pm->packet_buffer+pm->pbp,&(eh->ethertype), 2);
pm->pbp+=2;
}
if(p->ih){
printf("Writing IH\n");
IPv4_Header ih = p->ih;
uint8_t temp;
uint16_t temp16;
temp = ih->version;
temp <<= 4;
temp |= ih->ihl;
memcpy(pm->packet_buffer+pm->pbp,&temp,1);
pm->pbp++;
temp = ih->dscp;
temp <<= 6;
temp |= ih->ecn;
memcpy(pm->packet_buffer+pm->pbp,&temp,1);
pm->pbp++;
memcpy(pm->packet_buffer+pm->pbp,&(ih->total_length),2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&(ih->identification),2);
pm->pbp+=2;
temp16 = ih->fragment_offset;
temp16 <<= 3;
temp16 |= ih->flags;
temp16 = htons(temp16);
memcpy(pm->packet_buffer+pm->pbp,&temp16,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&(ih->ttl),1);
pm->pbp++;
memcpy(pm->packet_buffer+pm->pbp,&(ih->protocol),1);
pm->pbp++;
memcpy(pm->packet_buffer+pm->pbp,&(ih->header_checksum),2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,ih->source_ip,4);
pm->pbp+=4;
memcpy(pm->packet_buffer+pm->pbp,ih->destination_ip,4);
pm->pbp+=4;
}
if(p->th){
printf("Writing TH\n");
TCP_Header th = p->th;
uint8_t temp;
memcpy(pm->packet_buffer+pm->pbp,&th->source_port,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&th->destin_port,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&th->seq_num,4);
pm->pbp+=4;
memcpy(pm->packet_buffer+pm->pbp,&th->ack_number,4);
pm->pbp+=4;
temp = th->data_offset;
temp <<= 5;
temp |= th->reserved;
temp <<= 2;
temp |= th->ns;
memcpy(pm->packet_buffer+pm->pbp,&temp,1);
pm->pbp++;
temp = th->cwr;temp<<=1;
temp |= th->ece;temp<<=1;
temp |= th->urg;temp<<=1;
temp |= th->ack;temp<<=1;
temp |= th->psh;temp<<=1;
temp |= th->rst;temp<<=1;
temp |= th->syn;temp<<=1;
temp |= th->fin;temp<<=1;
memcpy(pm->packet_buffer+pm->pbp,&temp,1);
pm->pbp++;
memcpy(pm->packet_buffer+pm->pbp,&th->win_size,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&th->check,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&th->urgent_point,2);
pm->pbp+=2;
unsigned o_size = ((th->data_offset * 32) / 8) - 20;
th->options = (uint8_t*)calloc(o_size, sizeof(uint8_t));
memcpy(pm->packet_buffer+pm->pbp,th->options,o_size);
pm->pbp+=o_size;
}
if(p->uh){
printf("Writing UH\n");
UDP_Header uh = p->uh;
memcpy(pm->packet_buffer+pm->pbp,&uh->source_port,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&uh->destin_port,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&uh->length,2);
pm->pbp+=2;
memcpy(pm->packet_buffer+pm->pbp,&uh->check,2);
pm->pbp+=2;
}
if(pm->payload_size > 0){
printf("Writing Payload\n");
memcpy(pm->packet_buffer+pm->pbp, p->payload, pm->payload_size);
pm->pbp+=pm->payload_size;
}
return 1;
}
int emmit_packet(Packet_Meta pm, int port){
}
//-----------------------------------------------------
// Auxilary
//-----------------------------------------------------
void print_usage(char* usage){
printf("%s", usage);
}
void byte_replace(uint8_t* bytes, uint8_t* nbytes, int ibc, int nbc, int off){
if(off+nbc > ibc)
fprintf(stderr,"Offset to large\n");
else{
for(int byte = 0; byte < nbc; byte++){
bytes[byte+off] = nbytes[byte];
}
}
}
//-----------------------------------------------------
// Socket Aux
//-----------------------------------------------------
int socket_to_buffer(Packet_Meta pm){
memset(pm->packet_buffer,0,MAX_IPV4 + 1);
struct sockaddr saddr;
int saddr_len = sizeof (saddr);
int reclen = recvfrom(pm->socket,pm->packet_buffer,MAX_IPV4 + 1,0,&saddr,(socklen_t *)&saddr_len);
if(reclen<0){
fprintf(stderr, "Error reading from Socket\n");
return 0;
}
pm->byte_count = reclen;
return 1;
}
void reset_pbp(Packet_Meta pm){
pm->pbp = 0;
}
int get_pbp(Packet_Meta pm){
return pm->pbp;
}
//----------------------------------------------------
// Checksum calculation
//----------------------------------------------------
uint16_t calc_ipv4_check(Packet_Meta pm, Packet p){
int count16 = (sizeof(p->ih)-1)/2;
uint8_t* buffer = pm->packet_buffer+(sizeof(p->eh)-4);
unsigned long sum;
for(sum=0;count16>0;count16--)
sum+=htons(*(buffer)++);
sum = ((sum>>16)+(sum&0xFFFF));
sum += (sum>>16);
return (uint16_t)(~sum);
}
//-----------------------------------------------------
// Load into Memory
//-----------------------------------------------------
int load_ip_header_f(Packet_Meta pm, IPv4_Header ih){
//read in first byte containing both version and IHL
if(!pm || !ih){
fprintf(stderr,"Either Packet_Meta or IPv4_Header is \
null at loading ip header\n");
return 0;
}
uint8_t temp;
uint16_t temp16;
fread(&temp, 1, 1, pm->packet);
ih->ihl = 0xF & temp;
temp >>= 4;
ih->version = 0xF & temp;
//read in byte containing dscp and ecn
fread(&temp, 1, 1, pm->packet);
ih->ecn = 0x3 & temp;
temp >>= 2;
ih->dscp = 0x3F & temp;
//read in total length
fread(&(ih->total_length), 2, 1, pm->packet);
//identification
fread(&(ih->identification), 2, 1, pm->packet);
//flags and fragment offset
fread(&temp16, 2, 1, pm->packet);
temp16 = ntohs(temp16);
ih->fragment_offset = 0x1FFF & temp16;
temp16 >>= 13;
ih->flags = 0x7 & temp16;
//ttl byte
fread(&(ih->ttl), 1, 1, pm->packet);
//protocol
fread(&(ih->protocol), 1, 1, pm->packet);
//checksum
fread(&(ih->header_checksum), 2, 1, pm->packet);
//source ip
fread(ih->source_ip, 1, 4, pm->packet);
//dest ip
fread(ih->destination_ip, 1, 4, pm->packet);
//calculate total length
pm->byte_count = ntohs(ih->total_length);
if(pm->byte_count>MAX_IPV4){ // Invalid IPv4 packet
fprintf(stderr, "WARNING: Byte count greater than the max. Truncating");
pm->byte_count = MAX_IPV4;
ih->total_length = htons(pm->byte_count); // reset total_length
}
if(pm->byte_count<MIN_IPV4){
fprintf(stderr, "WARNING: Byte count less than min.");
}
//calculate byte count and payload size
pm->payload_size = ntohs(ih->total_length) - ((ih->ihl*32)/8);
pm->ip_payload_count = pm->payload_size;
if(pm->ethernet_flag)
pm->byte_count += 14;
if(pm->fcs_active)
pm->byte_count += 4;
if(pm->pre_del)
pm->byte_count += 8;
return 1;
}
int load_eII_header_f(Packet_Meta pm, Ethernet_Header eh){
if(!pm || !eh){
fprintf(stderr,"Either Packet_Meta or Eth_Header is \
null at loading eth header\n");
return 0;
}
//read in the 6 byte destination mac
fread(eh->destination, 1, 6, pm->packet);
//read in the source mac
fread(eh->source, 1, 6, pm->packet);
//read in the ethertype
fread(&(eh->ethertype), 2, 1, pm->packet);
return 1;
}
int load_tcp_header_f(Packet_Meta pm, TCP_Header th){
if(!pm || !th){
fprintf(stderr, "Either Packet_Meta or TCP_Header is \
null at loading tcp header\n");
return 0;
}
//read in source port
fread(&th->source_port, 2, 1, pm->packet);
//read in dest port
fread(&th->destin_port, 2, 1, pm->packet);
//read in sequence number
fread(&th->seq_num, 4, 1, pm->packet);
//read in the ack number
fread(&th->ack_number, 4, 1, pm->packet);
uint8_t temp;
fread(&temp, 1, 1, pm->packet);
//ns flag
th->ns = 0x1 & temp;
temp >>= 1;
//reserved should be zero
th->reserved = 0x7 & temp;
temp >>= 3;
//data_offset
th->data_offset = 0xF & temp;
fread(&temp, 1, 1, pm->packet);
//the rest of the flags
th->fin = 0x1 & temp;temp>>=1;
th->syn = 0x1 & temp;temp>>=1;
th->rst = 0x1 & temp;temp>>=1;
th->psh = 0x1 & temp;temp>>=1;
th->ack = 0x1 & temp;temp>>=1;
th->urg = 0x1 & temp;temp>>=1;
th->ece = 0x1 & temp;temp>>=1;
th->cwr = 0x1 & temp;temp>>=1;
//win_size
fread(&th->win_size, 2, 1, pm->packet);
//checksum
fread(&th->check, 2, 1, pm->packet);
//urgent pointer
fread(&th->urgent_point, 2, 1, pm->packet);
//size of options
unsigned o_size = ((th->data_offset * 32) / 8) - 20;
th->options = (uint8_t*)calloc(o_size, sizeof(uint8_t));
fread(th->options, 1, o_size, pm->packet);
//recalculate payload size
pm->payload_size = pm->payload_size - ((th->data_offset * 32) / 8);
return 1;
}
int load_udp_header_f(Packet_Meta pm, UDP_Header uh){
if(!pm || !uh){
fprintf(stderr, "Either Packet_Meta or UDP_Header is \
null at loading udp header\n");
return 0;
}
fread(&uh->source_port, 2, 1, pm->packet);
fread(&uh->destin_port, 2, 1, pm->packet);
fread(&uh->length, 2, 1, pm->packet);
fread(&uh->check, 2, 1, pm->packet);
//recalculate payload size
pm->payload_size = ntohs(uh->length) - 8;
return 1;
}
//-----------------------------------------------------
// load into memory - socket
//-----------------------------------------------------
int load_eII_header_s(Packet_Meta pm, Ethernet_Header eh){
if(!pm || !eh){
fprintf(stderr, "Ether Packet_Meta or Ethernet_Header is \
null at loading tcp header\n");
return 0;
}
//copy in the 6 byte dest mac
memcpy(eh->destination, pm->packet_buffer+pm->pbp, 6);
pm->pbp+=6;
//copy in the source
memcpy(eh->source, pm->packet_buffer+pm->pbp, 6);
pm->pbp+=6;
//ethertype
memcpy(&(eh->ethertype),pm->packet_buffer+pm->pbp, 2);
pm->pbp+=2;
return 1;
}
int load_ip_header_s(Packet_Meta pm, IPv4_Header ih){
if(!pm || !ih){
fprintf(stderr, "Ether Packet_Meta or IPv4_Header is \
null at loading tcp header\n");
return 0;
}
uint8_t temp;
uint16_t temp16;
//read in first byte containing both version and IHL
memcpy(&temp,pm->packet_buffer+pm->pbp,1);
pm->pbp++;
ih->ihl = 0xF & temp;
temp >>= 4;
ih->version = 0xF & temp;
//read in byte containing dscp and ecn
memcpy(&temp,pm->packet_buffer+pm->pbp,1);
pm->pbp++;
ih->ecn = 0x3 & temp;
temp >>= 2;
ih->dscp = 0x3F & temp;
//read in total length
memcpy(&(ih->total_length),pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
//identification
memcpy(&(ih->identification),pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
//flags and fragment offset
memcpy(&temp16,pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
temp16 = ntohs(temp16);
ih->fragment_offset = 0x1FFF & temp16;
temp16 >>= 13;
ih->flags = 0x7 & temp16;
//ttl byte
memcpy(&(ih->ttl),pm->packet_buffer+pm->pbp,1);
pm->pbp++;
//protocol
memcpy(&(ih->protocol),pm->packet_buffer+pm->pbp,1);
pm->pbp++;
//checksum
memcpy(&(ih->header_checksum),pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
//source ip
memcpy(ih->source_ip,pm->packet_buffer+pm->pbp,4);
pm->pbp+=4;
//dest ip
memcpy(ih->destination_ip,pm->packet_buffer+pm->pbp,4);
pm->pbp+=4;
//calculate payload size
pm->payload_size = ntohs(ih->total_length) - ((ih->ihl*32)/8);
return 1;
}
int load_tcp_header_s(Packet_Meta pm, TCP_Header th){
if(!pm || !th){
fprintf(stderr, "Either Packet_Meta or TCP_Header is \
null at loading tcp header\n");
return 0;
}
//read in source port
memcpy(&th->source_port, pm->packet_buffer+pm->pbp, 2);
pm->pbp+=2;
//read in dest port
memcpy(&th->destin_port, pm->packet_buffer+pm->pbp, 2);
pm->pbp+=2;
//read in sequence number
memcpy(&th->seq_num, pm->packet_buffer+pm->pbp, 4);
pm->pbp+=4;
//read in the ack number
memcpy(&th->ack_number, pm->packet_buffer+pm->pbp, 4);
pm->pbp+=4;
uint8_t temp;
memcpy(&temp, pm->packet_buffer+pm->pbp, 1);
pm->pbp++;
//ns flag
th->ns = 0x1 & temp;
temp >>= 1;
//reserved should be zero
th->reserved = 0x7 & temp;
temp >>= 3;
//data_offset
th->data_offset = 0xF & temp;
memcpy(&temp, pm->packet_buffer+pm->pbp, 1);
pm->pbp++;
//the rest of the flags
th->fin = 0x1 & temp;temp>>=1;
th->syn = 0x1 & temp;temp>>=1;
th->rst = 0x1 & temp;temp>>=1;
th->psh = 0x1 & temp;temp>>=1;
th->ack = 0x1 & temp;temp>>=1;
th->urg = 0x1 & temp;temp>>=1;
th->ece = 0x1 & temp;temp>>=1;
th->cwr = 0x1 & temp;temp>>=1;
//win_size
memcpy(&th->win_size, pm->packet_buffer+pm->pbp, 2);
pm->pbp+=2;
//checksum
memcpy(&th->check, pm->packet_buffer+pm->pbp, 2);
pm->pbp+=2;
//urgent pointer
memcpy(&th->urgent_point, pm->packet_buffer+pm->pbp, 2);
pm->pbp+=2;
//size of options
unsigned o_size = ((th->data_offset * 32) / 8) - 20;
th->options = (uint8_t*)calloc(o_size, sizeof(uint8_t));
memcpy(th->options, pm->packet_buffer+pm->pbp, o_size);
pm->pbp+=o_size;
//recalculate payload size
pm->payload_size = pm->payload_size - ((th->data_offset * 32) / 8);
return 1;
}
int load_udp_header_s(Packet_Meta pm, UDP_Header uh){
if(!pm || !uh){
fprintf(stderr, "Either Packet_Meta or UDP_Header is \
null at loading udp header\n");
return 0;
}
memcpy(&uh->source_port,pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
memcpy(&uh->destin_port,pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
memcpy(&uh->length,pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
memcpy(&uh->check,pm->packet_buffer+pm->pbp,2);
pm->pbp+=2;
//recalculate payload size
pm->payload_size = ntohs(uh->length) - 8;
return 1;
}
//-----------------------------------------------------
// display functions - ethernet
//-----------------------------------------------------
void de_destination(Packet p){
for(int byte = 0; byte<6; byte++){
if(byte!=5)
printf("%02x:",*(p->eh->destination+byte));
else
printf("%02x",*(p->eh->destination+byte));
}
}
void de_source(Packet p){
for(int byte = 0; byte<6; byte++){
if(byte!=5)
printf("%02x:",*(p->eh->source+byte));
else
printf("%02x",*(p->eh->source+byte));
}
}
void de_ethtype(Packet p){
printf("0x%04x", ntohs(p->eh->ethertype));
}
void de_fcs(Packet p){
if(p->eh->fcs)
printf("0x%08x", ntohl(p->eh->fcs));
else
printf("fcs not active");
}
//-----------------------------------------------------
// display functions - ip
//-----------------------------------------------------
void di_version(Packet p){
printf("%u",p->ih->version);
}
void di_headerlen(Packet p){
printf("%u (%u bytes)",p->ih->ihl,((p->ih->ihl)*32)/8);
}
void di_dscp(Packet p){
uint8_t dsc = 0; dsc = p->ih->dscp & 0x3f;
uint8_t temp = p->ih->dscp; temp >>= 6;
uint8_t ecn = 0; ecn = temp & 0x3;
printf("DSC: %u ECN: %u", dsc, ecn);
}
void di_totlen(Packet p){
printf("%u", ntohs(p->ih->total_length));
}
void di_ident(Packet p){
printf("0x%04x", ntohs(p->ih->identification));
}
void di_flags(Packet p){
printf("(0x%02x) Reserved: %u, Don't fragment: %u, More fragments: %u", \
p->ih->flags, p->ih->flags&0x4, p->ih->flags&0x2, p->ih->flags&0x1);
}
void di_fragoff(Packet p){
printf("%u", p->ih->fragment_offset);
}
void di_ttl(Packet p){
printf("%u", p->ih->ttl);
}
void di_protocol(Packet p){
printf("%u", p->ih->protocol);
}
void di_headcheck(Packet p){
printf("0x%04x", ntohs(p->ih->header_checksum));
}
void di_source(Packet p){
for(int byte = 0; byte<4; byte++){
if(byte<3)
printf("%u:", p->ih->source_ip[byte]);
else
printf("%u", p->ih->source_ip[byte]);
}
}
void di_dest(Packet p){
for(int byte = 0; byte<4; byte++){
if(byte<3)
printf("%u:", p->ih->destination_ip[byte]);
else
printf("%u", p->ih->destination_ip[byte]);
}
}
//-----------------------------------------------------
// display functions - tcp
//-----------------------------------------------------
void dt_sport(Packet p){
printf("%u", ntohs(p->th->source_port));
}
void dt_dport(Packet p){
printf("%u", ntohs(p->th->destin_port));
}
void dt_seq(Packet p){
printf("%u", ntohl(p->th->seq_num));
}
void dt_ack(Packet p){
printf("%u", ntohl(p->th->ack_number));
}
void dt_reserved(Packet p){ // should be zero
printf("%u", p->th->reserved);
}
void dt_data_offset(Packet p){
printf("%u", p->th->data_offset);
}
void dt_flags(Packet p){
printf("\tns: %d\
\n\tcwr: %d\
\n\tece: %d\
\n\turg: %d\
\n\tack: %d\
\n\tpsh: %d\
\n\trst: %d\
\n\tsyn: %d\
\n\tfin: %d",
p->th->ns,
p->th->cwr,
p->th->ece,
p->th->urg,
p->th->ack,
p->th->psh,
p->th->rst,
p->th->syn,
p->th->fin);
}
void dt_win_size(Packet p){
printf("%u", ntohs(p->th->win_size));
}
void dt_check(Packet p){
printf("0x%04x", ntohs(p->th->check));
}
void dt_urgent_point(Packet p){
printf("%u", ntohs(p->th->urgent_point));
}
void dt_options(Packet p){
unsigned osize = ((p->th->data_offset * 32) / 8) - 20;
for(unsigned byte = 1; byte < osize+1; byte++){
printf("%02x ", p->th->options[byte-1]);
if(byte%4==0&&byte!=osize)
printf("\n\t");
}
}
//-----------------------------------------------------
// display fucntions - udp
//-----------------------------------------------------
void du_sport(Packet p){
printf("%u", ntohs(p->uh->source_port));
}
void du_dport(Packet p){
printf("%u", ntohs(p->uh->destin_port));
}
void du_length(Packet p){
printf("%u", ntohs(p->uh->length));
}
void du_check(Packet p){
printf("0x%04x", ntohs(p->uh->check));
}
//-----------------------------------------------------
// payload
//-----------------------------------------------------
int load_payload_f(Packet p, Packet_Meta pm){
if(!p->ih)
fprintf(stderr,"Warning; ip header is unloaded, continuing anyway.\n");
if(!p || !pm){
fprintf(stderr,"Packet or packet meta is NULL at load payload\n");
return 0;
}
p->payload = (uint8_t*)calloc(pm->payload_size,sizeof(uint8_t));
if(!p->payload){
fprintf(stderr,"Calloc failed at allocating payload\n");
return 0;
}
fread(p->payload, 1, pm->payload_size, pm->packet);
return 1;
}
int load_payload_s(Packet p, Packet_Meta pm){
if(!p->ih)
fprintf(stderr,"Warning; ip header is unloaded, continuing anyway.\n");
if(!p || !pm){
fprintf(stderr,"Packet or packet meta is NULL at load payload\n");
return 0;
}
p->payload = (uint8_t*)calloc(pm->payload_size,sizeof(uint8_t));
if(!p->payload){
fprintf(stderr,"Calloc failed at allocating payload\n");
return 0;
}
memcpy(p->payload,pm->packet_buffer+pm->pbp,pm->payload_size);
pm->pbp+=pm->payload_size;
return 1;
}
void display_payload_x(Packet p, Packet_Meta pm){
unsigned align = 0x0;
printf("%04x: ",align);align+=0x8;
for(unsigned byte = 1; byte<pm->payload_size+1; byte++){
if(byte%8==0){
printf("%02x\n",p->payload[byte-1]);
if(byte!=pm->payload_size){
printf("%04x: ",align);align+=0x8;
}
}
else
printf("%02x ",p->payload[byte-1]);
}
}
void display_payload_c(Packet p, Packet_Meta pm, char no_a_c){
unsigned align = 0x0;
unsigned ch = 0;
printf("%04x: ",align);align+=0x8;
for(unsigned byte = 1; byte<pm->payload_size+1; byte++){
ch = p->payload[byte-1];
//non printables
if(p->payload[byte-1] < 32 || p->payload[byte-1] >= 127)
ch = no_a_c;
if(byte%8==0){
printf(" %c\n", ch);
if(byte!=pm->payload_size){
printf("%04x: ",align);align+=0x8;
}
}
else
printf(" %c ", ch);
}
}
//-----------------------------------------------------
// destruction
//-----------------------------------------------------
int destroy_packet(Packet p){
if(!p){
fprintf(stderr,"Packet is Null at destruction\n");
return 0;
}
if(p->payload)
free(p->payload);
if(p->ih){
if(p->ih->source_ip)
free(p->ih->source_ip);
if(p->ih->destination_ip)
free(p->ih->destination_ip);
free(p->ih);
}
if(p->eh){
if(p->eh->destination)
free(p->eh->destination);
if(p->eh->source)
free(p->eh->source);
free(p->eh);
}
if(p->th){
if(p->th->options)
free(p->th->options);
free(p->th);
}
if(p->uh)
free(p->uh);
free(p);
return 1;
}
int destructor(Packet_Meta pm, Packet p){
if(!pm || !p){
fprintf(stderr,"Packet meta and/or packet are Null at destruction\n");
return 0;
}
if(pm->packet){
fclose(pm->packet);
free(pm->packet_buffer);
}
destroy_packet(p);
free(pm);
return 1;
}