.github
alerting
client
config
controller
core
docs
example
jsonpath
k8s
k8stest
metric
pattern
security
storage
util
vendor
cloud.google.com
github.com
golang.org
x
crypto
net
bpf
context
http
http2
icmp
idna
internal
ipv4
batch.go
control.go
control_bsd.go
control_pktinfo.go
control_stub.go
control_unix.go
control_windows.go
control_zos.go
dgramopt.go
doc.go
endpoint.go
genericopt.go
header.go
helper.go
iana.go
icmp.go
icmp_linux.go
icmp_stub.go
packet.go
payload.go
payload_cmsg.go
payload_nocmsg.go
sockopt.go
sockopt_posix.go
sockopt_stub.go
sys_aix.go
sys_asmreq.go
sys_asmreq_stub.go
sys_asmreqn.go
sys_asmreqn_stub.go
sys_bpf.go
sys_bpf_stub.go
sys_bsd.go
sys_darwin.go
sys_dragonfly.go
sys_freebsd.go
sys_linux.go
sys_solaris.go
sys_ssmreq.go
sys_ssmreq_stub.go
sys_stub.go
sys_windows.go
sys_zos.go
zsys_aix_ppc64.go
zsys_darwin.go
zsys_dragonfly.go
zsys_freebsd_386.go
zsys_freebsd_amd64.go
zsys_freebsd_arm.go
zsys_freebsd_arm64.go
zsys_linux_386.go
zsys_linux_amd64.go
zsys_linux_arm.go
zsys_linux_arm64.go
zsys_linux_mips.go
zsys_linux_mips64.go
zsys_linux_mips64le.go
zsys_linux_mipsle.go
zsys_linux_ppc.go
zsys_linux_ppc64.go
zsys_linux_ppc64le.go
zsys_linux_riscv64.go
zsys_linux_s390x.go
zsys_netbsd.go
zsys_openbsd.go
zsys_solaris.go
zsys_zos_s390x.go
ipv6
AUTHORS
CONTRIBUTORS
LICENSE
PATENTS
oauth2
sys
term
text
time
google.golang.org
gopkg.in
k8s.io
sigs.k8s.io
modules.txt
watchdog
web
.dockerignore
.gitattributes
.gitignore
Dockerfile
LICENSE.md
Makefile
README.md
config.yaml
go.mod
go.sum
main.go
195 lines
5.6 KiB
Go
195 lines
5.6 KiB
Go
// Copyright 2017 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ipv4
|
|
|
|
import (
|
|
"net"
|
|
"runtime"
|
|
|
|
"golang.org/x/net/internal/socket"
|
|
)
|
|
|
|
// BUG(mikio): On Windows, the ReadBatch and WriteBatch methods of
|
|
// PacketConn are not implemented.
|
|
|
|
// BUG(mikio): On Windows, the ReadBatch and WriteBatch methods of
|
|
// RawConn are not implemented.
|
|
|
|
// A Message represents an IO message.
|
|
//
|
|
// type Message struct {
|
|
// Buffers [][]byte
|
|
// OOB []byte
|
|
// Addr net.Addr
|
|
// N int
|
|
// NN int
|
|
// Flags int
|
|
// }
|
|
//
|
|
// The Buffers fields represents a list of contiguous buffers, which
|
|
// can be used for vectored IO, for example, putting a header and a
|
|
// payload in each slice.
|
|
// When writing, the Buffers field must contain at least one byte to
|
|
// write.
|
|
// When reading, the Buffers field will always contain a byte to read.
|
|
//
|
|
// The OOB field contains protocol-specific control or miscellaneous
|
|
// ancillary data known as out-of-band data.
|
|
// It can be nil when not required.
|
|
//
|
|
// The Addr field specifies a destination address when writing.
|
|
// It can be nil when the underlying protocol of the endpoint uses
|
|
// connection-oriented communication.
|
|
// After a successful read, it may contain the source address on the
|
|
// received packet.
|
|
//
|
|
// The N field indicates the number of bytes read or written from/to
|
|
// Buffers.
|
|
//
|
|
// The NN field indicates the number of bytes read or written from/to
|
|
// OOB.
|
|
//
|
|
// The Flags field contains protocol-specific information on the
|
|
// received message.
|
|
type Message = socket.Message
|
|
|
|
// ReadBatch reads a batch of messages.
|
|
//
|
|
// The provided flags is a set of platform-dependent flags, such as
|
|
// syscall.MSG_PEEK.
|
|
//
|
|
// On a successful read it returns the number of messages received, up
|
|
// to len(ms).
|
|
//
|
|
// On Linux, a batch read will be optimized.
|
|
// On other platforms, this method will read only a single message.
|
|
//
|
|
// Unlike the ReadFrom method, it doesn't strip the IPv4 header
|
|
// followed by option headers from the received IPv4 datagram when the
|
|
// underlying transport is net.IPConn. Each Buffers field of Message
|
|
// must be large enough to accommodate an IPv4 header and option
|
|
// headers.
|
|
func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
|
|
if !c.ok() {
|
|
return 0, errInvalidConn
|
|
}
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
n, err := c.RecvMsgs([]socket.Message(ms), flags)
|
|
if err != nil {
|
|
err = &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
|
|
}
|
|
return n, err
|
|
default:
|
|
n := 1
|
|
err := c.RecvMsg(&ms[0], flags)
|
|
if err != nil {
|
|
n = 0
|
|
err = &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
|
|
}
|
|
if compatFreeBSD32 && ms[0].NN > 0 {
|
|
adjustFreeBSD32(&ms[0])
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
// WriteBatch writes a batch of messages.
|
|
//
|
|
// The provided flags is a set of platform-dependent flags, such as
|
|
// syscall.MSG_DONTROUTE.
|
|
//
|
|
// It returns the number of messages written on a successful write.
|
|
//
|
|
// On Linux, a batch write will be optimized.
|
|
// On other platforms, this method will write only a single message.
|
|
func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
|
|
if !c.ok() {
|
|
return 0, errInvalidConn
|
|
}
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
n, err := c.SendMsgs([]socket.Message(ms), flags)
|
|
if err != nil {
|
|
err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
|
|
}
|
|
return n, err
|
|
default:
|
|
n := 1
|
|
err := c.SendMsg(&ms[0], flags)
|
|
if err != nil {
|
|
n = 0
|
|
err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
// ReadBatch reads a batch of messages.
|
|
//
|
|
// The provided flags is a set of platform-dependent flags, such as
|
|
// syscall.MSG_PEEK.
|
|
//
|
|
// On a successful read it returns the number of messages received, up
|
|
// to len(ms).
|
|
//
|
|
// On Linux, a batch read will be optimized.
|
|
// On other platforms, this method will read only a single message.
|
|
func (c *packetHandler) ReadBatch(ms []Message, flags int) (int, error) {
|
|
if !c.ok() {
|
|
return 0, errInvalidConn
|
|
}
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
n, err := c.RecvMsgs([]socket.Message(ms), flags)
|
|
if err != nil {
|
|
err = &net.OpError{Op: "read", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
|
|
}
|
|
return n, err
|
|
default:
|
|
n := 1
|
|
err := c.RecvMsg(&ms[0], flags)
|
|
if err != nil {
|
|
n = 0
|
|
err = &net.OpError{Op: "read", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
|
|
}
|
|
if compatFreeBSD32 && ms[0].NN > 0 {
|
|
adjustFreeBSD32(&ms[0])
|
|
}
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
// WriteBatch writes a batch of messages.
|
|
//
|
|
// The provided flags is a set of platform-dependent flags, such as
|
|
// syscall.MSG_DONTROUTE.
|
|
//
|
|
// It returns the number of messages written on a successful write.
|
|
//
|
|
// On Linux, a batch write will be optimized.
|
|
// On other platforms, this method will write only a single message.
|
|
func (c *packetHandler) WriteBatch(ms []Message, flags int) (int, error) {
|
|
if !c.ok() {
|
|
return 0, errInvalidConn
|
|
}
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
n, err := c.SendMsgs([]socket.Message(ms), flags)
|
|
if err != nil {
|
|
err = &net.OpError{Op: "write", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
|
|
}
|
|
return n, err
|
|
default:
|
|
n := 1
|
|
err := c.SendMsg(&ms[0], flags)
|
|
if err != nil {
|
|
n = 0
|
|
err = &net.OpError{Op: "write", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
|
|
}
|
|
return n, err
|
|
}
|
|
}
|