Skip to content

Commit

Permalink
Add support for PTR queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed May 15, 2020
1 parent 41405f9 commit b869953
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ dependencies {
api("com.google.firebase:firebase-config-ktx:19.1.4")
api("com.google.firebase:firebase-crashlytics:17.0.0")
api("com.jakewharton.timber:timber:4.7.1")
api("dnsjava:dnsjava:3.0.2")
api("dnsjava:dnsjava:3.1.0")
api("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion")
api("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion")
kapt("androidx.room:room-compiler:$roomVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ sealed class DnsResolverCompat {

fun prepareDnsResponse(request: Message) = Message(request.header.id).apply {
header.setFlag(Flags.QR.toInt()) // this is a response
header.setFlag(Flags.RA.toInt()) // recursion available
if (request.header.getFlag(Flags.RD.toInt())) header.setFlag(Flags.RD.toInt())
request.question?.also { addRecord(it, Section.QUESTION) }
}
Expand Down Expand Up @@ -135,11 +136,22 @@ sealed class DnsResolverCompat {
val isIpv6 = when (val type = question?.type) {
Type.A -> false
Type.AAAA -> true
Type.PTR -> {
val ip = try {
// Android does not provide a PTR lookup API for Network prior to Android 10
ReverseMap.fromName(question.name)
} catch (e: IOException) {
throw UnsupportedOperationException(e) // unrecognized PTR name
}
val hostname = Name.fromString(GlobalScope.async(unboundedIO) { ip.hostName }.await())
return prepareDnsResponse(request).apply {
addRecord(PTRRecord(question.name, DClass.IN, TTL, hostname), Section.ANSWER)
}.toWire()
}
else -> throw UnsupportedOperationException("Unsupported query type $type")
}
val host = question.name.canonicalize().toString(true)
return prepareDnsResponse(request).apply {
header.setFlag(Flags.RA.toInt()) // recursion available
for (address in hostResolver(host).asIterable().run {
if (isIpv6) filterIsInstance<Inet6Address>() else filterIsInstance<Inet4Address>()
}) addRecord(when (address) {
Expand Down
2 changes: 1 addition & 1 deletion mobile/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ androidExtensions.isExperimental = true

dependencies {
implementation("androidx.browser:browser:1.2.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.0-beta5")
implementation("androidx.constraintlayout:constraintlayout:2.0.0-beta6")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion")
implementation("com.google.android.gms:play-services-vision:20.0.0")
implementation("com.google.firebase:firebase-ads:19.1.0")
Expand Down

0 comments on commit b869953

Please sign in to comment.