-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pcapng - moving towards comman CaptureFile
- Loading branch information
Showing
5 changed files
with
126 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
protocols/shared/src/main/scala/fs2/protocols/pcapng/CaptureFile.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2013 Functional Streams for Scala | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package fs2.protocols.pcapng | ||
|
||
import cats.effect.MonadCancelThrow | ||
import fs2.interop.scodec.StreamDecoder | ||
import fs2.protocols.pcap.LinkType | ||
import fs2.timeseries.TimeStamped | ||
import fs2.{Pipe, Pull, Stream} | ||
import scodec.bits.ByteVector | ||
|
||
object CaptureFile { | ||
|
||
val streamDecoder: StreamDecoder[BodyBlock] = | ||
StreamDecoder.once(SectionHeaderBlock.codec).flatMap { shb => | ||
StreamDecoder.many(BodyBlock.decoder(shb.ordering)) | ||
} | ||
|
||
def parse[F[_]: MonadCancelThrow, A]( | ||
f: (LinkType, ByteVector) => Option[A] | ||
): Pipe[F, Byte, TimeStamped[A]] = { bytes => | ||
def go( | ||
idbs: Vector[InterfaceDescriptionBlock], | ||
blocks: Stream[F, BodyBlock] | ||
): Pull[F, TimeStamped[A], Unit] = | ||
blocks.pull.uncons1.flatMap { | ||
case Some((idb: InterfaceDescriptionBlock, tail)) => | ||
go(idbs :+ idb, tail) | ||
case Some((epb: EnhancedPacketBlock, tail)) => | ||
val idb = idbs(epb.interfaceId.toInt) | ||
val ts = ((epb.timestampHigh << 32) | epb.timestampLow) * idb.if_tsresol | ||
val timeStamped = f(idb.linkType, epb.packetData).map(TimeStamped(ts, _)) | ||
Pull.outputOption1(timeStamped) >> go(idbs, tail) | ||
case Some((_, tail)) => go(idbs, tail) | ||
case None => Pull.done | ||
} | ||
|
||
bytes | ||
.through(streamDecoder.toPipeByte) | ||
.through(go(Vector.empty, _).stream) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
protocols/shared/src/main/scala/fs2/protocols/pcapng/SimplePacketBlock.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2013 Functional Streams for Scala | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package fs2.protocols.pcapng | ||
|
||
import scodec.Codec | ||
import scodec.bits._ | ||
import scodec.codecs._ | ||
|
||
case class SimplePacketBlock(length: Length, bytes: ByteVector) extends BodyBlock | ||
|
||
object SimplePacketBlock { | ||
|
||
private def hexConstant(implicit ord: ByteOrdering) = | ||
orderDependent(hex"00000003", hex"03000000") | ||
|
||
def codec(implicit ord: ByteOrdering): Codec[ProcessInformationBlock] = | ||
"SPB" | Block.ignoredCodec(hexConstant).as[ProcessInformationBlock] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters