abwihew 2015-12-06 01:24 采纳率: 0%
浏览 1549
已结题

c++ SYN 网络编程遇到问题 小白求教

以下是代码,运行无错误,但是用wireshark 抓不到发出的SYN包,求高手解释原因。。

 #include <stdlib.h>
#include <stdio.h>

#include <winsock2.h>       //加入WinSock的头文件
#include <WS2TCPIP.h>       //设置IP_HDRINCL选中需要
#include <time.h>

#include <iostream>
using namespace std;
#pragma comment(lib,"Ws2_32.lib")    //加入库函数


typedef struct //定义IP首部 
{ 
    unsigned char h_verlen; //4位首部长度,4位IP版本号 
    unsigned char tos; //8位服务类型TOS 
    unsigned short total_len; //16位总长度(字节) 
    unsigned short ident; //16位标识 
    unsigned short frag_and_flags; //3位标志位 
    unsigned char ttl; //8位生存时间 TTL 
    unsigned char proto; //8位协议 (TCP, UDP 或其他) 
    unsigned short checksum; //16位IP首部校验和 
    unsigned int sourceIP; //32位源IP地址 
    unsigned int destIP; //32位目的IP地址 
}IPHEADER; 


typedef struct  //定义TCP首部 
{ 
    USHORT th_sport; //16位源端口 
    USHORT th_dport; //16位目的端口 
    unsigned int th_seq; //32位序列号 
    unsigned int th_ack; //32位确认号 
    unsigned char th_lenres; //4位首部长度/6位保留字 
    unsigned char th_flag; //6位标志位 
    USHORT th_win; //16位窗口大小 
    USHORT th_sum; //16位校验和 
    USHORT th_urp; //16位紧急数据偏移量 
}TCPHEADER; 

typedef struct  //定义TCP伪首部 
{ 
    unsigned long saddr; //源地址 
    unsigned long daddr; //目的地址 
    char mbz; 
    char ptcl; //协议类型 
    unsigned short tcpl; //TCP长度 
}PSDHEADER;

//计算校验和函数
USHORT checksum(USHORT *buffer, int size)
{
    unsigned long cksum=0; 
    while(size >1) 
    { 
        cksum+=*buffer++; 
        size -=sizeof(USHORT); 
    } 
    if(size ) 
    { 
        cksum += *(UCHAR*)buffer; 
    } 

    cksum = (cksum >> 16) + (cksum & 0xffff); 
    cksum += (cksum >>16); 
    return (USHORT)(~cksum); 
}

//void packet_input();


int main()
{
    WSADATA WSAData;
    if ( WSAStartup(MAKEWORD(2,2), &WSAData)!=0 )
    {
        printf("InitWSAStartup Error!\n");
        return 0;
    }



    char ip[20]="192.168.3.1";
  u_short port;

    SOCKET sock; 
    SOCKADDR_IN addr_in;
    IPHEADER ipHeader; 
    TCPHEADER tcpHeader; 
    PSDHEADER psdHeader; 

    char szSendBuf[60]={0}; 
    int rect,nTimeOver;

    if ((sock = WSASocket(2,SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED))==INVALID_SOCKET)
    {
        printf("sock err!");
        return 0;
    }

    //设置IP_HDRINCL选项,声明自己填充IP头部
    BOOL blnFlag=TRUE; 

    if(setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&blnFlag, sizeof(blnFlag))==INVALID_SOCKET)
    {
        printf("opt err!");
        return 0;
    }

nTimeOver=1000; 
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&nTimeOver, sizeof(nTimeOver))==SOCKET_ERROR) 
    { 
        printf("time err!");
        return 0; 
    } 


//////////////////////////////////////////////////////////////////////////

    port=80;

    addr_in.sin_family=AF_INET; 
    addr_in.sin_port=htons(port);                 //目标端口
    addr_in.sin_addr.S_un.S_addr=inet_addr(ip);   //目标IP

    while(true)
    {
        printf(".");
        for(int counter=0;counter<10240;counter++)
        {

        //填充IP首部 
ipHeader.h_verlen=(4<<4 | sizeof(ipHeader)/sizeof(unsigned long)); //高四位IP版本号,低四位首部长度
ipHeader.tos=0; 
ipHeader.total_len=htons(sizeof(ipHeader)+sizeof(tcpHeader));   //16位总长度
ipHeader.ident=htons(1); 
ipHeader.frag_and_flags=0; 
ipHeader.ttl=128;     //8位生存时间TTL
ipHeader.proto=IPPROTO_TCP; 
ipHeader.checksum=0; 
ipHeader.sourceIP=inet_addr("192.168.3.70"); 
ipHeader.destIP=inet_addr(ip); 

        //填充TCP首部  
        tcpHeader.th_dport=htons(port);   //源端口号
        tcpHeader.th_sport=htons(2337);  //目的端口号
        tcpHeader.th_seq=0;   //SYN序列号
        tcpHeader.th_ack=0;     //ACK序列号置0
        tcpHeader.th_lenres=(sizeof(tcpHeader)/4<<4|0);   //tcp长度和保留位
        tcpHeader.th_flag=2; //修改这里来实现不同的标志位探测,2是SYN,1是FIN,16是ACK探测 等等 
        tcpHeader.th_win=htons(512); 
        tcpHeader.th_urp=0; 
        tcpHeader.th_sum=0;

        psdHeader.saddr=ipHeader.sourceIP; 
        psdHeader.daddr=ipHeader.destIP; 
        psdHeader.mbz=0; 
        psdHeader.ptcl=IPPROTO_TCP; 
        psdHeader.tcpl=htons(sizeof(tcpHeader));


        //计算校验和 
        memcpy(szSendBuf, &psdHeader, sizeof(psdHeader)); 
        memcpy(szSendBuf+sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader)); 
        tcpHeader.th_sum=checksum((USHORT *)szSendBuf,sizeof(psdHeader)+sizeof(tcpHeader)); 


        memcpy(szSendBuf, &ipHeader, sizeof(ipHeader)); 
        memcpy(szSendBuf+sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader)); 
        ipHeader.checksum=checksum((USHORT *)szSendBuf, sizeof(ipHeader)); 


        memcpy(szSendBuf, &ipHeader, sizeof(ipHeader)); 

rect=sendto(sock, szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader),0, (SOCKADDR*)&addr_in, sizeof(SOCKADDR)); 

        if (rect==SOCKET_ERROR) 
        { 
            printf("Send err!");
            return 0; 
        } 
        else
        {   printf("Ip Ident:%d\n",ntohs(ipHeader.ident));
        struct in_addr a;
        a.s_addr=ipHeader.sourceIP;
        printf("Ip saddr:%s\n",inet_ntoa(a));
        //printf("Ip daddr:%s\n",ipHeader.destIP);
        }
        }


    } // end while

    closesocket(sock); 

    return 0;
}

  • 写回答

5条回答 默认 最新

  • threenewbee 2015-12-06 03:42
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?