0

我正在浏览一段数据包注入器的代码。当我尝试编译它时,它显示错误:

IP-Packet-Injection.c:155: error: lvalue required as left operand of assignment
IP-Packet-Injection.c:156: error: lvalue required as left operand of assignment

该特定部分的代码是:

unsigned char *CreateIPHeader(/* Customize this as an exercise */)
{
        struct iphdr *ip_header;

        ip_header = (struct iphdr *)malloc(sizeof(struct iphdr));

        ip_header->version = 4;
        ip_header->ihl = (sizeof(struct iphdr))/4 ;
        ip_header->tos = 0;
        ip_header->tot_len = htons(sizeof(struct iphdr));
        ip_header->id = htons(111);
        ip_header->frag_off = 0;
        ip_header->ttl = 111;
        ip_header->protocol = IPPROTO_TCP;
        ip_header->check = 0; /* We will calculate the checksum later */
      /*this is line 155 */ (in_addr_t)ip_header->saddr = inet_addr(SRC_IP);
      /*this is line 156 */ (in_addr_t)ip_header->daddr = inet_addr(DST_IP);


        /* Calculate the IP checksum now : 
           The IP Checksum is only over the IP header */

        ip_header->check = ComputeIpChecksum((unsigned char *)ip_header, ip_header->ihl*4);

        return ((unsigned char *)ip_header);

}

我在代码中显示了第 155 行和第 156 行。我看不出那里有什么问题。谁能告诉我错误可能是什么?提前致谢。操作系统:Ubuntu,编译器:GCC。

4

1 回答 1

2

强制转换的结果是一个右值,所以你不能分配给它。对于这种情况,您通常必须执行以下操作:

*(in_addr_t *)(&(ip_header->saddr)) = in_addr(SRC_IP);

即,获取地址,将其转换为指向正确类型的指针,然后取消引用该指针。只要确保saddrdaddr成员的定义类型是可以实际保存地址的东西。通常应该是,但仔细检查不会有什么坏处。

于 2010-11-03T04:14:13.827 回答