本文整理汇总了Golang中syscall.SetsockoptInt函数的典型用法代码示例。如果您正苦于以下问题:Golang SetsockoptInt函数的具体用法?Golang SetsockoptInt怎么用?Golang SetsockoptInt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetsockoptInt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: advert
func advert(_ net.PacketConn, src, dst net.IP, p *net.IPNet) error {
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
return err
}
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
pdst := dst
if *useLimited && runtime.GOOS == "freebsd" {
dst = directed(p)
syscall.SetsockoptInt(s, syscall.IPPROTO_IP, 0x17, 1) // IP_ONESBCAST
}
sa := &syscall.SockaddrInet4{Port: *port}
copy(sa.Addr[:], src.To4())
if err := syscall.Bind(s, sa); err != nil {
syscall.Close(s)
return err
}
f := os.NewFile(uintptr(s), fmt.Sprintf("udp:%v->", src))
c, err := net.FilePacketConn(f)
f.Close()
if err != nil {
return err
}
defer c.Close()
// If you are lucky, you can see that on some platform the
// kernel sometimes transmits a wrong frame addressed to IPv4
// limited broadcast address with some nexthop's link-layer
// address on a broadcast-capable link.
// In general, using BPF for transmitting IPv4 limited
// broadcast addresses is a reasonable choice.
_, err = c.WriteTo([]byte(fmt.Sprintf("%s-%v", runtime.GOOS, pdst)), &net.UDPAddr{IP: dst, Port: *port})
return err
}
示例2: main
func main() {
log.SetPrefix("")
log.SetFlags(0)
if os.Geteuid() != 0 {
log.Fatal(errors.New("please run as root"))
}
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
check(err)
if len(os.Args) < 3 {
log.Fatal("usage: synflood <victimIP> <spoofedIP>")
}
raddr := net.ParseIP(os.Args[1])
addr := syscall.SockaddrInet4{
Port: 0,
Addr: to4Array(raddr),
}
p := packet(raddr)
switch runtime.GOOS {
case "darwin", "dragonfly", "freebsd", "netbsd":
// need to set explicitly
check(syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1))
// no need to receive anything
check(syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 1))
case "linux":
// no need to receive anything
check(syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 0))
}
for {
check(syscall.Sendto(fd, p, 0, &addr))
}
}
示例3: setDefaultMulticastSockopts
func setDefaultMulticastSockopts(fd *netFD) {
fd.incref()
defer fd.decref()
// Allow multicast UDP and raw IP datagram sockets to listen
// concurrently across multiple listeners.
syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
}
示例4: SetOriginalDestOptions
func SetOriginalDestOptions(fd int) error {
if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
return err
}
if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1); err != nil {
return err
}
return nil
}
示例5: ka_intvl
func ka_intvl(fd int, d time.Duration) error {
if d == 0 {
return os.NewSyscallError("ka_intv", syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, 0))
}
// cargo cult from src/net/tcpsockopt_unix.go
d += (time.Second - time.Nanosecond)
return os.NewSyscallError("ka_intv", syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(d.Seconds())))
}
示例6: setKernelSpecificSockopt
func setKernelSpecificSockopt(s syscall.Handle, f int) {
// Allow broadcast.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
if f == syscall.AF_INET6 {
// using ip, tcp, udp, etc.
// allow both protocols even if the OS default is otherwise.
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
}
}
示例7: setDefaultSockopts
func setDefaultSockopts(s, family, sotype int, ipv6only bool) error {
if family == syscall.AF_INET6 && sotype != syscall.SOCK_RAW {
// Allow both IP versions even if the OS default
// is otherwise. Note that some operating systems
// never admit this option.
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only))
}
// Allow broadcast.
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
}
示例8: socket
// Generic socket creation.
func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err os.Error) {
// See ../syscall/exec.go for description of ForkLock.
syscall.ForkLock.RLock()
s, e := syscall.Socket(f, p, t)
if e != 0 {
syscall.ForkLock.RUnlock()
return nil, os.Errno(e)
}
syscall.CloseOnExec(s)
syscall.ForkLock.RUnlock()
// Allow reuse of recently-used addresses.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
// Allow broadcast.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
if f == syscall.AF_INET6 {
// using ip, tcp, udp, etc.
// allow both protocols even if the OS default is otherwise.
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
}
if la != nil {
e = syscall.Bind(s, la)
if e != 0 {
closesocket(s)
return nil, os.Errno(e)
}
}
if ra != nil {
e = syscall.Connect(s, ra)
for e == syscall.EINTR {
e = syscall.Connect(s, ra)
}
if e != 0 {
closesocket(s)
return nil, os.Errno(e)
}
}
sa, _ := syscall.Getsockname(s)
laddr := toAddr(sa)
sa, _ = syscall.Getpeername(s)
raddr := toAddr(sa)
fd, err = newFD(s, f, p, net, laddr, raddr)
if err != nil {
closesocket(s)
return nil, err
}
return fd, nil
}
示例9: Listen
// Listen returns TCP listener with SO_REUSEPORT option set.
//
// Only tcp4 network is supported.
//
// ErrNoReusePort error is returned if the system doesn't support SO_REUSEPORT.
func Listen(network, addr string) (l net.Listener, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(network, addr); err != nil {
return nil, err
}
syscall.ForkLock.RLock()
fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err == nil {
syscall.CloseOnExec(fd)
}
syscall.ForkLock.RUnlock()
if err != nil {
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, soReusePort, 1); err != nil {
syscall.Close(fd)
return nil, &ErrNoReusePort{err}
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
name := fmt.Sprintf("reuseport.%d.%s.%s", os.Getpid(), network, addr)
file = os.NewFile(uintptr(fd), name)
if l, err = net.FileListener(file); err != nil {
file.Close()
return nil, err
}
if err = file.Close(); err != nil {
l.Close()
return nil, err
}
return l, err
}
示例10: setDefaultSockopts
func setDefaultSockopts(s syscall.Handle, f, t int) error {
switch f {
case syscall.AF_INET6:
// Allow both IP versions even if the OS default is otherwise.
// Note that some operating systems never admit this option.
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
}
// Allow broadcast.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
return nil
}
示例11: setDefaultMulticastSockopts
func setDefaultMulticastSockopts(s int) error {
// Allow multicast UDP and raw IP datagram sockets to listen
// concurrently across multiple listeners.
if err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
return os.NewSyscallError("setsockopt", err)
}
// Allow reuse of recently-used ports.
// This option is supported only in descendants of 4.4BSD,
// to make an effective multicast application that requires
// quick draw possible.
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1))
}
示例12: setKeepAlivePeriod
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
if err := fd.incref(); err != nil {
return err
}
defer fd.decref()
// The kernel expects seconds so round to next highest second.
d += (time.Second - time.Nanosecond)
secs := int(d.Seconds())
if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs); err != nil {
return os.NewSyscallError("setsockopt", err)
}
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, secs))
}
示例13: setKernelSpecificSockopt
func setKernelSpecificSockopt(s, f int) {
// Allow reuse of recently-used addresses and ports.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
// Allow broadcast.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
if f == syscall.AF_INET6 {
// using ip, tcp, udp, etc.
// allow both protocols even if the OS default is otherwise.
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
}
}
示例14: NewReusablePortListener
// NewReusablePortListener returns net.FileListener that created from
// a file discriptor for a socket with SO_REUSEPORT option.
func NewReusablePortListener(proto, addr string) (l net.Listener, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(proto, addr); err != nil {
return nil, err
}
syscall.ForkLock.RLock()
if fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); err != nil {
return nil, err
}
syscall.ForkLock.RUnlock()
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, reusePort, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, listenerBacklogMaxSize); err != nil {
syscall.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), getSocketFileName(proto, addr))
if l, err = net.FileListener(file); err != nil {
syscall.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
return nil, err
}
return l, err
}
示例15: main
func main() {
flag.Parse()
ip := net.ParseIP(*group)
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
log.Fatal(err)
}
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
lsa := syscall.SockaddrInet4{Port: *port}
copy(lsa.Addr[:], ip.To4())
if err := syscall.Bind(s, &lsa); err != nil {
syscall.Close(s)
log.Fatal(err)
}
f := os.NewFile(uintptr(s), "")
c, err := net.FilePacketConn(f)
f.Close()
if err != nil {
log.Fatal(err)
}
p := ipv4.NewPacketConn(c)
defer p.Close()
ift, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
avail := net.FlagMulticast | net.FlagUp
for _, ifi := range ift {
if ifi.Flags&avail != avail {
continue
}
if err := p.JoinGroup(&ifi, &net.UDPAddr{IP: ip}); err != nil {
log.Println(err, "on", ifi)
}
}
log.Println(c.LocalAddr())
go receiver(c)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-sig:
os.Exit(0)
}
}
}