桥接网络模式无法路由数据包。

huangapple go评论141阅读模式
英文:

bridged network mode unable to route packet

问题

我想从我的主机发送UDP数据包到我的虚拟机。我的代码没有错误,但我的虚拟机中的Wireshark没有捕获到任何数据包。

以下是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <iostream>
#include <tchar.h>
#define IN_PORT 8888
#define OUT_PORT 50

#pragma comment(lib, "ws2_32.lib") //Winsock Library
// 要运行,请使用以下命令:gcc flood.cpp -lwsock32 -lstdc++
int main() {
    SOCKET s;
    struct sockaddr_in src, dst;
    WSADATA wsa;
    long long count = 0;
    const char* srcIP = "192.168.137.1";
    const char* dstIP = "192.168.137.71";
    const char* pkt = "This is a Probe";
    // 初始化Winsock
    printf("\n初始化Winsock...");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
        printf("失败。错误代码:%d", WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    src.sin_family = AF_INET;
    src.sin_addr.s_addr = inet_addr(srcIP);
    src.sin_port = htons(IN_PORT);

    dst.sin_family = AF_INET;
    dst.sin_addr.s_addr = inet_addr(dstIP);
    dst.sin_port = htons(OUT_PORT);

    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) {
        printf("socket() 失败,错误代码:%d", WSAGetLastError());
        exit(EXIT_FAILURE);
    }

    if (bind(s, (struct sockaddr*)&src, sizeof(src)) == SOCKET_ERROR) {
        printf("绑定失败,错误代码:%d", WSAGetLastError());
        exit(EXIT_FAILURE);
    }

    if (sendto(s, pkt, strlen(pkt), 0, (struct sockaddr*)&dst, sizeof(dst)) == SOCKET_ERROR) {
        printf("sendto() 失败,错误代码:%d", WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    closesocket(s);
    WSACleanup();
    return 0;
}

我的命令: gcc flood.cpp -lwsock32 -lstdc++

更新1:
我使用VirtualBox。我的虚拟机是Windows Server 2019,防火墙已禁用。我使用的是桥接网络设置。我的主机和客户机可以相互ping通。但是,我的主机上的Wireshark(使用正确的网络接口卡)无法捕获任何数据包。

更新2:
我刚刚看到每次执行程序时都会发送一个ARP数据包,询问我的虚拟机的IP地址。看起来路由不成功,因此UDP数据包无法找到目标。可能是什么原因?

英文:

I want to send udp packets from my host to my virtual machine. My code shows no error but no packet is captured by wireshark in my virtual machine.
Below is my code

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;WinSock2.h&gt;
#include&lt;Ws2tcpip.h&gt;
#include&lt;iostream&gt;
#include&lt;tchar.h&gt;
#define IN_PORT 8888
#define OUT_PORT 50

#pragma comment(lib,&quot;ws2_32.lib&quot;) //Winsock Library
// to run use command gcc flood.cpp -lwsock32 -lstdc++
int main(){
    SOCKET s;
    struct sockaddr_in src,dst;
	WSADATA wsa;
	long long count = 0;
	const char* srcIP = &quot;192.168.137.1&quot;;
	const char* dstIP = &quot;192.168.137.71&quot;;
	const char* pkt = &quot;This is a Probe&quot;;
    //Initialise winsock
	printf(&quot;\nInitialising Winsock...&quot;);
	if (WSAStartup(MAKEWORD(2,2),&amp;wsa) != 0)
	{
		printf(&quot;Failed. Error Code : %d&quot;,WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	src.sin_family = AF_INET;
	src.sin_addr.s_addr = inet_addr(srcIP);
	src.sin_port = htons(IN_PORT);
	
	dst.sin_family = AF_INET;
	dst.sin_addr.s_addr = inet_addr(dstIP);
	dst.sin_port = htons(OUT_PORT);

	if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
	{
		printf(&quot;socket() failed with error code : %d&quot; , WSAGetLastError());
		exit(EXIT_FAILURE);
	}

	if( bind(s ,(struct sockaddr *)&amp;src , sizeof(src)) == SOCKET_ERROR)
	{
		printf(&quot;Bind failed with error code : %d&quot; , WSAGetLastError());
		exit(EXIT_FAILURE);
	}

	if (sendto(s, pkt, strlen(pkt), 0, (struct sockaddr*) &amp;dst, sizeof(dst)) == SOCKET_ERROR)
	{
		printf(&quot;sendto() failed with error code : %d&quot; , WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	closesocket(s);
	WSACleanup();
	return 0;
}

> my command: gcc flood.cpp -lwsock32 -lstdc++

Update1:
I use virtualbox. My virtual machine is windows server 2019 and the firewall is disabled. I'm using a bridged network setting. My host and client are pingable to each other. The wireshark in my host with the correct NIC can't capture any packet.

Update2:
I just saw a ARP packet which inquired my virtual machine IP is sent everytime I excuted the program. Looks like the routing is unsuccessful, thus, make the udp packet unable to locate the destination. What might be the case?

答案1

得分: 1

使用Wireshark在您的主机上,查看您的代码是否实际发送数据包到所需的地址。

然后,请检查您是否将数据包发送到虚拟机的地址。例如,在VirtualBox虚拟机中,可以在“网络设置”中查找地址,如下图所示:

桥接网络模式无法路由数据包。

接下来,很可能您的网络接口是NAT模式,因此当您发送数据包时,默认情况下会被丢弃。您可以选择将网络接口更改为桥接模式或按照这里描述的方式设置端口转发。

英文:

Use wireshark on your host to see if your code is actually sending packets to the desired address.

Then check that you are sending packets to the address of your virtual machine. To check that on a VirtualBox machine for example, look for the address at Network Settings, as described in the picture below

桥接网络模式无法路由数据包。

Next, it is likely that you have NAT'ted network interfaces so when you send a packet there, it will drop by default. You either need to change your network interface to bridge mode or set up port forwarding as described here

答案2

得分: 0

问题出在if语句。if中的函数没有被调用。我删除了所有的if语句,然后它就正常工作了。

英文:

I found out the problem is the if statement. The function in if isn't called. I deleted all the ifs and it worked.

huangapple
  • 本文由 发表于 2023年2月26日 21:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定