Jump to content




[1.5+] In-depth Modem Channels Tutorial

wireless networking peripheral

69 replies to this topic

#1 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 09 February 2013 - 05:18 PM

Note: This tutorial only applies to ComputerCraft versions 1.5+ It does not work for anything pre-1.5

Well this is the addition that you (and every hacker who ever used ComputerCraft) has been waiting for. The joy of having to create a whole new layer of security on top of our rednet enabled programs is finally upon us, and oh what glorious fun we will have. Those awful, infallibly secure modems will no longer be tolerated! What is this addition that I speak of? The introduction of channels to rednet!

What are channels exactly? Well in terms of ComputerCraft, channels are, quite simply, an open network which you can send and receive rednet messages on. There are several advantages to this, but I will leave it to you to deduce what they are.

How do channels work? Well first off, you're going to have to come to terms with the fact that you cannot access channel functionality through the rednet API. To use this awesome new feature, we're going to have to do some hardcore peripheral wrapping.

--So you want to use channels, eh?
--The first thing you'll need to do is wrap your modem													 '
--My modem just happens to be on the top of the computer, but you can place modems on any side
local modem = peripheral.wrap("top") --Wow. That was easy.

Yup. That's it. Now you have access to all of the functions which a modem has through the "modem" variable. These exposed functions are:
modem.open(integer channel)
modem.isOpen(integer channel)
modem.transmit(integer channel, integer replyChannel, string message)
modem.close(integer channel)
modem.closeAll()

Opening channels:
Opening channels is simple. All you have to do is...
local desiredChannel = 1
modem.open(desiredChannel)
But what does opening a channel mean exactly? Well, until you've opened a channel you cannot receive any messages on that channel. You can send messages though, so if all you need to do is send a bunch of messages then there is no need to open a channel.
Note: You can open 128 channels at any given time and receive messages on them.
Note 2: The largest channel you can open is 65535

Transmiting messages:
Transmitting messages is pretty simple too. Use the transmit function like so:
local sendOnChannel = 1
local replyChannel = 2
modem.transmit(sendOnChannel, replyChannel, "Ping!")
--[["
You've just sent a message on channel 1. Any computers that have opened
channel 1 can receive the message. You've also specified that the computer
should reply on channel 2.
"--]]

Receiving messages:
Receiving a message is simple as well. Everything boils down to events!
local modem = peripheral.wrap("top")
modem.open(1) --Receive messages from computers sending on channel 1
modem.open(2) --Opening channel 2 just for the heck of it
local messageArguments = {os.pullEvent("modem_message")}
for i,v in pairs(messageArguments) do
  print(v)
end
This will output the following if you use my transmit message code from above:
modem_message
top
1
2
Ping!
3 --This is the distance that two computers are apart.

And that's it! Channels are actually fairly simple, but as you can imagine they can be quite powerful.

Edit: Since I noticed that there was some confusion about being able to open more than one channel at the same time, here is an example of doing just that.
local modem = peripheral.wrap("top")
local userChannels = { --Define the channels that regular users send on
	[1] = true;
	[3] = true;
	[5] = true;
	[7] = true;
}

local adminChannels = { --Define the channels that admins send on
	[2] = true;
	[4] = true;
	[6] = true;
	[8] = true;
}

for i=1,8 do --Open all of the aforementioned channels
	modem.open(i)
end

while true do
	local message = {os.pullEvent("modem_message")} --Receive messages
	if userChannels[message[3]] then --If that message is sent on a channel that is defined in userChannels then
		print("User channel")
	else
		print("Admin channel")
	end
end

Adieu,

Bubba

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 February 2013 - 06:09 PM

Nice :) I'm glad someone has done this, saves me having to figure it out later ;)

I can see several advantages to channels, one being [censored so people have to figure it out] :P

So does the "rednet_message" event still exists?

#3 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 09 February 2013 - 06:15 PM

View PostTheOriginalBIT, on 09 February 2013 - 06:09 PM, said:

Nice :) I'm glad someone has done this, saves me having to figure it out later ;)

I can see several advantages to channels, one being [censored so people have to figure it out] :P

So does the "rednet_message" event still exists?

Nope. Apparently it's been removed completely.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 February 2013 - 06:25 PM

View PostBubba, on 09 February 2013 - 06:15 PM, said:

View PostTheOriginalBIT, on 09 February 2013 - 06:09 PM, said:

Nice :)/> I'm glad someone has done this, saves me having to figure it out later ;)/>

I can see several advantages to channels, one being [censored so people have to figure it out] :P/>

So does the "rednet_message" event still exists?

Nope. Apparently it's been removed completely.
Well how the hell is that "backward compatible with older versions"?!

#5 lieudusty

  • Members
  • 419 posts

Posted 09 February 2013 - 08:03 PM

Nice tutorial on modems and channels :D

#6 ElvishJerricco

  • Members
  • 803 posts

Posted 09 February 2013 - 08:15 PM

View PostTheOriginalBIT, on 09 February 2013 - 06:25 PM, said:

View PostBubba, on 09 February 2013 - 06:15 PM, said:

View PostTheOriginalBIT, on 09 February 2013 - 06:09 PM, said:

Nice :)/> I'm glad someone has done this, saves me having to figure it out later ;)/>

I can see several advantages to channels, one being [censored so people have to figure it out] :P/>

So does the "rednet_message" event still exists?

Nope. Apparently it's been removed completely.
Well how the hell is that "backward compatible with older versions"?!

At least the Rednet API functions the same.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 February 2013 - 08:42 PM

View PostElvishJerricco, on 09 February 2013 - 08:15 PM, said:

At least the Rednet API functions the same.
The only thing I use from that is the rednet.send... I make my own event loops, meaning that all my programs will now need to say
if event[1] == "rednet_message" or event[1] == "modem_message" then

Although I do like how the side is returned for the message, means there is finally a use to turning on multiple modems...

#8 redeye83

  • Members
  • 202 posts
  • LocationUnited Kingdom

Posted 10 February 2013 - 02:37 AM

cool tutorial, I'm sure I will find a use for this on one of my projects.

One question, you said

Quote

The joy of creating a whole new layer of security on top of our rednet enabled programs is finally upon us, and oh what glorious fun we will have.

How does this make it more secure?

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 February 2013 - 02:51 AM

View Postredeye83, on 10 February 2013 - 02:37 AM, said:

How does this make it more secure?
Think about it... but you can only receive on one specific channel at a time (from one modem)... and there are lots of channels... why do u think this makes it more secure?

#10 redeye83

  • Members
  • 202 posts
  • LocationUnited Kingdom

Posted 10 February 2013 - 02:54 AM

View PostTheOriginalBIT, on 10 February 2013 - 02:51 AM, said:

View Postredeye83, on 10 February 2013 - 02:37 AM, said:

How does this make it more secure?
Think about it... but you can only receive on one specific channel at a time (from one modem)... and there are lots of channels... why do u think this makes it more secure?
I'm hoping its because I has ninja stars but I doubt it lol. My next question was if you could just open every channel at once and "snoop" for any messages. But surly it would then just be a case of opening and closing every channel one after the other until it gets a messages and then will keep listening on that channel and recording everything?

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 February 2013 - 02:58 AM

View Postredeye83, on 10 February 2013 - 02:54 AM, said:

I'm hoping its because I has ninja stars but I doubt it lol.
No. It means that modems listening on another channel wouldn't get the message, ergo, more security...

View Postredeye83, on 10 February 2013 - 02:54 AM, said:

My next question was if you could just open every channel at once and "snoop" for any messages. But surly it would then just be a case of opening and closing every channel one after the other until it gets a messages and then will keep listening on that channel and recording everything?
I don't think you can set it to listen on all... but since you change it with the peripheral then it should allow you to have 6 modems all listening on different channels...

#12 redeye83

  • Members
  • 202 posts
  • LocationUnited Kingdom

Posted 10 February 2013 - 03:05 AM

is there no way to password protect channels? or is there a encryption api already outthere?

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 February 2013 - 03:10 AM

View Postredeye83, on 10 February 2013 - 03:05 AM, said:

is there no way to password protect channels? or is there a encryption api already outthere?
Idk about password protecting... why don't you search for one of the several encryption apis........ :P

#14 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 10 February 2013 - 03:29 AM

Nice tutorial for when 1.5 actually comes out.

I only saw the beta testing post now (5 minutes ago) and didn't feel like testing it in game xD

#15 CastleMan2000

  • Members
  • 195 posts
  • LocationThe trashcan where all Undertale trash is

Posted 10 February 2013 - 04:29 AM

This topic is incredibly helpful! Thank you!

#16 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 10 February 2013 - 04:54 AM

Wow... Code for snooping on all channels...

modem = peripheral.wrap(modemside)
for i=0, 65535 do -- Or how ever many channels there are
modem.open(i)
end

Would this allow you to snoop on rednet.send messages?

Edit: Wait.. can you only listen on 1 chaannel?

#17 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 10 February 2013 - 05:00 AM

View PostTheOriginalBIT, on 10 February 2013 - 02:58 AM, said:

I don't think you can set it to listen on all... but since you change it with the peripheral then it should allow you to have 6 modems all listening on different channels...


You can listen on as many channels as you want simultaneously with only one peripheral. I'll put this in the tutorial so people are aware of it.
Edit: The advantage to having more than one modem connected is really just for ease of use. For example, if you're building a chat program and you want administrators to be able to send commands, they can do so on one channel which the modem on the left side will receive, and regular messages will be sent to another channel which the modem on the right side will receive. This will make it easier to identify and separate the two categories when receiving messages.

View Postredeye83, on 10 February 2013 - 02:37 AM, said:

How does this make it more secure?

It doesn't. I was actually be sarcastic xD

View PostMudkipTheEpic, on 10 February 2013 - 04:54 AM, said:

Wow... Code for snooping on all channels...

modem = peripheral.wrap(modemside)
for i=1, 65535 do -- Or how ever many channels there are
modem.open(i)
end

Would this allow you to snoop on rednet.send messages?

Perfectly valid code actually.

#18 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 10 February 2013 - 05:08 AM

View PostBubba, on 10 February 2013 - 05:00 AM, said:

View PostMudkipTheEpic, on 10 February 2013 - 04:54 AM, said:

Wow... Code for snooping on all channels...

modem = peripheral.wrap(modemside)
for i=1, 65535 do -- Or how ever many channels there are
modem.open(i)
end

Would this allow you to snoop on rednet.send messages?

Perfectly valid code actually.
Valid code for what? Snooping on rednet.send's? Or just listening on all channels?

#19 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 10 February 2013 - 05:12 AM

View PostMudkipTheEpic, on 10 February 2013 - 05:08 AM, said:

Valid code for what? Snooping on rednet.send's? Or just listening on all channels?

Opening that many channels, and in the process making the modem throw an event every time a message is sent on a channel on or under 65535. All it takes to snoop is to add this:
while true do
  print(os.pullEvent("modem_message"))
end


#20 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 10 February 2013 - 05:29 AM

View PostBubba, on 10 February 2013 - 05:12 AM, said:

View PostMudkipTheEpic, on 10 February 2013 - 05:08 AM, said:

Valid code for what? Snooping on rednet.send's? Or just listening on all channels?

Opening that many channels, and in the process making the modem throw an event every time a message is sent on a channel on or under 65535. All it takes to snoop is to add this:
while true do
  print(os.pullEvent("modem_message"))
end

So rednet.send isn't secure anymore? :o





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users