forked from apache/whirr
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WHIRR-128. Fix DNS resolution for clients running within EC2. Contrib…
…uted by Tibor Kiss. git-svn-id: https://svn.apache.org/repos/asf/incubator/whirr/trunk@1032844 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
7 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
services/hadoop/src/main/java/org/apache/whirr/service/hadoop/DnsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.whirr.service.hadoop; | ||
|
||
import java.io.IOException; | ||
|
||
import org.xbill.DNS.DClass; | ||
import org.xbill.DNS.ExtendedResolver; | ||
import org.xbill.DNS.Message; | ||
import org.xbill.DNS.Name; | ||
import org.xbill.DNS.Record; | ||
import org.xbill.DNS.Resolver; | ||
import org.xbill.DNS.ReverseMap; | ||
import org.xbill.DNS.Section; | ||
import org.xbill.DNS.Type; | ||
|
||
public class DnsUtil { | ||
|
||
/** | ||
* resolve the reverse dns name for the given IP address | ||
* | ||
* @param hostIp | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static String resolveAddress(String hostIp) throws IOException { | ||
Resolver res = new ExtendedResolver(); | ||
|
||
Name name = ReverseMap.fromAddress(hostIp); | ||
int type = Type.PTR; | ||
int dclass = DClass.IN; | ||
Record rec = Record.newRecord(name, type, dclass); | ||
Message query = Message.newQuery(rec); | ||
Message response = res.send(query); | ||
|
||
Record[] answers = response.getSectionArray(Section.ANSWER); | ||
if (answers.length == 0) | ||
return hostIp; | ||
else { | ||
String revaddr = answers[0].rdataToString(); | ||
return revaddr.endsWith(".") ? revaddr.substring(0, revaddr.length() - 1) : revaddr; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
services/hadoop/src/test/java/org/apache/whirr/service/hadoop/DnsUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.whirr.service.hadoop; | ||
|
||
import static org.junit.Assert.*; | ||
import static java.lang.System.out; | ||
|
||
import java.io.IOException; | ||
import java.net.Inet4Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.UnknownHostException; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
|
||
import org.apache.whirr.service.hadoop.DnsUtil; | ||
import org.junit.Test; | ||
import org.xbill.DNS.Address; | ||
|
||
public class DnsUtilTest { | ||
|
||
@Test | ||
public void testResolveAddress() throws IOException { | ||
// test it with all interfaces | ||
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); | ||
while (en.hasMoreElements()) { | ||
NetworkInterface netint = (NetworkInterface) en.nextElement(); | ||
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); | ||
for (InetAddress inetAddress : Collections.list(inetAddresses)) { | ||
if (inetAddress instanceof Inet4Address) { | ||
long start = System.currentTimeMillis(); | ||
String reverse = DnsUtil.resolveAddress(inetAddress.getHostAddress()); | ||
long end = System.currentTimeMillis(); | ||
// we know that java.net.InetAddress's getHostName takes > 4.5s if | ||
// there is no reverse address assigned to it | ||
// but DnsUtil can resolve any address without this delaying problem. | ||
assertTrue("DnsUtil.resolveAddress takes " + (end - start) | ||
+ " millis, it should be shorter than a second", | ||
end - start < 1000); | ||
if (inetAddress.toString().substring(1).equals(reverse)) { | ||
out.printf( | ||
"InetAddress %s on interface %s does not have reverse dns name, so their reverse remains: %s\n", | ||
inetAddress, netint.getDisplayName(), reverse); | ||
} else { | ||
if (inetAddress.isLoopbackAddress()) { | ||
out.printf( | ||
"InetAddress %s on loopback interface %s obtained reverse name as %s\n", | ||
inetAddress, netint.getDisplayName(), reverse); | ||
} else { | ||
out.printf( | ||
"InetAddress %s on interface %s has reverse dns name: %s\n", | ||
inetAddress, netint.getDisplayName(), reverse); | ||
try { | ||
InetAddress checkedAddr = Address.getByName(reverse); | ||
assertEquals(inetAddress, checkedAddr); | ||
} catch (UnknownHostException uhex) { | ||
fail("InetAddress " + inetAddress + " on interface " | ||
+ netint.getDisplayName() + " got " + reverse | ||
+ " reverse dns name which in return is an unknown host!"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |