开发者

Error while creating ip header for a customized packet

I am going through a piece of code for packet injector. When I tried to compile it, It is showing the error :

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

Code for that particular part is :

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_a开发者_StackOverflow中文版ddr(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);

}

I have shown line 155 and 156 in the code. I can't see any problem there. Can anybody tell me what the error can be? Thanks in advance. Operating system : Ubuntu, compiler : GCC.


The result of a cast is an rvalue, so you can't assign to it. For a situation like this, you typically have to do something like:

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

I.e., take the address, cast that to a pointer to the correct type, and dereference that pointer. Just make sure the defined type of the saddr and daddr members is something that can actually hold the address. It normally should be, but it won't hurt to double check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜