Force NAT UPNP port forwarding - c#

I would like to forward ports automatically with my application, however UPNP is off by default, but Spotify was able to forward ports when even UPNP is disabled, same applies to uTorrent and others.
How do I force port forwarding, or Enable UPNP on rounter, forward port and disable it again?

Maybe these services use UDP hole punching.
See: Wikipedia article

Related

port forwarding camera ip c#

I'm not good in networking so for some people my question can look silly. I'm trying to connect to my ip camera from outside of my local network. I know that I need to do port forwarding. I read that ssh could be good idea. Imagine that my outside ip is 10.20.30.40 and my camera ip local ip is 1.2.3.4. When port forwarding will work I think that I will connect to camera using this address
"http://10.20.30.40/index1.htm". So all I need is port forwarding. Client can't do this on his own. I need to write an app to do this automatically. How can I do this using c# and am I going in right direction?
You can accomplish this on most home routers in their default configuration using UPnP - Universal Plug and Play.
This is a standardised mechanism for applications behind a NAT router to programmatically request ports to be forwarded to them.
See this question for details on accessing UPnP functionality from .NET. You need to persuade the router to forward TCP port 80 (standard port for HTTP) to your camera's LAN IP address.
Only way you can accomplish correctly configured port forwarding is through manual router configuration.
There is no way around it, for logical and obvious security reasons.
Ok, problem solved, when I turn upnp on my router on web pages didnt load. It turns out that camera had default port set to 80. I've changed it to 1001 and then set upnp enable on router and camera automatically forwarded port 1001 to it self. All I have to do programaticaly is set port on camera.

C# TcpListener external IP

I'm creating a TcpListener, and I want clients from other computers to be able to join my listener.
I've read and understood that I have to do Port Forwarding, but it doesn't make any sense to me - when I publish my app, I want other people to create this Listener, and I can't tell them to do Port Forwarding.
Is there any possibility to create a TcpListener that clients will be able to join without Port Forwarding?
Thank you.
Well, lets try to clear somethings out first.
The main reason to use port forwarding is because you have a NAT router in front of an internal network. To setup a port forward is to instruct the NAT router to forward traffic to a certain port on the public interface to a port on an internal computer.
If you don't have a NAT router you don't need port forwarding.
Many routers today support UPnP, a technique to kindly ask the router to create a specific port forward. A suitable library to use might be ManagedUPnP.
However you still need to figure out the public IP of the router and what port you have opened and communicate that to your other applications.
If your router does not allow UPnP or there are other fire wall rules in place you can not set up a port forwarding correctly.
You can create server application and forward ports on your pc. Client application (this one you will publish) will just connect to your pc so they can be on the NAT. You can also combine your application with some php/asp pages but it depends on data you would like to send. If it's some kind of PC statistics like uptime, hardware etc. you would just use http query in client app to website script you've created (for instance mypage.com/?uptime=100&ram=2gb&hash=xxxx etc.)
Only the server (the computer which is accepting TCP requests) needs to have the port forwarded.
The common model is that you (the developer/producer of the service) host the server. Then customers (people who subscribe to your service) connect to your service using either an IP or a URL. If your service is behind a firewall (you have a router between your computer and your internet modem) then you will have to forward the port. You will probably also have to open the port in Window's firewall, but I expect you have done this already. In this model the customer does not have to do anything with their router (it is like using a web browser).
If you are making a product where your customers are hosting the service then they will have to deal with the port issues. In which case you could try ManagedUPnP like Albin Sunnanbo suggests or redirect them to one of the many sites explaining how to setup port forwarding.

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.

Programming P2P application

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.

client server app upnp / port forward question

I am thinking about writing a client server app using sockets in c#. My question is, if the server is behind a router and upnp is enabled, once the server starts listening does upnp automatically forward data incoming to that computer if it is destined for said port? I don't want the user to have to start forwarding ports, I am hoping my server app can be zero configuration.
Thanks in advance.
I worked on a uPNP tool a little while ago for a work application for file sharing across multiple sites.
I can confirm that during the port configuration via uPNP, that you do indeed specify which port and end point you would like to listen.
If uPNP reports back OK. All requests to the port will be forwarded to the passed end point.
I will find the class I made \ modified and give it as an example shortly....

Categories