Question
*****Please post the solution as a TEXT not as a PICTURE ***** 1. Modify the NetworkResolverDemo program given in class, such that it prints all
*****Please post the solution as a TEXT not as a PICTURE *****
1. Modify the NetworkResolverDemo program given in class, such that it prints all the IP addresses for the entered host name, whether the host is reachable through each of them, and whether the address is an IPv4 or IPv6 address. The output of your program should be in the following format: Host name: xxxxxx IPv4, 192.1.1.0, reachable. IPv6, 1E0F:0:0:0:0:1:0:101, unreachable. . . .
2. You are required to study the following SpamCheck program. Then, test the program for the following IP addresses: 207.34.56.23, 125.12.32.4, 130.130.130.130.
Note that this program and its full explanation are taken as is from chapter 4 (pages 111 and 112) of Java Network Programming, Elliotte Rusty Harold, O'Reilly, Fourth Edition. A number of services monitor spammers, and inform clients whether a host attempting to connect to them is a known spammer or not. These real-time blackhole lists need to respond to queries extremely quickly, and process a very high load. Thousands, maybe millions, of hosts query them repeatedly to find out whether an IP address attempting a connection is or is not a known spammer.
The nature of the problem requires that the response be fast, and ideally it should be cacheable. Furthermore, the load should be distributed across many servers, ideally ones located around the world. Although this could conceivably be done using a web server, SOAP, UDP, a custom protocol, or some other mechanism, this service is in fact cleverly implemented using DNS and DNS alone.
To find out if a certain IP address is a known spammer, reverse the bytes of the address, add the domain of the blackhole service, and look it up. If the address is found, its a spammer. If it isnt, its not. For instance, if you want to ask sbl.spamhaus.org if 207.87.34.17 is a spammer, you would look up the hostname 17.34.87.207.sbl.spamhaus.org. (Note that despite the numeric component, this is a hostname ASCII string, not a dotted quad IP address.)
If the DNS query succeeds (and, more specifically, if it returns the address 127.0.0.2), then the host is known to be a spammer. If the lookup failsthat is, it throws an UnknownHostException it isnt.
import java.net.*;
public class SpamCheck {
public static final String BLACKHOLE = "sbl.spamhaus.org";
public static void main(String[] args) throws UnknownHostException {
for (String arg: args) {
if (isSpammer(arg)) {
System.out.println(arg + " is a known spammer.");
} else {
System.out.println(arg + " appears legitimate.");
}
}
}
private static boolean isSpammer(String arg) {
try {
InetAddress address = InetAddress.getByName(arg);
byte[] quad = address.getAddress();
String query = BLACKHOLE;
for (byte octet : quad) {
int unsignedByte = octet
query = unsignedByte + "." + query;
}
InetAddress.getByName(query);
return true;
} catch (UnknownHostException e) {
return false;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started