ping: Code cleanup
This commit is contained in:
parent
548f51cc9d
commit
a0b34a4be9
1 changed files with 25 additions and 20 deletions
45
ping/ping.go
45
ping/ping.go
|
|
@ -33,29 +33,34 @@ type Conn struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Connect(ctx context.Context, logger logger.ContextLogger, privileged bool, controlFunc control.Func, destination netip.Addr) (*Conn, error) {
|
func Connect(ctx context.Context, logger logger.ContextLogger, privileged bool, controlFunc control.Func, destination netip.Addr) (*Conn, error) {
|
||||||
conn, err := connect0(ctx, privileged, controlFunc, destination)
|
c := &Conn{
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Conn{
|
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
privileged: privileged,
|
privileged: privileged,
|
||||||
conn: conn,
|
|
||||||
destination: destination,
|
destination: destination,
|
||||||
}, nil
|
}
|
||||||
|
err := c.connect(controlFunc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect0(ctx context.Context, privileged bool, controlFunc control.Func, destination netip.Addr) (net.Conn, error) {
|
func (c *Conn) connect(controlFunc control.Func) (err error) {
|
||||||
if (runtime.GOOS == "linux" || runtime.GOOS == "android") && !privileged {
|
if c.IsLinuxUnprivileged() {
|
||||||
return newUnprivilegedConn(ctx, controlFunc, destination)
|
c.conn, err = newUnprivilegedConn(c.ctx, controlFunc, c.destination)
|
||||||
} else {
|
} else {
|
||||||
return connect(privileged, controlFunc, destination)
|
c.conn, err = connect(c.privileged, controlFunc, c.destination)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) IsLinuxUnprivileged() bool {
|
||||||
|
return (runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) ReadIP(buffer *buf.Buffer) error {
|
func (c *Conn) ReadIP(buffer *buf.Buffer) error {
|
||||||
if c.destination.Is6() || (runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged {
|
if c.destination.Is6() || c.IsLinuxUnprivileged() {
|
||||||
var readMsg func(b, oob []byte) (n, oobn int, addr netip.Addr, err error)
|
var readMsg func(b, oob []byte) (n, oobn int, addr netip.Addr, err error)
|
||||||
switch conn := c.conn.(type) {
|
switch conn := c.conn.(type) {
|
||||||
case *net.IPConn:
|
case *net.IPConn:
|
||||||
|
|
@ -102,7 +107,7 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
|
||||||
}
|
}
|
||||||
ttl = controlMessage.TTL
|
ttl = controlMessage.TTL
|
||||||
}
|
}
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
icmpHdr := header.ICMPv4(buffer.Bytes())
|
icmpHdr := header.ICMPv4(buffer.Bytes())
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
icmpHdr.SetChecksum(0)
|
icmpHdr.SetChecksum(0)
|
||||||
|
|
@ -141,7 +146,7 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
|
||||||
trafficClass = controlMessage.TrafficClass
|
trafficClass = controlMessage.TrafficClass
|
||||||
}
|
}
|
||||||
icmpHdr := header.ICMPv6(buffer.Bytes())
|
icmpHdr := header.ICMPv6(buffer.Bytes())
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
}
|
}
|
||||||
icmpHdr.SetChecksum(0)
|
icmpHdr.SetChecksum(0)
|
||||||
|
|
@ -182,7 +187,7 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
|
||||||
ipHdr.SetChecksum(0)
|
ipHdr.SetChecksum(0)
|
||||||
ipHdr.SetChecksum(^ipHdr.CalculateChecksum())
|
ipHdr.SetChecksum(^ipHdr.CalculateChecksum())
|
||||||
icmpHdr := header.ICMPv4(ipHdr.Payload())
|
icmpHdr := header.ICMPv4(ipHdr.Payload())
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
}
|
}
|
||||||
icmpHdr.SetChecksum(0)
|
icmpHdr.SetChecksum(0)
|
||||||
|
|
@ -195,7 +200,7 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
|
||||||
}
|
}
|
||||||
ipHdr.SetDestinationAddr(c.source.Load())
|
ipHdr.SetDestinationAddr(c.source.Load())
|
||||||
icmpHdr := header.ICMPv6(ipHdr.Payload())
|
icmpHdr := header.ICMPv6(ipHdr.Payload())
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
}
|
}
|
||||||
icmpHdr.SetChecksum(0)
|
icmpHdr.SetChecksum(0)
|
||||||
|
|
@ -215,7 +220,7 @@ func (c *Conn) ReadICMP(buffer *buf.Buffer) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
if !c.destination.Is6() {
|
if !c.destination.Is6() {
|
||||||
ipHdr := header.IPv4(buffer.Bytes())
|
ipHdr := header.IPv4(buffer.Bytes())
|
||||||
buffer.Advance(int(ipHdr.HeaderLength()))
|
buffer.Advance(int(ipHdr.HeaderLength()))
|
||||||
|
|
@ -242,7 +247,7 @@ func (c *Conn) WriteIP(buffer *buf.Buffer) error {
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
if !c.destination.Is6() {
|
if !c.destination.Is6() {
|
||||||
ipHdr := header.IPv4(buffer.Bytes())
|
ipHdr := header.IPv4(buffer.Bytes())
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
icmpHdr := header.ICMPv4(ipHdr.Payload())
|
icmpHdr := header.ICMPv4(ipHdr.Payload())
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
icmpHdr.SetChecksum(0)
|
icmpHdr.SetChecksum(0)
|
||||||
|
|
@ -253,7 +258,7 @@ func (c *Conn) WriteIP(buffer *buf.Buffer) error {
|
||||||
return common.Error(c.conn.Write(ipHdr.Payload()))
|
return common.Error(c.conn.Write(ipHdr.Payload()))
|
||||||
} else {
|
} else {
|
||||||
ipHdr := header.IPv6(buffer.Bytes())
|
ipHdr := header.IPv6(buffer.Bytes())
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
icmpHdr := header.ICMPv6(ipHdr.Payload())
|
icmpHdr := header.ICMPv6(ipHdr.Payload())
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
icmpHdr.SetChecksum(0)
|
icmpHdr.SetChecksum(0)
|
||||||
|
|
@ -271,7 +276,7 @@ func (c *Conn) WriteIP(buffer *buf.Buffer) error {
|
||||||
|
|
||||||
func (c *Conn) WriteICMP(buffer *buf.Buffer) error {
|
func (c *Conn) WriteICMP(buffer *buf.Buffer) error {
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
if !((runtime.GOOS == "linux" || runtime.GOOS == "android") && !c.privileged) {
|
if !c.IsLinuxUnprivileged() {
|
||||||
if !c.destination.Is6() {
|
if !c.destination.Is6() {
|
||||||
icmpHdr := header.ICMPv4(buffer.Bytes())
|
icmpHdr := header.ICMPv4(buffer.Bytes())
|
||||||
icmpHdr.SetIdent(^icmpHdr.Ident())
|
icmpHdr.SetIdent(^icmpHdr.Ident())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue