Skip to content

Commit

Permalink
Merge pull request #450 from wavesplatform/fix-handshake
Browse files Browse the repository at this point in the history
Application and node names bytes representation length fixed
  • Loading branch information
alexeykiselev authored Aug 28, 2017
2 parents f99177d + 314d5c4 commit c25acae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/main/scala/com/wavesplatform/network/Handshake.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ case class Handshake(
nodeNonce: Long,
declaredAddress: Option[InetSocketAddress]) {
def encode(out: ByteBuf): out.type = {
out.writeByte(applicationName.length)
out.writeBytes(applicationName.getBytes(Charsets.UTF_8))
val applicationNameBytes = applicationName.getBytes(Charsets.UTF_8)
val nodeNameBytes = nodeName.getBytes(Charsets.UTF_8)
out.writeByte(applicationNameBytes.length)
out.writeBytes(applicationNameBytes)
out.writeInt(applicationVersion._1)
out.writeInt(applicationVersion._2)
out.writeInt(applicationVersion._3)
out.writeByte(nodeName.length)
out.writeBytes(nodeName.getBytes(Charsets.UTF_8))
out.writeByte(nodeNameBytes.length)
out.writeBytes(nodeNameBytes)
out.writeLong(nodeNonce)
declaredAddress match {
case None => out.writeInt(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wavesplatform.settings
import java.io.File
import java.net.{InetSocketAddress, URI}

import com.google.common.base.Charsets
import com.typesafe.config.Config
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
Expand Down Expand Up @@ -32,6 +33,7 @@ case class NetworkSettings(file: Option[File],
uPnPSettings: UPnPSettings)

object NetworkSettings {
private val MaxNodeNameBytesLength = 127
implicit val networkSettingsValueReader: ValueReader[NetworkSettings] =
(cfg: Config, path: String) => fromConfig(cfg.getConfig(path))

Expand All @@ -40,6 +42,8 @@ object NetworkSettings {
val bindAddress = new InetSocketAddress(config.as[String]("bind-address"), config.as[Int]("port"))
val nonce = config.getOrElse("nonce", randomNonce)
val nodeName = config.getOrElse("node-name", s"Node-$nonce")
require(nodeName.getBytes(Charsets.UTF_8).length <= MaxNodeNameBytesLength,
s"Node name should have length less than $MaxNodeNameBytesLength bytes")
val declaredAddress = config.getAs[String]("declared-address").map { address =>
val uri = new URI(s"my://$address")
new InetSocketAddress(uri.getHost, uri.getPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ class NetworkSettingsSpecification extends FlatSpec with Matchers {
networkSettings.nonce should not be 0
networkSettings.nodeName should be(s"Node-${networkSettings.nonce}")
}

it should "fail with IllegalArgumentException on too long node name" in {
val config = loadConfig(ConfigFactory.parseString("waves.network.node-name = очень-длинное-название-в-многобайтной-кодировке-отличной-от-однобайтной-кодировки-американского-института-стандартов"))
intercept[IllegalArgumentException] {
config.as[NetworkSettings]("waves.network")
}
}
}

0 comments on commit c25acae

Please sign in to comment.