xref: /aosp_15_r20/external/pigweed/pw_router/public/pw_router/packet_parser.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <cstdint>
17 #include <optional>
18 
19 #include "pw_bytes/span.h"
20 #include "pw_span/span.h"
21 
22 namespace pw::router {
23 
24 // A PacketParser is an abstract interface for extracting data from different
25 // kinds of transport layer packets or frames. It is used by routers to examine
26 // fields within packets to know how to route them.
27 class PacketParser {
28  public:
29   virtual ~PacketParser() = default;
30 
31   // Parses a packet, storing its data for subsequent calls to Get* functions.
32   // Any currently stored packet is cleared. Returns true if successful, or
33   // false if the packet is incomplete or corrupt.
34   //
35   // The raw binary data passed to this function is guaranteed to remain valid
36   // through all subsequent Get* calls made for the packet's information, so
37   // implementations may store and use it directly.
38   virtual bool Parse(ConstByteSpan packet) = 0;
39 
40   // Extracts the destination address the last parsed packet, if it exists.
41   //
42   // Guaranteed to only be called if Parse() succeeded and while the data passed
43   // to Parse() is valid.
44   virtual std::optional<uint32_t> GetDestinationAddress() const = 0;
45 };
46 
47 }  // namespace pw::router
48