Skip to content

Commit

Permalink
Prevent discovering own address, enable ktlint for ipv8-android
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSkala committed Feb 25, 2020
1 parent 7524076 commit 8d15b5a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ class DemoApplication : Application() {
val driver = AndroidSqliteDriver(Database.Schema, this, "trustchain.db")
val store = TrustChainSQLiteStore(Database(driver))
val randomWalk = RandomWalk.Factory()
val nsd = NetworkServiceDiscovery.Factory(getSystemService()!!)
return OverlayConfiguration(
TrustChainCommunity.Factory(settings, store),
listOf(randomWalk, nsd)
listOf(randomWalk)
)
}

Expand Down
9 changes: 9 additions & 0 deletions ipv8-android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'org.jlleitschuh.gradle.ktlint'

ktlint {
version = "$ktlint_version"
android = true
outputToConsole = true
ignoreFailures = true
}

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import android.content.Intent
import android.net.ConnectivityManager
import android.os.Build
import androidx.core.content.getSystemService
import androidx.lifecycle.ProcessLifecycleOwner
import nl.tudelft.ipv8.IPv8
import nl.tudelft.ipv8.IPv8Configuration
import nl.tudelft.ipv8.android.keyvault.AndroidCryptoProvider
import nl.tudelft.ipv8.android.messaging.udp.AndroidUdpEndpoint
import nl.tudelft.ipv8.android.peerdiscovery.NetworkServiceDiscovery
import nl.tudelft.ipv8.android.service.IPv8Service
import nl.tudelft.ipv8.keyvault.CryptoProvider
import nl.tudelft.ipv8.keyvault.PrivateKey
import nl.tudelft.ipv8.keyvault.defaultCryptoProvider
import java.net.InetAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import nl.tudelft.ipv8.Overlay
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.ipv8.messaging.udp.UdpEndpoint
import nl.tudelft.ipv8.peerdiscovery.strategy.DiscoveryStrategy
import java.net.InetAddress
import kotlin.random.Random

private val logger = KotlinLogging.logger {}

Expand Down Expand Up @@ -64,7 +64,6 @@ class NetworkServiceDiscovery(

if (serviceInfo.serviceName == serviceName) {
logger.debug { "Found its own service" }
return
}

val serviceId = getServiceId(serviceInfo.serviceName)
Expand Down Expand Up @@ -112,22 +111,23 @@ class NetworkServiceDiscovery(
val peer = overlay.myPeer
val address = Address(serviceInfo.host.hostAddress, serviceInfo.port)

logger.debug { "Discovered address: $address" }

overlay.network.discoverAddress(peer, address, overlay.serviceId)
overlay.walkTo(address)
if (overlay.myEstimatedLan != address) {
logger.debug { "Discovered address: $address" }
overlay.network.discoverAddress(peer, address, overlay.serviceId)
} else {
logger.debug { "Resolved its own IP address" }
}
}
}
}

private fun registerService(port: Int, serviceId: String) {
val serviceInfo = NsdServiceInfo().apply {
// The name is subject to change based on conflicts
// with other services advertised on the same network.
serviceName = serviceId
serviceType = SERVICE_TYPE
setPort(port)
}
private fun registerService(port: Int, serviceName: String) {
val serviceInfo = NsdServiceInfo()
// The name is subject to change based on conflicts
// with other services advertised on the same network.
serviceInfo.serviceName = serviceName
serviceInfo.serviceType = SERVICE_TYPE
serviceInfo.port = port

logger.debug { "Registering service info $serviceInfo" }

Expand All @@ -152,16 +152,21 @@ class NetworkServiceDiscovery(
val endpoint = overlay.endpoint
if (endpoint is UdpEndpoint) {
val socketPort = endpoint.getSocketPort()
logger.debug { "Registering service ${overlay.serviceId} on port $socketPort" }
registerService(socketPort, overlay.serviceId)
val serviceName = overlay.serviceId + "_" + Random.nextInt(10000)
logger.debug { "Registering service $serviceName on port $socketPort" }
registerService(socketPort, serviceName)
} else {
logger.error { "Overlay endpoint is not UdpEndpoint" }
}
discoverServices()
}

override fun takeStep() {
// NOOP
val addresses = overlay.getWalkableAddresses()
if (addresses.isNotEmpty()) {
val address = addresses.random()
overlay.walkTo(address)
}
}

override fun unload() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.Lifecycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import nl.tudelft.ipv8.android.IPv8Android
import kotlin.system.exitProcess

class StopIPv8Receiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Expand Down
8 changes: 4 additions & 4 deletions ipv8/src/main/java/nl/tudelft/ipv8/Community.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ abstract class Community : Overlay {
protected val prefix: ByteArray
get() = ByteArray(0) + 0.toByte() + VERSION + serviceId.hexToBytes()

var myEstimatedWan: Address = Address.EMPTY
var myEstimatedLan: Address = Address.EMPTY
override var myEstimatedWan: Address = Address.EMPTY
override var myEstimatedLan: Address = Address.EMPTY

private var lastBootstrap: Date? = null

Expand Down Expand Up @@ -432,7 +432,7 @@ abstract class Community : Overlay {
}

companion object {
val DEFAULT_ADDRESSES = listOf(
val DEFAULT_ADDRESSES = listOf<Address>(
// Dispersy
// Address("130.161.119.206", 6421),
// Address("130.161.119.206", 6422),
Expand All @@ -449,7 +449,7 @@ abstract class Community : Overlay {
// Address("81.171.27.194", 6527),
// Address("81.171.27.194", 6528)
// py-ipv8 + LibNaCL
// Address("131.180.27.161", 6427),
Address("131.180.27.161", 6427),
// kotlin-ipv8
Address("178.62.16.66", 8090)
)
Expand Down
3 changes: 3 additions & 0 deletions ipv8/src/main/java/nl/tudelft/ipv8/Overlay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ interface Overlay : EndpointListener {
var maxPeers: Int
var cryptoProvider: CryptoProvider

var myEstimatedWan: Address
var myEstimatedLan: Address

private val globalTime: ULong
get() = myPeer.lamportTimestamp

Expand Down

0 comments on commit 8d15b5a

Please sign in to comment.