-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new tool pcap-unpack and version v0.2
- Loading branch information
Showing
6 changed files
with
175 additions
and
6 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
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
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
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,79 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/Eyevinn/pcap-tools/internal" | ||
) | ||
|
||
const ( | ||
appName = "pcap-unpack" | ||
) | ||
|
||
var usg = `Usage of %s: | ||
%s unpacks UDP streams from from a Wireshark/tcpdump capture. | ||
The output is saved as files with names input_destAddress_port.ts | ||
(assuming that the streams are MPEG-2 TS streams). | ||
` | ||
|
||
type options struct { | ||
dst string | ||
version bool | ||
} | ||
|
||
func parseOptions(fs *flag.FlagSet, args []string) (*options, error) { | ||
fs.Usage = func() { | ||
fmt.Fprintf(os.Stderr, usg, appName, appName) | ||
fmt.Fprintf(os.Stderr, "\n%s [options] pcapfile [pcapfile ....]\n\noptions:\n", appName) | ||
fs.PrintDefaults() | ||
} | ||
|
||
opts := options{} | ||
fs.StringVar(&opts.dst, "dst", "", "Destination directory for output files") | ||
fs.BoolVar(&opts.version, "version", false, "Get mp4ff version") | ||
|
||
err := fs.Parse(args[1:]) | ||
return &opts, err | ||
} | ||
|
||
func main() { | ||
if err := run(os.Args); err != nil { | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run(args []string) error { | ||
fs := flag.NewFlagSet(appName, flag.ContinueOnError) | ||
opts, err := parseOptions(fs, args) | ||
|
||
if err != nil { | ||
if errors.Is(err, flag.ErrHelp) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if opts.version { | ||
fmt.Printf("%s %s\n", appName, internal.GetVersion()) | ||
return nil | ||
} | ||
|
||
pcapFiles := fs.Args() | ||
if len(pcapFiles) == 0 { | ||
return fmt.Errorf("no pcap files specified") | ||
} | ||
|
||
for _, pcapFile := range pcapFiles { | ||
err := processPCAP(pcapFile, opts.dst) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,75 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/layers" | ||
"github.com/google/gopacket/pcap" | ||
Check failure on line 11 in cmd/pcap-unpack/pcap.go GitHub Actions / lint
|
||
) | ||
|
||
func processPCAP(pcapFile string, dst string) error { | ||
if pcapFile == "" { | ||
return fmt.Errorf("pcapFile is required") | ||
} | ||
handle, err := pcap.OpenOffline(pcapFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer handle.Close() | ||
|
||
return processPcapHandle(handle, pcapFile, dst) | ||
} | ||
|
||
func processPcapHandle(handle *pcap.Handle, fileName, dstDir string) error { | ||
// Loop through packets from source | ||
packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) | ||
packetChannel := packetSource.Packets() | ||
udpDsts := make(map[string]io.WriteCloser) | ||
for packet := range packetChannel { | ||
if packet == nil { | ||
return nil | ||
} | ||
udpLayer := packet.Layer(layers.LayerTypeUDP) | ||
if udpLayer == nil { | ||
continue | ||
} | ||
ipLayer := packet.Layer(layers.LayerTypeIPv4) | ||
if ipLayer == nil { | ||
continue | ||
} | ||
ip := ipLayer.(*layers.IPv4) | ||
udp, _ := udpLayer.(*layers.UDP) | ||
if udp == nil { | ||
continue | ||
} | ||
dstPort := int(udp.DstPort) | ||
dst := fmt.Sprintf("%s_%d.ts", ip.DstIP, dstPort) | ||
if _, ok := udpDsts[dst]; !ok { | ||
var dstPath string | ||
switch { | ||
case dstDir == "": | ||
dstPath = fmt.Sprintf("%s_%s", fileName, dst) | ||
default: | ||
base := filepath.Base(fileName) | ||
dstPath = filepath.Join(dstDir, fmt.Sprintf("%s_%s", base, dst)) | ||
} | ||
fh, err := os.Create(dstPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer fh.Close() | ||
fmt.Println("Created", dstPath) | ||
udpDsts[dst] = fh | ||
} | ||
fh := udpDsts[dst] | ||
_, err := fh.Write(udp.Payload) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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