Repository navigation
Expand file tree
/
Copy pathip.c
More file actions
56 lines (49 loc) · 2.7 KB
/
Copy pathip.c
File metadata and controls
56 lines (49 loc) · 2.7 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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "ip.h"
#include "inet.h"
void ip_hdr_dbg(struct ip_hdr *ip_header, int dump_form){
char ip_src[IP_CHAR_LEN];
char ip_dest[IP_CHAR_LEN];
unsigned char *hdr = malloc(sizeof(unsigned char) * sizeof(*ip_header));
switch (dump_form){
case INTEGER:
ntop(ntohl(ip_header->source_address), ip_src);
ntop(ntohl(ip_header->destination_address), ip_dest);
printf("IP Header\n");
printf("Version : %d\n", ip_header->ihl);
printf("IHL : %d Bytes\n", (ip_header->version)*4);
printf("Type_of_Service: %d \n", ip_header->type_of_service);
printf("Total Length : %d Bytes\n", ntohs(ip_header->total_length)); printf("Identification : %2x \n", ip_header->identification);
printf("Flagment_Offset: %d \n", ntohs(ip_header->fragment_offset));
printf("Time To Live : %d \n", ip_header->ttl);
printf("Protocol : %d \n", ip_header->protocol);
printf("Checksum : %d \n", ntohs(ip_header->hdr_checksum));
printf("source IP : %s\n", ip_src);
printf("destination IP : %s\n", ip_dest);
printf("==================================================\n");
break;
case HEX:
memset(hdr,0, sizeof(unsigned char) * 20);
// set binary of the IP header
memcpy(hdr,ip_header, sizeof(*ip_header));
for (int i = 0; i < sizeof(*ip_header); ++i) printf("%02X ", hdr[i]);
printf("\n");
printf("IP Header\n");
printf("Version : %x\n", ip_header->ihl);
printf("IHL : %x Bytes\n", ip_header->version);
printf("Type_of_Service: %x \n", ip_header->type_of_service);
printf("Total Length : %2x Bytes\n", ntohs(ip_header->total_length));
printf("Identification : %2x \n", ntohs(ip_header->identification));
printf("Flagment_Offset: %2x \n", ntohs(ip_header->fragment_offset));
printf("Time To Live : %x \n", ip_header->ttl);
printf("Protocol : %x \n", ip_header->protocol);
printf("Checksum : %2x \n", ntohs(ip_header->hdr_checksum));
printf("source IP : %4x \n", ntohl(ip_header->source_address));
printf("destination IP : %4x \n", ntohl(ip_header->destination_address));
printf("__________________________________________________\n");
break;
}
}