-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try a multi-package Golang program named pktcollector
It does not use multiple packages yet. Starting out as just a copy of trypcap program.
- Loading branch information
1 parent
a3a28d4
commit 1abeaab
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
all: build | ||
|
||
format: pktcollector.go | ||
gofmt -w pktcollector.go | ||
|
||
vet: pktcollector.go format | ||
go vet pktcollector | ||
|
||
build: pktcollector.go vet | ||
go build pktcollector.go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module pktcollector | ||
|
||
go 1.22.4 | ||
|
||
require github.com/google/gopacket v1.1.19 | ||
|
||
require golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= | ||
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= | ||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= | ||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright © 2024 Andy Fingerhut | ||
// License: https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// trypcap exercises a few features of the pcap library from Go | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
//"time" | ||
|
||
"github.com/google/gopacket" | ||
//"github.com/google/gopacket/layers" | ||
"github.com/google/gopacket/pcap" | ||
) | ||
|
||
// Things in pcap package: | ||
// func FindAllDevs() (ifs []Interface, err error) | ||
|
||
var iface = flag.String("iface", "", "Select interface where to capture") | ||
var fname = flag.String("file", "", "pcap file to read packets from") | ||
|
||
func printUsage(progName string) { | ||
fmt.Fprintf(os.Stderr, "usage: %s\n", progName) | ||
fmt.Fprintln(os.Stderr, " [ --iface <interface_name> ]") | ||
fmt.Fprintln(os.Stderr, " [ --file <pcap_filename> ]") | ||
fmt.Fprintln(os.Stderr) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *iface == "" && *fname == "" { | ||
printUsage(os.Args[0]) | ||
os.Exit(1) | ||
} | ||
|
||
var handle *pcap.Handle | ||
if *iface != "" { | ||
intfs, err := pcap.FindAllDevs() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
return | ||
} | ||
fmt.Printf("Found %d interfaces:\n\n", len(intfs)) | ||
for idx, intf := range intfs { | ||
fmt.Printf("%2d 0x%04x %s %s %d addresses: %v\n", idx, intf.Flags, intf.Name, intf.Description, len(intf.Addresses), intf.Addresses) | ||
} | ||
|
||
// Opening Device | ||
snaplen := 10000 | ||
promisc := true | ||
//timeout := 30 * time.Second | ||
timeout := pcap.BlockForever | ||
handle, err = pcap.OpenLive(*iface, int32(snaplen), promisc, timeout) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer handle.Close() | ||
fmt.Printf("Opened interface %s promisc=%v\n", *iface, promisc) | ||
} | ||
if *fname != "" { | ||
var err error | ||
handle, err = pcap.OpenOffline(*fname) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer handle.Close() | ||
fmt.Printf("Opened file %s\n", *fname) | ||
} | ||
fmt.Printf(" handle=%v\n", handle) | ||
|
||
packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) | ||
n := 0 | ||
for packet := range packetSource.Packets() { | ||
n += 1 | ||
fmt.Printf("\n%d\n", n) | ||
fmt.Println(packet.Dump()) | ||
|
||
//p := gopacket.NewPacket(packet, CiscoS1PuntHeaderType, gopacket.Lazy) | ||
//fmt.Println(p) | ||
} | ||
} |