-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_headers.c
63 lines (53 loc) · 1.98 KB
/
handle_headers.c
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
#include "pcap_sniff.h"
void HandleTCPHeader(struct iphdr *ip, unsigned int size_ip, const u_char *packet) {
unsigned int size_tcp;
unsigned short recv_check, calc_check; /* checksum variables */
struct tcphdr *tcp;
/*
* define/compute tcp header offset
*/
tcp = (struct tcphdr*) (packet + SIZE_ETHERNET + size_ip);
size_tcp = tcp->doff * 4;
if (size_tcp < 20) {
printf(" * Invalid TCP header length: %u bytes\n", size_tcp);
return;
}
recv_check = tcp->check;
calc_check = CalcTCPChecksum(ip, (unsigned short *) tcp);
printf(" Src port: %d\n", ntohs(tcp->source));
printf(" Dst port: %d\n", ntohs(tcp->dest));
printf(" Received checksum: %d\n", recv_check);
printf(" Calculated checksum: %d\n", calc_check);
}
void HandleUDPHeader(struct iphdr *ip, unsigned int size_ip, const u_char *packet) {
unsigned int size_udp;
unsigned short recv_check, calc_check; /* checksum variables */
struct udphdr *udp;
/*
* define/compute udp header offset
*/
udp = (struct udphdr*) (packet + SIZE_ETHERNET + size_ip);
size_udp = udp->len * 4;
if (size_udp < 20) {
printf(" * Invalid UDP header length: %u bytes\n", size_udp);
return;
}
recv_check = udp->check;
calc_check = CalcUDPChecksum(ip, (unsigned short *) udp);
printf(" Src port: %d\n", ntohs(udp->source));
printf(" Dst port: %d\n", ntohs(udp->dest));
printf(" Received checksum: %d\n", recv_check);
printf(" Calculated checksum: %d\n", calc_check);
}
void HandleICMPHeader(struct iphdr *ip, unsigned int size_ip, const u_char *packet) {
unsigned short recv_check, calc_check; /* checksum variables */
struct icmphdr *icmp;
/*
* define ICMP header offset
*/
icmp = (struct icmphdr*) (packet + SIZE_ETHERNET + size_ip);
recv_check = icmp->checksum;
calc_check = CalcICMPChecksum((unsigned short *)icmp, sizeof(struct icmphdr) + ntohs(ip->tot_len));
printf(" Received checksum: %d\n", recv_check);
printf(" Calculated checksum: %d\n", calc_check);
}