0

这是我在混杂模式下从以太网接收原始数据包的代码。编译时我在代码行中收到警告

logfile=open("sniff_data.bin",O_CREAT|O_APPEND|O_TRUNC,0777);

警告:赋值使指针从整数不进行强制转换[默认启用]。我检查了谷歌,但找不到解决方案。谁能告诉我解决方案以及我哪里出错了?

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 i,j;

int main()
{
    int saddr_size,data_size;
    struct sockaddr saddr;
    gopromiscous();
    unsigned char *buffer = (unsigned char *) malloc(1024);  

    logfile=open("sniff_data.bin",O_CREAT|O_APPEND|O_TRUNC,0777);  

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

    if(sock_raw < 0)
    {

        perror("Socket Error");
        return 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;
        }

        fwrite(buffer, data_size, 1, logfile);    

    }

    fclose(logfile);
    close(sock_raw);
    printf("\n");
    printf(" Finished\n\n");
    return 0;
}


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

 }
4

3 回答 3

6

从男人 2 打开

    int open(const char *path, int oflag, ...);

Open 返回一个 int(文件描述符),而不是 FILE *。因此,警告以及您的代码无法正常工作的原因。

您可能希望将 FILE * 更改为 int,并使用 write(2) 和 close(2) 而不是 fwrite 和 flcose

或使用 fopen(3)

于 2013-11-08T05:22:40.363 回答
2

或使用 fopen() 返回一个 FILE *

于 2013-11-08T05:28:26.277 回答
0

open()函数调用返回file descriptor类型 int。但是您分配的file streamFILE *类型的开放返回值。

当您使用时,open()您应该使用close()特定的描述符而不是fclose()

于 2013-11-08T05:23:11.503 回答