-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap.go
120 lines (103 loc) · 2.73 KB
/
pcap.go
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
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy ofthe License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specificlanguage governing permissions and
* limitations under the License.
*
*/
package packetinjector
import (
"bytes"
"io"
"time"
"github.com/google/gopacket/pcapgo"
"github.com/skydive-project/skydive/logging"
)
// PcapPacketGenerator reads packets from a pcap file and inject it into a channel
type PcapPacketGenerator struct {
*PacketInjectionRequest
close chan bool
}
// Close the packet generator
func (p *PcapPacketGenerator) Close() {
p.close <- true
}
func packetNumberFromPcap(b []byte) (uint64, error) {
buffer := bytes.NewReader(b)
pcapReader, err := pcapgo.NewReader(buffer)
if err != nil {
return 0, err
}
var count uint64
for {
data, _, _ := pcapReader.ReadPacketData()
if len(data) == 0 {
break
}
count++
}
return count, nil
}
// PacketSource returns a channel when pcap packets are pushed
func (p *PcapPacketGenerator) PacketSource() chan *Packet {
ch := make(chan *Packet)
go func() {
defer close(ch)
var pcapReader *pcapgo.Reader
var err error
if p.Count == 0 {
if p.Count, err = packetNumberFromPcap(p.Pcap); err != nil {
logging.GetLogger().Errorf("Error while reading pcap file: %s", err)
return
}
}
for i := uint64(0); i < p.Count; i++ {
if pcapReader == nil {
buffer := bytes.NewReader(p.Pcap)
if pcapReader, err = pcapgo.NewReader(buffer); err != nil {
logging.GetLogger().Errorf("Error while reading pcap file: %s", err)
return
}
}
data, _, err := pcapReader.ReadPacketData()
if err != nil && err != io.EOF {
logging.GetLogger().Warningf("Failed to read packet: %s", err)
return
}
logging.GetLogger().Debugf("Read %d bytes of pcap", len(data))
if len(data) == 0 {
pcapReader = nil
continue
}
select {
case ch <- &Packet{data: data}:
case <-p.close:
return
}
if i != p.Count-1 && p.Interval != 0 {
select {
case <-p.close:
return
case <-time.After(time.Millisecond * time.Duration(p.Interval)):
}
}
}
}()
return ch
}
// NewPcapPacketGenerator returns a new pcap packet generator
func NewPcapPacketGenerator(pp *PacketInjectionRequest) (*PcapPacketGenerator, error) {
return &PcapPacketGenerator{
PacketInjectionRequest: pp,
close: make(chan bool, 1),
}, nil
}