Skip to main content

netgauze_pcap_decoder/
protocol_handler.rs

1// Copyright (C) 2025-present The NetGauze Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://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,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12// implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use bytes::BytesMut;
17use netgauze_pcap_reader::TransportProtocol;
18use serde::Serialize;
19use std::collections::HashMap;
20use std::io;
21use std::net::{IpAddr, SocketAddr};
22
23#[derive(Debug, serde::Serialize)]
24pub struct SerializableInfo<I> {
25    pub(crate) source_address: SocketAddr,
26    pub(crate) destination_address: SocketAddr,
27    pub(crate) info: I,
28}
29
30#[derive(Debug, Serialize, PartialEq)]
31pub enum DecodeOutcome<M, E> {
32    Success(((IpAddr, u16, IpAddr, u16), M)),
33    Error(E),
34}
35
36pub trait ProtocolHandler<Message, Codec, ErrorMessage>
37where
38    Codec: Default,
39{
40    fn decode(
41        &self,
42        flow_key: (IpAddr, u16, IpAddr, u16),
43        protocol: TransportProtocol,
44        packet_data: &[u8],
45        exporter_peers: &mut HashMap<(IpAddr, u16, IpAddr, u16), (Codec, BytesMut)>,
46    ) -> Option<Vec<DecodeOutcome<Message, ErrorMessage>>>;
47
48    fn serialize(
49        &self,
50        data: DecodeOutcome<Message, ErrorMessage>,
51    ) -> io::Result<serde_json::Value>;
52}