I'm trying to write a program that reads the network card and then discover if it is a TCP or a UDP packet. I'm not used to network protocols neither the use of sockets. The code below is the code of my thread one, it does the job of reading and knowing if it is a UDP or TCP. But with the last parameter as 0 (I'm trying to receive all sorts of packets), I'm not getting the right ones. Thanks for your help guys (=
void *Thread1(){
int saddr_size , data_size, raw_socket, seconds, tcp, udp;
struct sockaddr saddr;
struct iphdr *header;
unsigned char *buffer = (unsigned char *)malloc(65536);
raw_socket = socket(AF_INET , SOCK_RAW , 0);
seconds = 30;
tcp = 0;
udp = 0;
while(seconds--){
data_size = recvfrom(raw_socket , buffer , 65536 , 0 , &saddr , &saddr_size);
header = (struct iphdr*)buffer;
if((unsigned int)header->protocol == 6){
printf("This is a UDP packet\n");
//Buffer12->isTCP[seconds] = 0;
//Buffer12->isUDP[seconds] = 1;
//Buffer12->bytes = ntohs(udph->tot_len);
} else if((unsigned int)header->protocol == 17){
printf("This is a TCP packet\n");
//Buffer12->isTCP[seconds] = 1;
//Buffer12->isUDP[seconds] = 0;
//Buffer12->bytes = ntohs(tcph->tot_len);
}
sleep(1);
}
return NULL;
}