标题:这段代码不知道哪里出了问题,求大神指点下。
取消只看楼主
尽在天涯
Rank: 1
等 级:新手上路
帖 子:18
专家分:1
注 册:2012-11-17
结帖率:50%
已结贴  问题点数:10 回复次数:3 
这段代码不知道哪里出了问题,求大神指点下。
#include <stdio.h>
#include <iostream.h>
#include <Winsock2.h>
#include <ws2tcpip.h>
#pragma comment (lib,"Ws2_32.lib")
#define BUFFER_SIZE 65535
#define IO_RCVALL _WSAIOW(IOC_VENDOR,1)

typedef struct IP_HEAD       //定义IP头
{
    union
    {
        unsigned char Version;           //版本(前4位)      
        unsigned char HeadLen;            //报头标长(后4位),IP头长度
    };
    unsigned char ServiceType;           //服务类型
    unsigned short TotalLen;              //数据报总长
    unsigned short Identifier;                    //标识
    union
    {
        unsigned short Flags;             //标识(前3位)
        unsigned short FragOffset;           //分段偏移(后13位)
    };
    unsigned char TimeToLive;            //生存周期
    unsigned char Protocol;              //协议
    unsigned short HeadChecksum;             //头校验和
    unsigned int SourceAddr;              //源地址
    unsigned int DestinAddr;              //目地地址
    unsigned char Options;               //选项
}IP_HEAD;

char * parseServiceType_getProcedence(BYTE b)
{
    switch(b>>5)                            //获取服务类型字段中优先级子域
    {
    case 7:                           
        return "Network Control";            //网络控制
        break;
    case 6:
        return "Internet work Control";     //网络控制
        break;
    case 5:
        return "CRITIC/ECP";              
        break;
    case 4:
        return "Flash Override";            //最优先信号
        break;
    case 3:
        return "Flsah";                  
        break;
    case 2:
        return "Immediate";              
        break;
    case 1:
        return "Priority";                    //协议
        break;
    case 0:
        return "Routine";                    //路由
        break;
    default:
        return "Unknow";
        break;
    }
}
char * parseServiceType_getTOS(BYTE b)
{
    b=(b>>1)&0x0f;                            //获取服务类型字段中的TOS子域
    switch(b)                        
    {                        
    case 0:
        return "Normal service";            //正常运行
        break;
    case 1:
        return "Minimize monetary cost";    //成本
        break;
    case 2:
        return "Maximize reliability";      //可靠性
        break;
    case 4:
        return "Maximize throughput";       //吞吐量
        break;
    case 8:
        return "Minimize delay";            //延迟
        break;
    case 15:
        return "Maximize security";         //安全性
        break;               
    default:
        return "Unknow";         
    }
}
char * getProtocol(BYTE Protocol)           //获取协议字段共8位
{
    switch(Protocol)                        //以下为协议号说明:
    {                              
    case 1:
        return "ICMP";                      //Internet控制报文协议
    case 2:
        return "IGMP";                      //Internet组管理协议
    case 4:
       return "IP in IP";                    //移动IP数据封装和隧道
    case 6:
        return "TCP";                        //传输控制协议
    case 8:
        return "EGP";                        //外部网关协议
    case 17:
        return "UDP";                        //用户数据报文协议
    case 41:
        return "IPv6";
    case 46:
        return "RSVP";                    //资源预留协议
    case 89:
        return "OSPF";                //Open Shortest Path First 开发式最短路径优先
    default:
        return "UNKNOW";
    }
}
void ipparse(FILE* file,char* buffer)
{
    IP_HEAD ip=*(IP_HEAD*)buffer;        //通过指针把缓冲区的内容强制转化为IP_HEADER数据结构
    fseek(file,0,SEEK_END);
    fprintf(file,"版本号=%d\r\n",ip.Version>>4);
    fprintf(file,"报头标长= %d (BYTE)\r\n",((ip.HeadLen & 0x0f)*4));
    fprintf(file,"服务器类型 = %s,%s\r\n",parseServiceType_getProcedence(ip.ServiceType),
parseServiceType_getTOS(ip.ServiceType));
    fprintf(file,"总长度 = %d(BYTE)\r\n",ip.TotalLen);
    fprintf(file,"标识 = %d\r\n",ip.Identifier);
    fprintf(file,"标志位 DF=%d , MF=%d\r\n",((ip.Flags>>14)&0x01),((ip.Flags>>13)&0x01));
    fprintf(file,"分段偏移值 = %d\r\n",(ip.FragOffset&0x1fff));
    fprintf(file,"生存期 = %d (hops)\r\n",ip.TimeToLive);
    fprintf(file,"协议 = %s\r\n",getProtocol(ip.Protocol));
    fprintf(file,"头校验和 = 0x%0x\r\n",ip.HeadChecksum);
    fprintf(file,"源IP地址 = %s\r\n",inet_ntoa(*(in_addr*)&ip.SourceAddr));
    fprintf(file,"目的IP地址 = %s\r\n",inet_ntoa(*(in_addr*)&ip.DestinAddr));
    fprintf(file,"---------------------------------------------\r\n");
}

int main()
{   
    FILE * file;
    if((file=fopen("history.txt","wb+"))==NULL)
    {   
        printf("fail to open file %s");
        return -1;   
    }

    unsigned char rv;
    WSADATA WSAData;             //定义了能够储存WSAStarup调用返回值的结构
    rv=MAKEWORD(2,2);            //Winsock2版本
    WSAStartup(rv,&WSAData);
   
    SOCKET sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP);  //创建套接字,sock为套接字描述符
    BOOL flag=true;
    setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(CHAR*)&flag,sizeof(flag));//任意类型、任意状态套接口的设置选项值

    char hostName[128];
    gethostname(hostName,100);     //获取主机名
   
    hostent * pHostIP;                //获取本地IP地址
    pHostIP = gethostbyname(hostName);

    sockaddr_in addr_in;
    addr_in.sin_addr=*(in_addr *)pHostIP->h_addr_list[0];
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = htons(6000);  //将无符号短整型主机数据转换为网络字节顺序数据
    bind(sock,(PSOCKADDR)&addr_in,sizeof(addr_in));

    DWORD dwBufferLen[10];                       //设置网卡为混杂模式
    DWORD dwBufferInLen=1;   
    DWORD dwBytesReturned=0;
    WSAIoctl(sock, IO_RCVALL,&dwBufferInLen,sizeof(dwBufferInLen),
    &dwBufferLen,sizeof(dwBufferLen),&dwBytesReturned,NULL,NULL);                                
    char buffer[BUFFER_SIZE];           //设置缓冲区
    char i,a;
    printf("数据包捕捉和解析程序正在启动\n");
    for(a=1;a<=10;a++)
        {   
            printf("...");
            Sleep(300);
        }
    printf("\n\n确定要接收并解析本机的数据包吗? Y/N \n");
    scanf("%c",&i);
    system("cls");

    while(i=='Y'||i=='y')
    {
        int size=recv(sock,buffer,BUFFER_SIZE,0);             //接收数据包
        if (size>0)
        {   
            printf("\n\n数据包捕获解析程序\n");
            printf("---------------------------------------------\n");
            ipparse(stdout,buffer);
            ipparse(file,buffer);
            printf("是否要继续接收并解析本机的数据包? Y/N \n");
            fflush(stdin);
            scanf("%c",&i);
            continue;
        }
        else
        fclose(file);
        return 0;
    }   
    closesocket(sock);
}
搜索更多相关主题的帖子: comment include 
2013-07-08 09:54
尽在天涯
Rank: 1
等 级:新手上路
帖 子:18
专家分:1
注 册:2012-11-17
得分:0 
运行之后没有结果,直接是please press any key to continue!
2013-07-08 12:28
尽在天涯
Rank: 1
等 级:新手上路
帖 子:18
专家分:1
注 册:2012-11-17
得分:0 
有没有可能是这个history.txt文件的问题。
    FILE * file;
    if((file=fopen("history.txt","wb+"))==NULL)
    {   
        printf("fail to open file %s");
        return -1;   
    }
我的这个文件是空的。
2013-07-08 12:49
尽在天涯
Rank: 1
等 级:新手上路
帖 子:18
专家分:1
注 册:2012-11-17
得分:0 
在我的电脑上运行捕捉不到IP数据包,但是在其他人的电脑上能够捕捉到。这是什么原因啊啊啊啊 啊
2013-07-10 10:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-416881-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.102491 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved