/
packet.ex
39 lines (34 loc) · 916 Bytes
/
packet.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
defimpl String.Chars, for: ExPcap.Packet do
@doc """
Prints a pcap packet in a human friendly manner.
"""
@spec to_string(ExPcap.Packet.t) :: String.t
def to_string(packet) do
String.trim("""
Packet
------
header:
#{packet.packet_header}
parsed:
#{packet.parsed_packet_data
|> Enum.map(&String.Chars.to_string/1)
|> Enum.join("\n ")}
raw:
#{packet.raw_packet_data}
""")
end
end
defmodule ExPcap.Packet do
@moduledoc """
This module represents a single pcap packet. It contains a header and both raw
and parsed versions of the body.
"""
defstruct packet_header: %ExPcap.PacketHeader{},
raw_packet_data: %ExPcap.PacketData{},
parsed_packet_data: Packet
@type t :: %ExPcap.Packet{
packet_header: ExPcap.PacketHeader.t,
raw_packet_data: ExPcap.PacketData.t,
parsed_packet_data: Packet
}
end