Commits

György Kohut  committed a7121f6

shadowserver_asn: allow incomplete response

  • Participants
  • Parent commits 29b6fc4

Comments (0)

Files changed (1)

File src/main/java/org/honeynet/hbbackend/shadowserver_asn/ShadowserverAsn.java

 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Types;
 import java.util.regex.Pattern;
 
 import javax.annotation.Resource;
 	private final static String SERVER = "asn.shadowserver.org";
 	private final static int SERVER_PORT = 43;
 	private final static int SO_TIMEOUT = 3000;
-    private final static Pattern SPLIT_REGEX = Pattern.compile("\\s+\\|\\s+");
+    private final static Pattern SPLIT_REGEX = Pattern.compile("\\s\\|\\s");
     
     public static final String SUB_NAME = "shadowserver_asn";
 	
 	private String emsg;
 	
 	
+	public static class IncompleteResponseException extends Exception { }
+	
+	
 	@Schedule(second="*/1", minute="*", hour="*", persistent=false)
 	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
 	public void work() {
 			String ip = msg.getStringProperty("ip");
 			log.debug("received new msg | ip = {}", ip);
 			
-			store(ip, retrieve(ip));
+			try {
+				store(ip, retrieve(ip));
+			}
+			catch (IncompleteResponseException e) {
+				return;
+			}
 		}
 		catch (JMSException e) {
 			log.error("got JMSException", e);
 	}
 	
 	
-	private Result retrieve(String ip) {
+	private Result retrieve(String ip) throws IncompleteResponseException {
 		log.trace("retrieve()");
 		
 		try {
 			Result result = new Result();
 			int i = -1;
 			try {
-				result.asn = handleLongField(responseFields[i=1]);
-				result.as_name = handleStringField(responseFields[i=3]);
-				result.cc = handleStringField(responseFields[i=4]);
-				result.dom = handleStringField(responseFields[i=5]);
-				result.isp = handleStringField(responseFields[i=6]);
-				result.bgp_prefix = handleStringField(responseFields[i=2]);
+				result.asn = handleLongField(responseFields[i=1], true);
+				result.as_name = handleStringField(responseFields[i=3], true);
+				result.cc = handleStringField(responseFields[i=4], true);
+				result.dom = handleStringField(responseFields[i=5], true);
+				result.isp = handleStringField(responseFields[i=6], true);
+				result.bgp_prefix = handleStringField(responseFields[i=2], true);
+				
+				if (result.asn == null && result.cc == null) {
+					log.warn("ignoring response as neither asn nor cc is specified | ip={}");
+					throw new IncompleteResponseException();
+				}
 			}
 			catch (ValidationException e) {
 				emsg = "invalid response: field " + i + ": " + e.getMessage();
 			
 			pStmt = db.prepareStatement("insert into asn_shadowserver (ip, asn, as_name, cc, dom, isp, bgp_prefix) values (inet(?),?,?,?,?,?,inet(?)) returning id");
 			pStmt.setString(1, ip);
-			pStmt.setLong(2, result.asn);
-			pStmt.setString(3, result.as_name);
-			pStmt.setString(4, result.cc);
-			pStmt.setString(5, result.dom);
-			pStmt.setString(6, result.isp);
-			pStmt.setString(7, result.bgp_prefix);
+			if (result.asn == null)
+				pStmt.setNull(2, Types.BIGINT);
+			else pStmt.setLong(2, result.asn);
+			if (result.as_name == null)
+				pStmt.setNull(3, Types.VARCHAR);
+			else pStmt.setString(3, result.as_name);
+			if (result.cc == null)
+				pStmt.setNull(4, Types.VARCHAR);
+			else pStmt.setString(4, result.cc);
+			if (result.dom == null)
+				pStmt.setNull(5, Types.VARCHAR);
+			else pStmt.setString(5, result.dom);
+			if (result.isp == null)
+				pStmt.setNull(6, Types.VARCHAR);
+			else pStmt.setString(6, result.isp);
+			if (result.bgp_prefix == null)
+				pStmt.setNull(7, Types.VARCHAR);
+			else pStmt.setString(7, result.bgp_prefix);
 			queryRes = pStmt.executeQuery();
 			queryRes.next();
 			recordId = queryRes.getLong(1);
 	}
 	
 	
-	private static String handleStringField(String value) throws ValidationException {
+	private static String handleStringField(String value, boolean allowEmpty) throws ValidationException {
 		value = value.trim();
-    	if (value.equals("")) throw new ValidationException("empty field");
+		if (value.equals("")) {
+			if (allowEmpty) {
+				return null;
+			}
+			else {
+				throw new ValidationException("empty field");
+			}
+		}
         return value;
 	}
 	
-	private static long handleLongField(String value) throws ValidationException {
+	private static String handleStringField(String value) throws ValidationException {
+		return handleStringField(value, false);
+	}
+	
+	private static Long handleLongField(String value, boolean allowEmpty) throws ValidationException {
 		value = value.trim();
-    	if (value.equals("")) throw new ValidationException("empty field");
+		if (value.equals("")) {
+			if (allowEmpty) {
+				return null;
+			}
+			else {
+				throw new ValidationException("number not parsable");
+			}
+		}
     	try {
     		return Long.parseLong(value);
     	}
 		}
 	}
 	
+	private static Long handleLongField(String value) throws ValidationException {
+		return handleLongField(value, false);
+	}
+	
 	
 	private static class Result {	
-		long asn;
+		Long asn;
 		String as_name;
 		String cc;
 		String dom;