Limiting number of parts allowed in network packets as a safeguard for weaker computers, as discussed in #876. Configurable, of course.

This commit is contained in:
Florian Nücke 2015-02-01 14:47:57 +01:00
parent c30d5b29ff
commit e6668d59b4
3 changed files with 24 additions and 10 deletions

View File

@ -925,6 +925,14 @@ opencomputers {
# not apply to HTTP traffic. # not apply to HTTP traffic.
maxNetworkPacketSize: 8192 maxNetworkPacketSize: 8192
# The maximum number of "data parts" a network packet is allowed to have.
# When sending a network message, from Lua this may look like so:
# component.modem.broadcast(port, "first", true, "third", 123)
# This limits how many arguments can be passed and are wrapped into a
# packet. This limit mostly serves as a protection for lower-tier
# computers, to avoid them getting nuked by more powerful computers.
maxNetworkPacketParts: 8
# The maximum number of ports a single network card can have opened at # The maximum number of ports a single network card can have opened at
# any given time. # any given time.
maxOpenPorts: 16 maxOpenPorts: 16

View File

@ -271,6 +271,7 @@ class Settings(val config: Config) {
val inputUsername = config.getBoolean("misc.inputUsername") val inputUsername = config.getBoolean("misc.inputUsername")
val maxClipboard = config.getInt("misc.maxClipboard") max 0 val maxClipboard = config.getInt("misc.maxClipboard") max 0
val maxNetworkPacketSize = config.getInt("misc.maxNetworkPacketSize") max 0 val maxNetworkPacketSize = config.getInt("misc.maxNetworkPacketSize") max 0
val maxNetworkPacketParts = config.getInt("misc.maxNetworkPacketParts") max 0
val maxOpenPorts = config.getInt("misc.maxOpenPorts") max 0 val maxOpenPorts = config.getInt("misc.maxOpenPorts") max 0
val maxWirelessRange = config.getDouble("misc.maxWirelessRange") max 0 val maxWirelessRange = config.getDouble("misc.maxWirelessRange") max 0
val rTreeMaxEntries = 10 val rTreeMaxEntries = 10

View File

@ -662,17 +662,22 @@ object Network extends api.detail.NetworkAPI {
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //
class Packet(var source: String, var destination: String, var port: Int, var data: Array[AnyRef], var ttl: Int = 5) extends api.network.Packet { class Packet(var source: String, var destination: String, var port: Int, var data: Array[AnyRef], var ttl: Int = 5) extends api.network.Packet {
val size = Option(data).fold(0)(_.foldLeft(0)((acc, arg) => { val size = Option(data).fold(0)(values => {
acc + (arg match { if (values.length > Settings.get.maxNetworkPacketParts) {
case null | Unit | None => 4 throw new IllegalArgumentException("packet has too many parts")
case _: java.lang.Boolean => 4 }
case _: java.lang.Integer => 4 values.length * 2 + values.foldLeft(0)((acc, arg) => {
case _: java.lang.Double => 8 acc + (arg match {
case value: java.lang.String => value.length max 1 case null | Unit | None => 4
case value: Array[Byte] => value.length max 1 case _: java.lang.Boolean => 4
case _ => throw new IllegalArgumentException("unsupported data type") case _: java.lang.Integer => 4
case _: java.lang.Double => 8
case value: java.lang.String => value.length max 1
case value: Array[Byte] => value.length max 1
case _ => throw new IllegalArgumentException("unsupported data type")
})
}) })
})) })
override def hop() = new Packet(source, destination, port, data, ttl - 1) override def hop() = new Packet(source, destination, port, data, ttl - 1)