Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
More DNS logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehearn committed Dec 28, 2014
1 parent 6031b38 commit e855c4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/main/kotlin/dns.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.xbill.DNS.*
import java.net.*

// A small, simple DNS server.
class DnsServer(private val dnsName: String, private val port: Int, private val crawler: Crawler) {
class DnsServer(private val dnsName: Name, private val port: Int, private val crawler: Crawler) {
private val log = LoggerFactory.getLogger("cartographer.dnsserver")

public fun start() {
Expand All @@ -21,27 +21,42 @@ class DnsServer(private val dnsName: String, private val port: Int, private val
val outBits = processMessage(Message(inBits))
val outPacket = DatagramPacket(outBits, outBits.size(), inPacket.getSocketAddress())
socket.send(outPacket)
} catch (e: Exception) {
} catch (e: Throwable) {
log.error("Error handling DNS request", e)
}
}
})
}

fun processMessage(message: Message): ByteArray {
if (message.getHeader().getOpcode() != Opcode.QUERY)
val header = message.getHeader()
if (header.getOpcode() != Opcode.QUERY) {
log.error("Got message with unimplemented opcode {}", header.getOpcode())
return errorMessage(message, Rcode.NOTIMP)
val response = Message(message.getHeader().getID())
}
if (header.getRcode() != Rcode.NOERROR) {
log.error("Got message with bad rcode: ${header.getRcode()}")
return errorMessage(message, Rcode.FORMERR)
}
val queryName = message.getQuestion().getName()
if (queryName != dnsName) {
log.error("Got query with unrecognised name ${queryName}")
return errorMessage(message, Rcode.NXDOMAIN)
}
val response = Message(header.getID())
if (header.getFlag(Flags.RD.toInt()))
response.getHeader().setFlag(Flags.RD.toInt())
response.getHeader().setFlag(Flags.QR.toInt());
response.getHeader().setFlag(Flags.AA.toInt());
val ips = crawler.getSomePeers(30, -1)
for (ip in ips) {
val ipaddr = ip.first.getAddress()
val TTL = 60L // seconds
try {
if (ipaddr is Inet4Address)
response.addRecord(ARecord(Name(dnsName), 1, TTL, ipaddr), Section.ANSWER)
response.addRecord(ARecord(dnsName, 1, TTL, ipaddr), Section.ANSWER)
else if (ipaddr is Inet6Address)
response.addRecord(AAAARecord(Name(dnsName), 1, TTL, ipaddr), Section.ANSWER)
response.addRecord(AAAARecord(dnsName, 1, TTL, ipaddr), Section.ANSWER)
} catch(e: Exception) {
log.error("Failed to add record for ${ipaddr}: ${e}")
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.lang.management.ManagementFactory
import javax.management.ObjectName
import joptsimple.OptionParser
import kotlin.platform.platformStatic
import org.xbill.DNS.Name

public class BitcoinHTTPSeed {
class object {
Expand Down Expand Up @@ -45,7 +46,7 @@ public class BitcoinHTTPSeed {
HTTPServer(options.valueOf(httpPort).toInt(), "", dir.resolve("privkey"), crawler, params.getPaymentProtocolId())
if (options.has(dnsname)) {
val s = options.valueOf(dnsname)
DnsServer(if (s.endsWith('.')) s else s + '.', options.valueOf(dnsPort).toInt(), crawler).start()
DnsServer(Name(if (s.endsWith('.')) s else s + '.'), options.valueOf(dnsPort).toInt(), crawler).start()
}
crawler.start()
}
Expand Down

0 comments on commit e855c4d

Please sign in to comment.