2010-11-03 47 views
0

我正在瀏覽數據包注入器的一段代碼。當我試圖編譯,它是示出了錯誤:爲自定義數據包創建IP標頭時出錯

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。

回答

2

轉換的結果是一個右值,所以你不能指定它。對於這樣的情況下,你通常需要做的是這樣的:

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

即,取地址,強制轉換成一個指向正確的類型,並取消引用該指針。只要確保saddrdaddr成員的定義類型是可以實際上包含的地址。它通常應該是,但是雙重檢查不會受到傷害。