Skip to content

Commit e5fbd01

Browse files
committed
(chore) better protocl/control message naming
1 parent 8ca334c commit e5fbd01

File tree

9 files changed

+159
-159
lines changed

9 files changed

+159
-159
lines changed

pipetap-dll/include/inject/control_server.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace pipetap::inject {
2525
void send_hello();
2626
void send_error(uint32_t code, const char* what);
2727

28-
void send_pipe_io(uint16_t tlv_type, HANDLE pipe,
28+
void send_pipe_io(uint16_t message_type, HANDLE pipe,
2929
const void* buf, uint32_t total,
3030
uint8_t dir, uint64_t op_id,
3131
const char* apiName);
@@ -58,9 +58,9 @@ namespace pipetap::inject {
5858
static DWORD WINAPI control_pipe_thread_thunk(LPVOID);
5959
void control_pipe_thread();
6060

61-
void handle_inbound_tlv(HANDLE h);
61+
void handle_inbound_commands(HANDLE h);
6262

63-
void send_tlv_streamed(uint16_t type,
63+
void send_control_message(uint16_t type,
6464
const void* meta, uint32_t meta_len,
6565
const void* payload, uint32_t payload_len);
6666

pipetap-dll/inject/control_server.cpp

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@
99
#include <memory>
1010
#include <atomic>
1111
#include <string>
12-
13-
#include "pipetap/log.h"
14-
#include "pipetap/controlpipe.h"
15-
#include "pipetap/winpipe_helpers.h"
16-
17-
#include "inject/control_server.h"
18-
#include "inject/hook_guards.h"
19-
#include "inject/pipe_utils.h"
20-
#include "inject/tcp_pipe_proxy.h"
21-
#include "inject/namedpipe_client.h"
22-
23-
24-
namespace pipetap::inject {
25-
26-
static inline HANDLE atomic_load_handle(const HANDLE& h) {
27-
return (HANDLE)InterlockedCompareExchangePointer((PVOID*)&h, nullptr, nullptr);
28-
}
29-
static inline HANDLE atomic_exchange_handle(HANDLE& h, HANDLE hNew) {
30-
return (HANDLE)InterlockedExchangePointer((PVOID*)&h, hNew);
31-
}
12+
13+
#include "pipetap/log.h"
14+
#include "pipetap/controlpipe.h"
15+
#include "pipetap/winpipe_helpers.h"
16+
17+
#include "inject/control_server.h"
18+
#include "inject/hook_guards.h"
19+
#include "inject/pipe_utils.h"
20+
#include "inject/tcp_pipe_proxy.h"
21+
#include "inject/namedpipe_client.h"
22+
23+
24+
namespace pipetap::inject {
25+
26+
static inline HANDLE atomic_load_handle(const HANDLE& h) {
27+
return (HANDLE)InterlockedCompareExchangePointer((PVOID*)&h, nullptr, nullptr);
28+
}
29+
static inline HANDLE atomic_exchange_handle(HANDLE& h, HANDLE hNew) {
30+
return (HANDLE)InterlockedExchangePointer((PVOID*)&h, hNew);
31+
}
3232

3333
struct SrwExclusive {
3434
SRWLOCK* l; explicit SrwExclusive(SRWLOCK* p) : l(p) { AcquireSRWLockExclusive(l); }
@@ -213,7 +213,7 @@ namespace pipetap::inject {
213213
strncpy_s(hello.proc_name, sizeof(hello.proc_name), name.c_str(), _TRUNCATE);
214214
log::printf("SendHello: pid=%lu exe=%s", (unsigned long)hello.pid, hello.proc_name);
215215

216-
send_tlv_streamed(PT_HELLO, &hello, (uint32_t)sizeof(hello), nullptr, 0);
216+
send_control_message(PT_HELLO, &hello, (uint32_t)sizeof(hello), nullptr, 0);
217217
}
218218

219219
void ControlServer::send_error(uint32_t code, const char* what)
@@ -224,10 +224,10 @@ namespace pipetap::inject {
224224
e.code = code;
225225
strncpy_s(e.what, sizeof(e.what), (what ? what : "err"), _TRUNCATE);
226226

227-
send_tlv_streamed(PT_ERROR, &e, (uint32_t)sizeof(e), nullptr, 0);
227+
send_control_message(PT_ERROR, &e, (uint32_t)sizeof(e), nullptr, 0);
228228
}
229229

230-
void ControlServer::send_pipe_io(uint16_t tlv_type, HANDLE pipe,
230+
void ControlServer::send_pipe_io(uint16_t message_type, HANDLE pipe,
231231
const void* buf, uint32_t total,
232232
uint8_t dir, uint64_t op_id,
233233
const char* apiName)
@@ -282,15 +282,15 @@ namespace pipetap::inject {
282282
const uint32_t image_len = meta.image_len;
283283
const uint32_t payload_len = sample;
284284

285-
std::vector<::pipetap::TlvFragment> fragments;
286-
fragments.reserve(1 + (name_len ? 1 : 0) + (api_len ? 1 : 0) + (image_len ? 1 : 0) + ((payload_len && buf) ? 1 : 0));
287-
fragments.push_back({ &meta, meta_len });
288-
if (name_len) fragments.push_back({ pipeName.data(), name_len });
289-
if (api_len) fragments.push_back({ api.data(), api_len });
290-
if (image_len) fragments.push_back({ peerImg.data(), image_len });
291-
if (payload_len && buf) fragments.push_back({ buf, payload_len });
292-
293-
auto msg = ::pipetap::BuildTlvMessage(tlv_type, fragments);
285+
std::vector<::pipetap::ControlMessageFragment> fragments;
286+
fragments.reserve(1 + (name_len ? 1 : 0) + (api_len ? 1 : 0) + (image_len ? 1 : 0) + ((payload_len && buf) ? 1 : 0));
287+
fragments.push_back({ &meta, meta_len });
288+
if (name_len) fragments.push_back({ pipeName.data(), name_len });
289+
if (api_len) fragments.push_back({ api.data(), api_len });
290+
if (image_len) fragments.push_back({ peerImg.data(), image_len });
291+
if (payload_len && buf) fragments.push_back({ buf, payload_len });
292+
293+
auto msg = ::pipetap::BuildControlMessage(message_type, fragments);
294294

295295
AcquireSRWLockExclusive(&ctrl_lock_);
296296
{
@@ -319,7 +319,7 @@ namespace pipetap::inject {
319319
r.is_message_mode = is_message_mode;
320320
r.out_buf_hint = out_hint;
321321
r.in_buf_hint = in_hint;
322-
send_tlv_streamed(PT_EVT_PROXY_OPENED, &r, sizeof(r), nullptr, 0);
322+
send_control_message(PT_EVT_PROXY_OPENED, &r, sizeof(r), nullptr, 0);
323323
}
324324

325325
void ControlServer::send_proxy_closed(uint64_t session_id,
@@ -331,7 +331,7 @@ namespace pipetap::inject {
331331
ev.session_id = session_id;
332332
ev.reason = reason;
333333
ev.win32_error = win32_error;
334-
send_tlv_streamed(PT_EVT_PROXY_CLOSED, &ev, sizeof(ev), nullptr, 0);
334+
send_control_message(PT_EVT_PROXY_CLOSED, &ev, sizeof(ev), nullptr, 0);
335335
}
336336

337337
BOOL ControlServer::wait_for_edit_and_maybe_replace(uint64_t op_id,
@@ -412,7 +412,7 @@ namespace pipetap::inject {
412412

413413
send_hello();
414414

415-
handle_inbound_tlv(hCmdSrv);
415+
handle_inbound_commands(hCmdSrv);
416416

417417
HANDLE myR = atomic_exchange_handle(ctrl_r_, INVALID_HANDLE_VALUE);
418418
HANDLE myW = atomic_exchange_handle(ctrl_w_, INVALID_HANDLE_VALUE);
@@ -454,22 +454,22 @@ namespace pipetap::inject {
454454
1, 0, 256 * 1024, 0, nullptr);
455455
}
456456

457-
void ControlServer::send_tlv_streamed(uint16_t type,
458-
const void* meta, uint32_t meta_len,
459-
const void* payload, uint32_t payload_len)
460-
{
461-
if (!connected_.load(std::memory_order_relaxed)) return;
457+
void ControlServer::send_control_message(uint16_t type,
458+
const void* meta, uint32_t meta_len,
459+
const void* payload, uint32_t payload_len)
460+
{
461+
if (!connected_.load(std::memory_order_relaxed)) return;
462462

463463
HANDLE hW = atomic_load_handle(ctrl_w_);
464464
if (ctrl_broken_.load(std::memory_order_acquire) || !hW || hW == INVALID_HANDLE_VALUE) {
465465
connected_.store(false, std::memory_order_release);
466466
return;
467467
}
468-
469-
std::vector<::pipetap::TlvFragment> fragments;
470-
if (meta_len && meta) fragments.push_back({ meta, meta_len });
471-
if (payload_len && payload) fragments.push_back({ payload, payload_len });
472-
auto msg = ::pipetap::BuildTlvMessage(type, fragments);
468+
469+
std::vector<::pipetap::ControlMessageFragment> fragments;
470+
if (meta_len && meta) fragments.push_back({ meta, meta_len });
471+
if (payload_len && payload) fragments.push_back({ payload, payload_len });
472+
auto msg = ::pipetap::BuildControlMessage(type, fragments);
473473

474474
AcquireSRWLockExclusive(&ctrl_lock_);
475475
{
@@ -485,7 +485,7 @@ namespace pipetap::inject {
485485
ReleaseSRWLockExclusive(&ctrl_lock_);
486486
}
487487

488-
void ControlServer::handle_inbound_tlv(HANDLE h)
488+
void ControlServer::handle_inbound_commands(HANDLE h)
489489
{
490490
for (;;)
491491
{
@@ -496,21 +496,21 @@ namespace pipetap::inject {
496496
SuppressHooksGuard _guard;
497497
if (!PeekNamedPipe(h, nullptr, 0, nullptr, &totalAvail, &bytesLeftMsg)) break;
498498
}
499-
if (totalAvail < sizeof(PT_TlvHeader)) { Sleep(1); continue; }
500-
501-
PT_TlvHeader hdr{};
502-
{
503-
SuppressHooksGuard _guard;
504-
if (!::pipetap::PipeReadExact(h, &hdr, static_cast<DWORD>(sizeof(hdr)))) break;
505-
}
506-
507-
if (hdr.length > (512u * 1024u * 1024u)) break;
508-
509-
std::vector<uint8_t> val(hdr.length);
510-
if (hdr.length) {
511-
SuppressHooksGuard _guard;
512-
if (!::pipetap::PipeReadExact(h, val.data(), hdr.length)) break;
513-
}
499+
if (totalAvail < sizeof(PT_ControlMessageHeader)) { Sleep(1); continue; }
500+
501+
PT_ControlMessageHeader hdr{};
502+
{
503+
SuppressHooksGuard _guard;
504+
if (!::pipetap::PipeReadExact(h, &hdr, static_cast<DWORD>(sizeof(hdr)))) break;
505+
}
506+
507+
if (hdr.length > (512u * 1024u * 1024u)) break;
508+
509+
std::vector<uint8_t> val(hdr.length);
510+
if (hdr.length) {
511+
SuppressHooksGuard _guard;
512+
if (!::pipetap::PipeReadExact(h, val.data(), hdr.length)) break;
513+
}
514514

515515
ctrl_broken_.store(false, std::memory_order_release);
516516
connected_.store(true, std::memory_order_release);
@@ -624,7 +624,7 @@ namespace pipetap::inject {
624624
break;
625625

626626
default:
627-
// Ignore unknown TLVs
627+
// Ignore unknown control messages
628628
break;
629629
}
630630
}

pipetap-gui/app/ctrlclient.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,32 @@ namespace pipetap::ctrlclient {
7070
if (ev_to_close != INVALID_HANDLE_VALUE) CloseHandle(ev_to_close);
7171
}
7272

73-
bool CtrlClient::SendTLV(uint16_t type, const void* v1, uint32_t n1, const void* v2, uint32_t n2) {
73+
bool CtrlClient::SendControlMessage(uint16_t type, const void* v1, uint32_t n1, const void* v2, uint32_t n2) {
7474

75-
std::vector<::pipetap::TlvFragment> fragments;
75+
std::vector<::pipetap::ControlMessageFragment> fragments;
7676
size_t expected = 0;
7777
if (n1 && v1) ++expected;
7878
if (n2 && v2) ++expected;
7979
fragments.reserve(expected);
8080
if (n1 && v1) fragments.push_back({ v1, n1 });
8181
if (n2 && v2) fragments.push_back({ v2, n2 });
82-
auto msg = ::pipetap::BuildTlvMessage(type, fragments);
82+
auto msg = ::pipetap::BuildControlMessage(type, fragments);
8383

8484
HANDLE h = INVALID_HANDLE_VALUE;
8585
{
8686
std::lock_guard<std::mutex> lk(wr_mtx_);
8787
if (!connected.load(std::memory_order_acquire) || h_cmd_ == INVALID_HANDLE_VALUE) {
88-
pipetap::log::App.Error("CtrlClient::SendTLV: not connected");
88+
pipetap::log::App.Error("CtrlClient::SendControlMessage: not connected");
8989
return false;
9090
}
9191
h = h_cmd_;
9292
}
9393

9494
DWORD wrote = 0;
95-
pipetap::log::App.Infof("CtrlClient::SendTLV: type=%u total=%lu", (unsigned)type, (unsigned long)msg.size());
95+
pipetap::log::App.Infof("CtrlClient::SendControlMessage: type=%u total=%lu", (unsigned)type, (unsigned long)msg.size());
9696
if (!WriteFile(h, msg.data(), (DWORD)msg.size(), &wrote, nullptr) || wrote != msg.size()) {
9797
DWORD le = GetLastError();
98-
pipetap::log::App.Errorf("CtrlClient::SendTLV: write failed wrote=%lu/%lu le=%lu",
98+
pipetap::log::App.Errorf("CtrlClient::SendControlMessage: write failed wrote=%lu/%lu le=%lu",
9999
(unsigned long)wrote, (unsigned long)msg.size(), (unsigned long)le);
100100

101101
{
@@ -169,7 +169,7 @@ namespace pipetap::ctrlclient {
169169
return true;
170170
}
171171

172-
bool CtrlClient::ReadNextMessage(DWORD cur_pid, PT_TlvHeader& hdr, std::vector<uint8_t>& val)
172+
bool CtrlClient::ReadNextMessage(DWORD cur_pid, PT_ControlMessageHeader& hdr, std::vector<uint8_t>& val)
173173
{
174174
HANDLE ev_snapshot = INVALID_HANDLE_VALUE;
175175
{
@@ -252,7 +252,7 @@ namespace pipetap::ctrlclient {
252252
}
253253
}
254254

255-
PT_TlvHeader hdr{};
255+
PT_ControlMessageHeader hdr{};
256256
std::vector<uint8_t> val;
257257
if (!ReadNextMessage(cur_pid, hdr, val)) {
258258
continue;

pipetap-gui/app/ui/proxy.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ namespace pipetap::ui::proxy {
210210
rep.action = replace ? 1 : 0;
211211
rep.new_size = replace ? (uint32_t)len : 0;
212212
if (replace && bytes && len)
213-
client->SendTLV(PT_CMD_EDIT_REPLY, &rep, (uint32_t)sizeof(rep), bytes, (uint32_t)len);
213+
client->SendControlMessage(PT_CMD_EDIT_REPLY, &rep, (uint32_t)sizeof(rep), bytes, (uint32_t)len);
214214
else
215-
client->SendTLV(PT_CMD_EDIT_REPLY, &rep, (uint32_t)sizeof(rep));
215+
client->SendControlMessage(PT_CMD_EDIT_REPLY, &rep, (uint32_t)sizeof(rep));
216216
}
217217

218218
static IncomingBuffer* EnsureIncoming(Tab* t) {
@@ -880,7 +880,7 @@ namespace pipetap::ui::proxy {
880880
}
881881
}
882882

883-
static void OnCtrlMsgWithManager(Manager& mgr, const PT_TlvHeader& hdr, const std::vector<uint8_t>& val)
883+
static void OnCtrlMsgWithManager(Manager& mgr, const PT_ControlMessageHeader& hdr, const std::vector<uint8_t>& val)
884884
{
885885
if (hdr.type == PT_HELLO && val.size() >= sizeof(PT_Hello)) {
886886
PT_Hello hello{}; std::memcpy(&hello, val.data(), sizeof(hello));
@@ -899,7 +899,7 @@ namespace pipetap::ui::proxy {
899899
return;
900900
}
901901

902-
// Accept both IO and EVENT TLVs here
902+
// Accept both IO and EVENT control messages here
903903
const bool is_pipeio_or_event =
904904
(hdr.type == PT_PIPE_WRITE || hdr.type == PT_PIPE_READ ||
905905
hdr.type == PT_TNP_REQUEST || hdr.type == PT_TNP_RESPONSE ||
@@ -1016,12 +1016,12 @@ namespace pipetap::ui::proxy {
10161016
return;
10171017
}
10181018

1019-
// finally, log unrecognized TLVs
1020-
pipetap::log::App.Info("[control] unrecognized TLV or short frame");
1019+
// finally, log unrecognized control messages
1020+
pipetap::log::App.Info("[control] unrecognized control message or short frame");
10211021
}
10221022

10231023
BoundCtrlHandler BindHandler(Manager& m) {
1024-
return [&m](const PT_TlvHeader& hdr, const std::vector<uint8_t>& val) {
1024+
return [&m](const PT_ControlMessageHeader& hdr, const std::vector<uint8_t>& val) {
10251025
OnCtrlMsgWithManager(m, hdr, val);
10261026
};
10271027
}

0 commit comments

Comments
 (0)