-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPcap.hs
More file actions
69 lines (53 loc) · 1.86 KB
/
Pcap.hs
File metadata and controls
69 lines (53 loc) · 1.86 KB
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
module Pcap where
import Data.ByteString as BS (ByteString)
import Data.ByteString.Lazy as LBS (ByteString, fromStrict)
import Data.Binary
import Data.Binary.Get
import Text.Bytedump
import Network.Pcap (PktHdr(..), Link(..), PcapHandle, nextBS)
import Datalink
import Network
import Transport
data Pcap = Pcap
{ _pcapHdr :: !PktHdr
, _datalink :: !Datalink
, _network :: !Network
, _transport :: !Transport
, _payload :: LBS.ByteString
}
deriving (Show)
-- |'nextPcap' reads the next packet from 'PcapHandle' if there is more data.
nextPcap :: PcapHandle -> Link -> IO (Maybe Pcap)
nextPcap h link = do
(p, q) <- nextBS h
if hdrWireLength p == 0
then
return Nothing
else
return $! Just . runGet (pcap p link) . fromStrict $ q
-- |Decode a pcap packet.
pcap :: PktHdr -> Link -> Get Pcap
pcap hdr link = do
dl <- datalink link
net <- network dl
trans <- transport net
pload <- getRemainingLazyByteString
return $! Pcap hdr dl net trans pload
-- Useful for dumping raw packets for debugging
class Pretty a where
pretty :: a -> String
instance Pretty Pcap where
pretty (Pcap hdr dl net trans pload) = unlines [show hdr, pretty dl, pretty net, pretty trans, pretty pload]
instance Pretty Datalink where
pretty (Ethernet frame) = show frame
pretty (UnsupportedDatalink link raw) = "UnsupportedDatalink " ++ show link ++ "\n" ++ pretty raw
instance Pretty Network where
pretty (IP4 p) = show p
pretty (UnsupportedNetwork net raw) = "UnsupportedNetwork " ++ show net ++ "\n" ++ pretty raw
instance Pretty Transport where
pretty (UDP p) = show p
pretty (UnsupportedTransport t raw) = "UnsupportedTransport " ++ show t ++ "\n" ++ pretty raw
instance Pretty BS.ByteString where
pretty = dumpBS
instance Pretty LBS.ByteString where
pretty = dumpLBS