Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 430 lines (382 sloc) 12.797 kB
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
1 %% Copyright (c) 2009, Michael Santos <michael.santos@gmail.com>
2 %% All rights reserved.
4324dcf @msantos die trailing whitespace die!
authored
3 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
4 %% Redistribution and use in source and binary forms, with or without
5 %% modification, are permitted provided that the following conditions
6 %% are met:
4324dcf @msantos die trailing whitespace die!
authored
7 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
8 %% Redistributions of source code must retain the above copyright
9 %% notice, this list of conditions and the following disclaimer.
4324dcf @msantos die trailing whitespace die!
authored
10 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
11 %% Redistributions in binary form must reproduce the above copyright
12 %% notice, this list of conditions and the following disclaimer in the
13 %% documentation and/or other materials provided with the distribution.
4324dcf @msantos die trailing whitespace die!
authored
14 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
15 %% Neither the name of the author nor the names of its contributors
16 %% may be used to endorse or promote products derived from this software
17 %% without specific prior written permission.
4324dcf @msantos die trailing whitespace die!
authored
18 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
19 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 %% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 %% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 %% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 %% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 %% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 %% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 %% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 %% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 %% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 %% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 %% POSSIBILITY OF SUCH DAMAGE.
31 -module(epcap_net).
32
33 -include("epcap_net.hrl").
34
965b308 @msantos Decapsulate truncated packets. Convert sniff to a gen_server.
authored
35 -define(ETHERHDRLEN, 16).
36 -define(IPV4HDRLEN, 20).
88cacab @oliv3 IPv6 support
oliv3 authored
37 -define(IPV6HDRLEN, 40).
965b308 @msantos Decapsulate truncated packets. Convert sniff to a gen_server.
authored
38 -define(TCPHDRLEN, 20).
39 -define(UDPHDRLEN, 8).
40 -define(ICMPHDRLEN, 8).
41
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
42 -export([
43 checksum/1,
44 decapsulate/1,
45 makesum/1,
46 valid/1,
47 ether/1,
48 ether_addr/1,
3a1efac @msantos Recursively decapsulate the packet.
authored
49 ether_type/1,
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
50 icmp/1,
51 ipv4/1,
3a1efac @msantos Recursively decapsulate the packet.
authored
52 ipv6/1,
1fac0f7 @msantos Now compiler friendly, typo free!
authored
53 payload/1,
965b308 @msantos Decapsulate truncated packets. Convert sniff to a gen_server.
authored
54 proto/1,
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
55 tcp/1,
56 tcp_flags/1,
57 udp/1
58 ]).
59
60 -define(is_print(C), C >= $ , C =< $~).
61
6ee4d73 @msantos Use the #ether{} type field.
authored
62
3a1efac @msantos Recursively decapsulate the packet.
authored
63 decapsulate(Data) ->
64 decapsulate({ether, Data}, []).
65
66 decapsulate(stop, Packet) ->
67 lists:reverse(Packet);
68 decapsulate({unsupported, Data}, Packet) ->
69 decapsulate(stop, [{unsupported, Data}|Packet]);
70 decapsulate({ether, Data}, Packet) when byte_size(Data) >= ?ETHERHDRLEN ->
71 {Hdr, Payload} = ether(Data),
72 decapsulate({ether_type(Hdr#ether.type), Payload}, [Hdr|Packet]);
73 decapsulate({ipv4, Data}, Packet) when byte_size(Data) >= ?IPV4HDRLEN ->
74 {Hdr, Payload} = ipv4(Data),
75 decapsulate({proto(Hdr#ipv4.p), Payload}, [Hdr|Packet]);
76 decapsulate({ipv6, Data}, Packet) when byte_size(Data) >= ?IPV6HDRLEN ->
77 {Hdr, Payload} = ipv6(Data),
78 decapsulate({proto(Hdr#ipv6.next), Payload}, [Hdr|Packet]);
79 decapsulate({tcp, Data}, Packet) when byte_size(Data) >= ?TCPHDRLEN ->
80 {Hdr, Payload} = tcp(Data),
81 decapsulate(stop, [Payload, Hdr|Packet]);
82 decapsulate({udp, Data}, Packet) when byte_size(Data) >= ?UDPHDRLEN ->
83 {Hdr, Payload} = udp(Data),
84 decapsulate(stop, [Payload, Hdr|Packet]);
85 decapsulate({icmp, Data}, Packet) when byte_size(Data) >= ?ICMPHDRLEN ->
86 {Hdr, Payload} = icmp(Data),
87 decapsulate(stop, [Payload, Hdr|Packet]);
88 decapsulate({_, Data}, Packet) ->
89 decapsulate(stop, [{truncated, Data}|Packet]).
90
f9441d2 @msantos Change the ethernet types to match naming in if_ether.h
authored
91 ether_type(?ETH_P_IP) -> ipv4;
92 ether_type(?ETH_P_IPV6) -> ipv6;
3a1efac @msantos Recursively decapsulate the packet.
authored
93 ether_type(_) -> unsupported.
965b308 @msantos Decapsulate truncated packets. Convert sniff to a gen_server.
authored
94
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
95 proto(?IPPROTO_ICMP) -> icmp;
96 proto(?IPPROTO_TCP) -> tcp;
3a1efac @msantos Recursively decapsulate the packet.
authored
97 proto(?IPPROTO_UDP) -> udp;
98 proto(_) -> unsupported.
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
99
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
100
101 %%
102 %% Ethernet
103 %%
6ee4d73 @msantos Use the #ether{} type field.
authored
104 ether(<<Dhost:6/bytes, Shost:6/bytes, Type:16, Payload/binary>>) ->
ec0ca16 @msantos pcap can't return the ethernet CRC trailer.
authored
105 % Len = byte_size(Packet) - 4,
106 % <<Payload:Len/bytes, CRC:4/bytes>> = Packet,
6ee4d73 @msantos Use the #ether{} type field.
authored
107 {#ether{
88cacab @oliv3 IPv6 support
oliv3 authored
108 dhost = Dhost, shost = Shost,
109 type = Type
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
110 }, Payload};
111 ether(#ether{
112 dhost = Dhost, shost = Shost,
113 type = Type
114 }) ->
115 <<Dhost:6/bytes, Shost:6/bytes, Type:16>>.
116
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
117
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
118 %%
119 %% IPv4
120 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
121 ipv4(
4324dcf @msantos die trailing whitespace die!
authored
122 <<4:4, HL:4, ToS:8, Len:16,
88cacab @oliv3 IPv6 support
oliv3 authored
123 Id:16, 0:1, DF:1, MF:1, %% RFC791 states it's a MUST
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
124 Off:13, TTL:8, P:8, Sum:16,
125 SA1:8, SA2:8, SA3:8, SA4:8,
126 DA1:8, DA2:8, DA3:8, DA4:8,
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
127 Payload/binary>>
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
128 ) ->
129 {#ipv4{
b29c704 @msantos ipv4 should really only handle ipv4 packets.
authored
130 hl = HL, tos = ToS, len = Len,
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
131 id = Id, df = DF, mf = MF,
132 off = Off, ttl = TTL, p = P, sum = Sum,
133 saddr = {SA1,SA2,SA3,SA4},
134 daddr = {DA1,DA2,DA3,DA4}
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
135 }, Payload};
136 ipv4(#ipv4{
137 hl = HL, tos = ToS, len = Len,
138 id = Id, df = DF, mf = MF,
139 off = Off, ttl = TTL, p = P, sum = Sum,
140 saddr = {SA1,SA2,SA3,SA4},
141 daddr = {DA1,DA2,DA3,DA4}
142 }) ->
143 <<4:4, HL:4, ToS:8, Len:16,
144 Id:16, 0:1, DF:1, MF:1, %% RFC791 states it's a MUST
145 Off:13, TTL:8, P:8, Sum:16,
146 SA1:8, SA2:8, SA3:8, SA4:8,
147 DA1:8, DA2:8, DA3:8, DA4:8>>.
148
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
149
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
150 %%
151 %% IPv6
152 %%
3a1efac @msantos Recursively decapsulate the packet.
authored
153 ipv6(
aefeb1a @msantos Incorrect size specifier for IPv6.
authored
154 <<6:4, Class:8, Flow:20,
3a1efac @msantos Recursively decapsulate the packet.
authored
155 Len:16, Next:8, Hop:8,
156 Src:128,
157 Dst:128,
158 Payload/binary>>
159 ) ->
160 {#ipv6{
161 class = Class, flow = Flow,
162 len = Len, next = Next, hop = Hop,
163 saddr = Src, daddr = Dst
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
164 }, Payload};
165 ipv6(#ipv6{
166 class = Class, flow = Flow,
167 len = Len, next = Next, hop = Hop,
168 saddr = Src, daddr = Dst
169 }) ->
170 <<6:4, Class:8, Flow:20,
171 Len:16, Next:8, Hop:8,
172 Src:128,
173 Dst:128>>.
174
3a1efac @msantos Recursively decapsulate the packet.
authored
175
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
176 %%
177 %% TCP
178 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
179 tcp(
180 <<SPort:16, DPort:16,
181 SeqNo:32,
182 AckNo:32,
183 Off:4, 0:4, CWR:1, ECE:1, URG:1, ACK:1,
184 PSH:1, RST:1, SYN:1, FIN:1, Win:16,
185 Sum:16, Urp:16,
186 Payload/binary>>
187 ) ->
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
188 {Opt, Data} = tcp_options(tcp_offset(Off), Payload),
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
189 {#tcp{
190 sport = SPort, dport = DPort,
191 seqno = SeqNo,
192 ackno = AckNo,
193 off = Off, cwr = CWR, ece = ECE, urg = URG, ack = ACK,
194 psh = PSH, rst = RST, syn = SYN, fin = FIN, win = Win,
195 sum = Sum, urp = Urp,
196 opt = Opt
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
197 }, Data};
198 tcp(#tcp{
199 sport = SPort, dport = DPort,
200 seqno = SeqNo,
201 ackno = AckNo,
202 off = Off, cwr = CWR, ece = ECE, urg = URG, ack = ACK,
203 psh = PSH, rst = RST, syn = SYN, fin = FIN, win = Win,
204 sum = Sum, urp = Urp
205 }) ->
206 <<SPort:16, DPort:16,
207 SeqNo:32,
208 AckNo:32,
209 Off:4, 0:4, CWR:1, ECE:1, URG:1, ACK:1,
210 PSH:1, RST:1, SYN:1, FIN:1, Win:16,
211 Sum:16, Urp:16>>.
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
212
213 tcp_offset(N) when N > 5 -> (N - 5) * 4;
214 tcp_offset(_) -> 0.
215
216 tcp_options(Offset, Payload) when Offset > 0 ->
217 <<Opt:Offset/bytes, Msg/binary>> = Payload,
218 {Opt, Msg};
219 tcp_options(_, Payload) ->
220 {<<>>, Payload}.
221
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
222
223 %%
224 %% UDP
225 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
226 udp(<<SPort:16, DPort:16, ULen:16, Sum:16, Payload/binary>>) ->
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
227 {#udp{sport = SPort, dport = DPort, ulen = ULen, sum = Sum}, Payload};
228 udp(#udp{sport = SPort, dport = DPort, ulen = ULen, sum = Sum}) ->
229 <<SPort:16, DPort:16, ULen:16, Sum:16>>.
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
230
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
231
232 %%
233 %% ICMP
234 %%
235
236 % Destination Unreachable Message
237 icmp(<<?ICMP_DEST_UNREACH:8, Code:8, Checksum:16, _Unused:32, Payload/binary>>) ->
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
238 {#icmp{
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
239 type = ?ICMP_DEST_UNREACH, code = Code, checksum = Checksum
240 }, Payload};
241 icmp(#icmp{
242 type = ?ICMP_DEST_UNREACH, code = Code, checksum = Checksum
243 }) ->
244 <<?ICMP_DEST_UNREACH:8, Code:8, Checksum:16>>;
245
246 % Time Exceeded Message
247 icmp(<<?ICMP_TIME_EXCEEDED:8, Code:8, Checksum:16, _Unused:32, Payload/binary>>) ->
248 {#icmp{
249 type = ?ICMP_TIME_EXCEEDED, code = Code, checksum = Checksum
250 }, Payload};
251 icmp(#icmp{
252 type = ?ICMP_TIME_EXCEEDED, code = Code, checksum = Checksum
253 }) ->
254 <<?ICMP_TIME_EXCEEDED:8, Code:8, Checksum:16>>;
255
256 % Parameter Problem Message
257 icmp(<<?ICMP_PARAMETERPROB:8, Code:8, Checksum:16, Pointer:8, _Unused:24, Payload/binary>>) ->
258 {#icmp{
259 type = ?ICMP_PARAMETERPROB, code = Code, checksum = Checksum, pointer = Pointer
260 }, Payload};
261 icmp(#icmp{
262 type = ?ICMP_PARAMETERPROB, code = Code, checksum = Checksum, pointer = Pointer
263 }) ->
264 <<?ICMP_PARAMETERPROB:8, Code:8, Checksum:16, Pointer:8>>;
265
266 % Source Quench Message
267 icmp(<<?ICMP_SOURCE_QUENCH:8, 0:8, Checksum:16, _Unused:32, Payload/binary>>) ->
268 {#icmp{
269 type = ?ICMP_SOURCE_QUENCH, code = 0, checksum = Checksum
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
270 }, Payload};
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
271 icmp(#icmp{
272 type = ?ICMP_SOURCE_QUENCH, code = Code, checksum = Checksum
273 }) ->
274 <<?ICMP_SOURCE_QUENCH:8, Code:8, Checksum:16>>;
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
275
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
276 % Redirect Message
277 icmp(<<?ICMP_REDIRECT:8, Code:8, Checksum:16, DA1, DA2, DA3, DA4, Payload/binary>>) ->
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
278 {#icmp{
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
279 type = ?ICMP_REDIRECT, code = Code, checksum = Checksum, gateway = {DA1,DA2,DA3,DA4}
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
280 }, Payload};
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
281 icmp(#icmp{
282 type = ?ICMP_REDIRECT, code = Code, checksum = Checksum, gateway = {DA1,DA2,DA3,DA4}
283 }) ->
284 <<?ICMP_REDIRECT:8, Code:8, Checksum:16, DA1, DA2, DA3, DA4>>;
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
285
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
286 % Echo or Echo Reply Message
1d97e93 @msantos Specification for ECHO REPLY and defaults for icmp record
authored
287 icmp(<<Type:8, Code:8, Checksum:16, Id:16, Sequence:16, Payload/binary>>)
3f1dc79 @msantos Typo in ICMP packet type
authored
288 when Type =:= ?ICMP_ECHO; Type =:= ?ICMP_ECHOREPLY ->
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
289 {#icmp{
1d97e93 @msantos Specification for ECHO REPLY and defaults for icmp record
authored
290 type = Type, code = Code, checksum = Checksum, id = Id,
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
291 sequence = Sequence
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
292 }, Payload};
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
293 icmp(#icmp{
1d97e93 @msantos Specification for ECHO REPLY and defaults for icmp record
authored
294 type = Type, code = Code, checksum = Checksum, id = Id,
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
295 sequence = Sequence
1d97e93 @msantos Specification for ECHO REPLY and defaults for icmp record
authored
296 })
3f1dc79 @msantos Typo in ICMP packet type
authored
297 when Type =:= ?ICMP_ECHO; Type =:= ?ICMP_ECHOREPLY ->
1d97e93 @msantos Specification for ECHO REPLY and defaults for icmp record
authored
298 <<Type:8, Code:8, Checksum:16, Id:16, Sequence:16>>;
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
299
300 % Timestamp or Timestamp Reply Message
301 icmp(<<Type:8, 0:8, Checksum:16, Id:16, Sequence:16, TS_Orig:32, TS_Recv:32, TS_Tx:32>>)
302 when Type =:= ?ICMP_TIMESTAMP; Type =:= ?ICMP_TIMESTAMPREPLY ->
303 {#icmp{
304 type = Type, code = 0, checksum = Checksum, id = Id,
305 sequence = Sequence, ts_orig = TS_Orig, ts_recv = TS_Recv, ts_tx = TS_Tx
306 }, <<>>};
307 icmp(#icmp{
308 type = Type, code = Code, checksum = Checksum, id = Id,
309 sequence = Sequence, ts_orig = TS_Orig, ts_recv = TS_Recv, ts_tx = TS_Tx
310 }) when Type =:= ?ICMP_TIMESTAMP; Type =:= ?ICMP_TIMESTAMPREPLY ->
311 <<Type:8, Code:8, Checksum:16, Id:16, Sequence:16, TS_Orig:32, TS_Recv:32, TS_Tx:32>>;
312
313 % Information Request or Information Reply Message
314 icmp(<<Type:8, 0:8, Checksum:16, Id:16, Sequence:16>>)
315 when Type =:= ?ICMP_INFO_REQUEST; Type =:= ?ICMP_INFO_REPLY ->
316 {#icmp{
317 type = Type, code = 0, checksum = Checksum, id = Id,
318 sequence = Sequence
319 }, <<>>};
320 icmp(#icmp{
321 type = Type, code = Code, checksum = Checksum, id = Id,
322 sequence = Sequence
323 }) when Type =:= ?ICMP_INFO_REQUEST; Type =:= ?ICMP_INFO_REPLY ->
324 <<Type:8, Code:8, Checksum:16, Id:16, Sequence:16>>;
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
325
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
326 % Catch/build arbitrary types
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
327 icmp(<<Type:8, Code:8, Checksum:16, Un:32, Payload/binary>>) ->
328 {#icmp{
329 type = Type, code = Code, checksum = Checksum, un = Un
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
330 }, Payload};
331 icmp(#icmp{type = Type, code = Code, checksum = Checksum, un = Un}) ->
332 <<Type:8, Code:8, Checksum:16, Un:32>>.
333
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
334
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
335 %%
336 %% Utility functions
337 %%
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
338 payload(Payload) ->
339 [ to_ascii(C) || <<C:8>> <= Payload ].
340
341
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
342 % TCP pseudoheader checksum
8e1b1b8 @msantos Pass ipv4 packet as a list to checksum
authored
343 checksum([#ipv4{
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
344 saddr = {SA1,SA2,SA3,SA4},
814d769 @msantos Specify the protocol in the checksum
authored
345 daddr = {DA1,DA2,DA3,DA4}
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
346 },
347 #tcp{
348 off = Off
349 } = TCPhdr,
350 Payload
8e1b1b8 @msantos Pass ipv4 packet as a list to checksum
authored
351 ]) ->
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
352 Len = Off * 4,
353 TCP = tcp(TCPhdr#tcp{sum = 0}),
354 Pad = case Len rem 2 of
355 0 -> 0;
356 1 -> 8
357 end,
358 checksum(
359 list_to_binary([
360 <<SA1,SA2,SA3,SA4,
361 DA1,DA2,DA3,DA4,
362 0:8,
814d769 @msantos Specify the protocol in the checksum
authored
363 ?IPPROTO_TCP:8,
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
364 Len:16>>,
365 TCP,
366 Payload,
367 <<0:Pad>>
368 ]));
369
370 % UDP pseudoheader checksum
8e1b1b8 @msantos Pass ipv4 packet as a list to checksum
authored
371 checksum([#ipv4{
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
372 saddr = {SA1,SA2,SA3,SA4},
814d769 @msantos Specify the protocol in the checksum
authored
373 daddr = {DA1,DA2,DA3,DA4}
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
374 },
375 #udp{
376 sport = SPort,
377 dport = DPort,
378 ulen = Len
379 },
380 Payload
8e1b1b8 @msantos Pass ipv4 packet as a list to checksum
authored
381 ]) ->
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
382 Pad = case Len rem 2 of
383 0 -> 0;
384 1 -> 8
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
385 end,
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
386 checksum(
387 list_to_binary([
388 <<SA1,SA2,SA3,SA4,
389 DA1,DA2,DA3,DA4,
390 0:8,
814d769 @msantos Specify the protocol in the checksum
authored
391 ?IPPROTO_UDP:8,
7bd31ac @msantos Add record to binary conversion and ICMP types
authored
392 Len:16,
393
394 SPort:16,
395 DPort:16,
396 Len:16,
397 0:16,
398 Payload/binary,
399 0:Pad>>
8e1b1b8 @msantos Pass ipv4 packet as a list to checksum
authored
400 ]));
401
402 checksum(#ipv4{} = H) ->
403 checksum(ipv4(H));
404 checksum(Hdr) ->
405 lists:foldl(fun compl/2, 0, [ W || <<W:16>> <= Hdr ]).
0406018 @msantos Add licensing and do cleanup. In other words, do stuff.
authored
406
407 makesum(Hdr) -> 16#FFFF - checksum(Hdr).
408
409 compl(N) when N =< 16#FFFF -> N;
410 compl(N) -> (N band 16#FFFF) + (N bsr 16).
411 compl(N,S) -> compl(N+S).
412
413 valid(16#FFFF) -> true;
414 valid(_) -> false.
415
416 to_ascii(C) when ?is_print(C) -> C;
417 to_ascii(_) -> $..
418
419 ether_addr(B) when is_binary(B) ->
420 ether_addr(binary_to_list(B));
421 ether_addr(L) when is_list(L) ->
422 [ hd(io_lib:format("~.16B", [N])) || N <- L ].
423
424 tcp_flags(#tcp{cwr = CWR, ece = ECE, urg = URG, ack = ACK,
425 psh = PSH, rst = RST, syn = SYN, fin = FIN}) ->
426 [ atom_to_list(F) || {F,V} <-
427 [{cwr,CWR}, {ece,ECE}, {urg,URG}, {ack,ACK}, {psh,PSH}, {rst,RST}, {syn,SYN}, {fin,FIN}], V =:= 1 ].
428
429
Something went wrong with that request. Please try again.