Skip to content

Commit adf3964

Browse files
committed
默认输出 pcap 格式, 将非 ethernet 封装的转换为 ethernet
1 parent 0f8eba2 commit adf3964

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

pcap.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,26 @@ func (p *Pcap) init() error {
7777
}
7878
p.info = info
7979

80+
if p.file.finder.OnlyEthernet && !p.info.IsEthernet() {
81+
err = errors.New(fmt.Sprintf("pcap not ethernet encapsulation (%s), by %s", p.info.Encapsulation, p.file.finder))
82+
return
83+
}
84+
8085
p.workingDirectory = filepath.Join(p.file.finder.workingDirectory, p.file.relativeDirectory)
8186
err = os.MkdirAll(p.workingDirectory, os.ModePerm)
8287
if err != nil {
8388
return
8489
}
8590
// copy to working directory
8691
p.copyFilePath = filepath.Join(p.workingDirectory, p.file.baseName)
87-
err = p.file.copyTo(p.copyFilePath)
92+
if p.info.IsEthernet() {
93+
err = p.file.copyTo(p.copyFilePath)
94+
} else {
95+
fmt.Printf("not a ethernet pcap\n")
96+
ret := pcapTool.convertDLT2Ethernet(p.file.path, p.copyFilePath, 0)
97+
err = ret.err
98+
}
99+
88100
if err != nil {
89101
return
90102
}
@@ -95,7 +107,7 @@ func (p *Pcap) init() error {
95107
return
96108
}
97109
dst := filepath.Join(p.workingDirectory, fmt.Sprintf("%s.adjust-time", p.file.name))
98-
result := pcapTool.adjustTime(p.file.path, dst, p.timeOffset())
110+
result := pcapTool.adjustTime(p.copyFilePath, dst, p.timeOffset())
99111
adjf := File{path: dst}
100112
defer adjf.delete()
101113
if !result.succeed {
@@ -110,7 +122,7 @@ func (p *Pcap) init() error {
110122
pcapType = "pcapng"
111123
}
112124
rfFile := filepath.Join(p.workingDirectory, fmt.Sprintf("%s.tshark_rf", p.file.name))
113-
result := pcapTool.tsharkReadFilter(p.file.path, rfFile, p.file.finder.TsharkReadFilter, pcapType, 0)
125+
result := pcapTool.tsharkReadFilter(p.copyFilePath, rfFile, p.file.finder.TsharkReadFilter, pcapType, 0)
114126
if !result.succeed {
115127
err = result.err
116128
return
@@ -131,14 +143,14 @@ func (p *Pcap) init() error {
131143
if !p.file.finder.modifier.KeepIp {
132144
// first, generate cache file
133145
p.cacheFilePath = filepath.Join(p.workingDirectory, fmt.Sprintf("%s.cache", p.file.name))
134-
result := pcapTool.generateCache(p.file.path, p.cacheFilePath, 0)
146+
result := pcapTool.generateCache(p.copyFilePath, p.cacheFilePath, 0)
135147
if !result.succeed {
136148
err = result.err
137149
return
138150
}
139151

140152
ipv6File := filepath.Join(p.workingDirectory, fmt.Sprintf("%s.ipv6", p.file.name))
141-
result = pcapTool.filterIPv6(p.file.path, ipv6File, 0)
153+
result = pcapTool.filterIPv6(p.copyFilePath, ipv6File, 0)
142154
if !result.succeed {
143155
err = result.err
144156
return
@@ -154,7 +166,7 @@ func (p *Pcap) init() error {
154166

155167
endpoints := p.file.finder.modifier.randomEndPoints(p.hasIPv6)
156168
modifyIPFile := filepath.Join(p.workingDirectory, fmt.Sprintf("%s.modify-ip", p.file.name))
157-
result = pcapTool.modifyIp(p.file.path, modifyIPFile, p.cacheFilePath, endpoints, 0)
169+
result = pcapTool.modifyIp(p.copyFilePath, modifyIPFile, p.cacheFilePath, endpoints, 0)
158170
modifyf := File{path: modifyIPFile}
159171
defer modifyf.delete()
160172
if !result.succeed {
@@ -179,23 +191,31 @@ func (p *Pcap) new() (string, error) {
179191
nid := p.counter.Inc()
180192

181193
srcBase := filepath.Join(p.workingDirectory, fmt.Sprintf("%s_%06d", p.file.name, nid))
194+
var ext = ".pcap"
195+
src := fmt.Sprintf("%s%s", srcBase, ext)
182196

183-
src := fmt.Sprintf("%s%s", srcBase, p.file.ext)
184-
err := copyTo(p.copyFilePath, src)
185-
if err != nil {
186-
return "", err
197+
if p.info.IsPcapNG() {
198+
result := pcapTool.pcapng2pcap(p.copyFilePath, src)
199+
if !result.succeed {
200+
return "", result.err
201+
}
202+
} else {
203+
err := copyTo(p.copyFilePath, src)
204+
if err != nil {
205+
return "", err
206+
}
187207
}
188208

189-
nfrf := fmt.Sprintf("%s.rf%s", srcBase, p.file.ext)
190-
nfp426 := fmt.Sprintf("%s.p426%s", srcBase, p.file.ext)
191-
nft := fmt.Sprintf("%s.adjust-time%s", srcBase, p.file.ext)
192-
nfm := fmt.Sprintf("%s.modify-ip%s", srcBase, p.file.ext)
209+
nfrf := fmt.Sprintf("%s.rf%s", srcBase, ext)
210+
nfp426 := fmt.Sprintf("%s.p426%s", srcBase, ext)
211+
nft := fmt.Sprintf("%s.adjust-time%s", srcBase, ext)
212+
nfm := fmt.Sprintf("%s.modify-ip%s", srcBase, ext)
193213

194214
if p.file.finder.modifier.TsharkReadFilter != "" {
195215
pcapType := "pcap"
196-
if p.info.IsPcapNG() {
197-
pcapType = "pcapng"
198-
}
216+
//if p.info.IsPcapNG() {
217+
// pcapType = "pcapng"
218+
//}
199219
result := pcapTool.tsharkReadFilter(src, nfrf, p.file.finder.modifier.TsharkReadFilter, pcapType, 0)
200220
if !result.succeed {
201221
return "", result.err
@@ -207,7 +227,7 @@ func (p *Pcap) new() (string, error) {
207227
}
208228

209229
if p.file.finder.modifier.P426 {
210-
err := ConvertPCAP(src, nfp426, p.info.IsPcapNG())
230+
err := ConvertPCAP(src, nfp426, false)
211231
if err != nil {
212232
return "", err
213233
}
@@ -218,7 +238,7 @@ func (p *Pcap) new() (string, error) {
218238
}
219239

220240
if p.file.finder.modifier.ShufflePayload > 0 || p.file.finder.modifier.shufflePacket {
221-
err := shufflePCAP(src, nfp426, p.info.IsPcapNG(), ShuffleOptions{
241+
err := shufflePCAP(src, nfp426, false, ShuffleOptions{
222242
KeepN: p.file.finder.modifier.ShufflePayload,
223243
RandomPacket: p.file.finder.modifier.shufflePacket,
224244
RandomPacketN: p.file.finder.modifier.shufflePacketN,

pcap_tool.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ func (p *PcapTool) check() error {
5757
return nil
5858
}
5959

60+
func (p *PcapTool) pcapng2pcap(src, dst string) *ExecResult {
61+
return execShellCommand(fmt.Sprintf("%s -F pcap %s %s", p.Editcap, src, dst), config.CommandTimeout)
62+
}
63+
6064
func (p *PcapTool) adjustTime(src, dst string, timeOffset int64) *ExecResult {
61-
return execShellCommand(fmt.Sprintf("%s -t %d %s %s", p.Editcap, timeOffset, src, dst), config.CommandTimeout)
65+
return execShellCommand(fmt.Sprintf("%s -t %d -F pcap %s %s", p.Editcap, timeOffset, src, dst), config.CommandTimeout)
6266
}
6367

6468
func (p *PcapTool) generateCache(src, dst string, timeout time.Duration) *ExecResult {
@@ -85,6 +89,23 @@ func (p *PcapTool) modifyIp(src, dst, cache, endpoints string, timeout time.Dura
8589
return execShellCommand(cmd, timeout)
8690
}
8791

92+
func (p *PcapTool) convertDLT2Ethernet(src, dst string, timeout time.Duration) *ExecResult {
93+
if timeout == 0 {
94+
timeout = config.CommandTimeout
95+
}
96+
97+
cacheFilePath := fmt.Sprintf("%s.cache", dst)
98+
result := pcapTool.generateCache(src, cacheFilePath, 0)
99+
if !result.succeed {
100+
return result
101+
}
102+
defer deleteFile(cacheFilePath)
103+
104+
cmd := fmt.Sprintf("%s --fixcsum --infile=%s --outfile=%s --skipbroadcast --cachefile=%s --dlt=enet --enet-mac-seed=5",
105+
p.Tcprewrite, src, dst, cacheFilePath)
106+
return execShellCommand(cmd, timeout)
107+
}
108+
88109
func (p *PcapTool) filterIPv6(src, dst string, timeout time.Duration) *ExecResult {
89110
if timeout == 0 {
90111
timeout = config.CommandTimeout

0 commit comments

Comments
 (0)