why do i get fwrite error while saving the data to a binary file?
have written a c program where packets are received from the network and saves it in a binary file called 'sniff_data.bin'. But during the compilation i am getting error as 'too few arguments to function fwrite'. What is wrong in my program? Can anybody help me out to solve it please? (In the program, handler used to open the binary file is 'logfile') here is my code:
here is my code:
void ProcessPacket(unsigned char* , int);
void print_ip_header(unsigned char* , int);
void print_tcp_packet(unsigned char * , int );
void print_udp_packet(unsigned char * , int );
void print_icmp_packet(unsigned char* , int );
void PrintData (unsigned char* , int);
FILE *logfile;
struct sockaddr_in source,dest;
int tcp=0,udp=0,icmp=0,others=0,igmp=0,total=0,i,j;
int main()
{
int saddr_size , data_size;
struct sockaddr saddr;
gopromiscous();
unsigned char *buffer = (unsigned char *) malloc(1024);
logfile=fopen("sniff_data.bin","wb");
if(logfile==NULL)
{ printf("Unable to create sniff_data file.");
}
printf("\n Starting..\n");
int sock_raw = socket( AF_PACKET , SOCK_RAW , htons(ETH_P_ALL)) ; setsockopt(sock_raw , SOL_SOCKET , SO_BINDTODEVICE , "eth0" , strlen("eth0")+ 1 );
int count=30;
while(count>=0)
{ count--; saddr_size = sizeof saddr; //Receive a packet data_size = recvfrom(sock_raw , buffer , 1024, 0 , &saddr ,
(socklen_t*)&saddr_size); if(data_size <0 ) { printf("Recvfrom error , failed to get packets\n"); return 1; } // if (fwrite(buffer, data_size, 1, logfile) != 1)
fwrite(buffer, data_size, 1, logfile); //Now process the packet ProcessPacket(buffer , data_size);
}
fclose(logfile);
close(sock_raw);
printf("\n");
printf(" Finished\n\n");
return 0;
}
void ProcessPacket(unsigned char* buffer, int size)
{
//Get the IP Header part of this packet , excluding the ethernet header
struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
++total;
switch (iph->protocol) //Check the Protocol and do accordingly...
{ case 1: //ICMP Protocol ++icmp; print_icmp_packet( buffer , size); break; case 2: //IGMP Protocol ++igmp; break; case 6: //TCP Protocol ++tcp; print_tcp_packet(buffer , size); break; case 17: //UDP Protocol ++udp; print_udp_packet(buffer , size); break; default: //Some Other Protocol like ARP etc. ++others; break;
}
printf(" TCP : %d UDP : %d ICMP : %d IGMP : %d Others : %d
Total : %d\r", tcp , udp , icmp , igmp , others , total);
}
void print_ethernet_header(unsigned char* Buffer, int Size)
{
struct ethhdr *eth = (struct ethhdr *)Buffer;
fwrite(logfile , "\n");
fwrite(logfile , "Ethernet Header\n");
fwrite(logfile , " |-Destination Address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
eth->h_dest[0] , eth->h_dest[1] , eth->h_dest[2] , eth->h_dest[3] , eth->h_dest[4] ,
eth->h_dest[5] );
fwrite(logfile , " |-Source Address : %.2X-%.2X-%.2X-%.2X-%.2X-%.2X \n",
eth->h_source[0] , eth->h_source[1] , eth->h_source[2] , eth->h_source[3] ,
eth->h_source[4] , eth->h_source[5] );
fwrite(logfile , " |-Protocol : %u \n",(unsigned short)eth->h_proto);
}
void print_ip_header(unsigned char* Buffer, int Size)
{
print_ethernet_header(Buffer , Size);
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr) );
iphdrlen =iph->ihl*4;
memset(&source, 0, sizeof(source));
source.sin_addr.s_addr = iph->saddr;
memset(&dest, 0, sizeof(dest));
dest.sin_addr.s_addr = iph->daddr;
fwrite(logfile , "\n");
fwrite(logfile , "IP Header\n");
fwrite(logfile , " |-IP Version : %d\n",(unsigned int)iph->version);
fwrite(logfile , " |-IP Header Length : %d DWORDS or %d Bytes\n",(unsigned
int)iph->ihl,((unsigned int)(iph->ihl))*4);
fwrite(logfile , " |-Type Of Service : %d\n",(unsigned int)iph->tos);
fwrite(logfile , " |-IP Total Length : %d Bytes(Size of
Packet)\n",ntohs(iph->tot_len));
fwrite(logfile , " |-Identification : %d\n",ntohs(iph->id));
//fprintf(logfile , " |-Reserved ZERO Field : %d\n",(unsigned
int)iphdr->ip_reserved_zero);
//fprintf(logfile , " |-Dont Fragment Field : %d\n",(unsigned
int)iphdr->ip_dont_fragment);
//fprintf(logfile , " |-More Fragment Field : %d\n",(unsigned
int)iphdr->ip_more_fragment);
fwrite(logfile , " |-TTL : %d\n",(unsigned int)iph->ttl);
fwrite(logfile , " |-Protocol : %d\n",(unsigned int)iph->protocol);
fwrite(logfile , " |-Checksum : %d\n",ntohs(iph->check));
fwrite(logfile , " |-Source IP : %s\n",inet_ntoa(source.sin_addr));
fwrite(logfile , " |-Destination IP : %s\n",inet_ntoa(dest.sin_addr));
}
void print_tcp_packet(unsigned char* Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)( Buffer + sizeof(struct ethhdr) );
iphdrlen = iph->ihl*4;
struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + tcph->doff*4;
fwrite(logfile , "\n\n***********************TCP
Packet*************************\n");
print_ip_header(Buffer,Size);
fwrite(logfile , "\n");
fwrite(logfile , "TCP Header\n");
fwrite(logfile , " |-Source Port : %u\n",ntohs(tcph->source));
fwrite(logfile , " |-Destination Port : %u\n",ntohs(tcph->dest));
fwrite(logfile , " |-Sequence Number : %u\n",ntohl(tcph->seq));
fwrite(logfile , " |-Acknowledge Number : %u\n",ntohl(tcph->ack_seq));
fwrite(logfile , " |-Header Length : %d DWORDS or %d BYTES\n" ,(unsigned
int)tcph->doff,(unsigned int)tcph->doff*4);
//fprintf(logfile , " |-CWR Flag : %d\n",(unsigned int)tcph->cwr);
//fprintf(logfile , " |-ECN Flag : %d\n",(unsigned int)tcph->ece);
fwrite(logfile , " |-Urgent Flag : %d\n",(unsigned int)tcph->urg);
fwrite(logfile , " |-Acknowledgement Flag : %d\n",(unsigned int)tcph->ack);
fwrite(logfile , " |-Push Flag : %d\n",(unsigned int)tcph->psh);
fwrite(logfile , " |-Reset Flag : %d\n",(unsigned int)tcph->rst);
fwrite(logfile , " |-Synchronise Flag : %d\n",(unsigned int)tcph->syn);
fwrite(logfile , " |-Finish Flag : %d\n",(unsigned int)tcph->fin);
fwrite(logfile , " |-Window : %d\n",ntohs(tcph->window));
fwrite(logfile , " |-Checksum : %d\n",ntohs(tcph->check));
fwrite(logfile , " |-Urgent Pointer : %d\n",tcph->urg_ptr);
fwrite(logfile , "\n");
fwrite(logfile , " DATA Dump ");
fwrite(logfile , "\n");
fwrite(logfile , "IP Header\n");
PrintData(Buffer,iphdrlen);
fwrite(logfile , "TCP Header\n");
PrintData(Buffer+iphdrlen,tcph->doff*4);
fwrite(logfile , "Data Payload\n");
PrintData(Buffer + header_size , Size - header_size );
fwrite(logfile , "\n###########################################################");
}
void print_udp_packet(unsigned char *Buffer , int Size)
{ unsigned short iphdrlen; struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr)); iphdrlen = iph->ihl*4; struct udphdr *udph = (struct udphdr*)(Buffer + iphdrlen + sizeof(struct ethhdr)); int header_size = sizeof(struct ethhdr) + iphdrlen + sizeof udph; fwrite(logfile , "\n\n***********************UDP Packet*************************\n");
print_ip_header(Buffer,Size);
fwrite(logfile , "\nUDP Header\n");
fwrite(logfile , " |-Source Port : %d\n" , ntohs(udph->source));
fwrite(logfile , " |-Destination Port : %d\n" , ntohs(udph->dest));
fwrite(logfile , " |-UDP Length : %d\n" , ntohs(udph->len));
fwrite(logfile , " |-UDP Checksum : %d\n" , ntohs(udph->check));
fwrite(logfile , "\n");
fwrite(logfile , "IP Header\n");
PrintData(Buffer , iphdrlen);
fwrite(logfile , "UDP Header\n");
PrintData(Buffer+iphdrlen , sizeof udph);
fwrite(logfile , "Data Payload\n");
//Move the pointer ahead and reduce the size of string
PrintData(Buffer + header_size , Size - header_size);
fwrite(logfile , "\n###########################################################"); }
void print_icmp_packet(unsigned char* Buffer , int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr));
iphdrlen = iph->ihl * 4;
struct icmphdr *icmph = (struct icmphdr *)(Buffer + iphdrlen + sizeof(struct
ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + sizeof icmph;
fwrite(logfile , "\n\n***********************ICMP
Packet*************************\n");
print_ip_header(Buffer , Size);
fwrite(logfile , "\n");
fwrite(logfile , "ICMP Header\n");
fwrite(logfile , " |-Type : %d",(unsigned int)(icmph->type));
if((unsigned int)(icmph->type) == 11)
{ fwrite(logfile , " (TTL Expired)\n");
}
else if((unsigned int)(icmph->type) == ICMP_ECHOREPLY)
{ fwrite(logfile , " (ICMP Echo Reply)\n");
}
fwrite(logfile , " |-Code : %d\n",(unsigned int)(icmph->code));
fwrite(logfile , " |-Checksum : %d\n",ntohs(icmph->checksum));
//fprintf(logfile , " |-ID : %d\n",ntohs(icmph->id));
//fprintf(logfile , " |-Sequence : %d\n",ntohs(icmph->sequence));
fwrite(logfile , "\n");
fwrite(logfile , "IP Header\n");
PrintData(Buffer,iphdrlen);
fwrite(logfile , "UDP Header\n");
PrintData(Buffer + iphdrlen , sizeof icmph);
fwrite(logfile , "Data Payload\n");
//Move the pointer ahead and reduce the size of string
PrintData(Buffer + header_size , (Size - header_size) );
fwrite(logfile , "\n###########################################################");
}
int gopromiscous()
{ int fd; struct ifreq eth; fd = socket(AF_INET, SOCK_PACKET, htons(0x800)); strcpy(eth.ifr_name, "eth0"); ioctl(fd, SIOCGIFFLAGS, ð); eth.ifr_flags |= IFF_PROMISC; ioctl(fd, SIOCSIFFLAGS, ð); printf("\n Entered Promiscuous Mode Successfully\n");
}
void PrintData (unsigned char* data , int Size)
{
int i , j;
for(i=0 ; i < Size ; i++)
{ if( i!=0 && i%16==0) //if one line of hex printing is complete... { fwrite(logfile , " "); for(j=i-16 ; j<i ; j++) { if(data[j]>=32 && data[j]<=128) fwrite(logfile , "%c",(unsigned char)data[j]); //if its a number or alphabet else fwrite(logfile , "."); //otherwise print a dot } fwrite(logfile , "\n"); } if(i%16==0) fwrite(logfile , " "); fwrite(logfile , " %02X",(unsigned int)data[i]); if( i==Size-1) //print the last spaces { for(j=0;j<15-i%16;j++) { fwrite(logfile , " "); //extra spaces } fwrite(logfile , " "); for(j=i-i%16 ; j<=i ; j++) { if(data[j]>=32 && data[j]<=128) { fwrite(logfile , "%c",(unsigned char)data[j]); } else { fwrite(logfile , "."); } } fwrite(logfile , "\n" ); } } } 1 Answer
I think this is the wrong forum for your question, but it looks like you have confused fwrite with fprintf.
fwrite always takes 4 parameters, none of which are '%c' or '%s'.
fprintf always requires two parameters, but may have more depending on the number of arguments specified in the format string (i.e. "%c %c %s" would mean fprintf requires 5 arguments -- one for the FILE*, the format string, and the three arguments (two characters and a string) .
1