Created
July 31, 2023 11:26
-
-
Save ArtDu/d0ce085c3c46a6403dc5d93048ca3b8a to your computer and use it in GitHub Desktop.
send custom iproto packet via netbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| msgpack = require'msgpack' | |
| datetime = require'datetime' | |
| conn = require('net.box').connect('localhost:3301') | |
| IPROTO_REQUEST_TYPE = 0x00 | |
| IPROTO_SYNC = 0x01 | |
| IPROTO_TUPLE = 0x21 | |
| IPROTO_FUNCTION_NAME = 0x22 | |
| IPROTO_EXPR = 0x27 | |
| IPROTO_CALL = 0x0a | |
| IPROTO_EVAL = 0x08 | |
| function inject_call(conn, raw_val) | |
| local header = msgpack.encode({ | |
| [IPROTO_REQUEST_TYPE] = IPROTO_EVAL, | |
| [IPROTO_SYNC] = conn:_next_sync(), | |
| }) | |
| local body = msgpack.encode({ | |
| [IPROTO_EXPR] = 'return ...', | |
| -- Reserve a slot for raw_val. | |
| [IPROTO_TUPLE] = {box.NULL}, | |
| }) | |
| -- Replace the nil placeholder with val | |
| body = string.sub(body, 1, 3) .. raw_val .. string.sub(body, 5) | |
| local size = msgpack.encode(#header + #body) | |
| local request = size..header..body | |
| return conn:_inject(request) | |
| end | |
| -- for checking | |
| tohex = function(s) return (s:gsub('.', function(c) return string.format('%02X ', string.byte(c)) end)) end | |
| tohex(msgpack.encode('1')) -- 'A1 31' | |
| inject_call(conn, string.fromhex('a131')) -- ['1'] - equals to 'return 1' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment