_site/cover/pcapng_name_resolution_block.COVER.html

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_name_resolution_block).
16 -export([parse/2]).
17
18
19 -define(NRES_IP4RECORD, 1).
20 -define(NRES_IP6RECORD, 2).
21
22
23 -spec parse(binary(), fun((binary()) -> integer())) -> map().
24
25 parse(Data, I) ->
26 2 records(Data, I).
27
28
29 records(Data, I) ->
30 2 records(Data, I, #{}).
31
32 records(<<0:16, 0:16, Options/binary>>, I, Records) ->
33 2 #{type => name_resolution_block,
34 records => Records,
35 options => pcapng_options:parse(Options, I)};
36
37 records(<<Type:16/bits, Length:16/bits, Data/binary>>, I, Records) ->
38 45 case {I(Type), pcapng:variable_length(Data, I(Length))} of
39 {?NRES_IP4RECORD,
40 #{body := <<A1:8, A2:8, A3:8, A4:8, Names/binary>>,
41 remainder := Remainder}} ->
42 42 records(Remainder, I, maps:put({A1, A2, A3, A4}, split(Names),
43 Records));
44
45 {?NRES_IP6RECORD,
46 #{body := <<IP:16/bytes, Names/binary>>, remainder := Remainder}} ->
47 3 records(Remainder, I, maps:put(IP, split(Names), Records))
48 end.
49
50 split(Names) ->
51 45 lists:droplast(binary:split(Names, <<0:8>>, [global])).
Line Hits Source