• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Server Host/Port Validation

 
Chris Bicnal
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just fire a separate thread for that. I do want your app to stay responsive, because the user can decide to close it
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chris Bicnal
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:I used following code to try to get the registry


    Me too!!!
     
    Grary Stimon
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting! I only use the Registry class when I start the server, never after that...

    Grary
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Grary Stimon wrote:Interesting!

    Yeah, sometimes I tell something interesting, but unfortunately these moments are very rarely
     
    Grary Stimon
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 611
    1
    IntelliJ IDE Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    Norbert Lebenthal
    Ranch Hand
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 611
    1
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Glad I could help :-)

    Good luck with the rest of your project!
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic