[英]C# UDP client reading multicast IP (local interface), and sending unicast UDP to VPN
我有一個VPN客戶端,可以通過VPN管道發送單播UDP數據包,但不支持多播IP(UDP)數據包(OpenVPN)。 所以我想我可以編寫這個帶有IP(端口3000)多播的小墊片,並通過VPN作為單播發送它們。
我看到組播數據包到了,但我看不到任何東西到達VPN端(WireShark沒有幫助,因為不像IpConfig / all)它沒有看到VPN接口。)。
我認為我的問題可能是我不清楚BIND和CONNECT之間的區別,以及我應該將UDPClient綁定到哪個接口(vpn或local)。
命令行是:
239.0.0.0 198.168.0.15 10.4.30.239 172.27.225.77
arg[0] = multicast from address
arg[1] = unicast to address
arg[2] = local physical ethernet assigned address
arg[3] = virtual address of computer on VPN network (client)
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace MUTunnel
{
public class EchoBot
{
protected UdpClient sender;
protected UdpClient listener;
protected const int listenPort = 3000;
protected const int outPort = 3000;
protected IPEndPoint inPoint;
protected IPEndPoint outPoint;
protected IPAddress listenAddress;
protected IPAddress sendAddress;
protected IPAddress localAddress;
protected IPAddress vpnAddress;
public EchoBot(string from, string to, string local, string vpn)
{
bool parsed = IPAddress.TryParse(from, out listenAddress);
parsed = IPAddress.TryParse(to, out sendAddress) && parsed;
parsed = IPAddress.TryParse(local, out localAddress) && parsed;
parsed = IPAddress.TryParse(vpn, out vpnAddress) && parsed;
if (!parsed)
{
System.Console.WriteLine("Usage: MUTunnel <source multicast IP> <dest unicast IP> <local host IP address> <vpn IP address>");
Environment.Exit(1);
}
listener = new UdpClient();
listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
inPoint = new IPEndPoint(localAddress, listenPort);
listener.Client.Bind(inPoint);
listener.JoinMulticastGroup(listenAddress);
sender = new UdpClient();
sender.Ttl = 64;
sender.AllowNatTraversal(true);
//outPoint = new IPEndPoint(sendAddress, outPort);
sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sender.Connect(sendAddress, outPort);
}
public void send(byte[] bytes)
{
sender.Send(bytes, bytes.Length);
}
public void loop()
{
bool done = false;
try
{
while (!done)
{
byte[] bytes = listener.Receive(ref inPoint);
Console.WriteLine("Received Multicast from {0} : length {1}\n", listenAddress.ToString(), bytes.Length);
this.send(bytes);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
sender.Close();
}
}
}
class Program
{
static void Main(string[] args)
{
if (args.Length != 4)
{
System.Console.WriteLine("Usage: MUTunnel <source multicast IP> <dest unicast IP> <local host IP address> <vpn IP address>");
Environment.Exit(1);
}
EchoBot echoBot = new EchoBot(args[0], args[1], args[2], args[3]);
Console.WriteLine("MUTunnel Waiting for messsages...");
echoBot.loop();
}
}
}
ipconfig / all讀取如下(當OpenVPN客戶端運行時)
P:\>ipconfig /all
Windows IP Configuration
Host Name . . . . . . . . . . . . : YSG4206
Primary Dns Suffix . . . . . . . : draper.com
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : draper.com
Ethernet adapter Ethernet 3:
Connection-specific DNS Suffix . : draper.com
Description . . . . . . . . . . . : Killer E2200 Gigabit Ethernet Controller
Physical Address. . . . . . . . . : F8-B1-56-FF-8B-36
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 10.4.30.239(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.254.0
Lease Obtained. . . . . . . . . . : Monday, June 18, 2018 5:28:03 PM
Lease Expires . . . . . . . . . . : Thursday, June 21, 2018 8:46:50 PM
Default Gateway . . . . . . . . . : 10.4.31.254
DHCP Server . . . . . . . . . . . : 140.102.100.111
DNS Servers . . . . . . . . . . . : 10.10.20.11
10.10.20.12
NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : TAP Adapter OAS NDIS 6.0
Physical Address. . . . . . . . . : 00-FF-91-E7-8A-38
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 172.27.225.77(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
NetBIOS over Tcpip. . . . . . . . : Enabled
P:\>
添加了sender.Ttl = 64; sender.AllowNatTraversal(真); 還在客戶端上運行了RawCap並選擇了VPN接口。 數據包轉儲未顯示我正在轉換為單播並嘗試發送到129.168.0.15的多播數據包到達該接口。
只是其他一些東西。 (請參閱來自RawCap的.pccap文件的wireshark顯示。
我懷疑你沒有任何東西聽你單播的另一端。
單播是用於描述通信的術語,其中一條信息從一個點發送到另一個點。 在這種情況下,只有一個發送者和一個接收者 。
我添加了一個額外的監聽器來監聽發送端點,一切正常,並將打印出已發送的消息。
使用ip:198.168.0.15在計算機上運行附加偵聽器
IPAddress localAddress = IPAddress.Parse("198.168.0.15");
const int listenPort = 3000;
var listener = new UdpClient();
listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
var inPoint = new IPEndPoint(localAddress, listenPort);
listener.Client.Bind(inPoint);
while (true)
{
byte[] bytes = listener.Receive(ref inPoint);
Console.WriteLine("Received Message from {0} : message {1}\n", localAddress.ToString(),Encoding.ASCII.GetString(bytes));
Thread.Sleep(1000);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.