@Test public void parseDomain(){ parser = (DomainNameParserImpl) context.getBean(IDomainNameParser.class); UDPPacket packet = new UDPPacket(1393515429, 547730, 288, 16, dnsresponsedata); DomainNameSystem dns = parser.parseDomainName(packet); assertNotNull(dns); }
@Override public DomainNameSystem parseDomainName(UDPPacket packet) { DomainNameSystem domain = new DomainNameSystem(); domain.setPacket(packet); start = packet.getDataOffset(); data = packet.getData(); bytes = ByteBuffer.wrap(data); bytes.position(start); bytes.getShort(); short flags = bytes.getShort(); boolean response = (flags & 0x80) != 0; domain.setResponse(response); short queries = bytes.getShort(); if (queries != 1) { LOGGER.warn("DNS packet with more than one query"); return null; } short answers = bytes.getShort(); bytes.getShort(); bytes.getShort(); String domainName = readDomainName(); short qtype = bytes.getShort(); short qclass = bytes.getShort(); if ((qtype != TYPE_A && qtype != TYPE_AAAA) || qclass != 1) { return null; } domain.setDomainName(domainName); if (response) { Set<InetAddress> ipAddresses = new HashSet<InetAddress>(); String cname = domainName; for (int i = 0; i < answers; ++i) { String domainname = readDomainName(); qtype = bytes.getShort(); qclass = bytes.getShort(); bytes.getInt(); short len = bytes.getShort(); if (!domainname.equals(domainName) && !domainname.equals(cname)) { LOGGER.warn("Unexpected answer domain: " + domainname); bytes.position(bytes.position() + len); continue; } if (qclass != 1) { LOGGER.warn("Unrecognized DNS answer class:" + qclass); bytes.position(bytes.position() + len); continue; } switch (qtype) { case TYPE_A : case TYPE_AAAA : byte[] bdata = new byte[len]; bytes.get(bdata, 0, len); try { ipAddresses.add(InetAddress.getByAddress(bdata)); } catch (UnknownHostException e) { LOGGER.warn("Unexpected exception reading IP address from DNS response"); } break; case TYPE_CNAME : cname = readDomainName(); break; default : LOGGER.warn("Unhandled DNS answer type:" + qtype); bytes.position(bytes.position() + len); } } domain.setCname(cname); domain.setIpAddresses(ipAddresses); } return domain; }
DomainNameParserImpl implements IDomainNameParser { @Override public DomainNameSystem parseDomainName(UDPPacket packet) { DomainNameSystem domain = new DomainNameSystem(); domain.setPacket(packet); start = packet.getDataOffset(); data = packet.getData(); bytes = ByteBuffer.wrap(data); bytes.position(start); bytes.getShort(); short flags = bytes.getShort(); boolean response = (flags & 0x80) != 0; domain.setResponse(response); short queries = bytes.getShort(); if (queries != 1) { LOGGER.warn("DNS packet with more than one query"); return null; } short answers = bytes.getShort(); bytes.getShort(); bytes.getShort(); String domainName = readDomainName(); short qtype = bytes.getShort(); short qclass = bytes.getShort(); if ((qtype != TYPE_A && qtype != TYPE_AAAA) || qclass != 1) { return null; } domain.setDomainName(domainName); if (response) { Set<InetAddress> ipAddresses = new HashSet<InetAddress>(); String cname = domainName; for (int i = 0; i < answers; ++i) { String domainname = readDomainName(); qtype = bytes.getShort(); qclass = bytes.getShort(); bytes.getInt(); short len = bytes.getShort(); if (!domainname.equals(domainName) && !domainname.equals(cname)) { LOGGER.warn("Unexpected answer domain: " + domainname); bytes.position(bytes.position() + len); continue; } if (qclass != 1) { LOGGER.warn("Unrecognized DNS answer class:" + qclass); bytes.position(bytes.position() + len); continue; } switch (qtype) { case TYPE_A : case TYPE_AAAA : byte[] bdata = new byte[len]; bytes.get(bdata, 0, len); try { ipAddresses.add(InetAddress.getByAddress(bdata)); } catch (UnknownHostException e) { LOGGER.warn("Unexpected exception reading IP address from DNS response"); } break; case TYPE_CNAME : cname = readDomainName(); break; default : LOGGER.warn("Unhandled DNS answer type:" + qtype); bytes.position(bytes.position() + len); } } domain.setCname(cname); domain.setIpAddresses(ipAddresses); } return domain; } }
DomainNameParserImpl implements IDomainNameParser { @Override public DomainNameSystem parseDomainName(UDPPacket packet) { DomainNameSystem domain = new DomainNameSystem(); domain.setPacket(packet); start = packet.getDataOffset(); data = packet.getData(); bytes = ByteBuffer.wrap(data); bytes.position(start); bytes.getShort(); short flags = bytes.getShort(); boolean response = (flags & 0x80) != 0; domain.setResponse(response); short queries = bytes.getShort(); if (queries != 1) { LOGGER.warn("DNS packet with more than one query"); return null; } short answers = bytes.getShort(); bytes.getShort(); bytes.getShort(); String domainName = readDomainName(); short qtype = bytes.getShort(); short qclass = bytes.getShort(); if ((qtype != TYPE_A && qtype != TYPE_AAAA) || qclass != 1) { return null; } domain.setDomainName(domainName); if (response) { Set<InetAddress> ipAddresses = new HashSet<InetAddress>(); String cname = domainName; for (int i = 0; i < answers; ++i) { String domainname = readDomainName(); qtype = bytes.getShort(); qclass = bytes.getShort(); bytes.getInt(); short len = bytes.getShort(); if (!domainname.equals(domainName) && !domainname.equals(cname)) { LOGGER.warn("Unexpected answer domain: " + domainname); bytes.position(bytes.position() + len); continue; } if (qclass != 1) { LOGGER.warn("Unrecognized DNS answer class:" + qclass); bytes.position(bytes.position() + len); continue; } switch (qtype) { case TYPE_A : case TYPE_AAAA : byte[] bdata = new byte[len]; bytes.get(bdata, 0, len); try { ipAddresses.add(InetAddress.getByAddress(bdata)); } catch (UnknownHostException e) { LOGGER.warn("Unexpected exception reading IP address from DNS response"); } break; case TYPE_CNAME : cname = readDomainName(); break; default : LOGGER.warn("Unhandled DNS answer type:" + qtype); bytes.position(bytes.position() + len); } } domain.setCname(cname); domain.setIpAddresses(ipAddresses); } return domain; }  }
DomainNameParserImpl implements IDomainNameParser { @Override public DomainNameSystem parseDomainName(UDPPacket packet) { DomainNameSystem domain = new DomainNameSystem(); domain.setPacket(packet); start = packet.getDataOffset(); data = packet.getData(); bytes = ByteBuffer.wrap(data); bytes.position(start); bytes.getShort(); short flags = bytes.getShort(); boolean response = (flags & 0x80) != 0; domain.setResponse(response); short queries = bytes.getShort(); if (queries != 1) { LOGGER.warn("DNS packet with more than one query"); return null; } short answers = bytes.getShort(); bytes.getShort(); bytes.getShort(); String domainName = readDomainName(); short qtype = bytes.getShort(); short qclass = bytes.getShort(); if ((qtype != TYPE_A && qtype != TYPE_AAAA) || qclass != 1) { return null; } domain.setDomainName(domainName); if (response) { Set<InetAddress> ipAddresses = new HashSet<InetAddress>(); String cname = domainName; for (int i = 0; i < answers; ++i) { String domainname = readDomainName(); qtype = bytes.getShort(); qclass = bytes.getShort(); bytes.getInt(); short len = bytes.getShort(); if (!domainname.equals(domainName) && !domainname.equals(cname)) { LOGGER.warn("Unexpected answer domain: " + domainname); bytes.position(bytes.position() + len); continue; } if (qclass != 1) { LOGGER.warn("Unrecognized DNS answer class:" + qclass); bytes.position(bytes.position() + len); continue; } switch (qtype) { case TYPE_A : case TYPE_AAAA : byte[] bdata = new byte[len]; bytes.get(bdata, 0, len); try { ipAddresses.add(InetAddress.getByAddress(bdata)); } catch (UnknownHostException e) { LOGGER.warn("Unexpected exception reading IP address from DNS response"); } break; case TYPE_CNAME : cname = readDomainName(); break; default : LOGGER.warn("Unhandled DNS answer type:" + qtype); bytes.position(bytes.position() + len); } } domain.setCname(cname); domain.setIpAddresses(ipAddresses); } return domain; }  @Override DomainNameSystem parseDomainName(UDPPacket packet); }
DomainNameParserImpl implements IDomainNameParser { @Override public DomainNameSystem parseDomainName(UDPPacket packet) { DomainNameSystem domain = new DomainNameSystem(); domain.setPacket(packet); start = packet.getDataOffset(); data = packet.getData(); bytes = ByteBuffer.wrap(data); bytes.position(start); bytes.getShort(); short flags = bytes.getShort(); boolean response = (flags & 0x80) != 0; domain.setResponse(response); short queries = bytes.getShort(); if (queries != 1) { LOGGER.warn("DNS packet with more than one query"); return null; } short answers = bytes.getShort(); bytes.getShort(); bytes.getShort(); String domainName = readDomainName(); short qtype = bytes.getShort(); short qclass = bytes.getShort(); if ((qtype != TYPE_A && qtype != TYPE_AAAA) || qclass != 1) { return null; } domain.setDomainName(domainName); if (response) { Set<InetAddress> ipAddresses = new HashSet<InetAddress>(); String cname = domainName; for (int i = 0; i < answers; ++i) { String domainname = readDomainName(); qtype = bytes.getShort(); qclass = bytes.getShort(); bytes.getInt(); short len = bytes.getShort(); if (!domainname.equals(domainName) && !domainname.equals(cname)) { LOGGER.warn("Unexpected answer domain: " + domainname); bytes.position(bytes.position() + len); continue; } if (qclass != 1) { LOGGER.warn("Unrecognized DNS answer class:" + qclass); bytes.position(bytes.position() + len); continue; } switch (qtype) { case TYPE_A : case TYPE_AAAA : byte[] bdata = new byte[len]; bytes.get(bdata, 0, len); try { ipAddresses.add(InetAddress.getByAddress(bdata)); } catch (UnknownHostException e) { LOGGER.warn("Unexpected exception reading IP address from DNS response"); } break; case TYPE_CNAME : cname = readDomainName(); break; default : LOGGER.warn("Unhandled DNS answer type:" + qtype); bytes.position(bytes.position() + len); } } domain.setCname(cname); domain.setIpAddresses(ipAddresses); } return domain; }  @Override DomainNameSystem parseDomainName(UDPPacket packet);  }