Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers for encoding and decoding IPROTO requests #8054

Closed
locker opened this issue Dec 13, 2022 · 0 comments · Fixed by #9317
Closed

Add helpers for encoding and decoding IPROTO requests #8054

locker opened this issue Dec 13, 2022 · 0 comments · Fixed by #9317
Assignees
Labels
3.0 Target is 3.0 and all newer release/master branches feature A new functionality

Comments

@locker
Copy link
Member

locker commented Dec 13, 2022

In the scope of #7716, we added the ability to override specific IPROTO requests on a configured Tarantool sever and send IPROTO responses over an IPROTO connection established by the server. Unfortunately, there's still no convenient helpers available that would allow to implement the IPROTO protocol in Lua on top of raw sockets, without configuring box. It could be useful for application developers that want to emulate IPROTO for some reason as well as in tests. Currently, we have to generate IPROTO requests manually, which is tedious:

local function auth(sock_path, user, tuple)
local hdr = msgpack.encode({[IPROTO_REQUEST_TYPE] = IPROTO_AUTH})
local body = msgpack.encode({
[IPROTO_USER] = user,
[IPROTO_TUPLE] = tuple,
})
local len = hdr:len() + body:len()
t.assert_lt(len, 256)
local s = socket.tcp_connect('unix/', sock_path)
local data = s:read(128) -- greeting
t.assert_equals(#data, 128)
data = '\xce\00\00\00' .. string.char(len) .. hdr .. body
t.assert_equals(s:write(data), #data) -- request
data = s:read(5) -- fixheader
t.assert_equals(#data, 5)
len = msgpack.decode(data)
data = s:read(len) -- response
t.assert_equals(#data, len)
s:close()
hdr, len = msgpack.decode(data)
body = msgpack.decode(data, len)
return {
bit.band(hdr[IPROTO_REQUEST_TYPE], bit.bnot(IPROTO_TYPE_ERROR)),
body[IPROTO_ERROR],
}
end

What we need is:

  • Encoding and decoding of a greeting message.
  • Generating authentication data.
  • Handling of request/response fix header.

This is all done by net.box under the hood. We could probably just make the code public.

@locker locker added the feature A new functionality label Dec 13, 2022
@kyukhin kyukhin added the 2.11 Target is 2.11 and all newer release/master branches label Dec 19, 2022
@NeraverinTarantool NeraverinTarantool removed the 2.11 Target is 2.11 and all newer release/master branches label Jan 16, 2023
@locker locker self-assigned this Oct 24, 2023
@locker locker added the 3.0 Target is 3.0 and all newer release/master branches label Oct 24, 2023
locker added a commit to locker/tarantool that referenced this issue Oct 27, 2023
Closes tarantool#8054

@TarantoolBot document
Title: Document Lua helpers for encoding/decoding IPROTO packets

The following new constants and functions were added to the `box.iproto`
namespace:

 - `GREETING_SIZE`: Size of a Tarantool greeting message

 - `GREETING_PROTOCOL_LEN_MAX`: Max length of a protocol string that can
   be encoded in a Tarantool greeting message.

 - `GREETING_SALT_LEN_MAX`: Max length of a salt string that can be
   encoded in a Tarantool greeting message.

 - `box.iproto.encode_greeting({version = x, uuid = x, salt = x})`:
   Encodes a Tarantool greeting message. Takes a table. Returns a
   string. Raises on error. The protocol is set to "Binary" (IPROTO).

 - `box.iproto.decode_greeting(string)`: Decodes a Tarantool greeting
   message. Takes a string. Returns a table with the following fields:
   `protocol`, `version`, `uuid`, `salt`. Raises on error. The input
   string must be exactly `GREETING_SIZE` bytes long.

 - `box.iproto.encode_packet(header[, body])`: Encodes an IPROTO packet.
   Takes a packet header and optionally a body given as a table or a
   string. A table argument will be encoded in MsgPack. A string
   argument will be copied as is (it's supposed to contain valid MsgPack
   but it isn't enforced). Returns a string. Raises on error.

 - `box.iproto.decode_packet(string[, pos])`: Decodes an IPROTO packet.
   Takes a string containing one or more encoded IPROTO packets and
   optionally a position in the string to start decoding from. If the
   position is omitted, the function will start decoding from the
   beginning of the string. On success returns the decoded packet
   header, body, and the position in the string where decoding stopped.
   Both header and body are returned as `msgpack.object`. The body may
   be absent (set to nil). If the input is truncated, returns nil and
   the min number of bytes required to decode the packet. On failure,
   raises an error.

For examples, see [`test/app-luatest/iproto_encoder_test.lua`][1].

[1]: https://github.com/tarantool/tarantool/blob/master/test/app-luatest/iproto_encoder_test.lua
locker added a commit to locker/tarantool that referenced this issue Oct 30, 2023
Closes tarantool#8054

@TarantoolBot document
Title: Document Lua helpers for encoding/decoding IPROTO packets

The following new constants and functions were added to the `box.iproto`
namespace:

 - `GREETING_SIZE`: Size of a Tarantool greeting message

 - `GREETING_PROTOCOL_LEN_MAX`: Max length of a protocol string that can
   be encoded in a Tarantool greeting message.

 - `GREETING_SALT_LEN_MAX`: Max length of a salt string that can be
   encoded in a Tarantool greeting message.

 - `box.iproto.encode_greeting({version = x, uuid = x, salt = x})`:
   Encodes a Tarantool greeting message. Takes a table. Returns a
   string. Raises on error. The protocol is set to "Binary" (IPROTO).

 - `box.iproto.decode_greeting(string)`: Decodes a Tarantool greeting
   message. Takes a string. Returns a table with the following fields:
   `protocol`, `version`, `uuid`, `salt`. Raises on error. The input
   string must be exactly `GREETING_SIZE` bytes long.

 - `box.iproto.encode_packet(header[, body])`: Encodes an IPROTO packet.
   Takes a packet header and optionally a body given as a table or a
   string. A table argument will be encoded in MsgPack. A string
   argument will be copied as is (it's supposed to contain valid MsgPack
   but it isn't enforced). Returns a string. Raises on error.

 - `box.iproto.decode_packet(string[, pos])`: Decodes an IPROTO packet.
   Takes a string containing one or more encoded IPROTO packets and
   optionally a position in the string to start decoding from. If the
   position is omitted, the function will start decoding from the
   beginning of the string. On success returns the decoded packet
   header, body, and the position in the string where decoding stopped.
   Both header and body are returned as `msgpack.object`. The body may
   be absent (set to nil). If the input is truncated, returns nil and
   the min number of bytes required to decode the packet. On failure,
   raises an error.

For examples, see [`test/app-luatest/iproto_encoder_test.lua`][1].

[1]: https://github.com/tarantool/tarantool/blob/master/test/app-luatest/iproto_encoder_test.lua
locker added a commit that referenced this issue Oct 31, 2023
Closes #8054

@TarantoolBot document
Title: Document Lua helpers for encoding/decoding IPROTO packets

The following new constants and functions were added to the `box.iproto`
namespace:

 - `GREETING_SIZE`: Size of a Tarantool greeting message

 - `GREETING_PROTOCOL_LEN_MAX`: Max length of a protocol string that can
   be encoded in a Tarantool greeting message.

 - `GREETING_SALT_LEN_MAX`: Max length of a salt string that can be
   encoded in a Tarantool greeting message.

 - `box.iproto.encode_greeting({version = x, uuid = x, salt = x})`:
   Encodes a Tarantool greeting message. Takes a table. Returns a
   string. Raises on error. The protocol is set to "Binary" (IPROTO).

 - `box.iproto.decode_greeting(string)`: Decodes a Tarantool greeting
   message. Takes a string. Returns a table with the following fields:
   `protocol`, `version`, `uuid`, `salt`. Raises on error. The input
   string must be exactly `GREETING_SIZE` bytes long.

 - `box.iproto.encode_packet(header[, body])`: Encodes an IPROTO packet.
   Takes a packet header and optionally a body given as a table or a
   string. A table argument will be encoded in MsgPack. A string
   argument will be copied as is (it's supposed to contain valid MsgPack
   but it isn't enforced). Returns a string. Raises on error.

 - `box.iproto.decode_packet(string[, pos])`: Decodes an IPROTO packet.
   Takes a string containing one or more encoded IPROTO packets and
   optionally a position in the string to start decoding from. If the
   position is omitted, the function will start decoding from the
   beginning of the string. On success returns the decoded packet
   header, body, and the position in the string where decoding stopped.
   Both header and body are returned as `msgpack.object`. The body may
   be absent (set to nil). If the input is truncated, returns nil and
   the min number of bytes required to decode the packet. On failure,
   raises an error.

For examples, see [`test/app-luatest/iproto_encoder_test.lua`][1].

[1]: https://github.com/tarantool/tarantool/blob/master/test/app-luatest/iproto_encoder_test.lua
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0 Target is 3.0 and all newer release/master branches feature A new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants