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 <optional>
17 #include <span>
18 
19 #include "pw_bytes/span.h"
20 
21 namespace pw::router {
22 
23 // A PacketParser is an abstract interface for extracting data from different
24 // kinds of transport layer packets or frames. It is used by routers to examine
25 // fields within packets to know how to route them.
26 class PacketParser {
27  public:
28   virtual ~PacketParser() = default;
29 
30   // Parses a packet, storing its data for subsequent calls to Get* functions.
31   // Any currently stored packet is cleared. Returns true if successful, or
32   // false if the packet is incomplete or corrupt.
33   //
34   // The raw binary data passed to this function is guaranteed to remain valid
35   // through all subsequent Get* calls made for the packet's information, so
36   // implementations may store and use it directly.
37   virtual bool Parse(ConstByteSpan packet) = 0;
38 
39   // Extracts the destination address the last parsed packet, if it exists.
40   //
41   // Guaranteed to only be called if Parse() succeeded and while the data passed
42   // to Parse() is valid.
43   virtual std::optional<uint32_t> GetDestinationAddress() const = 0;
44 };
45 
46 }  // namespace pw::router
47