Skip to content

Commit

Permalink
Accept IPv6 addresses in pcap.ParseSocket (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwt authored Mar 5, 2020
1 parent 66b49b6 commit 022e2f1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 4 additions & 6 deletions pcap/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net"
"strconv"
"strings"
)

type Socket struct {
Expand All @@ -30,17 +29,16 @@ func (f Flow) String() string {
}

func ParseSocket(s string) (Socket, error) {
pair := strings.Split(s, ":")
if len(pair) == 2 {
ip := net.ParseIP(pair[0])
if host, port, err := net.SplitHostPort(s); err == nil {
ip := net.ParseIP(host)
if ip != nil {
port, err := strconv.Atoi(pair[1])
port, err := strconv.Atoi(port)
if err == nil {
return Socket{ip, port}, nil
}
}
}
return Socket{}, fmt.Errorf("address spec must have form ip:port (%s)", s)
return Socket{}, fmt.Errorf("address spec must have form ip4:port or [ip6]:port (%s)", s)
}

func ParseFlow(h0, h1 string) (Flow, error) {
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/pcap/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ var out1 = `
1425567047.804914
`

// 80.239.174.91.443 > 192.168.0.51.33773
// 80.239.174.91.443 (aka [::ffff:50ef:ae5b]:443) > 192.168.0.51.33773

// test simple flow extraction
var Test2 = test.Shell{
Name: "pcap-command",
Script: `pcap slice -r in.pcap 80.239.174.91:443 192.168.0.51:33773 | pcap ts -w out2`,
Script: `pcap slice -r in.pcap [::ffff:50ef:ae5b]:443 192.168.0.51:33773 | pcap ts -w out2`,
Input: []test.File{test.File{Name: "in.pcap"}},
Expected: []test.File{
test.File{"out2", test.Trim(out2)},
Expand Down

0 comments on commit 022e2f1

Please sign in to comment.