por que recebo o erro fwrite ao salvar os dados em um arquivo binário?

0

escreveu um programa em que os pacotes são recebidos da rede e os salva em um arquivo binário chamado 'sniff_data.bin'. Mas durante a compilação estou recebendo erro como "muito poucos argumentos para funcionar fwrite". O que está errado no meu programa? Alguém pode me ajudar a resolvê-lo, por favor? (No programa, manipulador usado para abrir o arquivo binário é 'logfile') aqui está o meu código:

aqui está o meu código:

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

    eth.ifr_flags |= IFF_PROMISC;

    ioctl(fd, SIOCSIFFLAGS, &eth);
    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" );
    }
  }
 }
    
por shishira 07.11.2013 / 06:27

1 resposta

0

Acho que este é o fórum errado para sua pergunta, mas parece que você confundiu fwrite com fprintf.

fwrite sempre leva 4 parâmetros, nenhum dos quais é '% c' ou '% s'.

O fprintf sempre requer dois parâmetros, mas pode ter mais dependendo do número de argumentos especificados na string de formato (por exemplo, "% c% c% s" significaria que fprintf requer 5 argumentos - um para o FILE *, o formato string, e os três argumentos (dois caracteres e uma string).

    
por Scott Salley 07.11.2013 / 07:29