Skip to content
This repository
Newer
Older
100644 508 lines (453 sloc) 14.982 kb
13e9d010 »
2010-09-11 Update copyright date
1 %% Copyright (c) 2009-2010, Michael Santos <michael.santos@gmail.com>
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
2 %% All rights reserved.
4324dcf9 »
2010-02-19 die trailing whitespace die!
3 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
4 %% Redistribution and use in source and binary forms, with or without
5 %% modification, are permitted provided that the following conditions
6 %% are met:
4324dcf9 »
2010-02-19 die trailing whitespace die!
7 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
8 %% Redistributions of source code must retain the above copyright
9 %% notice, this list of conditions and the following disclaimer.
4324dcf9 »
2010-02-19 die trailing whitespace die!
10 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
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.
4324dcf9 »
2010-02-19 die trailing whitespace die!
14 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
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.
4324dcf9 »
2010-02-19 die trailing whitespace die!
18 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
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.
bbd5d9c8 »
2010-12-28 Add comment pointing to pkt
31
32 %%% Code has moved to:
33 %%% https://github.com/msantos/pkt
34
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
35 -module(epcap_net).
36
37 -include("epcap_net.hrl").
38
965b308e »
2010-01-06 Decapsulate truncated packets. Convert sniff to a gen_server.
39 -define(ETHERHDRLEN, 16).
40 -define(IPV4HDRLEN, 20).
88cacab5 »
2010-01-22 IPv6 support
41 -define(IPV6HDRLEN, 40).
965b308e »
2010-01-06 Decapsulate truncated packets. Convert sniff to a gen_server.
42 -define(TCPHDRLEN, 20).
43 -define(UDPHDRLEN, 8).
44 -define(ICMPHDRLEN, 8).
45
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
46 -export([
47 checksum/1,
48 decapsulate/1,
49 makesum/1,
50 valid/1,
51 ether/1,
52 ether_addr/1,
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
53 ether_type/1,
dac39737 »
2010-10-22 Add support for arp
54 arp/1,
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
55 icmp/1,
56 ipv4/1,
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
57 ipv6/1,
1fac0f79 »
2009-12-04 Now compiler friendly, typo free!
58 payload/1,
965b308e »
2010-01-06 Decapsulate truncated packets. Convert sniff to a gen_server.
59 proto/1,
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
60 tcp/1,
61 tcp_flags/1,
62 udp/1
63 ]).
64
65 -define(is_print(C), C >= $ , C =< $~).
66
6ee4d73b »
2010-01-22 Use the #ether{} type field.
67
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
68 decapsulate(Data) ->
69 decapsulate({ether, Data}, []).
70
71 decapsulate(stop, Packet) ->
72 lists:reverse(Packet);
73 decapsulate({unsupported, Data}, Packet) ->
74 decapsulate(stop, [{unsupported, Data}|Packet]);
75 decapsulate({ether, Data}, Packet) when byte_size(Data) >= ?ETHERHDRLEN ->
76 {Hdr, Payload} = ether(Data),
77 decapsulate({ether_type(Hdr#ether.type), Payload}, [Hdr|Packet]);
dac39737 »
2010-10-22 Add support for arp
78 decapsulate({arp, Data}, Packet) when byte_size(Data) >= 28 -> % IPv4 ARP
79 {Hdr, Payload} = arp(Data),
80 decapsulate(stop, [Payload, Hdr|Packet]);
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
81 decapsulate({ipv4, Data}, Packet) when byte_size(Data) >= ?IPV4HDRLEN ->
82 {Hdr, Payload} = ipv4(Data),
83 decapsulate({proto(Hdr#ipv4.p), Payload}, [Hdr|Packet]);
84 decapsulate({ipv6, Data}, Packet) when byte_size(Data) >= ?IPV6HDRLEN ->
85 {Hdr, Payload} = ipv6(Data),
86 decapsulate({proto(Hdr#ipv6.next), Payload}, [Hdr|Packet]);
87 decapsulate({tcp, Data}, Packet) when byte_size(Data) >= ?TCPHDRLEN ->
88 {Hdr, Payload} = tcp(Data),
89 decapsulate(stop, [Payload, Hdr|Packet]);
90 decapsulate({udp, Data}, Packet) when byte_size(Data) >= ?UDPHDRLEN ->
91 {Hdr, Payload} = udp(Data),
92 decapsulate(stop, [Payload, Hdr|Packet]);
6e10beb4 »
2010-08-05 Add simplistic SCTP support (only DATA chunks are parsed in erlang re…
93 decapsulate({sctp, Data}, Packet) when byte_size(Data) >= 12 ->
94 {Hdr, Payload} = sctp(Data),
95 decapsulate(stop, [Payload, Hdr|Packet]);
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
96 decapsulate({icmp, Data}, Packet) when byte_size(Data) >= ?ICMPHDRLEN ->
97 {Hdr, Payload} = icmp(Data),
98 decapsulate(stop, [Payload, Hdr|Packet]);
99 decapsulate({_, Data}, Packet) ->
100 decapsulate(stop, [{truncated, Data}|Packet]).
101
f9441d2c »
2010-06-06 Change the ethernet types to match naming in if_ether.h
102 ether_type(?ETH_P_IP) -> ipv4;
103 ether_type(?ETH_P_IPV6) -> ipv6;
dac39737 »
2010-10-22 Add support for arp
104 ether_type(?ETH_P_ARP) -> arp;
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
105 ether_type(_) -> unsupported.
965b308e »
2010-01-06 Decapsulate truncated packets. Convert sniff to a gen_server.
106
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
107 proto(?IPPROTO_ICMP) -> icmp;
108 proto(?IPPROTO_TCP) -> tcp;
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
109 proto(?IPPROTO_UDP) -> udp;
6e10beb4 »
2010-08-05 Add simplistic SCTP support (only DATA chunks are parsed in erlang re…
110 proto(?IPPROTO_SCTP) -> sctp;
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
111 proto(_) -> unsupported.
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
112
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
113
114 %%
115 %% Ethernet
116 %%
6ee4d73b »
2010-01-22 Use the #ether{} type field.
117 ether(<<Dhost:6/bytes, Shost:6/bytes, Type:16, Payload/binary>>) ->
ec0ca161 »
2010-01-06 pcap can't return the ethernet CRC trailer.
118 % Len = byte_size(Packet) - 4,
119 % <<Payload:Len/bytes, CRC:4/bytes>> = Packet,
6ee4d73b »
2010-01-22 Use the #ether{} type field.
120 {#ether{
88cacab5 »
2010-01-22 IPv6 support
121 dhost = Dhost, shost = Shost,
122 type = Type
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
123 }, Payload};
124 ether(#ether{
125 dhost = Dhost, shost = Shost,
126 type = Type
127 }) ->
128 <<Dhost:6/bytes, Shost:6/bytes, Type:16>>.
129
dac39737 »
2010-10-22 Add support for arp
130 %%
131 %% ARP
132 %%
133 arp(<<Hrd:16, Pro:16,
134 Hln:8, Pln:8, Op:16,
135 Sha:6/bytes,
136 SA1:8, SA2:8, SA3:8, SA4:8,
137 Tha:6/bytes,
138 DA1:8, DA2:8, DA3:8, DA4:8,
139 Payload/binary>>
140 ) ->
141 {#arp{
142 hrd = Hrd, pro = Pro,
143 hln = Hln, pln = Pln, op = Op,
144 sha = Sha,
145 sip = {SA1,SA2,SA3,SA4},
146 tha = Tha,
147 tip = {DA1,DA2,DA3,DA4}
148 }, Payload};
149 arp(#arp{
150 hrd = Hrd, pro = Pro,
151 hln = Hln, pln = Pln, op = Op,
152 sha = Sha,
153 sip = {SA1,SA2,SA3,SA4},
154 tha = Tha,
155 tip = {DA1,DA2,DA3,DA4}
156 }) ->
157 <<Hrd:16, Pro:16,
158 Hln:8, Pln:8, Op:16,
159 Sha:6/bytes,
160 SA1:8, SA2:8, SA3:8, SA4:8,
161 Tha:6/bytes,
162 DA1:8, DA2:8, DA3:8, DA4:8>>.
163
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
164
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
165 %%
166 %% IPv4
167 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
168 ipv4(
4324dcf9 »
2010-02-19 die trailing whitespace die!
169 <<4:4, HL:4, ToS:8, Len:16,
88cacab5 »
2010-01-22 IPv6 support
170 Id:16, 0:1, DF:1, MF:1, %% RFC791 states it's a MUST
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
171 Off:13, TTL:8, P:8, Sum:16,
172 SA1:8, SA2:8, SA3:8, SA4:8,
173 DA1:8, DA2:8, DA3:8, DA4:8,
0ef9351f »
2010-11-21 IPv4 options
174 Rest/binary>>
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
175 ) ->
0ef9351f »
2010-11-21 IPv4 options
176 {Opt, Payload} = options(HL, Rest),
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
177 {#ipv4{
b29c7049 »
2010-01-22 ipv4 should really only handle ipv4 packets.
178 hl = HL, tos = ToS, len = Len,
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
179 id = Id, df = DF, mf = MF,
180 off = Off, ttl = TTL, p = P, sum = Sum,
181 saddr = {SA1,SA2,SA3,SA4},
0ef9351f »
2010-11-21 IPv4 options
182 daddr = {DA1,DA2,DA3,DA4},
183 opt = Opt
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
184 }, Payload};
185 ipv4(#ipv4{
186 hl = HL, tos = ToS, len = Len,
187 id = Id, df = DF, mf = MF,
188 off = Off, ttl = TTL, p = P, sum = Sum,
189 saddr = {SA1,SA2,SA3,SA4},
190 daddr = {DA1,DA2,DA3,DA4}
191 }) ->
192 <<4:4, HL:4, ToS:8, Len:16,
193 Id:16, 0:1, DF:1, MF:1, %% RFC791 states it's a MUST
194 Off:13, TTL:8, P:8, Sum:16,
195 SA1:8, SA2:8, SA3:8, SA4:8,
196 DA1:8, DA2:8, DA3:8, DA4:8>>.
197
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
198
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
199 %%
200 %% IPv6
201 %%
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
202 ipv6(
aefeb1af »
2010-01-25 Incorrect size specifier for IPv6.
203 <<6:4, Class:8, Flow:20,
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
204 Len:16, Next:8, Hop:8,
205 Src:128,
206 Dst:128,
207 Payload/binary>>
208 ) ->
209 {#ipv6{
210 class = Class, flow = Flow,
211 len = Len, next = Next, hop = Hop,
212 saddr = Src, daddr = Dst
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
213 }, Payload};
214 ipv6(#ipv6{
215 class = Class, flow = Flow,
216 len = Len, next = Next, hop = Hop,
217 saddr = Src, daddr = Dst
218 }) ->
219 <<6:4, Class:8, Flow:20,
220 Len:16, Next:8, Hop:8,
221 Src:128,
222 Dst:128>>.
223
3a1efacc »
2010-01-22 Recursively decapsulate the packet.
224
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
225 %%
226 %% TCP
227 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
228 tcp(
229 <<SPort:16, DPort:16,
230 SeqNo:32,
231 AckNo:32,
232 Off:4, 0:4, CWR:1, ECE:1, URG:1, ACK:1,
233 PSH:1, RST:1, SYN:1, FIN:1, Win:16,
234 Sum:16, Urp:16,
0ef9351f »
2010-11-21 IPv4 options
235 Rest/binary>>
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
236 ) ->
0ef9351f »
2010-11-21 IPv4 options
237 {Opt, Payload} = options(Off, Rest),
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
238 {#tcp{
239 sport = SPort, dport = DPort,
240 seqno = SeqNo,
241 ackno = AckNo,
242 off = Off, cwr = CWR, ece = ECE, urg = URG, ack = ACK,
243 psh = PSH, rst = RST, syn = SYN, fin = FIN, win = Win,
244 sum = Sum, urp = Urp,
245 opt = Opt
0ef9351f »
2010-11-21 IPv4 options
246 }, Payload};
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
247 tcp(#tcp{
248 sport = SPort, dport = DPort,
249 seqno = SeqNo,
250 ackno = AckNo,
251 off = Off, cwr = CWR, ece = ECE, urg = URG, ack = ACK,
252 psh = PSH, rst = RST, syn = SYN, fin = FIN, win = Win,
253 sum = Sum, urp = Urp
254 }) ->
255 <<SPort:16, DPort:16,
256 SeqNo:32,
257 AckNo:32,
258 Off:4, 0:4, CWR:1, ECE:1, URG:1, ACK:1,
259 PSH:1, RST:1, SYN:1, FIN:1, Win:16,
260 Sum:16, Urp:16>>.
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
261
0ef9351f »
2010-11-21 IPv4 options
262 options(Offset, Payload) when Offset > 5 ->
263 N = (Offset-5)*4,
264 <<Opt:N/binary, Payload1/binary>> = Payload,
265 {Opt, Payload1};
266 options(_, Payload) ->
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
267 {<<>>, Payload}.
268
6e10beb4 »
2010-08-05 Add simplistic SCTP support (only DATA chunks are parsed in erlang re…
269 %%
270 %% SCTP
271 %%
272 sctp(<<SPort:16, DPort:16, VTag:32, Sum:32, Payload/binary>>) ->
273 {#sctp{sport = SPort, dport = DPort, vtag = VTag, sum = Sum,
274 chunks=sctp_chunk_list_gen(Payload)}, []}.
275
276 sctp_chunk_list_gen(Payload) ->
277 sctp_chunk_list_gen(Payload, []).
278
279 sctp_chunk_list_gen(Payload, List) ->
280 % chop the first chunk off the payload
281 case sctp_chunk_chop(Payload) of
282 {Chunk, Remainder} ->
283 % loop
284 sctp_chunk_list_gen(Remainder, [Chunk|List]);
285 [] ->
286 List
287 end.
288
289 sctp_chunk_chop(<<>>) ->
290 [];
291 sctp_chunk_chop(<<Ctype:8, Cflags:8, Clen:16, Remainder/binary>>) ->
292 Payload = binary:part(Remainder, 0, Clen-4),
293 Tail = binary:part(Remainder, Clen-4, byte_size(Remainder)-(Clen-4)),
294 {sctp_chunk(Ctype, Cflags, Clen, Payload), Tail}.
295
296 sctp_chunk(Ctype, Cflags, Clen, Payload) ->
297 #sctp_chunk{type=Ctype, flags=Cflags, len = Clen-4,
298 payload=sctp_chunk_payload(Ctype, Payload)}.
299
300 sctp_chunk_payload(0, <<Tsn:32, Sid:16, Ssn:16, Ppi:32, Data/binary>>) ->
301 #sctp_chunk_data{tsn=Tsn, sid=Sid, ssn=Ssn, ppi=Ppi, data=Data};
302 sctp_chunk_payload(_, Data) ->
303 Data.
304
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
305
306 %%
307 %% UDP
308 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
309 udp(<<SPort:16, DPort:16, ULen:16, Sum:16, Payload/binary>>) ->
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
310 {#udp{sport = SPort, dport = DPort, ulen = ULen, sum = Sum}, Payload};
311 udp(#udp{sport = SPort, dport = DPort, ulen = ULen, sum = Sum}) ->
312 <<SPort:16, DPort:16, ULen:16, Sum:16>>.
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
313
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
314
315 %%
316 %% ICMP
317 %%
318
319 % Destination Unreachable Message
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
320 icmp(<<?ICMP_DEST_UNREACH:8, Code:8, Checksum:16, Unused:32/bits, Payload/binary>>) ->
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
321 {#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
322 type = ?ICMP_DEST_UNREACH, code = Code, checksum = Checksum, un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
323 }, Payload};
324 icmp(#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
325 type = ?ICMP_DEST_UNREACH, code = Code, checksum = Checksum, un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
326 }) ->
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
327 <<?ICMP_DEST_UNREACH:8, Code:8, Checksum:16, Unused:32/bits>>;
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
328
329 % Time Exceeded Message
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
330 icmp(<<?ICMP_TIME_EXCEEDED:8, Code:8, Checksum:16, Unused:32/bits, Payload/binary>>) ->
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
331 {#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
332 type = ?ICMP_TIME_EXCEEDED, code = Code, checksum = Checksum, un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
333 }, Payload};
334 icmp(#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
335 type = ?ICMP_TIME_EXCEEDED, code = Code, checksum = Checksum, un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
336 }) ->
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
337 <<?ICMP_TIME_EXCEEDED:8, Code:8, Checksum:16, Unused:32/bits>>;
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
338
339 % Parameter Problem Message
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
340 icmp(<<?ICMP_PARAMETERPROB:8, Code:8, Checksum:16, Pointer:8, Unused:24/bits, Payload/binary>>) ->
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
341 {#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
342 type = ?ICMP_PARAMETERPROB, code = Code, checksum = Checksum, pointer = Pointer,
343 un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
344 }, Payload};
345 icmp(#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
346 type = ?ICMP_PARAMETERPROB, code = Code, checksum = Checksum, pointer = Pointer,
347 un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
348 }) ->
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
349 <<?ICMP_PARAMETERPROB:8, Code:8, Checksum:16, Pointer:8, Unused:24/bits>>;
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
350
351 % Source Quench Message
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
352 icmp(<<?ICMP_SOURCE_QUENCH:8, 0:8, Checksum:16, Unused:32/bits, Payload/binary>>) ->
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
353 {#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
354 type = ?ICMP_SOURCE_QUENCH, code = 0, checksum = Checksum, un = Unused
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
355 }, Payload};
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
356 icmp(#icmp{
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
357 type = ?ICMP_SOURCE_QUENCH, code = Code, checksum = Checksum, un = Unused
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
358 }) ->
b51a4a26 »
2010-12-10 Include ICMP unused when constructing packet
359 <<?ICMP_SOURCE_QUENCH:8, Code:8, Checksum:16, Unused:32/bits>>;
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
360
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
361 % Redirect Message
362 icmp(<<?ICMP_REDIRECT:8, Code:8, Checksum:16, DA1, DA2, DA3, DA4, Payload/binary>>) ->
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
363 {#icmp{
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
364 type = ?ICMP_REDIRECT, code = Code, checksum = Checksum, gateway = {DA1,DA2,DA3,DA4}
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
365 }, Payload};
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
366 icmp(#icmp{
367 type = ?ICMP_REDIRECT, code = Code, checksum = Checksum, gateway = {DA1,DA2,DA3,DA4}
368 }) ->
369 <<?ICMP_REDIRECT:8, Code:8, Checksum:16, DA1, DA2, DA3, DA4>>;
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
370
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
371 % Echo or Echo Reply Message
1d97e933 »
2010-06-26 Specification for ECHO REPLY and defaults for icmp record
372 icmp(<<Type:8, Code:8, Checksum:16, Id:16, Sequence:16, Payload/binary>>)
3f1dc79b »
2010-06-26 Typo in ICMP packet type
373 when Type =:= ?ICMP_ECHO; Type =:= ?ICMP_ECHOREPLY ->
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
374 {#icmp{
1d97e933 »
2010-06-26 Specification for ECHO REPLY and defaults for icmp record
375 type = Type, code = Code, checksum = Checksum, id = Id,
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
376 sequence = Sequence
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
377 }, Payload};
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
378 icmp(#icmp{
1d97e933 »
2010-06-26 Specification for ECHO REPLY and defaults for icmp record
379 type = Type, code = Code, checksum = Checksum, id = Id,
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
380 sequence = Sequence
1d97e933 »
2010-06-26 Specification for ECHO REPLY and defaults for icmp record
381 })
3f1dc79b »
2010-06-26 Typo in ICMP packet type
382 when Type =:= ?ICMP_ECHO; Type =:= ?ICMP_ECHOREPLY ->
1d97e933 »
2010-06-26 Specification for ECHO REPLY and defaults for icmp record
383 <<Type:8, Code:8, Checksum:16, Id:16, Sequence:16>>;
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
384
385 % Timestamp or Timestamp Reply Message
386 icmp(<<Type:8, 0:8, Checksum:16, Id:16, Sequence:16, TS_Orig:32, TS_Recv:32, TS_Tx:32>>)
387 when Type =:= ?ICMP_TIMESTAMP; Type =:= ?ICMP_TIMESTAMPREPLY ->
388 {#icmp{
389 type = Type, code = 0, checksum = Checksum, id = Id,
390 sequence = Sequence, ts_orig = TS_Orig, ts_recv = TS_Recv, ts_tx = TS_Tx
391 }, <<>>};
392 icmp(#icmp{
393 type = Type, code = Code, checksum = Checksum, id = Id,
394 sequence = Sequence, ts_orig = TS_Orig, ts_recv = TS_Recv, ts_tx = TS_Tx
395 }) when Type =:= ?ICMP_TIMESTAMP; Type =:= ?ICMP_TIMESTAMPREPLY ->
396 <<Type:8, Code:8, Checksum:16, Id:16, Sequence:16, TS_Orig:32, TS_Recv:32, TS_Tx:32>>;
397
398 % Information Request or Information Reply Message
399 icmp(<<Type:8, 0:8, Checksum:16, Id:16, Sequence:16>>)
400 when Type =:= ?ICMP_INFO_REQUEST; Type =:= ?ICMP_INFO_REPLY ->
401 {#icmp{
402 type = Type, code = 0, checksum = Checksum, id = Id,
403 sequence = Sequence
404 }, <<>>};
405 icmp(#icmp{
406 type = Type, code = Code, checksum = Checksum, id = Id,
407 sequence = Sequence
408 }) when Type =:= ?ICMP_INFO_REQUEST; Type =:= ?ICMP_INFO_REPLY ->
409 <<Type:8, Code:8, Checksum:16, Id:16, Sequence:16>>;
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
410
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
411 % Catch/build arbitrary types
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
412 icmp(<<Type:8, Code:8, Checksum:16, Un:32, Payload/binary>>) ->
413 {#icmp{
414 type = Type, code = Code, checksum = Checksum, un = Un
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
415 }, Payload};
416 icmp(#icmp{type = Type, code = Code, checksum = Checksum, un = Un}) ->
417 <<Type:8, Code:8, Checksum:16, Un:32>>.
418
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
419
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
420 %%
421 %% Utility functions
422 %%
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
423 payload(Payload) ->
424 [ to_ascii(C) || <<C:8>> <= Payload ].
425
426
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
427 % TCP pseudoheader checksum
8e1b1b8b »
2010-07-09 Pass ipv4 packet as a list to checksum
428 checksum([#ipv4{
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
429 saddr = {SA1,SA2,SA3,SA4},
814d769a »
2010-06-26 Specify the protocol in the checksum
430 daddr = {DA1,DA2,DA3,DA4}
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
431 },
432 #tcp{
433 off = Off
434 } = TCPhdr,
435 Payload
8e1b1b8b »
2010-07-09 Pass ipv4 packet as a list to checksum
436 ]) ->
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
437 Len = Off * 4,
438 TCP = tcp(TCPhdr#tcp{sum = 0}),
439 Pad = case Len rem 2 of
440 0 -> 0;
441 1 -> 8
442 end,
443 checksum(
b1fd14b7 »
2010-11-20 Clean up the checksumming
444 <<SA1,SA2,SA3,SA4,
445 DA1,DA2,DA3,DA4,
446 0:8,
447 ?IPPROTO_TCP:8,
448 Len:16,
449 TCP/binary,
450 Payload/bits,
451 0:Pad>>
452 );
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
453
454 % UDP pseudoheader checksum
8e1b1b8b »
2010-07-09 Pass ipv4 packet as a list to checksum
455 checksum([#ipv4{
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
456 saddr = {SA1,SA2,SA3,SA4},
814d769a »
2010-06-26 Specify the protocol in the checksum
457 daddr = {DA1,DA2,DA3,DA4}
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
458 },
459 #udp{
460 ulen = Len
b1fd14b7 »
2010-11-20 Clean up the checksumming
461 } = Hdr,
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
462 Payload
8e1b1b8b »
2010-07-09 Pass ipv4 packet as a list to checksum
463 ]) ->
b1fd14b7 »
2010-11-20 Clean up the checksumming
464 UDP = udp(Hdr#udp{sum = 0}),
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
465 Pad = case Len rem 2 of
466 0 -> 0;
467 1 -> 8
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
468 end,
7bd31ac8 »
2010-06-26 Add record to binary conversion and ICMP types
469 checksum(
b1fd14b7 »
2010-11-20 Clean up the checksumming
470 <<SA1,SA2,SA3,SA4,
471 DA1,DA2,DA3,DA4,
472 0:8,
473 ?IPPROTO_UDP:8,
474 Len:16,
475 UDP/binary,
476 Payload/bits,
477 0:Pad>>
478 );
8e1b1b8b »
2010-07-09 Pass ipv4 packet as a list to checksum
479
480 checksum(#ipv4{} = H) ->
481 checksum(ipv4(H));
482 checksum(Hdr) ->
483 lists:foldl(fun compl/2, 0, [ W || <<W:16>> <= Hdr ]).
04060181 »
2009-12-03 Add licensing and do cleanup. In other words, do stuff.
484
485 makesum(Hdr) -> 16#FFFF - checksum(Hdr).
486
487 compl(N) when N =< 16#FFFF -> N;
488 compl(N) -> (N band 16#FFFF) + (N bsr 16).
489 compl(N,S) -> compl(N+S).
490
491 valid(16#FFFF) -> true;
492 valid(_) -> false.
493
494 to_ascii(C) when ?is_print(C) -> C;
495 to_ascii(_) -> $..
496
497 ether_addr(B) when is_binary(B) ->
498 ether_addr(binary_to_list(B));
499 ether_addr(L) when is_list(L) ->
500 [ hd(io_lib:format("~.16B", [N])) || N <- L ].
501
502 tcp_flags(#tcp{cwr = CWR, ece = ECE, urg = URG, ack = ACK,
503 psh = PSH, rst = RST, syn = SYN, fin = FIN}) ->
504 [ atom_to_list(F) || {F,V} <-
505 [{cwr,CWR}, {ece,ECE}, {urg,URG}, {ack,ACK}, {psh,PSH}, {rst,RST}, {syn,SYN}, {fin,FIN}], V =:= 1 ].
506
507
Something went wrong with that request. Please try again.