Skip to content

Commit acd2af2

Browse files
author
shadowy-pycoder
committed
The config File field now takes io.Writer instead of file path
1 parent 1480d53 commit acd2af2

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

cmd/mshark/cli.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"os"
8+
"path/filepath"
89
"strings"
910
"text/tabwriter"
1011

@@ -59,7 +60,7 @@ func root(args []string) error {
5960
flags.DurationVar(&conf.Timeout, "t", 0, "The maximum duration of the packet capture process. Example: 5s")
6061
flags.IntVar(&conf.PacketCount, "c", 0, "The maximum number of packets to capture.")
6162
flags.StringVar(&conf.Expr, "e", "", `BPF filter expression. Example: "ip proto tcp"`)
62-
flags.StringVar(&conf.File, "f", "", "File path to write captured packet data to. Defaults to stdout.")
63+
path := flags.String("f", "", "File path to write captured packet data to. Defaults to stdout.")
6364
flags.BoolFunc("pcap", "Create a PCAP file in the current working directory.", func(flagValue string) error {
6465
conf.Pcap = true
6566
return nil
@@ -82,6 +83,15 @@ func root(args []string) error {
8283
return err
8384
}
8485

86+
if *path != "" {
87+
f, err := os.OpenFile(filepath.FromSlash(*path), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
88+
if err != nil {
89+
return fmt.Errorf("failed to open file: %v", err)
90+
}
91+
defer f.Close()
92+
conf.File = f
93+
}
94+
8595
if err := ms.OpenLive(&conf); err != nil {
8696
return err
8797
}

mshark.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mshark
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"net"
78
"os"
89
"path/filepath"
@@ -33,7 +34,7 @@ type Config struct {
3334
Timeout time.Duration // The maximum duration of the packet capture process.
3435
PacketCount int // The maximum number of packets to capture.
3536
Expr string // BPF filter expression.
36-
File string // File path to write captured packet data to. Defaults to /dev/stdout
37+
File io.Writer // File to write captured packet data to. Defaults to /dev/stdout
3738
Pcap bool // Create a PCAP file in the current working directory.
3839
}
3940

@@ -108,17 +109,12 @@ func OpenLive(conf *Config) error {
108109
b := make([]byte, snaplen)
109110

110111
// file to write packets
111-
var f *os.File
112-
if conf.File != "" {
113-
f, err = os.OpenFile(filepath.FromSlash(conf.File), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
114-
if err != nil {
115-
return fmt.Errorf("failed to open file: %v", err)
116-
}
117-
defer f.Close()
112+
var f io.Writer
113+
if conf.File != nil {
114+
f = conf.File
118115
} else {
119116
f = os.Stdout
120117
}
121-
122118
// number of packets
123119
count := conf.PacketCount
124120
if count < 0 {
@@ -200,64 +196,64 @@ func OpenLive(conf *Config) error {
200196
return nil
201197
}
202198

203-
func parsePacket(f *os.File, data []byte, packetNum uint64, timestamp time.Time, layers *layers) error {
199+
func parsePacket(f io.Writer, data []byte, packetNum uint64, timestamp time.Time, layers *layers) error {
204200

205201
fmt.Fprintf(f, "- Packet: %d Timestamp: %s\n", packetNum, timestamp.Format("2006-01-02T15:04:05-0700"))
206-
f.WriteString("==================================================================" + "\n")
202+
fmt.Fprintln(f, "==================================================================")
207203
if err := layers.ether.Parse(data); err != nil {
208204
return err
209205
}
210-
f.WriteString(layers.ether.String() + "\n")
206+
fmt.Fprintln(f, layers.ether.String())
211207
switch layers.ether.NextLayer() {
212208
case "IPv4":
213209
if err := layers.ip.Parse(layers.ether.Payload); err != nil {
214210
return err
215211
}
216-
f.WriteString(layers.ip.String() + "\n")
212+
fmt.Fprintln(f, layers.ip.String())
217213
switch layers.ip.NextLayer() {
218214
case "TCP":
219215
if err := layers.tcp.Parse(layers.ip.Payload); err != nil {
220216
return err
221217
}
222-
f.WriteString(layers.tcp.String() + "\n")
218+
fmt.Fprintln(f, layers.tcp.String())
223219
case "UDP":
224220
if err := layers.udp.Parse(layers.ip.Payload); err != nil {
225221
return err
226222
}
227-
f.WriteString(layers.udp.String() + "\n")
223+
fmt.Fprintln(f, layers.udp.String())
228224
case "ICMP":
229225
if err := layers.icmp.Parse(layers.ip.Payload); err != nil {
230226
return err
231227
}
232-
f.WriteString(layers.icmp.String() + "\n")
228+
fmt.Fprintln(f, layers.icmp.String())
233229
}
234230
case "IPv6":
235231
if err := layers.ip6.Parse(layers.ether.Payload); err != nil {
236232
return err
237233
}
238-
f.WriteString(layers.ip6.String() + "\n")
234+
fmt.Fprintln(f, layers.ip6.String())
239235
switch layers.ip6.NextLayer() {
240236
case "TCP":
241237
if err := layers.tcp.Parse(layers.ip6.Payload); err != nil {
242238
return err
243239
}
244-
f.WriteString(layers.tcp.String() + "\n")
240+
fmt.Fprintln(f, layers.tcp.String())
245241
case "UDP":
246242
if err := layers.udp.Parse(layers.ip6.Payload); err != nil {
247243
return err
248244
}
249-
f.WriteString(layers.udp.String() + "\n")
245+
fmt.Fprintln(f, layers.udp.String())
250246
case "ICMPv6":
251247
if err := layers.icmp6.Parse(layers.ip6.Payload); err != nil {
252248
return err
253249
}
254-
f.WriteString(layers.icmp6.String() + "\n")
250+
fmt.Fprintln(f, layers.icmp6.String())
255251
}
256252
case "ARP":
257253
if err := layers.arp.Parse(layers.ether.Payload); err != nil {
258254
return err
259255
}
260-
f.WriteString(layers.arp.String() + "\n")
256+
fmt.Fprintln(f, layers.arp.String())
261257
}
262258
return nil
263259
}

mshark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package mshark
22

33
import (
4-
"os"
4+
"io"
55
"testing"
66
)
77

@@ -12,7 +12,7 @@ func BenchmarkOpenLive(b *testing.B) {
1212
Snaplen: 1600,
1313
Promisc: true,
1414
PacketCount: b.N,
15-
File: os.DevNull,
15+
File: io.Discard,
1616
}
1717
if err := OpenLive(&conf); err != nil {
1818
b.Fatal(err)

0 commit comments

Comments
 (0)