I'm writing a program that is accessed from a local network and also from the outside. I have to identify local and outside requests in order to give different functionality to each. Is that possible?
An alternative is to document the IP addresses of all the local computers and to get the IP address of the client thru Request.ServerVariables["Remote_ADDRS"] and compare them, if the request IP address doesn't match that means the request is not local. That seems to be awkward and inefficient because I would have to save all local addresses and any new computer would have to be regestered.
Is there a simple way to identify if a request is coming from the local network or from the outside?
Thank you!
Your network administrator will be able to tell you the IP range of the internal network. Then you can simply check if the client IP falls into the internal range.
This way, you only need to change your configuration, when the internal ranges is changed. This should not happen too often.
Edit: Here is a question and answer about IP ranges (albeit in Java, but should be very simple to convert).
Are you on a domain with the internal users? If so you could just offer the internal functionality only to those who login with integrated authentication.
Related
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. :)
Is it possible that store IP and/or MAC address of client system in Cookie in ASP.NET
It is possible to store anything relatively small (<100K) in cookies, obtaining information is whole other story.
After you obtained IP / MAC (hard to do on web page, and "really useful IP" is pretty much non-existent thing) you may need to encode value to put in cookies.
Obtaining MAC - not possible via normal JavaScript. Trusted site/trusted control may be able to obtain one MAC addresses in JavaScript.
Obtaining IP - externally visible IP is available on your server as header, you can also find existing service that will give you such IP. Note that it most cases it is different from IP of local machine visible locally.
The IP address of the user is stored in Request.UserHostAddress.
I want to get the IP address I am being NATed behind (e.g. the one presented to the outside world, rather than my local IP).
Something like the result you get from www.ipchicken.com.
How can I get this? The local IP info is easy to find, but I have no idea how to go about getting the IP assigned from the ISP.
The reason I need it is that my network infrastructure is such that I have two gateways out of the network. If one fails, it trips over transparently to the other. All well and good, but there is no alerting mechanism to tell me that I have failed over.
I believe it's quite hard to get this information. I guess another approach I could take is by putting a webservice on an externally hosted webserver - the idea being that it returns the IP of the querying host. (However, I have no idea how to do this either!) I suspect this might be the easiest way to go.
Your computer does not have access to this information locally; you need to get it from one of the many network services which do this.
There are lot's of ip services
This one, for example. Just make a GET request and parce a simple html : http://checkip.dyndns.org/
Some more details about this service (Policies and rules):
http://dyn.com/support/developers/checkip-tool/
upd
If you need to check your IP frequently you'd better to add your own web-service. .dyndns.org allows to check the ip once for 10 minutes.
You can run some .net code on web-service:
((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();
But I think a script language (python?) will be more suitable.
If you are programming an application which is heavily using the network (e.g. pings, dns resolves, etc), is it better to use IP addresses in the code (e.g. if making a folder path on a server - //192.183.181.182/test/test1 for example), or hostnames?
I am assuming IP addresses as they tend to be static in the enterprise, and hostnames can have aliases etc to a single IP address. Is this a valid reason to prefer IP addresses?
Thanks
The problem with using hardcoded IP addresses is that if you need to change it, you need to change the code, while if you use hostnames, if you need to change it, you can change where the hostname points, without touching the code :)
Fully agreeing with jcarlosn above, and throwing in my $0.02 as a network infrastructure guy...
As a network engineer, I could go on for quite some time about the number of instances where I've seen people mistakenly hard-code an IP into an application, and the suffering that happens for years because the company has to contort the growth of IT infrastructure around that original assumption of a hardcoded ip and a subnet in some particular facility. This has an impact on operational and capital expenses of the company.
Also consider what happens when that app is deployed on hundreds of PCs, and has now become business critical to your company and business partners.
Please use DNS names, or if you feel you must use an ip for some bizarre reason... use a local application config file as suggested in another post. Please understand though that a config file doesn't change the corporate downtime for a business-critical app to get migrated to a new IP address. If your desktop people are good, they can probably rewrite an application config file as quickly as you could migrate a DNS name... if you work for a company with no real scripting skills in the desktop deployment dept, please reconsider using DNS with no longer than a 30 minute TTL on that A record.
There are valid reasons for both. Multi-homed servers can cause issues when using names (as you mention), but IP addresses aren't necessarily stable either- especially in a DHCP environment that's not using reservations (more common than you might think).
If it were me, I'd put the path/hostname/IP in config- then you can change to whatever's needed without rebuilding the code. If you're doing name lookups yourself internally, just make sure your code is prepared for either an IP or a hostname.
IPv4 addresses (32bit addresses) are supposed to become obsolete in 2012,
(I think they made a movie about that, with earthquakes and tsunamis.)
There are already no new IPv4 addresses available for sale.
The new standard, IPv6, uses.much longer addresses.
Let the user decide, if he/she has a hostname, store that.
However, hostname may have more than 1 IP address.
If user provides an IP address (v4 or v6), store that.
If you get q MAC address, store that.
Explain the users pros and cons (e.g. names ate more static).
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.