Copy dependencies into vendor dir, using dep

This commit is contained in:
Bryan Boreham committed 2018-04-27 14:34:48 +01:00
1 parent 549ccff5eb
commit b534ea60f5
2857 files changed
+945317

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
/*
Package pcap allows users of gopacket to read packets off the wire or from
pcap files.
This package is meant to be used with its parent,
http://github.com/google/gopacket, although it can also be used independently
if you just want to get packet data from the wire.
Reading PCAP Files
The following code can be used to read in data from a pcap file.
if handle, err := pcap.OpenOffline("/path/to/my/file"); err != nil {
panic(err)
} else {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
handlePacket(packet) // Do something with a packet here.
}
}
Reading Live Packets
The following code can be used to read in data from a live device, in this case
"eth0".
if handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever); err != nil {
panic(err)
} else if err := handle.SetBPFFilter("tcp and port 80"); err != nil { // optional
panic(err)
} else {
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
handlePacket(packet) // Do something with a packet here.
}
}
Inactive Handles
Newer PCAP functionality requires the concept of an 'inactive' PCAP handle.
Instead of constantly adding new arguments to pcap_open_live, users now call
pcap_create to create a handle, set it up with a bunch of optional function
calls, then call pcap_activate to activate it. This library mirrors that
mechanism, for those that want to expose/use these new features:
inactive, err := pcap.NewInactiveHandle(deviceName)
if err != nil {
log.Fatal(err)
}
defer inactive.CleanUp()
// Call various functions on inactive to set it up the way you'd like:
if err = inactive.SetTimeout(time.Minute); err != nil {
log.Fatal(err)
} else if err = inactive.SetTimestampSource("foo"); err != nil {
log.Fatal(err)
}
// Finally, create the actual handle by calling Activate:
handle, err := inactive.Activate() // after this, inactive is no longer valid
if err != nil {
log.Fatal(err)
}
defer handle.Close()
// Now use your handle as you see fit.
PCAP Timeouts
pcap.OpenLive and pcap.SetTimeout both take timeouts.
If you don't care about timeouts, just pass in BlockForever,
which should do what you expect with minimal fuss.
A timeout of 0 is not recommended. Some platforms, like Macs
(http://www.manpages.info/macosx/pcap.3.html) say:
The read timeout is used to arrange that the read not necessarily return
immediately when a packet is seen, but that it wait for some amount of time
to allow more packets to arrive and to read multiple packets from the OS
kernel in one operation.
This means that if you only capture one packet, the kernel might decide to wait
'timeout' for more packets to batch with it before returning. A timeout of
0, then, means 'wait forever for more packets', which is... not good.
To get around this, we've introduced the following behavior: if a negative
timeout is passed in, we set the positive timeout in the handle, then loop
internally in ReadPacketData/ZeroCopyReadPacketData when we see timeout
errors.
PCAP File Writing
This package does not implement PCAP file writing. However, gopacket/pcapgo
does! Look there if you'd like to write PCAP files.
Note For Windows 10 Users
If you're trying to use 64-bit winpcap on Windows 10, you might have to do
the crazy hijinks detailed at
http://stackoverflow.com/questions/38047858/compile-gopacket-on-windows-64bit
*/
package pcap
+1005
View File
File diff suppressed because it is too large. Load diff
+109
View File
@@ -0,0 +1,109 @@
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
// +build ignore
// This binary tests that PCAP packet capture is working correctly by issuing
// HTTP requests, then making sure we actually capture data off the wire.
package main
import (
"errors"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"time"
"github.com/google/gopacket/pcap"
)
var mode = flag.String("mode", "basic", "One of: basic,filtered,timestamp")
func generatePackets() {
if resp, err := http.Get("http://code.google.com"); err != nil {
log.Printf("Could not get HTTP: %v", err)
} else {
resp.Body.Close()
}
}
func main() {
flag.Parse()
ifaces, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
for _, iface := range ifaces {
log.Printf("Trying capture on %q", iface.Name)
if err := tryCapture(iface); err != nil {
log.Printf("Error capturing on %q: %v", iface.Name, err)
} else {
log.Printf("Successfully captured on %q", iface.Name)
return
}
}
os.Exit(1)
}
func tryCapture(iface net.Interface) error {
if iface.Name[:2] == "lo" {
return errors.New("skipping loopback")
}
var h *pcap.Handle
var err error
switch *mode {
case "basic":
h, err = pcap.OpenLive(iface.Name, 65536, false, time.Second*3)
if err != nil {
return fmt.Errorf("openlive: %v", err)
}
defer h.Close()
case "filtered":
h, err = pcap.OpenLive(iface.Name, 65536, false, time.Second*3)
if err != nil {
return fmt.Errorf("openlive: %v", err)
}
defer h.Close()
if err := h.SetBPFFilter("port 80 or port 443"); err != nil {
return fmt.Errorf("setbpf: %v", err)
}
case "timestamp":
u, err := pcap.NewInactiveHandle(iface.Name)
if err != nil {
return err
}
defer u.CleanUp()
if err = u.SetSnapLen(65536); err != nil {
return err
} else if err = u.SetPromisc(false); err != nil {
return err
} else if err = u.SetTimeout(time.Second * 3); err != nil {
return err
}
sources := u.SupportedTimestamps()
if len(sources) == 0 {
return errors.New("no supported timestamp sources")
} else if err := u.SetTimestampSource(sources[0]); err != nil {
return fmt.Errorf("settimestampsource(%v): %v", sources[0], err)
} else if h, err = u.Activate(); err != nil {
return fmt.Errorf("could not activate: %v", err)
}
defer h.Close()
default:
panic("Invalid --mode: " + *mode)
}
go generatePackets()
h.ReadPacketData() // Do one dummy read to clear any timeouts.
data, ci, err := h.ReadPacketData()
if err != nil {
return fmt.Errorf("readpacketdata: %v", err)
}
log.Printf("Read packet, %v bytes, CI: %+v", len(data), ci)
return nil
}
+71
View File
@@ -0,0 +1,71 @@
// Copyright 2012 Google, Inc. All rights reserved.
// Copyright 2009-2011 Andreas Krennmair. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
//
// +build !windows
package pcap
/*
#include <stdlib.h>
#include <pcap.h>
// pcap_wait returns when the next packet is available or the timeout expires.
// Since it uses pcap_get_selectable_fd, it will not work in Windows.
int pcap_wait(pcap_t *p, int usec) {
fd_set fds;
int fd;
struct timeval tv;
fd = pcap_get_selectable_fd(p);
if(fd < 0) {
return fd;
}
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 0;
tv.tv_usec = usec;
if(usec != 0) {
return select(fd+1, &fds, NULL, NULL, &tv);
}
// block indefinitely if no timeout provided
return select(fd+1, &fds, NULL, NULL, NULL);
}
*/
import "C"
import (
"errors"
"unsafe"
)
func (p *Handle) openLive() error {
buf := (*C.char)(C.calloc(errorBufferSize, 1))
defer C.free(unsafe.Pointer(buf))
// Change the device to non-blocking, we'll use pcap_wait to wait until the
// handle is ready to read.
if v := C.pcap_setnonblock(p.cptr, 1, buf); v == -1 {
return errors.New(C.GoString(buf))
}
return nil
}
// waitForPacket waits for a packet or for the timeout to expire.
func (p *Handle) waitForPacket() {
// need to wait less than the read timeout according to pcap documentation.
// timeoutMillis rounds up to at least one millisecond so we can safely
// subtract up to a millisecond.
usec := timeoutMillis(p.timeout) * 1000
usec -= 100
C.pcap_wait(p.cptr, usec)
}
+23
View File
@@ -0,0 +1,23 @@
// Copyright 2012 Google, Inc. All rights reserved.
// Copyright 2009-2011 Andreas Krennmair. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package pcap
import (
"runtime"
)
func (p *Handle) openLive() error {
// do nothing
return nil
}
// waitForPacket waits for a packet or for the timeout to expire.
func (p *Handle) waitForPacket() {
// can't use select() so instead just switch goroutines
runtime.Gosched()
}
Binary file not shown.
Binary file not shown.
Binary file not shown.