-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathPcapExample.scala
147 lines (123 loc) · 5.21 KB
/
PcapExample.scala
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package scodec
package examples
import scala.concurrent.duration.*
import scodec.bits.BitVector
/** Processes libpcap files.
*
* @see http://wiki.wireshark.org/Development/LibpcapFileFormat
*/
object PcapCodec:
import scodec.codecs.*
enum ByteOrdering { case BigEndian, LittleEndian }
private val magicNumber = 0x000000a1b2c3d4L
val byteOrdering = "magic_number" | Codec[ByteOrdering](
(bo: ByteOrdering) =>
if (bo == ByteOrdering.BigEndian) uint32.encode(magicNumber) else uint32L.encode(magicNumber),
(buf: BitVector) =>
uint32.decode(buf).map {
_.map { mn =>
if (mn == magicNumber) ByteOrdering.BigEndian else ByteOrdering.LittleEndian
}
}
)
private def detectByteOrdering[A <: Tuple](
f: ByteOrdering ?=> Codec[A]
): Codec[ByteOrdering *: A] =
byteOrdering.flatPrepend(ordering => f(using ordering))
def gint16(using ordering: ByteOrdering): Codec[Int] =
if (ordering == ByteOrdering.BigEndian) int16 else int16L
def guint16(using ordering: ByteOrdering): Codec[Int] =
if (ordering == ByteOrdering.BigEndian) uint16 else uint16L
def gint32(using ordering: ByteOrdering): Codec[Int] =
if (ordering == ByteOrdering.BigEndian) int32 else int32L
def guint32(using ordering: ByteOrdering): Codec[Long] =
if (ordering == ByteOrdering.BigEndian) uint32 else uint32L
case class PcapHeader(
ordering: ByteOrdering,
versionMajor: Int,
versionMinor: Int,
thiszone: Int,
sigfigs: Long,
snaplen: Long,
network: Long
)
val pcapHeader: Codec[PcapHeader] =
detectByteOrdering {
("version_major" | guint16) ::
("version_minor" | guint16) ::
("thiszone" | gint32) ::
("sigfigs" | guint32) ::
("snaplen" | guint32) ::
("network" | guint32)
}
.as[PcapHeader]
case class PcapRecordHeader(
timestampSeconds: Long,
timestampMicros: Long,
includedLength: Long,
originalLength: Long
):
def timestamp: Double = timestampSeconds + (timestampMicros / (1.second.toMicros.toDouble))
def pcapRecordHeader(using ByteOrdering): Codec[PcapRecordHeader] = {
("ts_sec" | guint32) ::
("ts_usec" | guint32) ::
("incl_len" | guint32) ::
("orig_len" | guint32)
}.as[PcapRecordHeader]
case class PcapRecord(header: PcapRecordHeader, data: BitVector)
def pcapRecord(using ordering: ByteOrdering) =
("record_header" | pcapRecordHeader)
.flatZip { hdr =>
"record_data" | bits(hdr.includedLength * 8)
}
.as[PcapRecord]
case class PcapFile(header: PcapHeader, records: Vector[PcapRecord])
val pcapFile = pcapHeader.flatZip(hdr => vector(pcapRecord(using hdr.ordering))).as[PcapFile]
class PcapExample extends CodecSuite:
import PcapCodec.*
def bits =
BitVector.fromMmap(new java.io.FileInputStream(new java.io.File("/path/to/pcap")).getChannel.nn)
test("support reading an entire file and then decoding it all".ignore) {
pcapFile.decode(bits)
}
test(
"support reading the file header and then decoding each record, combining results via a monoid".ignore
) {
val fileHeader = pcapHeader.decode(bits.take(24 * 8)).require.value
given ByteOrdering = fileHeader.ordering
// Monoid that counts records
val (_, recordCount) = pcapRecord.decodeAll(_ => 1)(0, _ + _)(bits.drop(24 * 8))
// Monoid that accumulates records
val (_, records) = pcapRecord.decodeAll(Vector(_))(Vector.empty, _ ++ _)(bits.drop(24 * 8))
}
// Alternatively, don't pre-load all bytes... read each record header individually and use included size field to read more bytes
// See scodec-stream library at https://github.com/scodec/scodec-stream