I have been asked to read data from a binary file ( TCPdump ) using a pointer. I could read the data using a variable (non-pointer) and the console window did show me some numbers.
But when I tried doing the same thing with a pointer the program crashed? Is this because I'm not calling malloc() when initializing the pointer? Or is it the syntax when I'm using the pointer that is wrong?
I uploaded the TCPdump file to this folder (If you need to see it)
My Files
The package looks like:
Ethernet | IP | TCP | payload
14 bytes | IP_HL bytes | TH_OFF bytes |
main.c (Laboration3.c)
Code:
#include <stdio.h>
#include "framehdr.h"
#define SIZE_ETHERNET 14
int main() {
FILE *file = fopen("TCPdump", "rb");
if (file == NULL)
printf("Error");
printf("--- Ethernet ---\t");
printf("--- IP ---");
printf("\t\t--- TCP ---\n");
// Why doesn't this work?
struct ethernet_hdr* getEthernet;
fread((struct ethernet_hdr*)&getEthernet, sizeof(struct ethernet_hdr), 1, file);
printf("\ndhost: %s", getEthernet->dhost);
printf("shost: %s", getEthernet->shost);
struct ip_hdr getIP;
fread((struct ip_hdr*)&getIP, sizeof(struct ip_hdr), 1, file);
struct tcp_hdr getTCP;
fread((struct tcp_hdr*)&getTCP, sizeof(struct tcp_hdr), 1, file);
printf("\t\t vhl: %c", getIP.vhl); //printf("\t\t th_sport: %hd", getTCP.th_sport);
printf("\n\t\t\t tos: %c", getIP.tos); //printf("\t\t th_dport: %hd", getTCP.th_sport);
printf("\n\t\t\t len: %hd", getIP.len); //printf("\t\t\tth_seq: %d", getTCP.th_sport);
printf("\n\t\t\t id: %hd", getIP.id); printf("\t\t\tth_ack: %d", getTCP.ack);
printf("\n\t\t\t off: %hd", getIP.off); printf("\t\tth_offx2: %c", getTCP.offx2);
printf("\n\t\t\t ttl: %c", getIP.ttl); printf("\t\t\tth_flags: %c", getTCP.flags);
printf("\n\t\t\t p: %c", getIP.p); printf("\t\t\tth_win: %hd", getTCP.win);
printf("\n\t\t\t ip_sum: %hd", getIP.ip_sum); printf("\t\tth_sum: %hd", getTCP.sum);
printf("\n\t\t\t src: %d", getIP.src); printf("\tth_urp: %hd", getTCP.urp);
printf("\n\t\t\t dst: %d", getIP.dst);
fclose(file);
return 0;
}
framehdr.h
Code:
/*
* Credit to Tim Carstens for the contents of this file
* http://www.tcpdump.org/pcap.html
*/
#ifndef FRAMEHDR_H
#define FRAMEHDR_H
/* Ethernet addresses are 6 bytes */
#define ETHER_ADDR_LEN 6
/* Ethernet header */
struct ethernet_hdr {
unsigned char dhost[ETHER_ADDR_LEN]; /* Destination host address */
unsigned char shost[ETHER_ADDR_LEN]; /* Source host address */
unsigned short type; /* IP? ARP? RARP? etc */
};
/* IP header */
struct ip_hdr {
unsigned char vhl; /* Version and header length */
unsigned char tos; /* Type of service */
unsigned short len; /* Total length */
unsigned short id; /* Identification */
unsigned short off; /* Fragment offset field */
unsigned char ttl; /* Time to live */
unsigned char p; /* Protocol */
unsigned short ip_sum; /* Checksum */
unsigned int src, dst; /* Source and dest address */
};
#define IP_HL(ip) ((((ip)->vhl) & 0x0f) * 4) /* Gets length of the IP header, use with (ip_hdr *) */
/* TCP header */
typedef unsigned int tcp_seq;
struct tcp_hdr {
unsigned short sport; /* Source port */
unsigned short dport; /* Destination port */
tcp_seq seq; /* Sequence number */
tcp_seq ack; /* Acknowledgement number */
unsigned char offx2; /* Data offset, rsvd */
unsigned char flags;
unsigned short win; /* Window */
unsigned short sum; /* Checksum */
unsigned short urp; /* Urgent pointer */
};
#define TH_OFF(th) ((((th)->offx2 & 0xf0) >> 4) * 4) /* Gets length of the TCP header, use with (tcp_hdr *) */
#endif