streaming-pcap 1.1.1 → 1.1.1.1
raw patch · 8 files changed
+115/−149 lines, 8 filesdep +resourcetdep +streaming-attoparsecdep −criteriondep −streaming-utilsdep ~attoparsecdep ~basedep ~pcap
Dependencies added: resourcet, streaming-attoparsec
Dependencies removed: criterion, streaming-utils
Dependency ranges changed: attoparsec, base, pcap, streaming-bytestring, tasty
Files
- Network/Pcap/Streaming.hs +0/−47
- Network/Pcap/Streaming/Internal.hs +0/−25
- bench/Bench.hs +0/−16
- lib/Network/Pcap/Streaming.hs +48/−0
- lib/Network/Pcap/Streaming/Internal.hs +25/−0
- streaming-pcap.cabal +41/−60
- test/Test.hs +1/−1
- test/tsuru.pcap too large to diff
− Network/Pcap/Streaming.hs
@@ -1,47 +0,0 @@--- |--- Module : Network.Pcap.Streaming--- Copyright : (c) Colin Woodbury, 2018--- License : BSD3--- Maintainer: Colin Woodbury <colingw@gmail.com>------ A streaming interface to the <http://hackage.haskell.org/package/pcap-0.4.5.2 pcap>--- Haskell library, which itself is a binding to /libpcap/. Humbly adapted from--- <http://hackage.haskell.org/package/pcap-conduit pcap-conduit> by Austin Seipp.--module Network.Pcap.Streaming- ( Packet(..)- , PktHdr(..)- , offline- , online- ) where--import qualified Data.Attoparsec.ByteString.Streaming as A-import qualified Data.ByteString.Streaming as Q-import Data.Int (Int64)-import Network.Pcap-import Network.Pcap.Streaming.Internal-import Streaming-import qualified Streaming.Prelude as P--------- | Read `Packet`s from some file dump. Uses a custom parser, not /libpcap/.------ /SPECIALIZE/d for @ResourceT IO@.-offline :: MonadResource m => FilePath -> Stream (Of Packet) m ()-offline = void . A.parsed packetP . Q.drop 24 . Q.readFile-{-# SPECIALIZE offline :: FilePath -> Stream (Of Packet) (ResourceT IO) () #-}---- | Read `Packet`s from some network device. See `Network.Pcap.openLive`--- for a description of each argument.------ /SPECIALIZE/d for @IO@.-online :: MonadIO m => String -> Int -> Bool -> Int64 -> Stream (Of Packet) m ()-online n s p t = liftIO (openLive n s p t) >>= packets-{-# SPECIALIZE online :: String -> Int -> Bool -> Int64 -> Stream (Of Packet) IO () #-}--packets :: MonadIO m => PcapHandle -> Stream (Of Packet) m ()-packets h = do- (hdr,bs) <- liftIO (nextBS h)- if hdrCaptureLength hdr == 0 then pure () else P.yield (Packet hdr bs) *> packets h-{-# SPECIALIZE packets :: PcapHandle -> Stream (Of Packet) IO () #-}
− Network/Pcap/Streaming/Internal.hs
@@ -1,25 +0,0 @@-module Network.Pcap.Streaming.Internal where--import qualified Data.Attoparsec.ByteString as A-import qualified Data.ByteString as BS-import Data.Word (Word32)-import Network.Pcap--------- | A <https://en.wikipedia.org/wiki/Pcap pcap> packet. Assumes nothing about--- the contents or structure of the `bytes` value.-data Packet = Packet { header :: PktHdr, bytes :: BS.ByteString } deriving (Eq, Show)--packetP :: A.Parser Packet-packetP = do- hdr <- pktHeader- bts <- A.take . fromIntegral $ hdrCaptureLength hdr- pure $ Packet hdr bts--pktHeader :: A.Parser PktHdr-pktHeader = PktHdr <$> four <*> four <*> four <*> four--four :: A.Parser Word32-four = BS.foldr' (\w acc -> acc * 256 + fromIntegral w) 0 <$> A.take 4-{-# INLINE four #-}
− bench/Bench.hs
@@ -1,16 +0,0 @@-module Main where--import Criterion.Main-import Streaming (runResourceT)-import qualified Streaming.Prelude as P-import Network.Pcap.Streaming-------main :: IO ()-main = defaultMain- [ bgroup "Benchmarks"- [ bench "Count all entries in stream" . nfIO $ runResourceT (P.length_ $ offline "test/test.pcap")- , bench "Sum all timestamps" . nfIO $ runResourceT (P.sum_ . P.map (hdrSeconds . header) $ offline "test/test.pcap")- ]- ]
+ lib/Network/Pcap/Streaming.hs view
@@ -0,0 +1,48 @@+-- |+-- Module : Network.Pcap.Streaming+-- Copyright : (c) Colin Woodbury, 2018 - 2019+-- License : BSD3+-- Maintainer: Colin Woodbury <colin@fosskers.ca>+--+-- A streaming interface to the <http://hackage.haskell.org/package/pcap-0.4.5.2 pcap>+-- Haskell library, which itself is a binding to /libpcap/. Humbly adapted from+-- <http://hackage.haskell.org/package/pcap-conduit pcap-conduit> by Austin Seipp.++module Network.Pcap.Streaming+ ( Packet(..)+ , PktHdr(..)+ , offline+ , online+ ) where++import Control.Monad.Trans.Resource+import qualified Data.Attoparsec.ByteString.Streaming as A+import qualified Data.ByteString.Streaming as Q+import Data.Int (Int64)+import Network.Pcap+import Network.Pcap.Streaming.Internal+import Streaming+import qualified Streaming.Prelude as P++---++-- | Read `Packet`s from some file dump. Uses a custom parser, not /libpcap/.+--+-- /SPECIALIZE/d for @ResourceT IO@.+offline :: MonadResource m => FilePath -> Stream (Of Packet) m ()+offline = void . A.parsed packetP . Q.drop 24 . Q.readFile+{-# SPECIALIZE offline :: FilePath -> Stream (Of Packet) (ResourceT IO) () #-}++-- | Read `Packet`s from some network device. See `Network.Pcap.openLive` for a+-- description of each argument.+--+-- /SPECIALIZE/d for @IO@.+online :: MonadIO m => String -> Int -> Bool -> Int64 -> Stream (Of Packet) m ()+online n s p t = liftIO (openLive n s p t) >>= packets+{-# SPECIALIZE online :: String -> Int -> Bool -> Int64 -> Stream (Of Packet) IO () #-}++packets :: MonadIO m => PcapHandle -> Stream (Of Packet) m ()+packets h = do+ (hdr,bs) <- liftIO (nextBS h)+ if hdrCaptureLength hdr == 0 then pure () else P.yield (Packet hdr bs) *> packets h+{-# SPECIALIZE packets :: PcapHandle -> Stream (Of Packet) IO () #-}
+ lib/Network/Pcap/Streaming/Internal.hs view
@@ -0,0 +1,25 @@+module Network.Pcap.Streaming.Internal where++import qualified Data.Attoparsec.ByteString as A+import qualified Data.ByteString as BS+import Data.Word (Word32)+import Network.Pcap++---++-- | A <https://en.wikipedia.org/wiki/Pcap pcap> packet. Assumes nothing about+-- the contents or structure of the `bytes` value.+data Packet = Packet { header :: PktHdr, bytes :: BS.ByteString } deriving (Eq, Show)++packetP :: A.Parser Packet+packetP = do+ hdr <- pktHeader+ bts <- A.take . fromIntegral $ hdrCaptureLength hdr+ pure $ Packet hdr bts++pktHeader :: A.Parser PktHdr+pktHeader = PktHdr <$> four <*> four <*> four <*> four++four :: A.Parser Word32+four = BS.foldr' (\w acc -> acc * 256 + fromIntegral w) 0 <$> A.take 4+{-# INLINE four #-}
streaming-pcap.cabal view
@@ -1,79 +1,60 @@--- This file has been generated from package.yaml by hpack version 0.20.0.------ see: https://github.com/sol/hpack------ hash: f9eb58b0494a5226978946a213672383849ef6447e4052afd869091691f04040+cabal-version: 2.2 -name: streaming-pcap-version: 1.1.1-synopsis: Stream packets via libpcap.-description: Stream packets via libpcap. Requires `libpcap` to be installed.-category: Web-homepage: https://github.com/fosskers/streaming-pcap-author: Colin Woodbury-maintainer: colingw@gmail.com-copyright: 2018 Colin Woodbury-license: BSD3-license-file: LICENSE-build-type: Simple-cabal-version: >= 1.10+name: streaming-pcap+version: 1.1.1.1+synopsis: Stream packets via libpcap.+description: Stream packets via libpcap. Requires `libpcap` to be installed.+category: Web+homepage: https://github.com/fosskers/streaming-pcap+author: Colin Woodbury+maintainer: colin@fosskers.ca+copyright: 2018 - 2019 Colin Woodbury+license: BSD-3-Clause+license-file: LICENSE+build-type: Simple extra-source-files:- CHANGELOG.md README.md+ CHANGELOG.md test/test.pcap- test/tsuru.pcap -library- hs-source-dirs:- ./.- ghc-options: -fwarn-unused-imports -fwarn-unused-binds -fwarn-name-shadowing -fwarn-unused-matches -fwarn-incomplete-patterns -fwarn-incomplete-uni-patterns+common commons+ default-language: Haskell2010+ ghc-options: -Wall build-depends:- attoparsec >=0.13 && <0.14- , base >=4.7 && <5+ base >= 4.8 && < 5+ , attoparsec ^>=0.13 , bytestring- , pcap >=0.4 && <0.5+ , pcap ^>=0.4+ , resourcet ^>=1.2 , streaming >=0.1 && <0.3- , streaming-bytestring >=0.1 && <0.2- , streaming-utils >=0.1 && <0.2+ , streaming-attoparsec ^>= 1.0+ , streaming-bytestring ^>=0.1++library+ import: commons+ hs-source-dirs: lib exposed-modules: Network.Pcap.Streaming Network.Pcap.Streaming.Internal- default-language: Haskell2010 test-suite streaming-pcap-test+ import: commons type: exitcode-stdio-1.0 main-is: Test.hs- hs-source-dirs:- test- ghc-options: -fwarn-unused-imports -fwarn-unused-binds -fwarn-name-shadowing -fwarn-unused-matches -fwarn-incomplete-patterns -fwarn-incomplete-uni-patterns -threaded+ hs-source-dirs: test+ ghc-options: -threaded -with-rtsopts=-N build-depends:- attoparsec >=0.13 && <0.14- , base >=4.7 && <5- , bytestring- , pcap >=0.4 && <0.5- , streaming >=0.1 && <0.3- , streaming-bytestring >=0.1 && <0.2- , streaming-pcap- , streaming-utils >=0.1 && <0.2- , tasty >=0.11 && <1.1+ streaming-pcap+ , tasty >=0.11 && <1.3 , tasty-hunit >=0.9 && <0.11- default-language: Haskell2010 -benchmark kanji-bench- type: exitcode-stdio-1.0- main-is: Bench.hs- hs-source-dirs:- bench- ghc-options: -fwarn-unused-imports -fwarn-unused-binds -fwarn-name-shadowing -fwarn-unused-matches -fwarn-incomplete-patterns -fwarn-incomplete-uni-patterns -threaded -O2- build-depends:- attoparsec >=0.13 && <0.14- , base >=4.7 && <5- , bytestring- , criterion >=1.1 && <1.5- , pcap >=0.4 && <0.5- , streaming >=0.1 && <0.3- , streaming-bytestring >=0.1 && <0.2- , streaming-pcap- , streaming-utils >=0.1 && <0.2- default-language: Haskell2010+-- benchmark streaming-pcap-bench+-- import: commons+-- type: exitcode-stdio-1.0+-- main-is: Bench.hs+-- hs-source-dirs: bench+-- ghc-options: -threaded -O2+-- build-depends:+-- criterion >=1.1 && <1.5+-- , streaming-pcap
test/Test.hs view
@@ -1,10 +1,10 @@ module Main ( main ) where +import Control.Monad.Trans.Resource import qualified Data.Attoparsec.ByteString as A import qualified Data.ByteString as BS import Network.Pcap.Streaming import Network.Pcap.Streaming.Internal-import Streaming (runResourceT) import qualified Streaming.Prelude as P import Test.Tasty import Test.Tasty.HUnit
− test/tsuru.pcap
file too large to diff