日期:2014-05-16  浏览次数:20725 次

不到100行的libpcap程序,获取源IP与目的IP完全相同,不知道为什么,请高手给看看,谢谢
我编写的libpcap程序,分析网络数据包,发现分析出来的源IP与目的IP完全相同,而实际上不是这样的。请高手指点,谢谢。
直接编译:gcc   test.c   -o   test     -lpcap
test.c源代码如下(也可以直接下载附件):

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pcap.h>
#include <string.h>
#include <time.h>
#include <error.h>

struct   ether_header
{
        unsigned   char   source[6];
        unsigned   char   dest[6];
        unsigned   int   type;
};

struct   ip_header
{
#ifdef   WORDS_BIGENDIAN
        u_int8_t   ip_version:4,
                ip_header_length:4;
#else
        u_int8_t   ip_header_length:4,
                  ip_version:4;
#endif
        u_int8_t   ip_tos;             /*type   of   service   */
        u_int16_t   ip_length;         /*length   */
        u_int16_t   ip_id;         /*identity   */
        u_int16_t   ip_off;         /*   offset   */
        u_int8_t   ip_ttl;         /*time   to   live*/
        u_int8_t   ip_protocol;         /*   type   of   protocol*/
        u_int16_t   ip_chechsum;     /*   chechsum   */
        struct   in_addr   source;
        struct   in_addr   dest;
};

void   packet_callback(unsigned   char*   argument,const   struct   pcap_pkthdr*   packet_header,   const   unsigned   char*   packet_content)
{
        struct   ether_header*   eptr;
        struct   ip_header*   iptr;

        eptr=(struct   ether_header*)   packet_content;
        switch(ntohs(eptr-> type))
        {
                case   0x0800   :/*IP*/
                          iptr=(struct   ip_header*)(packet_content+14);
                          switch(iptr-> ip_protocol)
                          {
                                case   6:   /*   TCP   */
                                        printf( "%s   %s   \n ",   inet_ntoa(iptr-> source),inet_ntoa(iptr-> dest));
                  &nbs