1

I need to see if a request is coming from a certain domain (www.domain.com).

So far, I only know how to get the IP address of the request (by using the request.getRemoteHost() method). The problem is that one domain might map to a lot of different IP addresses (for balance purposes), so I can not do something like this:

   request.getRemoteHost().equals("200.50.40.30")

because there might be different IP address returned by the DNS when it resolves www.domain.com.

I want to be able to do something like this:

   request.getRemoteHost().equals("www.domain.com")

But so far, I have no clue (and Google didn't help me) on how to do that.

Does somebody have any ideas??

Thank you in advance! :)

  • possible duplicate of [Need to perform a reverse DNS lookup of a particular IP address in java](http://stackoverflow.com/questions/7097623/need-to-perform-a-reverse-dns-lookup-of-a-particular-ip-address-in-java) – gpeche Jul 03 '12 at 21:40

2 Answers2

2

Can you just feed IP to InetAddress and call getCanonnicalHostName()?

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
1

After contacting the domain I was trying to verify the request was coming from, they provided me the whole range of IPs their servers might start a request from. Having all those IPs and masks in hands, this is what I did to verify the request was coming from them:

        //Those IPs and Maks were provided by the domain
    String[] ipsAndMasks = { "AAA.BBB.CCC.DDD/26",
                         "EEE.BBB.CCC.DDD/24",
                 "FFF.BBB.CCC.DDD/29",
                 "GGG.BBB.CCC.DDD/22"};

    Collection<SubnetInfo> subnets = new ArrayList<SubnetInfo>();
    for (String ipAndMask : ipsAndMasks) {
        subnets.add(new SubnetUtils(ipAndMask).getInfo());
    }

    boolean requestIsComingFromTheCorrectDomain = false;
    String ipAddress = request.getRemoteAddr();
    for (SubnetInfo subnet : subnets) {
        if (subnet.isInRange(ipAddress)) {
            requestIsComingFromTheCorrectDomain = true;
            break;
        }
    }

Hope this code also helps someone!