Posted 28 July 2013 - 12:04 PM
first before i get into the problem at hand, im not too familiar with timers and modem_messages with the os.pullevent(). im not entirely sure if a modem_message will still trigger if the computer is going through a current process.
problem trying to solve:
i am trying to create dynamic rednet sucurity with the modem api, i know a computer can only have 128 maximum channels (or ports i call them) open at the same time, so i figure that a server can listen on a certain channel as a public channel for clients to connect to. channels 1 - 128 would be public and channels 129 - 65535 would be private channels for interacting with servers.
problem i ran into:
i think i am getting the timing wrong but my test client is pinging on the test server, but the client isnt showing the response the server should have sent. im not sure how long i should wait between the transmit and reply and a little longer of a wait for a timer so if nothing happens the timer will fire and the client will continue on to other public ports.
i have both programs here and a screenshot of 1 run on the client program to the server.
tell me what yall think and test it out
P.S. im not giving out my encoding and decoding port functions due for the security purpose of this build.
test server code
test client code
problem trying to solve:
i am trying to create dynamic rednet sucurity with the modem api, i know a computer can only have 128 maximum channels (or ports i call them) open at the same time, so i figure that a server can listen on a certain channel as a public channel for clients to connect to. channels 1 - 128 would be public and channels 129 - 65535 would be private channels for interacting with servers.
problem i ran into:
i think i am getting the timing wrong but my test client is pinging on the test server, but the client isnt showing the response the server should have sent. im not sure how long i should wait between the transmit and reply and a little longer of a wait for a timer so if nothing happens the timer will fire and the client will continue on to other public ports.
i have both programs here and a screenshot of 1 run on the client program to the server.
tell me what yall think and test it out
P.S. im not giving out my encoding and decoding port functions due for the security purpose of this build.
test server code
Spoiler
-- rednet security server
local serverProperties = {publicPort = 10}
local privatePorts = {}
MODEM = peripheral.wrap("top")
MODEM.open(serverProperties.publicPort)
print("opened: "..serverProperties.publicPort)
while true do
local ev = { os.pullEvent() }
if ev[1] == "modem_message" then
os.sleep(0.2)
local iC = ev[3]
local rC = ev[4]
local pkg = textutils.unserialize(ev[5])
if type(pkg) == "table" then -- verify if its a table
if pkg.cid ~= nil then -- pkg must have a cid
privatePorts[pkg.cid] = {status = nil, name = pkg.name, port = nil}
if pkg.protocol == "_ping" then
privatePorts[pkg.cid].status = 1
MODEM.transmit(rC,serverProperties.publicPort,"_pong")
print("pinged from "..pkg.cid)
elseif pkg.protocol == "whoareyou?" and privatePorts[pkg.cid].status == 1 then
privatePorts[pkg.cid].status = 2
MODEM.transmit(rC,serverProperties.publicPort,"bankSer")
elseif pkg.protocol == "connectionRequest" and privatePorts[pkg.cid].status == 2 then
local gPort = RNGen() --RANDOM NUMBER GENERATOR TAKEN OUT
local packet = encodePort(gPort) --ENCODE PORT TAKEN OUT
privatePorts[pkg.cid].status = 3
privatePorts[pkg.cid].port = gPort
MODEM.open(gPort)
print("opened: "..gPort)
print("for cid: "..privatePorts[pkg.cid].status)
print("encoded at: "..textutils.serialize(packet))
MODEM.transmit(rC,serverProperties.publicPort,textutils.serialize(packet))
elseif pkg.protocol == "connectionVerify" and privatePorts[pkg.cid].status == 3 then
privatePorts[pkg.cid].status = "successful"
MODEM.transmit(rC,rC,"connectionEstablished")
else -- if pkg.protocol doesnt exist or doesnt match
MODEM.transmit(rC,serverProperties.publicPort,"invalid protocol")
print("invalid protocol")
end
else -- if pkg.cid is nil
MODEM.transmit(rC,serverProperties.publicPort,"invalid Credentials")
print("invalid Credentials")
end
else -- if pkg is a string
MODEM.transmit(rC,serverProperties.publicPort,"invalid packet")
print("invalid packet..")
end
elseif ev[1] == "key" then
end
os.sleep(0.1)
end
test client code
Spoiler
-- rednet security client
MODEM = peripheral.wrap("top")
-- lets find the server
local serverFound = false
local connected = false
local targetServ = "bankSer"
local count = 1
local status = 1
local credentials = {cid = os.getComputerID(), name = os.getComputerLabel(), protocol = nil}
print("Searching..")
while not serverFound and count < 16 do -- 120 public ports
MODEM.open(count)
print("opened: "..count)
if status == 1 then
credentials.protocol = "_ping"
status = 2
MODEM.transmit(count,count,textutils.serialize(credentials))
elseif status == 2 then
credentials.protocol = "whoareyou?"
MODEM.transmit(count,count,textutils.serialize(credentials))
elseif status == 3 then
credentials.protocol = "connectionRequest"
MODEM.transmit(count,count,textutils.serialize(credentials))
elseif status == 4 then
credentials.protocol = "connectionVerify"
MODEM.transmit(privatePort,privatePort,textutils.serialize(credentials))
end
credentials.protocol = nil
local T = os.startTimer(0.3)
local ev = { os.pullEvent() }
if ev[1] == "modem_message" then
local iC = ev[3]
local rC = ev[4]
local pkg = textutils.unserialize(ev[5])
if pkg == "_pong" then
serverFound = true
print("PINGED!")
end
end
count = count + 1
os.sleep(0.1)
end
print("Search ended")