python / C# UDP hole punch - c#

I have tried implementing UDP hole punch from this theory:
A rendezvous server (S) is reachable on UDP by client (A) and client (B).
Clients A and B send a registration package each to S. S reads public IP and outer port and stores it.
S sends A-info (endPointA) to B and B-info (endPointB) to A.
With this information A can send to B's endPoint and B can send to A's endPoint.
server S is python 3 and clients A and B are C#
So far the theory. Here is what works:
S can send as many messages to A and B as it likes on their public endPoint.
A and B can send to each other on innerIP:localPort if they are behind the same router ONLY!
Here is what does not work:
A cannot send to B nor B to A with their public endPointA / endPointB
even if I create a new socket on the server S, it cannot get a message through to these endPoints
This last limitation makes me think there is some "lock" on routers that only allow the original socket on S to send to the clients(?)
So my question is: what now?
Is there some socket option, socket flag, socket anything that needs to be set to fix this?
I have even tried making a script on the server S that sends to the clients public IP on all ports from 32000-65535 with no messages coming through.
Code for server:
def ServerUdpStart(host, port):
"""Start up the server"""
global myServerUdp
success = True
try :
myServerUdp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error as msg:
print('UDP Failed to create socket. Error Code:', str(msg[0]), 'Message', msg[1])
success = False
# Bind socket to local host and port
try:
myServerUdp.bind( (host, port) )
except socket.error as msg:
print('UDP bind failed. Error Code : ', str(msg[0]), 'Message', msg[1])
success = False
return success
def ServerUdpReceiveData():
"""Incoming UDP data (runs in a thread)"""
global myServerUdp
if myServerUdp != None:
d = myServerUdp.recvfrom(1024)
data = d[0]
addr = d[1] # source tuple (ip, port) to store for clients
if data:
incomingUdpMessage(addr, data) # handle incoming data
Code for a client:
public class SocketServerUdpHandler{
// ports
private int _port = 0;
private int _localAssignedPort = 0;
// udp client
private UdpClient _clientUdp = null;
private IPEndPoint recvEndP = new IPEndPoint( IPAddress.Any, 0);
//********** START **********
public bool StartUdp(int po){
// port set by user
_port = po;
// client
bool created = CreateClient();
// start it up
if(created){
FindLocalAssignedPort();
StartListen();
}
return created;
}
//********** LISTENER **********
private bool CreateClient(){
// state
bool success = true;
try{
// udp client
_clientUdp = new UdpClient();
}catch(Exception e){
success = false;
}
return success;
}
private void StartListen(){
// listen
_clientUdp.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
private void ReceiveCallback(IAsyncResult ar){
// read incoming
byte[] receiveBytes = _clientUdp.EndReceive(ar, ref recvEndP);
// handle bytes ...
// listen
StartListen();
}
private void FindLocalAssignedPort(){
IPEndPoint tmpEndP = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 10001 );
byte[] msgByte = new byte[] {(byte) 9};
_clientUdp.Client.SendTo(msgByte, tmpEndP);
_localAssignedPort = ((IPEndPoint) _clientUdp.Client.LocalEndPoint).Port;
}
//********** SEND TO **********
public void writeSocketToEndPoint(IPEndPoint ipEndP, byte[] bytes){
_clientUdp.Client.SendTo(bytes, ipEndP);
}
}

Related

How to get information from a remote UDP socket? C# & IPv4

I have hardware that sends information to the address 192.168.0.255 at approximately 5 second intervals (In the following image, the Wireshark software showing the device with IP address 192.168.0.241 sending the message "Hallo" to the address 192.168.0.255 on port 7000):
On the other hand, I have a desktop app made in C # that tries to read that information as follows:
int PORT = 7000;
udpClient = new UdpClient();
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, PORT));
private async Task<string> getData()
{
try
{
var from = new IPEndPoint(0, 0);
while (true)
{
var recvBuffer = udpClient.Receive(ref from);
string result= Encoding.UTF8.GetString(recvBuffer);
if (result != null && resultado.Length > 0)
{
return result;
}
}
}
...
}
It doesn't work (udpClient.Receive never returns information, it is similar to that there is no socket information yet), but if I open a software tool from my PC that allows me to write information to a UDP scoket, the code works wonderfully (udpClient.Receive captures the sent information.)
Any suggestions or comments?
UdpClient client;
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, PORT);
private void Connect()
{
client = new UdpClient(7000);
client.Connect(endPoint);
client.BeginReceive(ReceiveCallback, null);
}
private void ReceiveCallback(IAsyncResult _result)
{
try
{
byte[] _data = client.EndReceive(_result, ref endPoint);
socket.BeginReceive(ReceiveCallback, null);
if (_data.Length < 4)
{
//disconnected
return;
}
//now data is anything received and if you want to view it as a string do:
string result = Encoding.Default.GetString(data);
//you can also convert it to other things like ints, float, etc
}
catch
{
//disconnected
}
}
Thanks to #MarkusSafar's suggestion, I put the desktop app on another PC and from there it manages to capture the hardware message. I'm not sure why it doesn't work on my PC, but the next step is to test this same code for an app developed for Android (Xamarin), I hope it works there too.

Send UDP Broadcast on RasPi 3

I need to send a UDP broadcast from a RaspberryPi 3 running Windows 10 IoT Core. My code so far:
internal class UdpInterface : IDisposable
{
private UdpClient udpClient;
internal UdpInterface(int localPort)
{
udpClient = new UdpClient(localPort);
udpClient.EnableBroadcast = true;
}
internal void BroadcastMessage(string message, int targetPort)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, targetPort);
byte[] bytes = Encoding.ASCII.GetBytes(message);
udpClient.Send(bytes, bytes.Length, ip);
}
public void Dispose()
{
udpClient.Close();
udpClient.Dispose();
}
}
I call it like this from a GPIO pin ValueChanged event:
_udp.BroadcastMessage("Video" + PinMapper(sender.PinNumber), _port);
I tried setting the Internet (Client & Server) capability in package.appxmanifest, and I tried using the DatagramSocket class before.
Do I need some other permissions/capabilities or is something wrong with my code? It did work when sending to target IPs before.
I'm verifying the packet with PacketSender on my dev machine.

Receiving unexpected udp packets in my c# client

I receive unexpected udp packets when i run the code in windows(both client and server in same system). My client is written in c# and the server is in python.
When i run in the same code in mac i don't have any problem and i receive expected messages(here i opened a port in mac for udp).
client(c#):
public static void Main(string[] args)
{
Console.WriteLine("Receiver");
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
//udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 137));
try
{
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 137);
string message ;
do
{
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
message = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received: " +
message);
//Console.WriteLine("This message was sent from " +
// RemoteIpEndPoint.Address.ToString() +
// " on their port number " +
// RemoteIpEndPoint.Port.ToString());
}
while (message != "exit");
udpClient.Close();
//udpClientB.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("Press Any Key to Continue");
Console.ReadKey();
}
server(python-3.6):
import socket
from time import sleep
rx=0 #000
ry=0 #000
rz=0 #000
e=0 #000
UDP_IP = "172.20.10.4"
UDP_PORT = 137
MESSAGE = ""
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
while(1):
if (rx<360):
rx=rx+1
if ((ry<360) & (rx>=360)):
ry=ry+1
if ((rx>=360) & (ry>=360)):
rx=0
ry=0
if (rz<360):
rz=rz+1
if (rz>=360):
rz = 0
if (e<10):
e=e+1
if(e>=10):
e=0
#verify rx
if (rx<10):
rxs='00'+str(rx)
if ((rx>=10) & (rx<100)):
rxs='0'+str(rx)
if (rx>=100):
rxs=str(rx)
#verify ry
if (ry<10):
rys='00'+str(ry)
if ((ry>=10) & (ry<100)):
rys='0'+str(ry)
if (ry>=100):
rys=str(ry)
#verify rz
if (rz<10):
rzs='00'+str(rz)
if ((rz>=10) & (rx<100)):
rzs='0'+str(rz)
if (rz>=100):
rzs=str(rz)
#verify e
if (e<10):
es='00'+str(e)
if ((e>=10) & (e<100)):
es='0'+str(e)
if (e>=100):
es=str(e)
MESSAGE = 'h'+'01'+'rx'+rxs+'ry'+rys+'rz'+rzs+'e'+es
#sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT))
sleep(0.1)
Expected message(i receive the below in mac):
This is the message you received: h01rx360ry151rz009e007
I receive the below in windows:
This is the message you received: ?{ EJFDEBFEEBFACACACACACACACACACAAA
Can someone please letme know where i went wrong with.
thanks in advance
If the udp packets are not associated with your server this might be helpful: https://forum.fortinet.com/tm.aspx?m=106656 and here: https://superuser.com/questions/637696/what-is-netbios-does-windows-need-its-ports-137-and-138-open
Windows is using port 137 for its own services. Try change your port on the Windows device.
Looks like you are having issues with port selection. The packets you see are from Windows.
You don't need the bind statement. This is a working example I'm using with .net core
internal class Program
{
private const int PORT = 5678;
private static void Main(string[] args)
{
Console.WriteLine("Packet Forwarding UP!");
var client = new UdpClient(PORT);
// can use IPAddress.Any or other IP depending on where they are coming from.
var ipEndPoint = new IPEndPoint(IPAddress.Loopback, PORT);
while (true)
{
Console.Write("Waiting... ");
var receive = client.Receive(ref ipEndPoint);
Console.WriteLine(" - Received Data - " + receive.Length);
}
}
}
I've been testing the client with PacketSender's Intense Traffic Generator and confirming traffic with NirSoft TcpUdpWatch

How to get IP addresses of hosts on local network running my program

I have built a peer to peer C# video conferencing application that uses a specific TCP port(17500) for audio communication. Currently, on my application interface, I enter the other IP address which has the program opened in order to communicate. What I want to do is to find the IP addresses automatically.
So, I though the best way to achieve this is to obtain the local IP addresses that are using the same TCP port number, 17500. How can I do that ? or is there any other methods getting IP addresses using the same application ?
As mentioned in comments, you need some kind of peer-discovery protocol.
As many multimedia devices, routers etc. use multicast based discovery protocols like SSDP, I created a similar discovery service sample .
Usage is simple. Just use
Discoverer.PeerJoined = ip => Console.WriteLine("JOINED:" + ip);
Discoverer.PeerLeft= ip => Console.WriteLine("LEFT:" + ip);
Discoverer.Start();
All your clients will use the same code.
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Caching; // add this library from the reference tab
using System.Text;
using System.Threading.Tasks;
namespace SO
{
public class Discoverer
{
static string MULTICAST_IP = "238.212.223.50"; //Random between 224.X.X.X - 239.X.X.X
static int MULTICAST_PORT = 2015; //Random
static UdpClient _UdpClient;
static MemoryCache _Peers = new MemoryCache("_PEERS_");
public static Action<string> PeerJoined = null;
public static Action<string> PeerLeft = null;
public static void Start()
{
_UdpClient = new UdpClient();
_UdpClient.Client.Bind(new IPEndPoint(IPAddress.Any, MULTICAST_PORT));
_UdpClient.JoinMulticastGroup(IPAddress.Parse(MULTICAST_IP));
Task.Run(() => Receiver());
Task.Run(() => Sender());
}
static void Sender()
{
var IamHere = Encoding.UTF8.GetBytes("I AM ALIVE");
IPEndPoint mcastEndPoint = new IPEndPoint(IPAddress.Parse(MULTICAST_IP), MULTICAST_PORT);
while (true)
{
_UdpClient.Send(IamHere, IamHere.Length, mcastEndPoint);
Task.Delay(1000).Wait();
}
}
static void Receiver()
{
var from = new IPEndPoint(0, 0);
while (true)
{
_UdpClient.Receive(ref from);
if (_Peers.Add(new CacheItem(from.Address.ToString(), from),
new CacheItemPolicy() {
SlidingExpiration = TimeSpan.FromSeconds(20),
RemovedCallback = (x) => { if (PeerLeft != null) PeerLeft(x.CacheItem.Key); }
}
)
)
{
if (PeerJoined != null) PeerJoined(from.Address.ToString());
}
Console.WriteLine(from.Address.ToString());
}
}
}
}
Now a little bit about the algorithm:
Every client multicasts a packet every seconds.
if the receiver(every client has it) gets a packet from an IP that isn't in its cache, it will fire PeerJoined method.
Cache will expire in 20 seconds. If a client doesn't receive a packet within that duration from another client in cache, it will fire PeerLeft method.
I believe if you are using a peer to peer application to exchange packets, when you need to know if someone "Is Online and Ready for connection", you need to send a broadcast. We can do it easily using an UDP connection.
I'll post an example where you use two methods: one to ask the entire network for ready clients in a broadcast message, and the other will start a listener to answer back broadcast asking message, or start a connection if a response of type "i am here" comes.
Hope it helps!
public sealed class UdpUtility
{
// Our UDP Port
private const int broadcastPort = 11000;
// Our message to ask if anyone is ready for connection
private const string askMessage = "ARE ANYONE OUT THERE?";
// Our answer message
private const string responseMessage = "I AM HERE!";
// We use this method to look for a client to connect with us.
// It will send a broadcast to the network, asking if any client is ready for connection.
public void SendBroadcastMessage()
{
var udp = new UdpClient(broadcastPort);
var endpoint = new IPEndPoint(IPAddress.Broadcast, broadcastPort);
try
{
var bytes = Encoding.ASCII.GetBytes(askMessage);
udp.Send(bytes, bytes.Length, endpoint);
}
catch (Exception ex)
{
// Treat your connection exceptions here!
}
}
// This method will start a listener on the port.
// The client will listen for the ask message and the ready message.
// It can then, answer back with a ready response, or start the TCP connection.
public void ListenBroadcastMessage()
{
var udp = new UdpClient(broadcastPort);
var endpoint = new IPEndPoint(IPAddress.Broadcast, broadcastPort);
bool received = false;
try
{
while (!received)
{
// We start listening broadcast messages on the broadcast IP Address interface.
// When a message comes, the endpoing IP Address will be updated with the sender IP Address.
// Then we can answer back the response telling that we are here, ready for connection.
var bytes = udp.Receive(ref endpoint);
var message = Encoding.ASCII.GetString(bytes);
if (message == askMessage)
{
// Our client received the ask message. We must answer back!
// When the client receives our response, his endpoint will be updated with our IP Address.
// The other client can, then, start the TCP connection and do the desired stuff.
var responseBytes = Encoding.ASCII.GetBytes(responseMessage);
udp.Send(responseBytes, responseBytes.Length, endpoint);
}
else if (message == responseMessage)
{
// We received a connection ready message! We can stop listening.
received = true;
// We received a response message!
// We can start our TCP connection here and do the desired stuff.
// Remember: The other client IP Address (the thing you want) will be on the
// endpoint object at this point. Just use it and start your TCP connection!
}
}
}
catch (Exception ex)
{
// Treat your connection exceptions here!
}
}
}
Invoke your command prompt to do "netstat -n" and extract the output.
Here is a piece of code taken from a program that I have wrote modified to fit your requirements. You will still need to further process the data to get the IP addresses
Process netP = new Process();
ProcessStartInfo netPI = new ProcessStartInfo();
netPI.FileName = "cmd";
netPI.UseShellExecute = false;
netPI.RedirectStandardOutput = true;
netPI.RedirectStandardInput = true;
netPI.RedirectStandardError = true;
netPI.CreateNoWindow = true;
netP.StartInfo = NetPI;
netP.Start();
while (!netP.Start())
Thread.Sleep(100);
StreamWriter sW = netP.StandardInput;
StreamReader sR = netP.StandardOutput;
sW.WriteLine("netstat -n")
sW.Close();
string data = sR.ReadToEnd();
sR.Close();
//Do some further processing to filter out the addresses and extract

Windows UWP Web Service Discovery on LAN

Im trying to build a webservice for clients on a local network. For the service i can target any version of the .NET Framework. The clients are mobile windows devices and i would like to use the universal windows platform (UWP) as the target framework.
The service will run on multiple machines with different network addresses. My goal is that a client can automatically detect the service as soon as he connects to that local network. I want to avoid any typing of ip-addresses by the user. But all samples i can find use a hard-coded service URL. Since i have no DNS-Server i have to enter (or hard-code) the service-ip-address into the clients.
Currently im running a WCF service with a UDPDiscoveryEndpoint which does exactly what i want. But unfortunately that part of WCF (the System.ServiceModel.Discovery namespace) is not available on WinRT and also not supported on the universal windows platform. I dont have to use WCF; any alternative library with service discovery functionality would be perfect.
So here is my question: Is there any way to do service discovery on a local network in a WinRT/ UWP App? I tried ASP.NET Web API and SignalR but it seems that this HTTP based services/frameworks dont support discovery at all.
Thank you!
I've managed to do webservice discovery in UWP using socket and broadcast messages.
Plese, check my answer for more details.
EDIT - As sugested by #naveen-vijay, I'm posting a more complete aswer instead of just a link to a solution.
Every WS will listen to a specific port, awaiting for some broadcasted message searching for WS running in the LAN.
The WS implementation is win32, and this is the code needed:
private byte[] dataStream = new byte[1024];
private Socket serverSocket;
private void InitializeSocketServer(string id)
{
// Sets the server ID
this._id = id;
// Initialise the socket
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Initialise the IPEndPoint for the server and listen on port 30000
IPEndPoint server = new IPEndPoint(IPAddress.Any, 30000);
// Associate the socket with this IP address and port
serverSocket.Bind(server);
// Initialise the IPEndPoint for the clients
IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
// Initialise the EndPoint for the clients
EndPoint epSender = (EndPoint)clients;
// Start listening for incoming data
serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(ReceiveData), epSender);
}
private void ReceiveData(IAsyncResult asyncResult)
{
// Initialise the IPEndPoint for the clients
IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
// Initialise the EndPoint for the clients
EndPoint epSender = (EndPoint)clients;
// Receive all data. Sets epSender to the address of the caller
serverSocket.EndReceiveFrom(asyncResult, ref epSender);
// Get the message received
string message = Encoding.UTF8.GetString(dataStream);
// Check if it is a search ws message
if (message.StartsWith("SEARCHWS", StringComparison.CurrentCultureIgnoreCase))
{
// Create a response messagem indicating the server ID and it's URL
byte[] data = Encoding.UTF8.GetBytes($"WSRESPONSE;{this._id};http://{GetIPAddress()}:5055/wsserver");
// Send the response message to the client who was searching
serverSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, epSender, new AsyncCallback(this.SendData), epSender);
}
// Listen for more connections again...
serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(this.ReceiveData), epSender);
}
private void SendData(IAsyncResult asyncResult)
{
serverSocket.EndSend(asyncResult);
}
The client implementation is UWP. I've created the following class to do the search:
public class WSDiscoveryClient
{
public class WSEndpoint
{
public string ID;
public string URL;
}
private List<WSEndpoint> _endPoints;
private int port = 30000;
private int timeOut = 5; // seconds
/// <summary>
/// Get available Webservices
/// </summary>
public async Task<List<WSEndpoint>> GetAvailableWSEndpoints()
{
_endPoints = new List<WSEndpoint>();
using (var socket = new DatagramSocket())
{
// Set the callback for servers' responses
socket.MessageReceived += SocketOnMessageReceived;
// Start listening for servers' responses
await socket.BindServiceNameAsync(port.ToString());
// Send a search message
await SendMessage(socket);
// Waits the timeout in order to receive all the servers' responses
await Task.Delay(TimeSpan.FromSeconds(timeOut));
}
return _endPoints;
}
/// <summary>
/// Sends a broadcast message searching for available Webservices
/// </summary>
private async Task SendMessage(DatagramSocket socket)
{
using (var stream = await socket.GetOutputStreamAsync(new HostName("255.255.255.255"), port.ToString()))
{
using (var writer = new DataWriter(stream))
{
var data = Encoding.UTF8.GetBytes("SEARCHWS");
writer.WriteBytes(data);
await writer.StoreAsync();
}
}
}
private async void SocketOnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
// Creates a reader for the incoming message
var resultStream = args.GetDataStream().AsStreamForRead(1024);
using (var reader = new StreamReader(resultStream))
{
// Get the message received
string message = await reader.ReadToEndAsync();
// Cheks if the message is a response from a server
if (message.StartsWith("WSRESPONSE", StringComparison.CurrentCultureIgnoreCase))
{
// Spected format: WSRESPONSE;<ID>;<HTTP ADDRESS>
var splitedMessage = message.Split(';');
if (splitedMessage.Length == 3)
{
var id = splitedMessage[1];
var url = splitedMessage[2];
_endPoints.Add(new WSEndpoint() { ID = id, URL = url });
}
}
}
}
}
In UWP, you can use the PeerFinder class, to discover other instances of your applications in a LAN.
I know this is not exactly service discovery, it's just peer discovery, but it should be enough for your scenario. Just use one instance of the application as your "service" that communicates with the other instances.
You can use this to find your peers and create a socket connection:
PeerFinder.DisplayName = "Doru " + Guid.NewGuid().ToString();
PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested;
PeerFinder.Start();
private async void PeerFinder_ConnectionRequested(object sender, ConnectionRequestedEventArgs args)
{
PeerInformation peer = args.PeerInformation;
StreamSocket socket = await PeerFinder.ConnectAsync(peer);
}
For a deeper understanding on how Peer Discovery works, checkout this link [minute 6:30].

Categories

Resources