Programming P2P application - c#

I am writing a custom p2p program that runs on port 4900. In some cases when the person is behind a router, this port is not accessible from the internet.
Is there an automatic way of enabling the access to the port from the internet. I am not really sure of how other p2p applications work.
Can anyone please throw some light on this?

P2P connectivity in a nutshell. Assume we're talking about UDP here. The steps below can also be applied to TCP with some adjustments.
Enumerate all your local IP addresses (usually only 1). Create a UDP socket on a given port number** for each adapter with an IP address.
For each socket created in step 1, contact a STUN or TURN server with that same socket to discover your external IP address and to discover what the internal port number maps to outside of the NAT (it's not always the same port value). That is, your local address 192.168.1.2:4900 might be 128.11.12.13:8888 to the outside world. And some NATs don't always use the same port mapping when using the same local port to other IP addresses. TURN will also provide you a "relay address". You can also use UPNP to get a port mapped address directly from your router, if it supports that protocol.
Through a rendezvous service (SIP, XMPP, instant message, web service, email, cups with strings), publish your address candidate list to a service or send a notification to the other client that says, "hey, I want to connect with you". This message includes all the "address candidates" (ip and port pairs) collected in steps 1 and 2.
The remote client, upon receiving the invite to connect, performs step 1 and 2 above as well. Then sends back his candidate list through the same channel that he received the inviter's candidate list on.
Hole punching step. Both clients, start sending test messages over UDP to the other side's address candidates and listening for the same messages on their end. Whenever a messages is received, reply back to the address from which it came. Eventually, the clients will discover that they have a pair of addresses that they can reliably send datagrams too. Typically, one endpoint makes the final decision on which address pair (sockets) to communicate with and the protocol facilitates this endpoint telling the other endpoint this decision.
**- usually best to not to rely on a well known port for P2P clients. Because two clients behind the same NAT or firewall would not likely be able to use your software at the same time.
Here is a quick summary of some technologies to explore.
STUN - Is a simple server and protocol for clients behind a NAT/route to discover what their external IP and port mappings are.
TURN is an expansion to STUN, but supports relaying for P2P connectivity scenarios where firewalls and NATs prevent direct connections.
ICE is a set of steps by which STUN and TURN are used for setting up a P2P connection. ICE is a formal protocol for steps 1-5 above. Two excellent set of slides on ICE are here and here.
WebRTC is a variant of the ICE standard as well as a reference library for make P2P sessions with STUN and TURN.
UPNP + Internet Gateway Device Protocol - Some routers support this for hosts to automatically obtain port mappings.
libnice is an open source C library for Linux (and might work on windows) that implements ICE.
libjingle is another ICE implementation (in C++) from Google. For Windows and Linux.
PJNATH is a library within the PJSIP suite of coding libraries. It is a good implementation of an ICE stack (C code) and has been ported to a lot of platforms. (Windows, Linux, Mac, iOS, Symbian, and soon Android).
And finally, I have a blatant plug for you to use my STUN server code base.

There are solutions in some cases, see UPnP: https://en.wikipedia.org/wiki/Universal_Plug_and_Play#NAT_traversal
My home router allows this, basically, the NAT can be configured automatically by the proper request from the computer.
I would not count on this to provide a big improvement in your availability, because not that many routers both support that and have it enabled.
EDIT: #David suggested this SO question for a .NET library for UPnP: Is there a UPnP Library for .NET (C# or VB.NET)?

I would use WebRTC technology as an open source framework for such application.
Official Website
In fact it is an open source project which supports all necessary for peer-to-peer technologies out of the box:
ICE and STUN (NAT traversal)
DTLS and SRTP (security)
AVPF for quality of streaming.

This may be a little more complicated than what you're looking for, but TCP Hole Punching is a technique that should work. http://en.wikipedia.org/wiki/TCP_hole_punching
Alternatively, UPnP works great for routers / firewalls that support it.

You have another option that is NAT Port Mapping Protocol (NAT-PMP)
NAT-PMP is widely used by VoIP applications like Skype or BitTorrent P2P clients.

For the simple beginnig I would recommend to see hole punching technique. Great video here. But be sure it will not always work relating the network topology. This solves the ICE technique which discovers how connection could be established.

Related

Is There any way to achieve a ZeroMQ fullduplex channel?

The project is to build a messaging mechanism between a Python and C# program via ZeroMQ .
I want messages to be able to travel in/out from both ends at any time, which is NOT a basic request-reply model a.k.a. REQ/REP.
One way I can think of is to build a PUB/SUB model on two ports, i.e. two one way channels.
Is there any method to get a real duplex channel?
There are several ways to do this with ZeroMQ. I suggest using a DEALER/ROUTER socket pair:
Choose one program to be the "server", the other the "client".
The server will bind a ROUTER socket on a port.
The client will connect a DEALER socket to the server's ROUTER port.
(Note: this implies that the client must know the server's IP and port in advance.)
At this point the client can send messages to the server, but the server has no way to send to the client.
The client sends a "HELLO" message to the server.
The server will receive a message that includes the client's address and the HELLO message. Now the server can send messages to the client using the client's address.
DEALER/ROUTER is considered an "advanced" socket pair in ZeroMQ. My description here is very high level. You'll really need to read the docs to get the most out of ZeroMQ.
Oh yes, Sir!
Use the PAIR-PAIR or even the XREQ-XREP ought make it.
The best next step is to carefully read the respective Scalable Formal Communication Pattern archetypes' access-points' API documentation, so as to cross-check, that all pieces of pre-wired behavioural logic meet your Project needs and voilá, harness them in your messaging setup and tune-up the settings so as to meet you performance and latency needs.
That is this simple ( thanks to all the genuine knowhow hidden in these builtins ).
Using for years this very sort of inter-platforms integration among Python + C/MQL4 and other computing nodes, so well worth one's time to learn the powers and strengths of ZeroMQ.

How to scan for a port waiting for a connection on a network

I am currently working on a little chat utility in C#.
The problem is I can't figure out how to scan the network for a specific port on all machines. I am currently using this method
IPGlobalProperties network = IPGlobalProperties.GetIPGlobalProperties();
IEnumerable<IPEndPoint> connections = network.GetActiveTcpListeners()
.Where(x => x.Port == ConstParams.iPort);
where ConstParams.iPort is the port I want to scan (6910 here).
The problem is that the returned values are only local ports and the "0.0.0.0" ip address...
How can I scan for all open ports (6910) on the current network?
Rather than using port scanning, I suggest you to implement a simple discovery mechanism based on multicast/broadcast communication.
During the startup the application should broadcast/multicast its IP/Port information. All running instances should respond to this message with their IP/Port information. This kind of a discovery mechanism is easy to implement, and it is both faster in runtime and more dynamic than the port scanning approach.
You should consider multicast, but rather than rolling your own, rely on an existing standard with library support, like mDNS:
http://en.wikipedia.org/wiki/Multicast_DNS
Or, since you said C#, using one of its native solutions:
http://msdn.microsoft.com/en-us/library/system.net.peertopeer.aspx
Scanning ports is a poor choice, you will most likely trigger firewalls on machines in the network to display your machine as an attacker. Any Intrusion detection systems on the networks could potentially be triggered as well. It's a lot of overhead for what you need.
I would recommend doing a broadcast using UDP or a multicast to discover other clients
http://www.codeproject.com/Articles/1705/IP-Multicasting-in-C
Another option would be to have a centralized server, either on a web server (php script, asp.net page, etc) or a web service (REST) which the chat client would connect to on start up, POSTing it's listening IP/Port, and then in turn would receive a list of all recently announced IP/Ports of the other clients on the network. You'd probably want some keep alive here, IE: the client would POST to the page every 5 minutes, if an IP does not POST for 10 minutes, it would be removed from the list.
To get the public IP of the machine, you could check out this page:
http://www.whatismyip.com/faq/automation.asp
You'd just need to send a web request to it to retrieve the IP. If you want to get the non 0.0.0.0/127.0.0.1 IP of the local interface, you can check out these posts:
Get local IP address
How do I get the Local Network IP address of a computer programmatically? (C#)
GetIPGlobalProperties only returns info about your local machine (see http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getipglobalproperties.aspx ).
To find out which other machines on the network have that port open, you'd have to iterate through the a range of IPs, attempting to connect on that port. There is no central repository to query on this.
This article describes an approach: http://www.dijksterhuis.org/building-a-simple-portscanner-in-c/

Hosting a game on a PC - UDP or UPNP communication?

I've built a server-application for a game that I want general people to use. Now, since I've worked with client/server solutions before, I know how tedious it is to host connections on some computers.
So, I heard about these rumors that I would like to get confirmed.
Using UDP for "hosting" a connection is good, because it is rarely blocked by router-firewalls compared to TCP.
Using UPNP for communicating with a router is good, because it allows you to add port forwarding for the game, making your server reachable no matter if you're using TCP or UDP.
I don't care about the software-firewall people may use. What I care about is the router firewall functionality.
Any help would be appreciated.
Thanks!
For your two rumors,
I don't know, I have not worked with firewalls enough, but I would not call that one true. Most routers block everything (TCP and UDP) unless you explicitly ask it to forward it for inbound connections via hand setup or UPNP. If you don't tell the router what computer wants info coming in to port 12345 how will it know what to do when a user sends a unsolicited packet to it (the definition of being a server).
Yes, learning how to do UPNP will make it a lot easier for your users to host games, however you should still provide instructions for people who do not have UPNP turned on or their router does not support it.
The other thing you may want to look in to is learning how to add your program to the windows firewall client allowed list as that is what most of your users will have. See this SO question for more details.

Monitor Internet Connection using C#

I want to monitor my router to see what is currently being downloaded and what application/Software that initiate it, who is the user doing this and download speedthat user occupy
i got all credential needed for both my wireless connection and the router
any head start for sth like that in C#?
the idea
First of all: Do you even know that your router can do this? Most routers I've seen do not have this level of traffic understanding and management.
If your router does, then there is one of two ways that such data is generally made available:
1) Through a HTTP interface (password protected)
or
2) Through an SNMP interface
To retrieve the appropriate HTTP URL, you simply get it as you would any other web page. Try something like: http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
To retrieve the SNMP settings, there are many options; try this question: What SNMP library for .NET makes traps, sets or gets simple?
However, most likely, your router will not actually have this information available.
So, edit: If your router doesn't support side-chaining, and doesn't support SNMP or similar statistics, then you can't do this in the general sense.
You could spin up a Linux box as the gateway for the machines, and use NAT session statistics to monitor this. The way to do this is to have two network interfaces (logical or physical), one of which gets an IP from your modem, and the other which is the default gateway for the wireless network. Turn on IP forwarding and masquerading (NAT) as well as a DHCP server for the wireless network side. Now, you can use iptables to look at active NAT sessions and how much data has been transferred. You can also use packet filters for more specific information.
Also, if you know which machine is doing the downloading, and are running Windows, you can use WMI ("perfmon.exe" to plot this) to see how much data is being transfered on the actual machine.

C#, socket through router

I made a remote engine for a game which must be able to works in P2P.
It perfectly works in LAN, but there's a problem when computers are behind router(s) and want to communicate through internet.
Is there any solution to this, which doesn't need to manipulate the router configuration?
Because since most of my gamers may not be very acknowledged in informatic, I'd like to solve this problem as easily as possible, without any intervention from them.
Thanks,
KiTe.
You need the client behind the router to initiate an OUTGOING connection. Once that's established you can have 2 way communication on it. This is why most P2P games have some sort of server to set up matches between clients. You can have each client establish a socket to the server and then connect them to each other.
There was an alternative called 'NAT hole punching' a while back, but I'm not sure how reliable that was.
This is what separates LogMeIn from VNC.
These days almost all home users are behind a form of NAT, macking it impossible in practice to set up real peer-to-peer communication as the application listenning port is unreachable from the net.
In theory there is UPnP which allows applications( running under elevated priviledges) to enable port forwarding dynamically on the home router (via Internet Gateway Device Protocol), but in practice this is so unreliable that I haven't seen any real use of it.
The most reliable solution is to have a central hub (your game server) that forwards packets between clients that initiate the connection from behind the NAT device. But that is a serious cost to you, as you'll need to cash out the cost of provisioning and operating this hubs which can be serious money, even with dynamic just-in-time solutions like EC2.
Update
Perhaps you can use the Codeplex UPnP NAT traveral project.

Categories