2010-02-21 40 views
0

我有以下代碼在分組淨荷中更多的數據

INT ParseData(無符號字符*分組,INT LEN) { 結構ethhdr * ethernet_header; struct iphdr * ip_header; struct tcphdr * tcp_header; unsigned char * data; int data_len;

/* Check if any data is there */ 

    if(len > (sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr))) 
    { 

      ip_header = (struct iphdr*)(packet + sizeof(struct ethhdr)); 


      data = (packet + sizeof(struct ethhdr) + ip_header->ihl*4 + sizeof(struct tcphdr)); 
      data_len = ntohs(ip_header->tot_len) - ip_header->ihl*4 - sizeof(struct tcphdr); 

      if(data_len) 
      { 
        printf("Data Len : %d\n", data_len); 
        PrintData("Data : ", data, data_len); 
        printf("\n\n"); 
        return 1; 
      } 
      else 
      { 
        printf("No Data in packet\n"); 
        return 0; 
      } 
    } 

}

我想在ASCII打印的有效載荷,並用這樣的

PrintData(字符* MESG,無符號字符* P,INT len)將 { 的printf一個簡單的函數(MESG);

while(len--) 
    { 
      if(isprint(*p)) 
        printf("%c", *p); 
      else 
        printf("."); 
      p++; 
    } 

}

的代碼看起來很好,沒有問題編譯/警告。問題在於第一個有效載荷 字符未在0位打印,而是在12個字節之後打印。

我認爲所有的「len」字節都是我必須打印的確切數據。我的數據點在 data =(packet + sizeof(struct ethhdr)+ ip_header-> ihl * 4 + sizeof(struct tcphdr));我的數據點在 data = 但是數據[0]不可打印。問題是什麼?我想念什麼?我必須檢查TCP選項部分嗎?

感謝

回答

0

這是正確的,加入的sizeof(結構tcphdr)只會讓你過去的頭,而不是選擇。要獲得實際的數據,您應該使用TCP標頭中的'offset'字段。該偏移量是從TCP報頭的開始計算的,並且是以4字節爲單位的,例如,如果偏移是8,則報頭+選項長度爲32

+0

換句話說代替的sizeof(結構tcphdr)我需要把 tcp_header-> DOFF * 4 – cateof

+0

是的,完全正確。 –