5

I need to tell if a user entered the local machine's name in a textbox. This turns out to be trickier than I originally thought.

string userInput = inputTextbox.Text.ToLower();
string machineName = Environment.MachineName.ToLower();

bool isLocal = userInput.Equals(machineName ) || // This is what we started with...
             userInput.Equals(".") ||             // Then we added this...
             userInput.Equals("localhost");       // And then we added this...

As you can see, it's gotten quite messy and unmaintainable. For example, the address 127.0.0.1 wasn't included. Our testing department keeps writing bugs every time they find a new name for home. We need to squash this bug once and for all.

Is there an easier way to do this?

Rainbolt
  • 3,542
  • 1
  • 20
  • 44
  • 1
    In theory, your check for `localhost` could be wrong. If the hosts file is adjusted then anything can point to localhost. I don't think there's an easy solution to this, only a best effort. – thekip Sep 05 '14 at 14:13
  • You may want to think about IP version 6 too – Liath Sep 05 '14 at 14:13
  • 2
    @Liath Haha... this is why I asked this question. Testing keeps writing bugs every time they find a new name for home. We need to squash this bug once and for all. – Rainbolt Sep 05 '14 at 14:14
  • I hope you do - I'm intrigued now. I do wonder if a DNS lookup could be the best way... particularly once your testers realise they could add anything they liked to their hosts file – Liath Sep 05 '14 at 14:15
  • 1
    I second what @Liath says about using DNS and resolving/checking against the IP(s) bound to the local machine. – admdrew Sep 05 '14 at 14:17
  • Be aware that 127.x.y.z does point to the local machine for every x, y \in {0...255}, z \in {1..254} – Alexander Sep 05 '14 at 14:23
  • Depending on what the application is allowed to do, it could open a socket and try to connect to that socket using the name entered. :) – Alexander Sep 05 '14 at 14:24
  • Take a look at this example http://www.csharp-examples.net/local-ip/ – Sorceri Sep 05 '14 at 14:25
  • 1
    I'm digging around in System.Net.Dns but I've not found the answer yet! – Liath Sep 05 '14 at 14:27
  • @Liath I found it, thanks to Andrew! See this answer: http://stackoverflow.com/a/7268316/3224483. Quite frankly, that deserves to be the highest voted answer on that other question, but it was posted three years too late. Go forth and make it so! I'll delete mine shortly, but I wanted to get in some advertising for that other answer. XD – Rainbolt Sep 05 '14 at 14:30
  • Don't delete the question, let it get marked as duplicate so other people can find the dupe if they use your wording :) – eddie_cat Sep 05 '14 at 14:32
  • @eddie_cat Ok. Voting to close my own question so it can serve as a signpost then. – Rainbolt Sep 05 '14 at 14:33

2 Answers2

0

There are many definition for this machine

  1. All IP addresses in all adapters (wireless, vpn and dhcp are here to mess up further)
  2. 127.x.y.x addresses (good catch @Alexander, I did not know that)
  3. Any references to these IP addresses in hosts file
  4. computer name
  5. computer fully qualified dns name
  6. dns records that point to that machine (I use multiple dns records point to same machine for http hostname header)
  7. . char (which is valid only for some cases, like SQL server)
  8. Any long number which can be converted to IP addresses listed above. For example try this: ping 2130706433

And this is what I managed to find in two minutes. But this will cover %80 of your cases. Other %20 will still be included in "prohibited" list.

BTW, Tell your test crew to find as many as they can find in one shot. This should reduce bounces from QA.

Erdogan Kurtur
  • 3,630
  • 21
  • 39
-1

You could do something like

List<String> localNames = new List<string>{Environment.MachineName.ToLower(), ".", "localhost"};

bool isLocal = localNames.Any(s=>s.Equals(userInput));

Then you just need to extend the list rather than adding additional conditions. Although not ideal, it makes the updating of your code easier. You could put in code to look up the local IP of the machine as well and add that automatically to the list of strings.

Personally, I've not seen any other way of doing something like this for all possible scenarios.

Pheonyx
  • 851
  • 6
  • 15