1 |
|
%% Copyright (c) 2015 Peter Morgan <peter.james.morgan@gmail.com> |
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 implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
-module(pcapng_ip). |
16 |
|
-export([decode/1]). |
17 |
|
|
18 |
|
-define(ICMP, 1). |
19 |
|
-define(IGMP, 2). |
20 |
|
-define(TCP, 6). |
21 |
|
-define(UDP, 17). |
22 |
|
-define(ENCAP, 41). |
23 |
|
-define(OSPF, 89). |
24 |
|
-define(SCTP, 132). |
25 |
|
|
26 |
|
decode(<<4:4, IHL:4, _:6, _:2, _TotalLength:16, _/binary>> = Packet) -> |
27 |
1506 |
Size=IHL*4, |
28 |
1506 |
<<Header:Size/binary, Data/binary>> = Packet, |
29 |
1506 |
decode(header(Header), Data); |
30 |
|
|
31 |
|
decode(<< |
32 |
|
6:4, |
33 |
|
TrafficClass:8, |
34 |
|
FlowLabel:20, |
35 |
|
PayloadLength:16, |
36 |
|
NextHeader:8, |
37 |
|
HopLimit:8, |
38 |
|
Source:128, |
39 |
|
Destination:128, |
40 |
|
_/binary |
41 |
|
>>) -> |
42 |
11 |
#{traffic_class => TrafficClass, |
43 |
|
flow_label => FlowLabel, |
44 |
|
payload_length => PayloadLength, |
45 |
|
next_header => NextHeader, |
46 |
|
hop_limit => HopLimit, |
47 |
|
source => Source, |
48 |
|
destination => Destination |
49 |
|
}. |
50 |
|
|
51 |
|
|
52 |
|
header(<< |
53 |
|
4:4, |
54 |
|
IHL:4, |
55 |
|
DSCP:6, |
56 |
|
ECN:2, |
57 |
|
TotalLength:16, |
58 |
|
Identification:16, |
59 |
|
Flags:3, |
60 |
|
FragmentOffset:13, |
61 |
|
TTL:8, |
62 |
|
Protocol:8, |
63 |
|
CheckSum:16, |
64 |
|
S1:8, |
65 |
|
S2:8, |
66 |
|
S3:8, |
67 |
|
S4:8, |
68 |
|
D1:8, |
69 |
|
D2:8, |
70 |
|
D3:8, |
71 |
|
D4:8>>) -> |
72 |
1506 |
#{ihl => IHL, |
73 |
|
dscp => DSCP, |
74 |
|
ecn => ECN, |
75 |
|
total_length => TotalLength, |
76 |
|
identification => Identification, |
77 |
|
flags => Flags, |
78 |
|
fragment_offset => FragmentOffset, |
79 |
|
ttl => TTL, |
80 |
|
protocol => Protocol, |
81 |
|
check_sum => CheckSum, |
82 |
|
source => {S1, S2, S3, S4}, |
83 |
|
destination => {D1, D2, D3, D4} |
84 |
|
}. |
85 |
|
|
86 |
|
decode(#{protocol := ?UDP} = Header, Packet) -> |
87 |
685 |
#{header => Header, udp => pcapng_ip_udp:decode(Packet)}; |
88 |
|
|
89 |
|
decode(#{protocol := ?TCP} = Header, Packet) -> |
90 |
821 |
#{header => Header, tcp => pcapng_ip_tcp:decode(Packet)}. |