-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathudp.ex
116 lines (103 loc) · 2.56 KB
/
udp.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
defimpl String.Chars, for: Protocol.Udp do
@doc """
Prints a UDP packet to a human readable string
"""
@spec to_string(Protocol.Udp.t) :: String.t
def to_string(udp) do
"""
Udp:
#{udp.header}
Length: #{byte_size(udp.data)}
Raw: #{ExPcap.Binaries.to_raw(udp.data)}
""" |> String.trim
end
end
defimpl String.Chars, for: Protocol.Udp.Header do
@doc """
Prints a UDP packet header to a human readable string
"""
@spec to_string(Protocol.Udp.t) :: String.t
def to_string(udp) do
"""
srcport: #{udp.srcport}
srcport: #{udp.destport}
length: #{ExPcap.Binaries.to_uint16(udp.length)}
checksum: #{ExPcap.Binaries.to_hex(udp.checksum)}
""" |> String.trim
end
end
defimpl PayloadType, for: Protocol.Udp do
@doc """
Returns the parser that will parse the body of the UDP packet
"""
@spec payload_parser(binary) :: Protocol.Udp.t
def payload_parser(_data) do
Protocol.Dns
end
end
defimpl PayloadParser, for: Protocol.Udp do
@doc """
Returns the parsed body of the UDP packet
"""
@spec from_data(binary) :: any
def from_data(data) do
data |> Protocol.Udp.from_data
end
end
defmodule Protocol.Udp.Header do
@moduledoc """
A parsed UDP packet header
"""
defstruct srcport: <<>>,
destport: <<>>,
length: <<>>,
checksum: <<>>
@type t :: %Protocol.Udp.Header{
srcport: non_neg_integer,
destport: non_neg_integer,
length: binary,
checksum: binary
}
end
defmodule Protocol.Udp do
@moduledoc """
A parsed UDP packet
"""
@bytes_in_header 8
defstruct header: %Protocol.Udp.Header{},
data: <<>>
@type t :: %Protocol.Udp{
header: Protocol.Udp.Header.t,
data: binary
}
@doc """
Parses the header of a UDP packet
"""
@spec header(binary) :: Protocol.Udp.Header.t
def header(data) do
<<
srcport :: unsigned-integer-size(16),
destport :: unsigned-integer-size(16),
length :: bytes-size(2),
checksum :: bytes-size(2),
_payload :: binary
>> = data
%Protocol.Udp.Header{
srcport: srcport,
destport: destport,
length: length,
checksum: checksum
}
end
@doc """
Returns a parsed UDP packet
"""
@spec from_data(binary) :: Protocol.Udp.t
def from_data(data) do
<< _header :: bytes-size(@bytes_in_header), payload :: binary >> = data
%Protocol.Udp{
header: header(data),
data: payload
}
end
end