frp/vendor/golang.org/x/net/ipv4/control_unix.go

74 lines
1.7 KiB
Go
Raw Normal View History

2017-06-04 14:47:24 +02:00
// Copyright 2012 The Go Authors. All rights reserved.
2017-06-04 14:07:03 +02:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2017-06-04 14:47:24 +02:00
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
2017-06-04 14:07:03 +02:00
package ipv4
import (
"unsafe"
"golang.org/x/net/internal/iana"
2017-06-04 14:47:24 +02:00
"golang.org/x/net/internal/socket"
2017-06-04 14:07:03 +02:00
)
2017-06-04 14:47:24 +02:00
func setControlMessage(c *socket.Conn, opt *rawOpt, cf ControlFlags, on bool) error {
2017-06-04 14:07:03 +02:00
opt.Lock()
defer opt.Unlock()
2017-06-04 14:47:24 +02:00
if so, ok := sockOpts[ssoReceiveTTL]; ok && cf&FlagTTL != 0 {
if err := so.SetInt(c, boolint(on)); err != nil {
2017-06-04 14:07:03 +02:00
return err
}
if on {
opt.set(FlagTTL)
} else {
opt.clear(FlagTTL)
}
}
2017-06-04 14:47:24 +02:00
if so, ok := sockOpts[ssoPacketInfo]; ok {
2017-06-04 14:07:03 +02:00
if cf&(FlagSrc|FlagDst|FlagInterface) != 0 {
2017-06-04 14:47:24 +02:00
if err := so.SetInt(c, boolint(on)); err != nil {
2017-06-04 14:07:03 +02:00
return err
}
if on {
opt.set(cf & (FlagSrc | FlagDst | FlagInterface))
} else {
opt.clear(cf & (FlagSrc | FlagDst | FlagInterface))
}
}
} else {
2017-06-04 14:47:24 +02:00
if so, ok := sockOpts[ssoReceiveDst]; ok && cf&FlagDst != 0 {
if err := so.SetInt(c, boolint(on)); err != nil {
2017-06-04 14:07:03 +02:00
return err
}
if on {
opt.set(FlagDst)
} else {
opt.clear(FlagDst)
}
}
2017-06-04 14:47:24 +02:00
if so, ok := sockOpts[ssoReceiveInterface]; ok && cf&FlagInterface != 0 {
if err := so.SetInt(c, boolint(on)); err != nil {
2017-06-04 14:07:03 +02:00
return err
}
if on {
opt.set(FlagInterface)
} else {
opt.clear(FlagInterface)
}
}
}
return nil
}
func marshalTTL(b []byte, cm *ControlMessage) []byte {
2017-06-04 14:47:24 +02:00
m := socket.ControlMessage(b)
m.MarshalHeader(iana.ProtocolIP, sysIP_RECVTTL, 1)
return m.Next(1)
2017-06-04 14:07:03 +02:00
}
func parseTTL(cm *ControlMessage, b []byte) {
cm.TTL = int(*(*byte)(unsafe.Pointer(&b[:1][0])))
}