C++ 原始套接字发送TCP SYN-FIN-。。在c++;
我的老师想让我们做一个关于Windows上c++中原始套接字的练习(用于学习tcp通信) 我有一个问题。我看到了很多文档,但我不知道如何解决它C++ 原始套接字发送TCP SYN-FIN-。。在c++;,c++,windows,tcp,raw-sockets,C++,Windows,Tcp,Raw Sockets,我的老师想让我们做一个关于Windows上c++中原始套接字的练习(用于学习tcp通信) 我有一个问题。我看到了很多文档,但我不知道如何解决它 int raw() { WSADATA WSAData; SOCKET sock; SOCKADDR_IN sin,din; WSAStartup(MAKEWORD(2, 2), &WSAData); char datagram[MAX_PACKET_SIZE]; struct iphdr *ip
int raw()
{
WSADATA WSAData;
SOCKET sock;
SOCKADDR_IN sin,din;
WSAStartup(MAKEWORD(2, 2), &WSAData);
char datagram[MAX_PACKET_SIZE];
struct iphdr *iph = (struct iphdr *)datagram;
struct tcphdr *tcph = (struct tcphdr *)((UCHAR *)iph + sizeof(tcphdr));
char new_ip[sizeof "255.255.255.255"];
sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
cout << "failled init socket" << endl ;
else{
memset(datagram, 0, MAX_PACKET_SIZE); // Clear the data
setup_ip_header(iph);
setup_tcp_header(tcph);
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.1.10"); //source ip
din.sin_family = AF_INET;
din.sin_port = htons(DEST_PORT);
din.sin_addr.s_addr = inet_addr(TARGET_SERV_IP); //ip serv to connect
tcph->port_dest = htons(DEST_PORT);
iph->ip_dest = din.sin_addr.s_addr;
iph->ip_source = sin.sin_addr.s_addr;
iph->ip_dest = inet_addr(TARGET_SERV_IP); //ip serv to connect
iph->ip_source = inet_addr("192.168.1.10"); //source ip
//iph->checksum = csum((unsigned short *)datagram, iph->tot_len >> 1);
iph->checksum = csum((unsigned short *)datagram, sizeof(struct iphdr));
int one = 1;
const int *val = &one;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)val, sizeof(one)) < 0)
printf("failled set socket option IP_HDRINCL");
else{
if (sendto(sock, /* our socket */
datagram, /* the buffer containing headers and data */
ntohs( iph->tot_len), /* total length of our datagram */
0, /* routing flags, normally always 0 */
(struct sockaddr *) &sin, /* socket addr, just like in */
sizeof(sin)) < 0) /* a normal send() */
cout << stderr << "sendto() error!!!.\n " << WSAGetLastError() << endl;
else
cout << "packet send\n" << endl;
}
closesocket(sock);
}
}
intraw()
{
WSADATA WSADATA;
插座;
SOCKADDR_IN sin,din;
WSAStartup(MAKEWORD(2,2)和WSAData);
字符数据报[最大数据包大小];
结构iphdr*iph=(结构iphdr*)数据报;
结构tcphdr*tcph=(结构tcphdr*)((UCHAR*)iph+sizeof(tcphdr));
char new_ip[sizeof“255.255.255.255”];
sock=插座(PF_INET、sock_RAW、IPPROTO_TCP);
if(sock==无效的_套接字)
cout ip_dest=din.sin_addr.s_addr;
iph->ip\u source=sin.sin\u addr.s\u addr;
iph->ip_dest=inet_addr(TARGET_SERV_ip);//要连接的ip服务
iph->ip_source=inet_addr(“192.168.1.10”);//源ip
//iph->checksum=csum((无符号短*)数据报,iph->tot_len>>1);
iph->checksum=csum((无符号短*)数据报,sizeof(struct iphdr));
int-one=1;
常量int*val=&one;
如果(设置锁定选项(锁定,IP协议,IP协议,(字符*)值,大小(一个))<0)
printf(“故障设置插座选项IP_HDRINCL”);
否则{
如果(发送到)(sock,/*我们的套接字*/
数据报,/*包含标题和数据的缓冲区*/
ntohs(iph->tot_len),/*数据报的总长度*/
0,/*路由标志,通常始终为0*/
(struct sockaddr*)&sin,/*socket addr,与*/
sizeof(sin))<0)/*正常发送()*/
不能在代码中设置iph->tot_len
我推荐使用C++的组网代码是使用STD::String或STD::Vector:
std::vector<uint8_t> packet(MAX_PACKET_SIZE, 0);
...
packet.resize(real_size);
std::矢量数据包(最大数据包大小,0);
...
数据包大小(实际大小);
然后使用地址(&packet[0])进行指针操作。您的应用程序是否以管理员权限运行?原始套接字仅限管理员使用。我尝试通过右键单击和“以管理员身份运行”来实现相同效果。您是否完全填充了tcph?正在设置一些古怪的重复iph字段。