Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Kevin Schoedel | a868187 | 2021-01-28 15:53:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2020-2021 Project CHIP Authors |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 4 | * Copyright (c) 2019 Google LLC |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * @file |
| 22 | * This file defines common preprocessor defintions, constants, |
| 23 | * functions, and globals for unit and functional tests for the |
| 24 | * Inet layer. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include "TestInetLayerCommon.hpp" |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
szatmz | 0a88e21 | 2020-12-09 13:56:49 -0500 | [diff] [blame] | 32 | #include <utility> |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 33 | |
| 34 | #include <nlbyteorder.hpp> |
| 35 | |
Kevin Schoedel | fe681d9 | 2021-11-30 15:28:04 -0500 | [diff] [blame] | 36 | #include <inet/IPPacketInfo.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 37 | #include <lib/support/CodeUtils.h> |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 38 | |
| 39 | #include "TestInetCommon.h" |
| 40 | |
| 41 | using namespace ::chip; |
| 42 | using namespace ::chip::Inet; |
| 43 | using namespace ::chip::System; |
| 44 | |
| 45 | // Type Definitions |
| 46 | |
| 47 | struct ICMPEchoHeader |
| 48 | { |
| 49 | uint8_t mType; |
| 50 | uint8_t mCode; |
| 51 | uint16_t mChecksum; |
| 52 | uint16_t mID; |
| 53 | uint16_t mSequenceNumber; |
| 54 | } __attribute__((packed)); |
| 55 | |
| 56 | typedef struct ICMPEchoHeader ICMPv4EchoHeader; |
| 57 | typedef struct ICMPEchoHeader ICMPv6EchoHeader; |
| 58 | |
| 59 | // Global Variables |
| 60 | |
| 61 | static const uint8_t kICMPv4_EchoRequest = 8; |
| 62 | static const uint8_t kICMPv4_EchoReply = 0; |
| 63 | |
| 64 | static const uint8_t kICMPv6_EchoRequest = 128; |
| 65 | static const uint8_t kICMPv6_EchoReply = 129; |
| 66 | |
| 67 | // clang-format off |
| 68 | const uint8_t gICMPv4Types[kICMPv4_FilterTypes] = |
| 69 | { |
| 70 | kICMPv4_EchoRequest, |
| 71 | kICMPv4_EchoReply |
| 72 | }; |
| 73 | |
| 74 | const uint8_t gICMPv6Types[kICMPv6_FilterTypes] = |
| 75 | { |
| 76 | kICMPv6_EchoRequest, |
| 77 | kICMPv6_EchoReply |
| 78 | }; |
| 79 | // clang-format on |
| 80 | |
| 81 | bool gSendIntervalExpired = true; |
| 82 | |
| 83 | uint32_t gSendIntervalMs = 1000; |
| 84 | |
Michael Spang | c76e82d | 2020-09-23 14:32:23 -0400 | [diff] [blame] | 85 | const char * gInterfaceName = nullptr; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 86 | |
Kevin Schoedel | 014d85c | 2021-10-27 09:25:05 -0400 | [diff] [blame] | 87 | InterfaceId gInterfaceId = InterfaceId::Null(); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 88 | |
| 89 | uint16_t gSendSize = 59; |
| 90 | |
| 91 | uint32_t gOptFlags = 0; |
| 92 | |
| 93 | namespace Common { |
| 94 | |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 95 | bool IsReceiver() |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 96 | { |
| 97 | return ((gOptFlags & kOptFlagListen) == kOptFlagListen); |
| 98 | } |
| 99 | |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 100 | bool IsSender() |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 101 | { |
| 102 | return (!IsReceiver()); |
| 103 | } |
| 104 | |
| 105 | bool IsTesting(const TestStatus & aTestStatus) |
| 106 | { |
| 107 | bool lStatus; |
| 108 | |
| 109 | lStatus = (!aTestStatus.mFailed && !aTestStatus.mSucceeded); |
| 110 | |
| 111 | return (lStatus); |
| 112 | } |
| 113 | |
| 114 | bool WasSuccessful(const TestStatus & aTestStatus) |
| 115 | { |
| 116 | bool lStatus = false; |
| 117 | |
| 118 | if (aTestStatus.mFailed) |
| 119 | lStatus = false; |
| 120 | else if (aTestStatus.mSucceeded) |
| 121 | lStatus = true; |
| 122 | |
| 123 | return (lStatus); |
| 124 | } |
| 125 | |
| 126 | static void FillDataBufferPattern(uint8_t * aBuffer, uint16_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) |
| 127 | { |
| 128 | for (uint16_t i = aPatternStartOffset; i < aLength; i++) |
| 129 | { |
| 130 | const uint8_t lValue = static_cast<uint8_t>(aFirstValue & 0xFF); |
| 131 | |
| 132 | aBuffer[i] = lValue; |
| 133 | |
| 134 | aFirstValue++; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint16_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) |
| 139 | { |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 140 | for (uint16_t i = aPatternStartOffset; i < aLength; i++) |
| 141 | { |
| 142 | const uint8_t lValue = aBuffer[i]; |
| 143 | |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 144 | if (lValue != static_cast<uint8_t>(aFirstValue)) |
| 145 | { |
| 146 | printf("Bad data value at offset %u (0x%04x): " |
| 147 | "expected 0x%02x, found 0x%02x\n", |
| 148 | i, i, aFirstValue, lValue); |
| 149 | DumpMemory(aBuffer + aPatternStartOffset, aLength - aPatternStartOffset, "0x", 16); |
| 150 | return false; |
| 151 | } |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 152 | |
| 153 | aFirstValue++; |
| 154 | } |
| 155 | |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 156 | return true; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 159 | static PacketBufferHandle MakeDataBuffer(uint16_t aDesiredLength, uint16_t aPatternStartOffset, uint8_t aFirstValue) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 160 | { |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 161 | VerifyOrReturnError(aPatternStartOffset <= aDesiredLength, PacketBufferHandle()); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 162 | |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 163 | PacketBufferHandle lBuffer = PacketBufferHandle::New(aDesiredLength); |
| 164 | VerifyOrReturnError(!lBuffer.IsNull(), lBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 165 | |
| 166 | aDesiredLength = min(lBuffer->MaxDataLength(), aDesiredLength); |
| 167 | |
| 168 | FillDataBufferPattern(lBuffer->Start(), aDesiredLength, aPatternStartOffset, aFirstValue); |
| 169 | |
| 170 | lBuffer->SetDataLength(aDesiredLength); |
| 171 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 172 | return lBuffer; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 175 | static PacketBufferHandle MakeDataBuffer(uint16_t aDesiredLength, uint16_t aPatternStartOffset) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 176 | { |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 177 | constexpr uint8_t lFirstValue = 0; |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 178 | return MakeDataBuffer(aDesiredLength, aPatternStartOffset, lFirstValue); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | template <typename tType> |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 182 | static PacketBufferHandle MakeICMPDataBuffer(uint16_t aDesiredUserLength, uint16_t aHeaderLength, uint16_t aPatternStartOffset, |
| 183 | uint8_t aType) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 184 | { |
| 185 | static uint16_t lSequenceNumber = 0; |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 186 | PacketBufferHandle lBuffer; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 187 | |
| 188 | // To ensure there is enough room for the user data and the ICMP |
| 189 | // header, include both the user data size and the ICMP header length. |
| 190 | |
Andrei Litvin | 2f16904 | 2020-12-01 10:58:43 -0500 | [diff] [blame] | 191 | lBuffer = MakeDataBuffer(static_cast<uint16_t>(aDesiredUserLength + aHeaderLength), aPatternStartOffset); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 192 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 193 | if (!lBuffer.IsNull()) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 194 | { |
| 195 | tType * lHeader = reinterpret_cast<tType *>(lBuffer->Start()); |
| 196 | |
| 197 | lHeader->mType = aType; |
| 198 | lHeader->mCode = 0; |
| 199 | lHeader->mChecksum = 0; |
Andrei Litvin | 2f16904 | 2020-12-01 10:58:43 -0500 | [diff] [blame] | 200 | lHeader->mID = static_cast<uint16_t>(rand() & UINT16_MAX); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 201 | lHeader->mSequenceNumber = nlByteOrderSwap16HostToBig(lSequenceNumber++); |
| 202 | } |
| 203 | |
| 204 | return (lBuffer); |
| 205 | } |
| 206 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 207 | PacketBufferHandle MakeICMPv4DataBuffer(uint16_t aDesiredUserLength) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 208 | { |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 209 | constexpr uint16_t lICMPHeaderLength = sizeof(ICMPv4EchoHeader); |
| 210 | constexpr uint16_t lPatternStartOffset = lICMPHeaderLength; |
| 211 | const uint8_t lType = gICMPv4Types[kICMP_EchoRequestIndex]; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 212 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 213 | return MakeICMPDataBuffer<ICMPv4EchoHeader>(aDesiredUserLength, lICMPHeaderLength, lPatternStartOffset, lType); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 216 | PacketBufferHandle MakeICMPv6DataBuffer(uint16_t aDesiredUserLength) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 217 | { |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 218 | constexpr uint16_t lICMPHeaderLength = sizeof(ICMPv6EchoHeader); |
| 219 | constexpr uint16_t lPatternStartOffset = lICMPHeaderLength; |
| 220 | const uint8_t lType = gICMPv6Types[kICMP_EchoRequestIndex]; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 221 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 222 | return MakeICMPDataBuffer<ICMPv6EchoHeader>(aDesiredUserLength, lICMPHeaderLength, lPatternStartOffset, lType); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 225 | PacketBufferHandle MakeDataBuffer(uint16_t aDesiredLength, uint8_t aFirstValue) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 226 | { |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 227 | constexpr uint16_t lPatternStartOffset = 0; |
| 228 | return MakeDataBuffer(aDesiredLength, lPatternStartOffset, aFirstValue); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 231 | PacketBufferHandle MakeDataBuffer(uint16_t aDesiredLength) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 232 | { |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 233 | constexpr uint16_t lPatternStartOffset = 0; |
| 234 | return MakeDataBuffer(aDesiredLength, lPatternStartOffset); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 237 | static bool HandleDataReceived(const PacketBufferHandle & aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer, |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 238 | uint16_t aPatternStartOffset, uint8_t aFirstValue) |
| 239 | { |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 240 | uint16_t lTotalDataLength = 0; |
| 241 | |
| 242 | // Walk through each buffer in the packet chain, checking the |
| 243 | // buffer for the expected pattern, if requested. |
| 244 | |
Kevin Schoedel | f8c29d4 | 2020-12-09 12:57:54 -0500 | [diff] [blame] | 245 | for (PacketBufferHandle lBuffer = aBuffer.Retain(); !lBuffer.IsNull(); lBuffer.Advance()) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 246 | { |
| 247 | const uint16_t lDataLength = lBuffer->DataLength(); |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 248 | const uint8_t * const p = lBuffer->Start(); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 249 | |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 250 | if (aCheckBuffer && !CheckDataBufferPattern(p, lDataLength, aPatternStartOffset, aFirstValue)) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 251 | { |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 252 | return false; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 255 | lTotalDataLength = static_cast<uint16_t>(lTotalDataLength + lDataLength); |
| 256 | aFirstValue = static_cast<uint8_t>(aFirstValue + lDataLength); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // If we are accumulating stats by packet rather than by size, |
| 260 | // then increment by one (1) rather than the total buffer length. |
| 261 | |
| 262 | aStats.mReceive.mActual += ((aStatsByPacket) ? 1 : lTotalDataLength); |
| 263 | |
Kevin Schoedel | f9550e1 | 2021-06-21 17:08:48 -0400 | [diff] [blame] | 264 | return true; |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 267 | static bool HandleICMPDataReceived(PacketBufferHandle aBuffer, uint16_t aHeaderLength, TransferStats & aStats, bool aStatsByPacket, |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 268 | bool aCheckBuffer) |
| 269 | { |
| 270 | const uint16_t lPatternStartOffset = 0; |
| 271 | bool lStatus; |
| 272 | |
| 273 | aBuffer->ConsumeHead(aHeaderLength); |
| 274 | |
| 275 | lStatus = HandleDataReceived(aBuffer, aStats, aStatsByPacket, aCheckBuffer, lPatternStartOffset); |
| 276 | |
| 277 | return (lStatus); |
| 278 | } |
| 279 | |
Kevin Schoedel | f4e7b55 | 2021-01-15 15:23:31 -0500 | [diff] [blame] | 280 | bool HandleICMPv4DataReceived(PacketBufferHandle && aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 281 | { |
| 282 | const uint16_t lICMPHeaderLength = sizeof(ICMPv4EchoHeader); |
| 283 | bool lStatus; |
| 284 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 285 | lStatus = HandleICMPDataReceived(std::move(aBuffer), lICMPHeaderLength, aStats, aStatsByPacket, aCheckBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 286 | |
| 287 | return (lStatus); |
| 288 | } |
| 289 | |
Kevin Schoedel | f4e7b55 | 2021-01-15 15:23:31 -0500 | [diff] [blame] | 290 | bool HandleICMPv6DataReceived(PacketBufferHandle && aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 291 | { |
| 292 | const uint16_t lICMPHeaderLength = sizeof(ICMPv6EchoHeader); |
| 293 | bool lStatus; |
| 294 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 295 | lStatus = HandleICMPDataReceived(std::move(aBuffer), lICMPHeaderLength, aStats, aStatsByPacket, aCheckBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 296 | |
| 297 | return (lStatus); |
| 298 | } |
| 299 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 300 | bool HandleDataReceived(const PacketBufferHandle & aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer, |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 301 | uint8_t aFirstValue) |
| 302 | { |
| 303 | const uint16_t lPatternStartOffset = 0; |
| 304 | bool lStatus; |
| 305 | |
| 306 | lStatus = HandleDataReceived(aBuffer, aStats, aStatsByPacket, aCheckBuffer, lPatternStartOffset, aFirstValue); |
| 307 | |
| 308 | return (lStatus); |
| 309 | } |
| 310 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 311 | bool HandleDataReceived(const PacketBufferHandle & aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 312 | { |
| 313 | const uint8_t lFirstValue = 0; |
| 314 | const uint16_t lPatternStartOffset = 0; |
| 315 | bool lStatus; |
| 316 | |
| 317 | lStatus = HandleDataReceived(aBuffer, aStats, aStatsByPacket, aCheckBuffer, lPatternStartOffset, lFirstValue); |
| 318 | |
| 319 | return (lStatus); |
| 320 | } |
| 321 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 322 | bool HandleUDPDataReceived(const PacketBufferHandle & aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 323 | { |
| 324 | bool lStatus; |
| 325 | |
| 326 | lStatus = HandleDataReceived(aBuffer, aStats, aStatsByPacket, aCheckBuffer); |
| 327 | |
| 328 | return (lStatus); |
| 329 | } |
| 330 | |
Kevin Schoedel | f54b925 | 2020-11-24 11:59:12 -0500 | [diff] [blame] | 331 | bool HandleTCPDataReceived(const PacketBufferHandle & aBuffer, TransferStats & aStats, bool aStatsByPacket, bool aCheckBuffer) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 332 | { |
| 333 | bool lStatus; |
| 334 | |
| 335 | lStatus = HandleDataReceived(aBuffer, aStats, aStatsByPacket, aCheckBuffer); |
| 336 | |
| 337 | return (lStatus); |
| 338 | } |
| 339 | |
| 340 | // Timer Callback Handler |
| 341 | |
Kevin Schoedel | 7359d19 | 2021-08-11 17:26:23 -0400 | [diff] [blame] | 342 | void HandleSendTimerComplete(System::Layer * aSystemLayer, void * aAppState) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 343 | { |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 344 | gSendIntervalExpired = true; |
| 345 | |
| 346 | DriveSend(); |
| 347 | } |
| 348 | |
| 349 | // Raw Endpoint Callback Handlers |
| 350 | |
Kevin Schoedel | 52db0e4 | 2021-10-28 13:59:25 -0400 | [diff] [blame] | 351 | void HandleRawMessageReceived(const UDPEndPoint * aEndPoint, const PacketBufferHandle & aBuffer, const IPPacketInfo * aPacketInfo) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 352 | { |
| 353 | char lSourceAddressBuffer[INET6_ADDRSTRLEN]; |
| 354 | char lDestinationAddressBuffer[INET6_ADDRSTRLEN]; |
| 355 | |
Boris Zbarsky | 381bed1 | 2021-02-10 12:48:21 -0500 | [diff] [blame] | 356 | aPacketInfo->SrcAddress.ToString(lSourceAddressBuffer); |
| 357 | aPacketInfo->DestAddress.ToString(lDestinationAddressBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 358 | |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 359 | printf("Raw message received from %s to %s (%u bytes)\n", lSourceAddressBuffer, lDestinationAddressBuffer, |
| 360 | static_cast<unsigned int>(aBuffer->DataLength())); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Kevin Schoedel | 52db0e4 | 2021-10-28 13:59:25 -0400 | [diff] [blame] | 363 | void HandleRawReceiveError(const UDPEndPoint * aEndPoint, const CHIP_ERROR & aError, const IPPacketInfo * aPacketInfo) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 364 | { |
| 365 | char lAddressBuffer[INET6_ADDRSTRLEN]; |
| 366 | |
Michael Spang | c76e82d | 2020-09-23 14:32:23 -0400 | [diff] [blame] | 367 | if (aPacketInfo != nullptr) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 368 | { |
Boris Zbarsky | 381bed1 | 2021-02-10 12:48:21 -0500 | [diff] [blame] | 369 | aPacketInfo->SrcAddress.ToString(lAddressBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 370 | } |
| 371 | else |
| 372 | { |
| 373 | strcpy(lAddressBuffer, "(unknown)"); |
| 374 | } |
| 375 | |
| 376 | printf("IP receive error from %s %s\n", lAddressBuffer, ErrorStr(aError)); |
| 377 | } |
| 378 | |
| 379 | // UDP Endpoint Callback Handlers |
| 380 | |
Kevin Schoedel | 52db0e4 | 2021-10-28 13:59:25 -0400 | [diff] [blame] | 381 | void HandleUDPMessageReceived(const UDPEndPoint * aEndPoint, const PacketBufferHandle & aBuffer, const IPPacketInfo * aPacketInfo) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 382 | { |
| 383 | char lSourceAddressBuffer[INET6_ADDRSTRLEN]; |
| 384 | char lDestinationAddressBuffer[INET6_ADDRSTRLEN]; |
| 385 | |
Boris Zbarsky | 381bed1 | 2021-02-10 12:48:21 -0500 | [diff] [blame] | 386 | aPacketInfo->SrcAddress.ToString(lSourceAddressBuffer); |
| 387 | aPacketInfo->DestAddress.ToString(lDestinationAddressBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 388 | |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 389 | printf("UDP packet received from %s:%u to %s:%u (%u bytes)\n", lSourceAddressBuffer, aPacketInfo->SrcPort, |
| 390 | lDestinationAddressBuffer, aPacketInfo->DestPort, static_cast<unsigned int>(aBuffer->DataLength())); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Kevin Schoedel | 52db0e4 | 2021-10-28 13:59:25 -0400 | [diff] [blame] | 393 | void HandleUDPReceiveError(const UDPEndPoint * aEndPoint, const CHIP_ERROR & aError, const IPPacketInfo * aPacketInfo) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 394 | { |
| 395 | char lAddressBuffer[INET6_ADDRSTRLEN]; |
| 396 | uint16_t lSourcePort; |
| 397 | |
Michael Spang | c76e82d | 2020-09-23 14:32:23 -0400 | [diff] [blame] | 398 | if (aPacketInfo != nullptr) |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 399 | { |
Boris Zbarsky | 381bed1 | 2021-02-10 12:48:21 -0500 | [diff] [blame] | 400 | aPacketInfo->SrcAddress.ToString(lAddressBuffer); |
Grant Erickson | acb73dc | 2020-04-10 13:48:10 -0700 | [diff] [blame] | 401 | lSourcePort = aPacketInfo->SrcPort; |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | strcpy(lAddressBuffer, "(unknown)"); |
| 406 | lSourcePort = 0; |
| 407 | } |
| 408 | |
| 409 | printf("UDP receive error from %s:%u: %s\n", lAddressBuffer, lSourcePort, ErrorStr(aError)); |
| 410 | } |
| 411 | |
Michael Spang | c9b0471 | 2020-09-23 10:14:41 -0400 | [diff] [blame] | 412 | } // namespace Common |