Socket receives packet wrong

101 views Asked by At

I have written a simple raw socket which is supposed to receive single ipv4 packets written and assembled using libnet. This is the code for the libnet packet, which works just fine (I think, there was no way to check the actual payload that was sent):

int main() {
uint32_t src, dst, seq, ack;
char payload1[1024], destip[16];
uint16_t sp = 2001, dp = 2000;
libnet_ptag_t tcp;
libnet_ptag_t ip4;
char errbuf[LIBNET_ERRBUF_SIZE];
int i, bytes;
int len;
libnet_t * l;

l = libnet_init(LIBNET_RAW4, NULL, errbuf);

//here follows the usual declaration of addresses and the payload using scanf 
//and the command terminal (scanf("%s", payload);)


tcp = libnet_build_tcp(sp, dp, seq, ack, TH_SYN, 0, 0, 0, LIBNET_TCP_H + 
      strlen(payload), NULL, 0, l, 0);


ip4 = libnet_build_ipv4(LIBNET_TCP_H + LIBNET_IPV4_H + strlen(payload), 0, 1, 
0, 64, IPPROTO_TCP, 0, src, dst, payload, strlen(payload), l, 0);   


bytes = libnet_write(l);
return 0;
}

This program works, as the packets appear in the network when using tcpdump. Now the program for the raw socket:

int main() {
struct sockaddr_in server_addr;
char message[2000];
int sockfd, bytes;

//server_addr is filled out for the listen() call...


sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);        

if(bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) 
{
printf("error binding\n");
perror("bind()");
exit(0);
}

while(1) {                                         
bytes = recv(sockfd, message, 2000, 0);
if(bytes == -1)
continue;

printf("captured %d bytes\n%s\n", bytes, message);
printf("as hex: %x\n", message);                   //I tried that to find 
                                                   //out, if there was any 
                                                   //kind of received message 
                                                   //because at first, the 
                                                   //socket didn't receive 
                                                   //anything, except that it 
                                                   //assured me the receiving 
                                                   //of ... bytes (, so I 
                                                   //wanted to print out the 
                                                   //raw bytes.)

 memset(message, 0, 2000);

}
}

Now, the socket manages to receive bytes, for the printed out byte value shows a number, also the exact number which is given as packet size from the first program (raw ipv4 header + payload = 40 bytes + payload size). Only the received message itself seems to suffer from some kind of error, because whatever I send in my packets, the %s output of the message is always "E". The hex output varies from packet to packet, but it doesn't mach the hex version of my given payload (, when I print out the payload from the packet program in hex) and it most certainly doesn't have any relevant value. Now I also thought, that maybe the ipv4 header is also received as the message, so I wrote a version which only reads the 40+ bytes of the message buffer, but the results ("E" and random hex) are the same.

What am I doing wrong? Could it be the wrong recv function? Or do I need to typecast my message buffer a little? Or could it even be an error in my packet sending program?

0

There are 0 answers