Skip to content

Commit

Permalink
Make port not configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Oct 20, 2023
1 parent b791b67 commit 40618c2
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal actual class NtpUdpSocketOperations {

actual fun prepareSocket(timeoutMilliseconds: Long) {}

actual fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: UInt) {}
actual fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: Byte) {}

actual fun closeSocket() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import kotlin.time.Duration.Companion.seconds
* also cause more server load.
* @param waitBetweenResolvedAddressQueries The amount of time to wait before consecutive requests
* to the same resolved address.
* @param pinnedPortNumber The port number to send packets on. If null, a random choice of port to
* be made for every packet, as recommended by RFC 9109.
* @param ntpVersion The version number to write in packets.
*/
class NTPServer(
Expand All @@ -27,6 +25,5 @@ class NTPServer(
val dnsLookupStrategy: DnsLookupStrategy = DnsLookupStrategy.ALL,
val queriesPerResolvedAddress: Short = 3,
val waitBetweenResolvedAddressQueries: Duration = 2.seconds,
val pinnedPortNumber: UInt? = null,
val ntpVersion: NTPVersion = NTPVersion.FOUR,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tidal.networktime.internal

import kotlin.random.Random
import kotlin.random.nextUInt
import kotlin.time.Duration

internal class NtpExchanger(
Expand All @@ -13,7 +12,6 @@ internal class NtpExchanger(
operator fun invoke(
address: String,
queryTimeout: Duration,
portNumber: UInt?,
ntpVersion: UInt,
): NtpExchangeResult? {
val ntpUdpSocketOperations = NtpUdpSocketOperations()
Expand All @@ -28,7 +26,7 @@ internal class NtpExchanger(
ntpUdpSocketOperations.exchangePacketInPlace(
buffer,
address,
portNumber ?: random.nextUInt(),
NTP_PORT_NUMBER.toByte(),
)
val responseTime = referenceClock.referenceEpochTime - requestTime
NtpExchangeResult(responseTime, ntpPacketDeserializer(buffer))
Expand All @@ -41,5 +39,6 @@ internal class NtpExchanger(

companion object {
private const val NTP_MODE_CLIENT = 3
private const val NTP_PORT_NUMBER = 123
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.tidal.networktime.internal
internal expect class NtpUdpSocketOperations() {
fun prepareSocket(timeoutMilliseconds: Long)

fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: UInt)
fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: Byte)

fun closeSocket()
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ internal class SyncSingular(
val ret = ntpExchanger(
address,
queryTimeout,
pinnedPortNumber,
when (ntpVersion) {
NTPVersion.ZERO -> 0U
NTPVersion.ONE -> 1U
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal actual class NtpUdpSocketOperations {
datagramSocket = DatagramSocket().apply { soTimeout = timeoutMilliseconds.toInt() }
}

actual fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: UInt) {
actual fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: Byte) {
val requestPacket =
DatagramPacket(buffer, buffer.size, InetAddress.getByName(address), portNumber.toInt())
datagramSocket!!.send(requestPacket)
Expand Down
1 change: 1 addition & 0 deletions samples/shared/src/commonMain/kotlin/root/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package root

import com.tidal.networktime.DnsLookupStrategy
import com.tidal.networktime.NTPServer
import com.tidal.networktime.SNTPClient
import kotlinx.coroutines.GlobalScope
Expand Down

0 comments on commit 40618c2

Please sign in to comment.