Jump to content




Rednet and Metatables

networking help

8 replies to this topic

#1 powerboat9

  • Members
  • 42 posts

Posted 29 November 2015 - 05:23 PM

If I were to send a table that had a metatable, would it keep the metatable? Would the metatable keep it's functions and coroutines?

Edited by powerboat9, 29 November 2015 - 05:24 PM.


#2 Lupus590

  • Members
  • 1,963 posts
  • LocationUK

Posted 29 November 2015 - 05:31 PM

Table.serialise is a function which converts a table to a string format. In order to send a table (via a modem or rednet) you need to serialise it first.

Unfortunately the current version of lua that CC uses cannot serialise functions and coroutines. So a table with any functions/coroutines will lose these when serialised.

As for the metatable thing, I have no idea.

As a work around for the function thing, you could include the source for the functions when you serialise it, the receiving program can then reconstruct the missing functions. This may be a bit messy, but it should work. You will not be able to do this with coroutines however.

Edited by Lupus590, 29 November 2015 - 05:31 PM.


#3 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 29 November 2015 - 05:35 PM

 powerboat9, on 29 November 2015 - 05:23 PM, said:

If I were to send a table that had a metatable, would it keep the metatable? Would the metatable keep it's functions and coroutines?

You could easily test this yourself, but I think the metatable is not sent. Coroutines will definitely not be sent as there's no way to serialize them, and textutils.serialize won't serialize functions either. You can, however, call string.dump on your functions to convert them to strings, but then you also run into the problem of sending non-plaintext strings over rednet, which IIRC is still broken. You would have to encode the binary string to something like base64 to fix that. On the other side, you would decode base64, then recompile the function with loadstring.

There's actually a lot you need to consider when sending functions across a network like this, as they may not even work on the other side depending on whether they're using upvalues (local variables outside the function), or the computers have different libraries loaded.

Why do you need to do this?

#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 29 November 2015 - 08:47 PM

You can send raw tables through modems, i believe.

#5 Lupus590

  • Members
  • 1,963 posts
  • LocationUK

Posted 29 November 2015 - 10:16 PM

 H4X0RZ, on 29 November 2015 - 08:47 PM, said:

You can send raw tables through modems, i believe.

I think that the modem methods just serialise the table for you, I'm not entirely sure.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,451 posts

Posted 29 November 2015 - 10:20 PM

No, table serialization is not done on the Lua side. I'd imagine a similar process is done on the Java side, since IIRC, metatables are indeed stripped, and the table that arrives at the client is a copy rather than a duplicate.

#7 Anavrins

  • Members
  • 775 posts

Posted 29 November 2015 - 10:25 PM

 H4X0RZ, on 29 November 2015 - 08:47 PM, said:

You can send raw tables through modems, i believe.
Yes, but the metatables does not come with it.

#8 powerboat9

  • Members
  • 42 posts

Posted 30 November 2015 - 01:16 AM

 Yevano, on 29 November 2015 - 05:35 PM, said:

Why do you need to do this?

I was wondering if you could send a table that had a metatable, and if the metatable could be used for code injection.

#9 Anavrins

  • Members
  • 775 posts

Posted 30 November 2015 - 02:31 AM

It would be too easy to be true lol, thought some trivia about "code injection", I remember Dan tweeting something a long time ago about a bug in GPS/unserialize code that allowed remote code execution.
It's been patched since, of course.
Edit: there it is... https://twitter.com/...979117746475008

Edited by Anavrins, 30 November 2015 - 03:15 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users