aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Server Host/Port Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Server Host/Port Validation" Watch "Server Host/Port Validation" New topic
Author

Server Host/Port Validation

Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Hi everyone,

I'm doing my final round of tests on my application and have discovered a nasty little problem.

When I launch the client in network mode (with a running server) I have code to validate whether the port number entered is in range. I also check the hostname/ip address is not blank. However, if someone were to enter a hostname of 'ojhslahdbskdbkashdasj' my application just hangs!

I've tried using the Socket class to test whether I can get a connection to the host/port specified before I launch the main client window, although this also hangs.

Has anyone solved this problem, or have you just acknowledged it as a know issue in choices.txt?

Thanks,

Chris
Vasya Pupkin
Ranch Hand

Joined: Dec 06, 2006
Posts: 39
I just fire a separate thread for that. I do want your app to stay responsive, because the user can decide to close it


SCJP, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

I execute these validations:
- port number should be in a range
- if user enters ip address, i check if it is a valid ip address format. if it is, i check with InetAddress.getByAddress if a host could be found with that ip address
- if user enters hostname, i check with InetAddress.getAllByName if a host could be found with that hostname

If no host could be found an UnknownHostException is thrown (by both methods) and that will result in disabling the connect-button, so user can't connect. My application will not hang and I also didn't have to create a seperate thread. Just keeping it as simple as possible.

Kind regards,
Roel

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Hey Roel,

That's an interesting approach, although looking at the JavaDoc I'm not sure I follow what your checks actually prove.

The IP validation makes sense (in terms of whether it's in the correct format) but I'm not sure about the host look up. Are you just calling 'getHostName()' and checking to see if it's a value that's not the IP address?

Thanks!

Chris
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

Hi Chris,

No, I'm just doing something like


And the same for when a hostname is entered. I tested it with 127.0.0.1 and localhost, with an ip-address in my home network and with a host name like 'sdfdsfs' and I always got the expected result. So that was good enough for me

Kind regards,
Roel
mayuresh chaubal
Greenhorn

Joined: Dec 15, 2005
Posts: 12
there are 2 occasions where we take host name.

1. host name entered by client to connect to server.
2. host name entered by server to address network interface on which it is going to run.
[as there can be more than 1 network interfaces declared in 1 machine]

for 1. i employed the socket trick. with hostname and port provided.
for 2. i itereated over network interfaces to check if the entered address exists.
kind of


Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Hey Mayuresh,

When you use a Socket to create a connection to a host that isn't available don't you find it takes a long while to time out?

I found that so am using the InetAddress.getByName() method to verify the host name. It doesn't do anything about the port although I've figured that out a different way.

As for an IP address I'm still confused by this. The InetAddress.getByAddress() always seems to execute successfully, even when all of my network interface cards are disabled!

I'm half tempted to document that only a textual hostname can be entered into the network client server host field, although I feel like that is a cop out.

Am I missing something out with the IP address validation, or have other people found the same?

Thanks,

Chris
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

Hi Chris,

I did some tests with my application. I entered following values:
- localhost2 --> application gives a validation error (not a valid hostname/ip address)
- localhost --> application says everything ok (as expected)
- fdsfsd --> application gives a validation error (not a valid hostname/ip address)
- 10.10.10.10 (no computer in my network has such an ip address, if I ping to this ip, i get "request timed out" error) --> application says everything ok (a validation error is expected)
- 10.1.10.100 (a computer in my network has such an ip address, if I ping to this ip, the small data packages are sent and received successfully) --> application says everything ok (as expected)

So the validation of the hostnames work as expected, but the ip address validation fails.

Kind regards,
Roel
Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Hey Roel,

That makes me feel better as that's the same behaviour I'm seeing.

I guess the best thing I can hope for (given how much I want to get this thing finished now) is to verify that the IP address is in the correct format (i.e. four numbers all between 0 and 255). I know that's not perfect as someone could enter 255.255.255.255 which is obviously not a valid address, but if I document this in the user guide and choices I'm sure it'll be fine.

Thanks for your help with this!

Chris
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

Hi Chris,

Not a problem! That's the reason of the ranch, helping each other

If you document it in userguide and choices, you certainly will be fine. The only thing I added in my userguide was something like "a valid server address is an IP address or hostname of any computer that is accessible through a network". And I believe I didn't mention anything about it in my choices.txt (it was already big enough )

Now it's time to go have a

Kind regards and good luck with your assignment,
Roel
Grary Stimon
Ranch Hand

Joined: Sep 30, 2008
Posts: 42
Ranchers,

(Maybe, posting a reply to a thread this old is unrealistic.)

I've been tussling with this issue for about a day -- trying to understand how to handle the case when my network client is passed a validly-formatted, but unavailable server IP address. Based on what you guys are saying, there's no solution? Seems like Vasya came closest to explicitly handling this, but how is the 'rescue' thread triggered?

Grary
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I added a method to my controller to test the connection (the IP address has already been validated), and it will display a message dialog if it gets a RemoteException. I added a println with e.getMessage() to my output because while I was testing on 2 computers, I got this message It basically calls the same method that will be used to set up the remote connection once validation passes. I think this is the same situation your talking about. Valid IP address, but might not work. If it's somewhere else, how would you know for sure if it will work unless you try it?


SCJP, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

Hi Grary,

June 2009 is not that old and there are people enough who seem to be living on this forum

This is how I handled:
  • The entered server address (ip or hostname) is validated: if it is valid, the connect-button will be enabled (otherwise it will be disabled)
  • when the connect-button is clicked the application tries to connect to the server (and get a remote reference to my business service). If it is retrieved successfully, the main application window is shown. If it is not (and a RemoteException is thrown), a message is shown to the user informing him the application will exit because no connection to the server could be made.

  • A little example to clarify the (boring) explanation. The server is running on a pc with ip 10.1.10.1 and the client application is ran on a pc with ip 10.1.10.2 (for simplicity the port number is omitted):
  • user enters "10.a.10.1": connect-button disabled (not valid ip)
  • user enters "localhost": valid hostname, but application will exit because no server running
  • user enters "10.1.10.1": valid ip and main application window will show (because successful connect to server)


  • Kind regards,
    Roel
    Grary Stimon
    Ranch Hand

    Joined: Sep 30, 2008
    Posts: 42
    Hey Anne,

    I added a method to my controller to test the connection (the IP address has already been validated), and it will display a message dialog if it gets a RemoteException.


    Thanks, that -- and Roel's comment -- spurred some further thinking: in fact my network client will throw an exception if Naming.lookup(url) cannot find the specified server, but it takes a good while. Did you employ any trick(s) to hasten the speed by which RemoteException would be delivered from Naming.lookup(url)? If not, I'm tempted to add some kind of status window...

    Grary
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5286
        
      13

    Hi Grary,

    I don't use Naming.lookup, so no idea how to speed that up. I used following code to try to get the registry:


    Kind regards,
    Roel
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2265
        
        3

    Roel De Nijs wrote:I used following code to try to get the registry


    Me too!!!


    Cheers, Bob "John Lennon" Perillo
    SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
    Grary Stimon
    Ranch Hand

    Joined: Sep 30, 2008
    Posts: 42
    Interesting! I only use the Registry class when I start the server, never after that...

    Grary
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5286
        
      13

    Grary Stimon wrote:Interesting!

    Yeah, sometimes I tell something interesting, but unfortunately these moments are very rarely
    Grary Stimon
    Ranch Hand

    Joined: Sep 30, 2008
    Posts: 42
    Hey y'all,

    I wanted to post one more comment on this matter, as I gone so far as to attempt to "trigger" an exception using other Ranchers' connection style with Registry suggested above.

    By now, I'm pretty confident that attempting to connect to an RMI server using a properly-formatted but unavailable server IP host address just won't yield an exception quickly.

    Last call, does anyone else know of a way to get rapid confirmation of IP address availability in the RMI scheme or by some other simple technique?

    Thanks,

    Grary
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    Reading this post, I tried to validate the IP address, from the String to the required byte[].

    EDIT: I changed a bit my strategy because I actually need to test against ipv4 and ipv6... and java has no built in mechanism for that.

    So in the end I do that:


    However, I'm still struggling with the string to byte array conversion.

    Indeed, with a string of 254.254.300.1, the byte array content is then (from eclipse debug perspective variable view) [-2, -2, 44, 1], which somehow doesn't trigger an exception in return Inet4Address.getByAddress(ip) != null;

    How shall I do this string to byte array fit for getByAddress(byte[]) conversion ?
    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    Norbert, if you want to validate your IP address, didn't you consider using
    InetAddress.getByName(-) method? (http://download.oracle.com/javase/6/docs/api/)

    It will check if your IP format is appropriate, just as the javadoc states.

    Cheers!


    OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    Pedro Kowalski wrote:Norbert, if you want to validate your IP address, didn't you consider using
    InetAddress.getByName(-) method? (http://download.oracle.com/javase/6/docs/api/)

    It will check if your IP format is appropriate, just as the javadoc states.

    Cheers!


    arg, I hadn't seen it

    anyway, I can go to bed with a "code working" feeling now. Thanks a lot !!!
    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    Glad I could help :-)

    Good luck with the rest of your project!
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5286
        
      13

    Hi Norbert,

    First of all: do you really need IPv6 addresses? I kept it simple and just used the ones we are familiar with for more than a decade

    Secondly, according to the javadoc of the InetAddress.getByName I think it's not useful to validate an IP address. You can use it though to validate a host name.
    Determines the IP address of a host, given the host's name.
    Parameters:
    host - the specified host, or null.
    Returns:
    an IP address for the given host name.


    Finally, I used the following scenario to validate the server's address:
  • 1/ check if the value entered by the user is a valid ip address (using a regex, just limited to IPv4 addresses)
  • 2a/ ip address is valid: split in quads, convert to byte[] and use InetAddress.getByAddress(byte[] addr) to verify if ip exists (just like your logic, but a bit simpler because I just had to handle 1 version of ip address
  • 2b/ ip address is not valid: use InetAddress.getByName(String host) to verify if it is a valid host name


  • Hope it helps!
    Kind regards,
    Roel
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    we're about to run out of IPv4 addresses, so IPv6 ones are more and more likely to be used.

    For the javadoc of InetAddress.getByName , it says on the same url you provided:

    Determines the IP address of a host, given the host's name.

    The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

    For host specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted. IPv6 scoped addresses are also supported. See here for a description of IPv6 scoped addresses.


    regarding whether the IP address properly points to something existing, wasn't it said earlier in the topic that actually InetAddress.getByAddress(byte[] addr) doesn't check for existence of the target ? If so, then InetAddress.getByName is sensibly the same.

    Side note: a regex for an IPv6 address looks like way more challenging
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5286
        
      13

    Ok, I just read the short description of the method and ignored the complete description, my mistake

    Norbert Lebenthal wrote:wasn't it said earlier in the topic that actually InetAddress.getByAddress(byte[] addr) doesn't check for existence of the target ? If so, then InetAddress.getByName is sensibly the same.

    Correct, I tested it here. Maybe you can run the same tests as I did in that reply and post your results. If both methods would give the same results, using InetAddress.getByName is simpler (and thus better)
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    Roel De Nijs wrote:Ok, I just read the short description of the method and ignored the complete description, my mistake

    I did it before as well lol

    Roel De Nijs wrote:
    Norbert Lebenthal wrote:wasn't it said earlier in the topic that actually InetAddress.getByAddress(byte[] addr) doesn't check for existence of the target ? If so, then InetAddress.getByName is sensibly the same.

    Correct, I tested it here. Maybe you can run the same tests as I did in that reply and post your results. If both methods would give the same results, using InetAddress.getByName is simpler (and thus better)


    I ran some tests at home, which basically confirmed yours (as far as I remember), I'll look at them when I'm back there tonigh.
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    well, here is a new test:

    when pinging it never returns

    test all green => same behaviour, just checking IP address syntax, no more no less.

    so InetAddress.getByName is simpler
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Server Host/Port Validation