为什么将数据保存到二进制文件时会出现 fwrite 错误?

为什么将数据保存到二进制文件时会出现 fwrite 错误?

编写了一个程序,从网络接收数据包并将其保存在名为“sniff_data.bin”的二进制文件中。但在编译过程中,我收到错误“函数 fwrite 的参数太少”。我的程序出了什么问题?有人能帮我解决吗?(在程序中,用于打开二进制文件的处理程序是“logfile”)这是我的代码:

这是我的代码:

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

答案1

我认为这个论坛不适合回答您的问题,但看起来您混淆了 fwrite 和 fprintf。

fwrite 总是接受 4 个参数,其中没有一个是“%c”或“%s”。

fprintf 始终需要两个参数,但可能具有更多参数,具体取决于格式字符串中指定的参数数量(即“%c %c %s”意味着 fprintf 需要 5 个参数——一个用于 FILE*,一个用于格式字符串,三个参数(两个字符和一个字符串)。

相关内容