-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathreader.go
More file actions
132 lines (109 loc) · 3.98 KB
/
Copy pathreader.go
File metadata and controls
132 lines (109 loc) · 3.98 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
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
/*
* GOPACP - PCAP file parsing in Golang
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package gopcap
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"os"
//"github.com/davecgh/go-spew/spew"
)
/////////////////////////////
// Reader
/////////////////////////////
// Reader struct
type Reader struct {
FileHandle *os.File
Buffer *bufio.Reader
Header FileHeader
}
// Open pcap file
func Open(filename string) (*Reader, error) {
var (
r = &Reader{}
err error
)
r.FileHandle, err = os.Open(filename)
if err != nil {
return nil, err
}
var buff [24]byte
if _, err := io.ReadFull(r.FileHandle, buff[:]); err != nil {
return nil, err
}
r.Header = FileHeader{
MagicNumber: binary.LittleEndian.Uint32(buff[:4]),
VersionMajor: binary.LittleEndian.Uint16(buff[4:6]),
VersionMinor: binary.LittleEndian.Uint16(buff[6:8]),
Thiszone: int32(binary.LittleEndian.Uint32(buff[8:12])),
Sigfigs: binary.LittleEndian.Uint32(buff[12:16]),
Snaplen: binary.LittleEndian.Uint32(buff[16:20]),
Network: binary.LittleEndian.Uint32(buff[20:24]),
}
r.Buffer = bufio.NewReader(r.FileHandle)
return r, nil
}
// ReadNextPacket reads the next packet. returns header,data,error
func (r *Reader) ReadNextPacket() (PacketHeader, []byte, error) {
var buff [16]byte
if _, err := io.ReadFull(r.Buffer, buff[:]); err != nil {
return PacketHeader{}, nil, err
}
pcaprecHdr := PacketHeader{
TsSec: int32(binary.LittleEndian.Uint32(buff[0:4])),
TsUsec: int32(binary.LittleEndian.Uint32(buff[4:8])),
CaptureLen: int32(binary.LittleEndian.Uint32(buff[8:12])),
OriginalLen: int32(binary.LittleEndian.Uint32(buff[12:16])),
}
var buf []byte
if pcaprecHdr.CaptureLen < 1 {
fmt.Println("invalid pcaprecHdr.CaptureLen:", pcaprecHdr.CaptureLen)
//spew.Dump(pcaprecHdr)
//spew.Dump(buff)
//panic("invalid capture length")
return pcaprecHdr, nil, io.EOF
} else if pcaprecHdr.CaptureLen > 262144 {
fmt.Println("pcaprecHdr.CaptureLen has %d-byte packet, bigger than maximum of 262144", pcaprecHdr.CaptureLen)
return pcaprecHdr, nil, io.EOF
} else {
buf = make([]byte, pcaprecHdr.CaptureLen)
if _, err := io.ReadFull(r.Buffer, buf); err != nil {
return pcaprecHdr, buf, err
}
}
return pcaprecHdr, buf, nil
}
// ReadNextPacketHeader read next packet header. returns header,data,error
// @TODO: add a bytes.Reader after the buffered reader, and seek inside that buffer instead of reading all the bytes.
func (r *Reader) ReadNextPacketHeader() (PacketHeader, []byte, error) {
var buff [16]byte
if _, err := io.ReadFull(r.Buffer, buff[:]); err != nil {
return PacketHeader{}, nil, err
}
pcaprecHdr := PacketHeader{
TsSec: int32(binary.LittleEndian.Uint32(buff[0:4])),
TsUsec: int32(binary.LittleEndian.Uint32(buff[4:8])),
CaptureLen: int32(binary.LittleEndian.Uint32(buff[8:12])),
OriginalLen: int32(binary.LittleEndian.Uint32(buff[12:16])),
}
var buf = make([]byte, pcaprecHdr.CaptureLen)
if _, err := io.ReadFull(r.Buffer, buf); err != nil {
return pcaprecHdr, buf, err
}
return pcaprecHdr, buf, nil
}
// Close pcap file
func (r *Reader) Close() error {
return r.FileHandle.Close()
}