diff --git a/network/src/main/java/de/muehlencord/shared/network/NetworkInformation.java b/network/src/main/java/de/muehlencord/shared/network/NetworkInformation.java index f3a78d1..d89f4b1 100644 --- a/network/src/main/java/de/muehlencord/shared/network/NetworkInformation.java +++ b/network/src/main/java/de/muehlencord/shared/network/NetworkInformation.java @@ -4,11 +4,13 @@ */ package de.muehlencord.shared.network; +import java.io.Serializable; + /** * * @author jomu */ -public class NetworkInformation { +public class NetworkInformation implements Serializable { // private String network; private String netname; diff --git a/network/src/main/java/de/muehlencord/shared/network/Whois.java b/network/src/main/java/de/muehlencord/shared/network/Whois.java index b4949ad..feb4bae 100644 --- a/network/src/main/java/de/muehlencord/shared/network/Whois.java +++ b/network/src/main/java/de/muehlencord/shared/network/Whois.java @@ -32,6 +32,9 @@ public class Whois { public Whois() { whois = new WhoisClient(); whois.setDefaultTimeout(60000); + network = null; + rootNetwork = new LinkedList<>(); + networkInformation = null; } @@ -64,30 +67,50 @@ public class Whois { switch (startString) { case "inetnum:": - // information on network server belongs to (upper level / level -1) - String inetnum = StringUtil.getValueBetweenKeywords(block, "inetnum:", "netname:"); - logger.info("inetnum: = " + inetnum); - String netname = StringUtil.getValueBetweenKeywords(block, "netname:", "descr:"); - logger.info("netname: = " + netname); - String descr = StringUtil.getValueBetweenKeywords(block, "descr:", "country:"); - logger.info("descr: = " + descr); - String country = StringUtil.getValueBetweenKeywords(block, "country:", "admin-c:"); - logger.info("country: = " + country); - String adminC = StringUtil.getValueBetweenKeywords(block, "admin-c:", "tech-c:"); - logger.info("admin-c: = " + adminC); - String techC = StringUtil.getValueBetweenKeywords(block, "tech-c:", "status:"); - logger.info("tech-c: = " + techC); - String status = StringUtil.getValueBetweenKeywords(block, "status:", "mnt-by:"); - logger.info("status: = " + status); - String mntBy = StringUtil.getValueBetweenKeywords(block, "mnt-by:", "source:"); - logger.info("mnt-by: = " + mntBy); + + String inetnum = null; + String country = null; + String netname = null; + String descr = null; + String[] lines = block.split ("\n"); + for (String line : lines) { + String lineStartString; + String value; + if (line.indexOf (" ") == -1) { + lineStartString = line; + value = ""; + } else { + lineStartString = line.substring(0, line.indexOf(" ")); + value = line.substring(line.indexOf(" ")); + value = value.trim(); + } + + switch (lineStartString) { + case "inetnum:": + inetnum = value; + break; + case "country:": + country = value; + break; + case "descr": + descr = value; + break; + case "netname": + netname = value; + break; + } + } + + if (inetnum == null) { + inetnum = ""; // FIXME throw exception + } // convert inetnum to CDIR String[] ipAddresses = inetnum.split(" "); // TODO - what happens if not 3 parts are found // FIXME add CDIR notation support String startIpAddress = ipAddresses[0]; String endIPAddress = ipAddresses[2]; - network = CidrTool.rangeToCidrList(startIpAddress, endIPAddress); + network = CidrTool.rangeToCidrList(startIpAddress, endIPAddress); logger.info("Network:" + network.toString()); this.networkInformation = new NetworkInformation(netname, descr, country); @@ -95,9 +118,8 @@ public class Whois { break; case "route:": // get information on level-2 network - String route = StringUtil.getValueBetweenKeywords(block, "route:", "descr:"); - this.rootNetwork = new LinkedList<>(); - this.rootNetwork.add(route); + String route = StringUtil.getValueBetweenKeywords(block, "route:", "descr:"); + this.rootNetwork.add (route); logger.info ("Root network "+this.rootNetwork.toString()); break; @@ -129,4 +151,8 @@ public class Whois { public List getRootNetwork() { return this.rootNetwork; } + + public NetworkInformation getNetworkInformation() { + return this.networkInformation; + } } diff --git a/network/src/test/java/de/muehlencord/shared/network/CidrToolTest.java b/network/src/test/java/de/muehlencord/shared/network/CidrToolTest.java new file mode 100644 index 0000000..c326607 --- /dev/null +++ b/network/src/test/java/de/muehlencord/shared/network/CidrToolTest.java @@ -0,0 +1,23 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package de.muehlencord.shared.network; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author jomu + */ +public class CidrToolTest { + + public CidrToolTest() { + } + + @Test + public void testCidrConversion() { + System.out.println (CidrTool.rangeToCidrList("213.55.64.0", "213.55.127.255")); + } +} \ No newline at end of file diff --git a/network/src/test/java/de/muehlencord/shared/network/WhoisTest.java b/network/src/test/java/de/muehlencord/shared/network/WhoisTest.java index 008640b..d5236e4 100644 --- a/network/src/test/java/de/muehlencord/shared/network/WhoisTest.java +++ b/network/src/test/java/de/muehlencord/shared/network/WhoisTest.java @@ -31,18 +31,56 @@ public class WhoisTest extends BaseTest { @Ignore public void testInternic() throws UnknownHostException, SocketException, IOException, ParseException { Whois whoIsClient = new Whois(); - // Denic answers "status: connect" only whoIsClient.execute ("whois.1api.net", "egameladder.com"); } @Test + @Ignore + public void testArin() throws UnknownHostException, SocketException, IOException, ParseException { + Whois whoIsClient = new Whois(); + whoIsClient.execute ("whois.arin.net", "204.232.209.184"); + } + + @Test + @Ignore + public void testApnic() throws UnknownHostException, SocketException, IOException, ParseException { + Whois whoIsClient = new Whois(); + whoIsClient.execute ("whois.apnic.net", "60.247.69.45"); + } + + @Test + public void testUnkown() throws UnknownHostException, SocketException, IOException, ParseException { + Whois whoIsClient = new Whois(); + // whoIsClient.execute("whois.arin.net", "174.139.180.10"); + // whoIsClient.execute("whois.twnic.net", "60.250.38.39"); + // whoIsClient.execute("whois.apnic.net", "60.28.27.14"); + // whoIsClient.execute("whois.apnic.net", "203.127.200.113"); + // whoIsClient.execute("whois.apnic.net", "182.72.111.26"); + // whoIsClient.execute("whois.ripe.net", "213.55.95.62"); // Transferred to Afrinic + whoIsClient.execute("whois.afrinic.net", "213.55.95.62"); + + + + + + + + + + } + + + + + + @Test + @Ignore public void testStepWise() throws UnknownHostException, SocketException, IOException, ParseException { Whois whoIsClient = new Whois(); // whoIsClient.execute ("whois.ripe.net", "88.198.181.181"); whoIsClient.execute ("whois.ripe.net", "212.204.60.1"); assertTrue ("network", whoIsClient.getNetwork().contains("212.204.60.0/24")); // whoIsClient.execute ("whois.ripe.net", "212.204.60.0"); - // whoIsClient.execute ("whois.ripe.net", "212.0.0.0/8"); - + // whoIsClient.execute ("whois.ripe.net", "212.0.0.0/8"); } } \ No newline at end of file