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.
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
# any given time.
maxOpenPorts: 16

View File

@ -271,6 +271,7 @@ class Settings(val config: Config) {
val inputUsername = config.getBoolean("misc.inputUsername")
val maxClipboard = config.getInt("misc.maxClipboard") 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 maxWirelessRange = config.getDouble("misc.maxWirelessRange") max 0
val rTreeMaxEntries = 10

View File

@ -662,7 +662,11 @@ 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 {
val size = Option(data).fold(0)(_.foldLeft(0)((acc, arg) => {
val size = Option(data).fold(0)(values => {
if (values.length > Settings.get.maxNetworkPacketParts) {
throw new IllegalArgumentException("packet has too many parts")
}
values.length * 2 + values.foldLeft(0)((acc, arg) => {
acc + (arg match {
case null | Unit | None => 4
case _: java.lang.Boolean => 4
@ -672,7 +676,8 @@ object Network extends api.detail.NetworkAPI {
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)