.github
alerting
client
config
controller
core
docs
example
jsonpath
k8s
k8stest
metric
pattern
security
static
storage
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
.dockerignore
.gitattributes
.gitignore
Dockerfile
LICENSE.md
README.md
config.yaml
go.mod
go.sum
main.go
187 lines
4.9 KiB
Go
187 lines
4.9 KiB
Go
// Copyright 2012 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"
|
|
"time"
|
|
|
|
"golang.org/x/net/internal/socket"
|
|
)
|
|
|
|
// BUG(mikio): On Windows, the JoinSourceSpecificGroup,
|
|
// LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
|
|
// IncludeSourceSpecificGroup methods of PacketConn and RawConn are
|
|
// not implemented.
|
|
|
|
// A Conn represents a network endpoint that uses the IPv4 transport.
|
|
// It is used to control basic IP-level socket options such as TOS and
|
|
// TTL.
|
|
type Conn struct {
|
|
genericOpt
|
|
}
|
|
|
|
type genericOpt struct {
|
|
*socket.Conn
|
|
}
|
|
|
|
func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
|
|
|
|
// NewConn returns a new Conn.
|
|
func NewConn(c net.Conn) *Conn {
|
|
cc, _ := socket.NewConn(c)
|
|
return &Conn{
|
|
genericOpt: genericOpt{Conn: cc},
|
|
}
|
|
}
|
|
|
|
// A PacketConn represents a packet network endpoint that uses the
|
|
// IPv4 transport. It is used to control several IP-level socket
|
|
// options including multicasting. It also provides datagram based
|
|
// network I/O methods specific to the IPv4 and higher layer protocols
|
|
// such as UDP.
|
|
type PacketConn struct {
|
|
genericOpt
|
|
dgramOpt
|
|
payloadHandler
|
|
}
|
|
|
|
type dgramOpt struct {
|
|
*socket.Conn
|
|
}
|
|
|
|
func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil }
|
|
|
|
// SetControlMessage sets the per packet IP-level socket options.
|
|
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
|
|
if !c.payloadHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
|
|
}
|
|
|
|
// SetDeadline sets the read and write deadlines associated with the
|
|
// endpoint.
|
|
func (c *PacketConn) SetDeadline(t time.Time) error {
|
|
if !c.payloadHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.payloadHandler.PacketConn.SetDeadline(t)
|
|
}
|
|
|
|
// SetReadDeadline sets the read deadline associated with the
|
|
// endpoint.
|
|
func (c *PacketConn) SetReadDeadline(t time.Time) error {
|
|
if !c.payloadHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.payloadHandler.PacketConn.SetReadDeadline(t)
|
|
}
|
|
|
|
// SetWriteDeadline sets the write deadline associated with the
|
|
// endpoint.
|
|
func (c *PacketConn) SetWriteDeadline(t time.Time) error {
|
|
if !c.payloadHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.payloadHandler.PacketConn.SetWriteDeadline(t)
|
|
}
|
|
|
|
// Close closes the endpoint.
|
|
func (c *PacketConn) Close() error {
|
|
if !c.payloadHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.payloadHandler.PacketConn.Close()
|
|
}
|
|
|
|
// NewPacketConn returns a new PacketConn using c as its underlying
|
|
// transport.
|
|
func NewPacketConn(c net.PacketConn) *PacketConn {
|
|
cc, _ := socket.NewConn(c.(net.Conn))
|
|
p := &PacketConn{
|
|
genericOpt: genericOpt{Conn: cc},
|
|
dgramOpt: dgramOpt{Conn: cc},
|
|
payloadHandler: payloadHandler{PacketConn: c, Conn: cc},
|
|
}
|
|
return p
|
|
}
|
|
|
|
// A RawConn represents a packet network endpoint that uses the IPv4
|
|
// transport. It is used to control several IP-level socket options
|
|
// including IPv4 header manipulation. It also provides datagram
|
|
// based network I/O methods specific to the IPv4 and higher layer
|
|
// protocols that handle IPv4 datagram directly such as OSPF, GRE.
|
|
type RawConn struct {
|
|
genericOpt
|
|
dgramOpt
|
|
packetHandler
|
|
}
|
|
|
|
// SetControlMessage sets the per packet IP-level socket options.
|
|
func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
|
|
if !c.packetHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on)
|
|
}
|
|
|
|
// SetDeadline sets the read and write deadlines associated with the
|
|
// endpoint.
|
|
func (c *RawConn) SetDeadline(t time.Time) error {
|
|
if !c.packetHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.packetHandler.IPConn.SetDeadline(t)
|
|
}
|
|
|
|
// SetReadDeadline sets the read deadline associated with the
|
|
// endpoint.
|
|
func (c *RawConn) SetReadDeadline(t time.Time) error {
|
|
if !c.packetHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.packetHandler.IPConn.SetReadDeadline(t)
|
|
}
|
|
|
|
// SetWriteDeadline sets the write deadline associated with the
|
|
// endpoint.
|
|
func (c *RawConn) SetWriteDeadline(t time.Time) error {
|
|
if !c.packetHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.packetHandler.IPConn.SetWriteDeadline(t)
|
|
}
|
|
|
|
// Close closes the endpoint.
|
|
func (c *RawConn) Close() error {
|
|
if !c.packetHandler.ok() {
|
|
return errInvalidConn
|
|
}
|
|
return c.packetHandler.IPConn.Close()
|
|
}
|
|
|
|
// NewRawConn returns a new RawConn using c as its underlying
|
|
// transport.
|
|
func NewRawConn(c net.PacketConn) (*RawConn, error) {
|
|
cc, err := socket.NewConn(c.(net.Conn))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
r := &RawConn{
|
|
genericOpt: genericOpt{Conn: cc},
|
|
dgramOpt: dgramOpt{Conn: cc},
|
|
packetHandler: packetHandler{IPConn: c.(*net.IPConn), Conn: cc},
|
|
}
|
|
so, ok := sockOpts[ssoHeaderPrepend]
|
|
if !ok {
|
|
return nil, errNotImplemented
|
|
}
|
|
if err := so.SetInt(r.dgramOpt.Conn, boolint(true)); err != nil {
|
|
return nil, err
|
|
}
|
|
return r, nil
|
|
}
|