This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DarkEyeDragon's profile picture

Prevent network spam?

Started by DarkEyeDragon, 18 June 2015 - 04:06 PM
DarkEyeDragon #1
Posted 18 June 2015 - 06:06 PM
So i've been working on a simple networking using the network cables in CC.
The idea is that i send a request to the "server"(aka main computer) and it would respond with and "accepted" message when the connection is allowed. I got that far.
The problem is that I want to set a max amount of connections to come in every x amount of seconds.

Example: I have my regular network running. Someone decides to decode the protocols my network uses and starts mass spamming my "server" how would i prevent it from accepting all those connections?
To say it very simple. Lets say i only want to allow 1 new connection at a time.

Code of the "server"

while true do
	rednet.open("back")
	local senderId, message, protocol = rednet.receive()
	print("Log: ".."["..senderId.."] "..message)
	rednet.send(senderId, "accepted")
	print("Log: Established new connection."
end
code of the client

rednet.open("back")
print("Requesting connection...")
rednet.send(4, "Requesting connection...")
local senderId, message, protocol = rednet.receive(5)
if message == "Accepted" and senderId == 4 then
   print("Connection established to server")
else
   term.setTextColor(colors.red)
   print("Connection timed out.")
   term.setTextColor(colors.white)
end
And is the current code i use a efficient way or are there better methods?
Edited on 18 June 2015 - 04:07 PM
Creator #2
Posted 18 June 2015 - 06:13 PM
You don't need to open rednet on every time you run the loop. You might check if the message is correct. Don't use rednet, use the modem api. Encrypt connections. Another thing. Have two shared passwords. One pc sends a random string encrypted wit the first password. The seconddecrypts it and encrypts itwith the second key. That way they know they are trusted. You may also use Diffie-Hellman.
DarkEyeDragon #3
Posted 18 June 2015 - 07:01 PM
You don't need to open rednet on every time you run the loop. You might check if the message is correct. Don't use rednet, use the modem api. Encrypt connections. Another thing. Have two shared passwords. One pc sends a random string encrypted wit the first password. The seconddecrypts it and encrypts itwith the second key. That way they know they are trusted. You may also use Diffie-Hellman.

Alright thanks.
Bomb Bloke #4
Posted 18 June 2015 - 11:28 PM
If you only want to accept about one message in a given second, just call sleep(1) immediately after each receive call.

Rednet messages come in via the event queue. Events are pulled from the front, and new events are added to the end. If you want to pull a specific type of event - say you eg call os.pullEvent("timer") - then all the events in the queue prior to the type you want, are thrown away.

sleep(1) places a timer event at the end of the queue after a second, then pulls that, so if your queue is otherwise full of rednet messages then they'll all go out the window.