summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/pcapgo/capture.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/pcapgo/capture.go')
-rw-r--r--src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/pcapgo/capture.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/pcapgo/capture.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/pcapgo/capture.go
new file mode 100644
index 00000000000..3c890e648c6
--- /dev/null
+++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/pcapgo/capture.go
@@ -0,0 +1,53 @@
+// 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 linux
+
+package pcapgo
+
+import (
+ "net"
+ "syscall"
+ "time"
+
+ "github.com/google/gopacket"
+ "github.com/mdlayher/raw"
+)
+
+// EthernetHandle wraps a raw.Conn, implementing gopacket.PacketDataSource so
+// that the handle can be used with gopacket.NewPacketSource.
+type EthernetHandle struct {
+ *raw.Conn
+}
+
+// ReadPacketData implements gopacket.PacketDataSource.
+func (h *EthernetHandle) ReadPacketData() ([]byte, gopacket.CaptureInfo, error) {
+ b := make([]byte, 4096) // TODO(correctness): how much space do we need?
+ n, _, err := h.ReadFrom(b)
+ if err != nil {
+ return nil, gopacket.CaptureInfo{}, err
+ }
+ data := b[:n]
+ return data, gopacket.CaptureInfo{
+ CaptureLength: len(data),
+ Length: len(data),
+ Timestamp: time.Now(),
+ }, nil
+}
+
+// NewEthernetHandle implements pcap.OpenLive for ethernet interfaces only.
+func NewEthernetHandle(ifname string) (*EthernetHandle, error) {
+ intf, err := net.InterfaceByName(ifname)
+ if err != nil {
+ return nil, err
+ }
+
+ conn, err := raw.ListenPacket(intf, syscall.ETH_P_ALL, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ return &EthernetHandle{conn}, nil
+}