C# - Globally intercept and modify DNS resolution responses - c#

There's a similar question ( Can I temporarily override DNS resolution within a .NET application? ) but I just can't figure it out with the meager response there.
Background info
I have a server set up in my home network, an old computer. Our router has the right ports forwarded, the server runs server software for things like http, svn, games, etc. I've got a domain name registered that always points to our external IP address. For all intents and purposes, I've got a typical webserver set up. My friends can game on my server by connecting via the domain name, I can push and pull svn projects, etc.
The only problem is that I also need to use my server when I'm connected to my home network (the same network as the server). Using the domain name results in Windows resolving it to our own IP address, and my router is too retarded to realize it just needs to forward it back into our network to the server as per the usual. I've done some looking around, configuring, telnetting and DNS overriding, but I have it on good authority that our ISP apparently crippled the DNS override feature of their routers to prevent this exact scenario. Apparently they don't like internal loopbacks.
I now basically have to keep 2 configs for each of my server's services: one config that specifies the domain name for when I'm abroad, and a second that specifies the server's internal IP for when I'm at home. It's frustrating because it just isn't always possible.
I want to instantiate a global DNS resolution request/response listener that will do the following: if the requested domain name matches a given string, override the IP in the response with one of my choice.
I've looked things up like easyhook, dllimport, msdn pages, etc, but I still can't figure out where to actually start, which classes I need to get access to, and so on. I basically have no pre-existing code for this particular problem.
I have Visual Studio, years of relatively simple programming experience and a good understanding of unfamiliar code and everything else, just no idea how to start or what to look for.
Many thanks for anything that can get me going.

Couple of pointers, DNS is as you mentioned some server somewhere that knows how to resolve a Domain name to an IP ADDRESS. There are two types of responses 1) Definitive for the domain and 2) Not Definitive for the Domain. Obviously any Definitive response will win.
DNS does not run at TCP/IP layer it runs one layer below using UDP (port 53). As a result DNS is tied into the TCP/IP stack whereby the stack first looks locally to a local host table and sees is the name is there, if it cannot find it the stack will send out the DNS request prior to the session start or SYN packet flying. In order for this to happen, the DNS servers must resolve the address.
I do not fully understand your issue because most Windows servers only have 1 IP Address and one associated Domain name. Unless of course they are using DHCP which just means the server uses multiple address on boot up and DNS server receive updates to what those new address are.
Public vs. Private IP addresses are the responsibility of a router. The router is configured to do NAT (Network Address Translation). This allows a private server to have unroutable addresses like 10.10.1.100 etc. But the router will answer ARPs for the public address making the public network think the router is the public address. So one of your solutions could be to convert your server to a private address and use a "real" router that can perform NAT. Port forwarding is not really routing rather it's a way to "trick" packets destined for one port number to be forwarded to another port dedicated for that application.
My suggestion is to either follow the suggestions above or add the Domain names you want to the local Host table on the server. This will get you what you want. A better solution is to configure your own server to be a DNS server definitive for your domain, that way you get to control everything.

I ended up solving this with (as a few have suggested) the hosts file after all.
I first used ManagedWifi to set a network connection monitor. It detects changes to my connection status and reports the network name.
Then I wrote a console app that stays open (using Hidden Start allows me to hide the window) and safely modifies the hosts file, then flushes the DNS cache. This seems to work in pretty much realtime. :)

Related

How can I connect to the net through a specific IP if my pc has multiple IP?

I have a desktop that has two Ethernet ports and each port connects to a different network(network A, network B).
I have a C# application that needs to run on this desktop and needs to access the net. The net should only be accessible from Network A.
The application seems to randomly select a network to try and access the net.
How can I get the application to look for and only connect through Network A?
Edit: just to add more info. I need to connect to the net via network A as I need to submit a httpWebRequest and the receiver will only accept info from Network A.
The mask determine the network according to IP Routing Protocol. You should have on the network, that goes towards the internet (primary), a mask of 0.0.0.0 which is the default. Then set mask on secondary to cover the secondary network only. Use 255.0.0.0,or 255.255.0.0, or 255.255.255.0
Assuming a legitimate scenario where both networks A and B are equally valid for raw internet access (say, a device with both a wifi and wired connection that are weighted equally), you can control this using routes.
Routes are not chosen by code in your application. Rather, they are part of the network configuration on the computer. You may feel tempted to use application code to alter the network configuration, but this is almost always a very bad idea.
To set the correct route in your network configuration, you need to know the IP address of your service (this kinda sucks, since often you only start out with a host name, and the IP address might even be dynamic in the case of cloud services). You must also know the network address for connection A. The network address is different from the gateway IP address, and typically ends with a 0 (though it is possible to construct network subnets with different network addresses).
Once you add the route, connections on the computer targeting your service's address will see this new route is more specific than the default gateways at A or B, and therefore always choose it.
A common use for routes is forcing certain traffic to pass over a special connection, such as a VPN.

how should I host my service such that other computers can consume myAppService using Internet

I have created a WCF service called MyAppService.
Now, I don't want to host that service on any servers. I would like to host it in my Computer. I know how to host a WCF service in Console/Windows App, as a Windows Service and in IIS.
But I have a problem to host this service in my computer. I mean in short that I want my Computer to Work as server and other computers should be able to consume myAppService from my computer via internet.
So, I would like to ask what is the best hosting option for me and how should I host my service such that other computers can consume myAppService using Internet. 1 more question : If the above problem is solved, then can I use netTcpBinding or I have to use some HttpBinding?
I'm still not 100% certain that I am answering the right question, but I'll give it a shot.
I believe what you are asking is: given that you are hosting a service in IIS on your personal computer, how do you allow others access to it? If my interpretation is incorrect, then please accept my apology in advance.
Note: I'm assuming IPv4 in this answer. IPv6 may be an option, but this answer would not apply.
There are two steps in the solution, and there are a few obstacles that you may or may not be able to overcome.
Get the client's web traffic to your public IP address.
Route incoming traffic to your computer.
Step 1
You can determine your public IP address in many ways, one of which is to ask Google: https://www.google.com/#q=what+is+my+ip
Obstacles are as follows:
Your IP address may change from time to time. The solution is to use a dynamic DNS service such as http://dyn.com/dns/, http://www.thatip.com/, or any of several alternatives that can be found online. These services will provide a URL that routes to your public IP, and they will provide a way to keep your public IP up-to-date so that name resoslution generally works. Keep in mind that DNS does not reliably change in realtime, however, so you should expect some downtime if and when your IP address changes.
Your ISP may block incoming traffic. Whether and how you can get around this is specific to your IP. Specifically, it is common for ISPs to block incoming port 20, 22, 23, 25, 80, and 443. You may be able to work around port blocking by choosing a non-standard port for your service. (This may not affect your WCF and IIS configuration as detailed in step 2.)
Particularly if you are using any kind of shared apartment connection, you may find that you do not have your own public IP address. If that is the case, then there is nothing you can do. If your router's WAN address (or computer's IP addres, if you're not using a router) is something in the 192.168.* or 10.* ranges, then it's time to look into server options.
Step 2
Once a client request has routed through the public internet to your public IP address, it needs to be routed to your computer.
The easiest, though least common these days, scenario is if your computer is directly connected to your cable/DSL modem. In this case, traffic that routes to your public IP is handled by your computer. This is the scenario where your WCF and IIS configuration needs to be listening on the actual port used by the client. For example, if your ISP is blocking port 80, then maybe you could configure your service to listen on port 34323, and that is the port that the client needs to request in the service call.
The most common scenario is that you have a router attached to your cable/DSL modem. In this case, you need to get into the administration control panel on your router and forward a port. For example, if your computer is configured to listen to port 80 for incoming service requests, then you can forward a port (for example, 34323) to port 80 on your local machine. How to forward the port differs by router. You should be able to find it by poking around or by doing an internet search.
The obstacle here is that your local IP address may change. You can overcome this by setting it to a static address outside of your router's DHCP assignment range or by setting up a DHCP reservation within your router. Either way will work. I suggest searching the internet for setting up a static IP or poking around in your router administration settings for DHCP reservations.
If you get all of your settings correct, and if there are no obstacles outside of your control, then a client on the internet can access your WCF service.
Edit:
I missed talking about local Windows firewall concerns. I could go into that, but allow me to link to this how-to guide that covers it pretty well. (Remember, a WCF service is conceptually similar to a website from a hosting standpoint, so much of this applies.) http://www.pcstats.com/articleview.cfm?articleID=1774

How to build Opera unite like functionality using .NET?

How to build Opera unite like functionality using .NET?
When we run opera browser and enable the opera unite then it shares our desktop on web and we can access it over internet without having a static IP, how does this works?
You need to map your dynamic IP to some static DNS name. One easy to use solution would be a DynDNS service. For these you get a small client program (or good routers already have a dyn dns client already implemented), that simply maps your current ip address to a specfic dns name.
After getting this done it depends on how variable you like it:
- Do you have one master that should be able to look on various client PCs for help?
- Do you like to connect two clients which are both have dynamic ips?
In the first case your master is the one how always ensure to map his current ip address to the dns name. On the router maybe some port forwarding is needed and your clients get a vnc client with reverse mode enabled. This allows everyone to built up a connection to your master and sharing the desktop without any changes on the client site. Only on the master site has something to be done (dynDns and port forwarding).
In the second case it is a little bit more complicated. Now you got two dynamic IPs that like to connect to each other. This is normally be done through some kind of mediator. Means both machine connect to some master (reachable through some DNS name). Now both machines have a bi-directional connection to the master and the master will simply take the data from the first client and forwards it to the second and vice versa. To take all the burden from the master it would be possible that the master in a first step sends the ip address of client A to B and vice versa. Then in a first step both trying to connect to each other directly. This method is known as UDP hole punching and does not always work, which then leads to a fallback to use the master instead.
Two examples that are using the second approach are Skype and join.me.
All these techniques don't have to do anything with .Net. It is pure knowledge about ethernet, TCP/IP, internet, etc. And due to the fact that you have to do a lot of work in this low-level area i would think that C# is not the ideal framework to accomplish this task. Maybe to put the GUI as last step over it all, but for the real work behind the scenes you need some decent network know-how which is mostly independent from the programming language you are using.
You should run your own DNS server. And whenever an user opens the browser and logs in, you should send the current IP address of the user to your server and update in your local DNS. Since you will have only a subdomain (desktop.operaunitecom), whenever someone hit your url it first goes to operaunite DNS server from where it will fetch your IP address. When the user's session closes you should remove the DNS entry. This could be a possible solution.

how can i send data over internet to any specific computer in subnet

I want to know how I can send or recieve data over internet to/from a computer in subnet
(this is specially in context to PPP users bcoz getting static IP is not so much in practice).
I actually want to create an application which can transfer file between 2 specific computer in WAN.
so what are things I need to know about to do the same..(ex. PRESENT IP or MAC ADDRESS etc..)
PROGRAMATICAL EXPLANATION ALTHOUGH PREFFERED,BUT IS NOT NECCESARY...
FTP?
There is a vast torrent of useful results in google, I seriously suggest to google before you ask here.
For instance, have a look at the top result: http://www.devarticles.com/c/a/C-Sharp/Network-Programming-in-C-sharp/
Apart from that, FTP, as suggested by Colin, may be what you're looking for. If you're new to using FTP in C# have a look at http://www.google.com/search?q=c%23+ftp
Put the information on a public IP server, so both computers keep polling if there is new data and send / download that data as needed. A single text file can hold necessary flags such as paths and other info you need.
server just need to be a web server, which means IIS or similar should be installed.
I'm thinking part of your question has to do with one of the computers not having a static IP address. If the two computers, A & B, don't know each others IP address, then an alternative is to use a server. Either A can store the information on the server to be picked up by B, or A can register his current IP address on the server for B to lookup and then connect to A, assuming firewalls don't interfere. Another scheme is that A and B can simultaneously connect to the server, and the server can relay data between the two.
Of course, all of this communication would be done using techniques such as those suggested by mafutrct and Colin.
This question sounds to me like the difficulties of NAT Traversal and trying to establish peer-to-peer connectivity over the Internet. I stumbled on this question researching the best way to set up Internet connectivity to a device that is sitting behind a firewall. I am assuming that the device behind the firewall has to initiate the connection, that a Internet server application (on a Public IP address) to at least manage the initiation of the connection is required, and the Internet server application may also have to act as a relay if the difficulties of NAT is not able to be traversed.
http://en.wikipedia.org/wiki/NAT_traversal
http://en.wikipedia.org/wiki/Peer-to-peer
A good example of this is LogMeIn. Where the application is installed on the computer that needs to be accessible over the Internet, that application communicates with the LogMeIn Internet servers, you can establish a connection to that computer either by initiating through the LogMeIn servers and connecting peer-to-peer over port 80/443 using NAT Traversal (this would be establishing a peer-to-peer connection) or by initiating through the LogMeIn server and the LogMeIn server acting as a relay if peer-to-peer is unable to be established.
Please note; you must have an application (something running) on both ends of the connection on the Internet, there is no other way. Just like FTP that was mentioned earlier (FTP Client, FTP Server)... but in this case it is peer-to-peer (basically you are writing your own Client and Server... or if you have to use a relay... Client--Relay--Server
http://en.wikipedia.org/wiki/LogMeIn
"The service connects the remote desktop and the local computer using SSL over TCP or UDP and utilizing NAT traversal techniques to achieve peer-to-peer connectivity when available."
The part I don't know is can the relay go in both directions; and I am figuring the device behind the firewall must have to constantly manage the open connection to the Internet Server that is the relay. Those are the questions I am wrestling with now.
Also, you may want to check out this post.
C# byte streams through TCP
From that time, till this date I have encounter so many problems with these four kinds of NAT that basically without an external server you cant do anything.
This is because, one computer can never find where the other one is located as suggested in the pic.
Here PC 'A' will never come to know about the port that corresponds to PC 'B' on R2B without an external server.Even more, if somehow u come to know about the ports you are still in a situation that the router wont allow you to access it if the request hasn't been made for your IP.

Acquire an IP address from dhcp server for my C# app - Like Virtual Machines

I'm developing an application of multiple socket connections (a TCP alarm watcher). Currently, in order to mock the alarms, i made small applications running on a VM Machine (that is, because the Vm have a different IP) that simulate the alarm endpoints.
What I want to do is to mock the alarms in the same machine I'm running my "server" (i.e. the first application), except that I want these mock alarms to have a different IP. I don't want my mocks running with the same IP that the first App (e.g. The server is 192.168.1.4; I want a mock to be 192.168.1.10, other being 192.168.1.11, etc.; all living in the same machine, just the way VM's can do that).
Virtual Machines in 'bridged network' mode can get a different IP from the DHCP server (that's where I get this idea). So, I'm a little dissoriented on where to investigate to complete the task, my question is:
How Can I make , programaticaly, my mock applications to get its own IP addresses via DHCP?
(or, Is it impossible, given the .NET Framework?)
Haj.-
You can certainly talk to the DHCP server -- the packet format is documented in RFC 2131. Doing this from C# is relatively simple.
However, all this does is "steal" an IP address from the DHCP server's pool. It doesn't actually bind it to the network stack.
It sounds like you still need to get your application to listen on this IP address. Unless this IP address is assigned to the network adapter on the machine, this won't work.
In short: you need multiple network adapters, or a multi-homed network adapter. In which case, you'd be better off letting it sort itself out with DHCP.
On the other hand, if all of your traffic is local, install the "Microsoft Loopback Adapter", and assign a bunch of IP addresses to that.
You will certainly be able to do this in C#, but I doubt that there is an existing class for doing this (its not a normal thing to do!)
You will probably end up having to do it yourself by recieving / sending the packets yourself.
I managed to find the following link which might be useful:
http://social.msdn.microsoft.com/Forums/en-US/wsk/thread/836c2150-583c-43a6-93b3-0e3202c2e2f5
(I know it says creating a DHCP server, but it could be a good place to start in terms of looking up the DHCP protocol workings)
Having said that I suppose you might have some problems requesting a lease for an IP address from a different IP address.

Categories

Resources