Copilot commented on code in PR #13383: URL: https://github.com/apache/trafficserver/pull/13383#discussion_r3582578236
########## include/ts/ts.h: ########## @@ -1585,10 +1585,19 @@ TSReturnCode TSHttpSsnClientFdGet(TSHttpSsn ssnp, int *fdp); /* TS-1008 END */ /** Change packet firewall mark for the client side connection - * - @note The change takes effect immediately - @return TS_SUCCESS if the client connection was modified + Sets the entire client-side packet firewall mark to @a mark; the whole mark is replaced. @a mark + is interpreted as a 32-bit unsigned bit pattern. + + @note The firewall mark is only honored on platforms whose OS supports it, specifically Linux via + @c SO_MARK. On platforms without @c SO_MARK support the call still returns TS_SUCCESS when a + client connection is present, but setting the mark has no effect at the OS layer (it is a safe + no-op). + + @note The change takes effect immediately on the live client connection + + @return TS_SUCCESS if the client connection was modified, TS_ERROR if there is no client + connection to modify Review Comment: The return-value documentation currently says TS_SUCCESS means the client connection “was modified”, but the implementation unconditionally returns TS_SUCCESS whenever a client netvc exists, even if `apply_options()` fails to apply `SO_MARK` (e.g. due to insufficient privileges). Wording should avoid implying OS-layer success until the underlying behavior is changed. ########## tests/tools/plugins/client_packet_mark.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"; +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); + } Review Comment: `snprintf()` can return a negative value on encoding errors; passing that directly as the length to `TSMimeHdrFieldValueStringSet` can result in undefined/incorrect behavior. Guard and clamp the returned length before using it. ########## doc/developer-guide/api/functions/TSHttpTxnClientPacketMarkSet.en.rst: ########## @@ -32,11 +32,24 @@ Synopsis Description =========== -Change packet firewall :arg:`mark` for the client side connection. +Change the packet firewall :arg:`mark` for the client side connection. The +entire firewall mark is replaced with :arg:`mark`, which is interpreted as a +32-bit unsigned bit pattern. + +Returns :const:`TS_SUCCESS` when the client connection was modified, and +:const:`TS_ERROR` when there is no client connection to modify. Review Comment: The current text implies TS_SUCCESS means the client connection “was modified”, but the implementation returns TS_SUCCESS whenever a client connection exists, even if applying `SO_MARK` fails (e.g. due to insufficient privileges). Adjust wording to reflect current semantics until error reporting is improved. -- 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]
