Hi, I've been messing around with raw sockets today, basically trying to recieve all incoming packets to dump the Ip header (this is just for curiosity not malicious intent, and i do mean that.). My problem is , how do i convert my read buffer to a struct iphdr.. infact im not fully sure im doing any this right.. as i am still quite new to c.
any help would be great, heres the code.
Code:
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#define BUF_SIZE 8192
int main(){
unsigned char buffer[BUF_SIZE];
unsigned char ipbuf[20];
struct iphdr *ip;
int fd = socket(PF_INET, SOCK_RAW, 6);
while (read (fd, buffer, BUF_SIZE) > 0){
bzero(buffer, BUF_SIZE);
int i;
for(i=0;i<20;i++){
ipbuf[i] = buffer[i]; //get first 20 bytes (ip header)
}
ip = ipbuf;
printf("%d", ip->version); //i get segmentation fault here
}
return 0;
}