Skip to content

Instantly share code, notes, and snippets.

@rikka0w0
Created September 7, 2023 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikka0w0/a9782418e91320763edcbcadea6c5ba8 to your computer and use it in GitHub Desktop.
Save rikka0w0/a9782418e91320763edcbcadea6c5ba8 to your computer and use it in GitHub Desktop.
Use powershell to allow a UDP IPv4 client to connect to an IPv6 host
Add-Type -TypeDefinition @"
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public static class UDPForward
{
public static void Main(string[] args)
{
// Define the local IPv4 listening endpoint
IPAddress localIPAddress = IPAddress.Any;
int localPort = 27016;
IPEndPoint localEndpoint = new IPEndPoint(localIPAddress, localPort);
// Define the remote domain name and port
string remoteHost = "IPv6 L4D2 Server";
int remotePort = 27016;
// Resolve the remote domain name to an IPv6 address
IPAddress[] remoteIPAddresses = Dns.GetHostAddresses(remoteHost);
IPEndPoint remoteEndpoint = new IPEndPoint(remoteIPAddresses[0], remotePort);
// Create a UDP client socket to listen for incoming IPv4 traffic
UdpClient udpClient = new UdpClient(localEndpoint);
// Create a UDP client socket with IPv6 address family to send data to the IPv6 host
UdpClient udpClientRemote = new UdpClient(remotePort, AddressFamily.InterNetworkV6);
udpClientRemote.Connect(remoteEndpoint);
// Start listening for incoming IPv4 traffic
Console.WriteLine("Listening for incoming UDP IPv4 traffic on " + localEndpoint.Address + ":" + "localEndpoint.Port");
// Start a thread for receiving IPv4 traffic and forwarding it
Thread receiveAndForwardThread = new Thread(() =>
{
while (true)
{
// Receive data from the IPv4 client
byte[] receivedData = udpClient.Receive(ref localEndpoint);
// Send the received data to the resolved IPv6 address
udpClientRemote.Send(receivedData, receivedData.Length);
}
});
// Start a thread for receiving IPv6 traffic
Thread receiveThreadRemote = new Thread(() =>
{
while (true)
{
// Receive data from the IPv6 server
byte[] receivedDataRemote = udpClientRemote.Receive(ref remoteEndpoint);
// Forward the received data to the local IPv4 client
udpClient.Send(receivedDataRemote, receivedDataRemote.Length, localEndpoint);
}
});
// Start both threads
receiveAndForwardThread.Start();
receiveThreadRemote.Start();
// Wait for threads to finish (in this case, indefinitely)
receiveAndForwardThread.Join();
receiveThreadRemote.Join();
}
}
"@
# Start the C# program
[UDPForward]::Main(@())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment