Thread: Raw Sockets Help

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Raw Sockets Help

    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;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You get seg fal because u havn&#180;t allocated space for your IP. And you are trying to assign a char * to struct iphdr *, did u enable your warning while compiling your code. The compiler might have complained about it.

    Hence your havn&#180;t allocated space you can;t dereference its member elements, which u are doing it.

    ssharish2005

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3
    right, so should i do something like this...

    Code:
    ip = malloc(sizeof(struct iphdr));
    ?

    im still confused as to how convert the buffer to the iphdr struct...

    sorry if im a pain in bum.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (read (fd, buffer, BUF_SIZE) > 0)
    > bzero(buffer, BUF_SIZE);
    You read a buffer, then immediately erase it.

    > struct iphdr *ip;
    Try
    struct iphdr ip;

    Then inside the loop
    memcpy( &ip, buffer, sizeof ip );

    Then
    printf("&#37;d", ip.version);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    3
    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3way handshake raw sockets
    By linuxtest in forum Linux Programming
    Replies: 2
    Last Post: 10-11-2008, 05:29 PM
  2. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  3. Raw Sockets problem.
    By Mad_guy in forum C Programming
    Replies: 3
    Last Post: 07-03-2005, 03:14 PM
  4. raw sockets?
    By Devil Panther in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-01-2003, 01:23 AM
  5. Problem with Raw Sockets
    By Encrypted in forum Linux Programming
    Replies: 2
    Last Post: 04-30-2003, 04:38 PM