CSDN草稿出問題了。讀到別人的草稿了。

來源:互聯網
上載者:User

 

使用WinPcap抓取ARP資料包,其中定義ARP資料包結構體如下:
typedef struct ArpPacket
{
 u_int16_t macaddrtype;
 u_int16_t proaddrtype;
 u_int8_t  macaddrlen;
 u_int8_t  proaddrlen;
 u_int16_t operationtype;
 u_int8_t  srcmac[6];
 struct in_addr srcip;// u_int8_t  srcip[4];定義為字元數組能正確輸出
 u_int8_t  dstmac[6];
 struct in_addr dstip;//u_int8_t  dstip[4];
}AP;

 

在來源程式中讀取協議欄位內容:

......
p=parp->srcmac;
printf("源MAC:%02x:%02x:%02x:%02x:%02x:%02x/n",p[0],p[1],p[2],p[3],p[4],p[5]);
p=parp->dstmac;
printf("源ip:%s/n",inet_ntoa(parp->srcip));// 出錯
printf("目的MAC:%02x:%02x:%02x:%02x:%02x:%02x/n",p[0],p[1],p[2],p[3],p[4],p[5]);
printf("目的ip:%s/n",inet_ntoa(parp->dstip));//出錯

 

結果:源MAC地址能正確輸出,但輸出源IP地址時,跳過2個位元組,輸出後面4個位元組內容;後

面目的MAC地址輸出往後6個位元組,輸出目的IP時,又跳過2個位元組,輸出後面4個位元組!

但當把源IP定義為"u_int8_t  srcip[4];"時就能正確輸出;

然而,IP資料包首部最後兩個欄位為源IP和目的IP,都定義為struct in_addr類型,可以正確

輸出IP地址;我將ARP資料包的兩個IP地址欄位移到ARP資料包結構體最後2個欄位,也能正確

輸出!

我現在的想法是:struct in_addr類型欄位不能放在結構體中間,放在最後可以,但不知道具

體原因。

 

 PS:

2010-08-09 補充

這是由於定義結構體時,位元組對齊問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.