JosiahWI commented on code in PR #13383:
URL: https://github.com/apache/trafficserver/pull/13383#discussion_r3581884605


##########
tests/tools/plugins/client_packet_mark_mask.cc:
##########
@@ -0,0 +1,164 @@
+/** @file
+
+  Test plugin for the TSHttpTxnClientPacketMarkSet API.
+
+  On each request it reads a target mark from request headers, applies it to 
the
+  client-side connection via TSHttpTxnClientPacketMarkSet, then reads the mark
+  back off the client socket with getsockopt(SO_MARK) and echoes the observed
+  value into the X-Client-Packet-Mark response header for the AuTest to assert
+  on.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include <ts/ts.h>
+
+extern "C" {
+#include <sys/socket.h>
+}
+
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <optional>
+#include <string>
+#include <string_view>
+
+namespace
+{
+constexpr std::string_view PLUGIN_NAME = "client_packet_mark_mask";
+constexpr std::string_view MARK_HEADER = "X-Set-Mark";
+constexpr std::string_view ECHO_HEADER = "X-Client-Packet-Mark";
+
+DbgCtl dbg_ctl{PLUGIN_NAME.data()};
+
+/** Read a header field and interpret its value as a 32-bit unsigned quantity.
+
+    Values are parsed with strtoul (base 0), so "0x0000000A" and "10" are both
+    accepted. Returns std::nullopt if the header is absent. */
+std::optional<uint32_t>
+get_uint_header(TSMBuffer bufp, TSMLoc hdr_loc, std::string_view header)
+{
+  TSMLoc field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, header.data(), 
static_cast<int>(header.length()));
+  if (field_loc == TS_NULL_MLOC) {
+    return std::nullopt;
+  }
+
+  int         value_len = 0;
+  const char *value_str = TSMimeHdrFieldValueStringGet(bufp, hdr_loc, 
field_loc, -1, &value_len);
+  uint32_t    result    = 0;
+  if (value_str != nullptr && value_len > 0) {
+    std::string value(value_str, value_len);
+    result = static_cast<uint32_t>(strtoul(value.c_str(), nullptr, 0));
+  }
+  TSHandleMLocRelease(bufp, hdr_loc, field_loc);
+  return result;
+}
+
+/** Create the echo header on the response with the value formatted as 0x%08x. 
*/
+void
+set_echo_header(TSMBuffer bufp, TSMLoc hdr_loc, uint32_t value)
+{
+  char formatted[16];
+  int  len = snprintf(formatted, sizeof(formatted), "0x%08x", value);
+
+  TSMLoc field_loc = TS_NULL_MLOC;
+  if (TSMimeHdrFieldCreateNamed(bufp, hdr_loc, ECHO_HEADER.data(), 
static_cast<int>(ECHO_HEADER.length()), &field_loc) ==
+      TS_SUCCESS) {
+    TSMimeHdrFieldValueStringSet(bufp, hdr_loc, field_loc, -1, formatted, len);
+    TSMimeHdrFieldAppend(bufp, hdr_loc, field_loc);
+    TSHandleMLocRelease(bufp, hdr_loc, field_loc);
+  }
+}
+
+int
+handle_send_response(TSCont /* contp ATS_UNUSED */, TSEvent event, void *edata)
+{
+  TSHttpTxn txnp = static_cast<TSHttpTxn>(edata);
+
+  if (event != TS_EVENT_HTTP_SEND_RESPONSE_HDR) {
+    TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
+    return 0;
+  }
+
+  TSMBuffer req_bufp = nullptr;
+  TSMLoc    req_loc  = TS_NULL_MLOC;
+  if (TSHttpTxnClientReqGet(txnp, &req_bufp, &req_loc) != TS_SUCCESS) {
+    TSError("[%s] Failed to get client request headers", PLUGIN_NAME.data());
+    TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
+    return 0;
+  }
+
+  std::optional<uint32_t> mark = get_uint_header(req_bufp, req_loc, 
MARK_HEADER);
+  TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, req_loc);
+
+  if (mark.has_value()) {
+    Dbg(dbg_ctl, "Calling unmasked set: mark=0x%08x", *mark);
+    TSHttpTxnClientPacketMarkSet(txnp, static_cast<int>(*mark));
+  }

Review Comment:
   Interesting suggestion, but not necessary. Not going to accept.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to