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 }
0 commit comments