src/pkg/net/sockoptip_linux.go - The Go Programming Language

Golang

Source file src/pkg/net/sockoptip_linux.go

     1	// Copyright 2011 The Go Authors.  All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// IP-level socket options for Linux
     6	
     7	package net
     8	
     9	import (
    10		"os"
    11		"syscall"
    12	)
    13	
    14	func ipv4MulticastInterface(fd *netFD) (*Interface, error) {
    15		if err := fd.incref(false); err != nil {
    16			return nil, err
    17		}
    18		defer fd.decref()
    19		mreq, err := syscall.GetsockoptIPMreqn(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_IF)
    20		if err != nil {
    21			return nil, os.NewSyscallError("getsockopt", err)
    22		}
    23		if int(mreq.Ifindex) == 0 {
    24			return nil, nil
    25		}
    26		return InterfaceByIndex(int(mreq.Ifindex))
    27	}
    28	
    29	func setIPv4MulticastInterface(fd *netFD, ifi *Interface) error {
    30		var v int32
    31		if ifi != nil {
    32			v = int32(ifi.Index)
    33		}
    34		mreq := &syscall.IPMreqn{Ifindex: v}
    35		if err := fd.incref(false); err != nil {
    36			return err
    37		}
    38		defer fd.decref()
    39		err := syscall.SetsockoptIPMreqn(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_IF, mreq)
    40		if err != nil {
    41			return os.NewSyscallError("setsockopt", err)
    42		}
    43		return nil
    44	}
    45	
    46	func ipv4MulticastTTL(fd *netFD) (int, error) {
    47		if err := fd.incref(false); err != nil {
    48			return 0, err
    49		}
    50		defer fd.decref()
    51		v, err := syscall.GetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_TTL)
    52		if err != nil {
    53			return -1, os.NewSyscallError("getsockopt", err)
    54		}
    55		return v, nil
    56	}
    57	
    58	func setIPv4MulticastTTL(fd *netFD, v int) error {
    59		if err := fd.incref(false); err != nil {
    60			return err
    61		}
    62		defer fd.decref()
    63		err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_TTL, v)
    64		if err != nil {
    65			return os.NewSyscallError("setsockopt", err)
    66		}
    67		return nil
    68	}
    69	
    70	func ipv4MulticastLoopback(fd *netFD) (bool, error) {
    71		if err := fd.incref(false); err != nil {
    72			return false, err
    73		}
    74		defer fd.decref()
    75		v, err := syscall.GetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP)
    76		if err != nil {
    77			return false, os.NewSyscallError("getsockopt", err)
    78		}
    79		return v == 1, nil
    80	}
    81	
    82	func setIPv4MulticastLoopback(fd *netFD, v bool) error {
    83		if err := fd.incref(false); err != nil {
    84			return err
    85		}
    86		defer fd.decref()
    87		err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP, boolint(v))
    88		if err != nil {
    89			return os.NewSyscallError("setsockopt", err)
    90		}
    91		return nil
    92	}
    93	
    94	func ipv4ReceiveInterface(fd *netFD) (bool, error) {
    95		if err := fd.incref(false); err != nil {
    96			return false, err
    97		}
    98		defer fd.decref()
    99		v, err := syscall.GetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_PKTINFO)
   100		if err != nil {
   101			return false, os.NewSyscallError("getsockopt", err)
   102		}
   103		return v == 1, nil
   104	}
   105	
   106	func setIPv4ReceiveInterface(fd *netFD, v bool) error {
   107		if err := fd.incref(false); err != nil {
   108			return err
   109		}
   110		defer fd.decref()
   111		err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_PKTINFO, boolint(v))
   112		if err != nil {
   113			return os.NewSyscallError("setsockopt", err)
   114		}
   115		return nil
   116	}
   117	
   118	func ipv6TrafficClass(fd *netFD) (int, error) {
   119		if err := fd.incref(false); err != nil {
   120			return 0, err
   121		}
   122		defer fd.decref()
   123		v, err := syscall.GetsockoptInt(fd.sysfd, syscall.IPPROTO_IPV6, syscall.IPV6_TCLASS)
   124		if err != nil {
   125			return 0, os.NewSyscallError("getsockopt", err)
   126		}
   127		return v, nil
   128	}
   129	
   130	func setIPv6TrafficClass(fd *netFD, v int) error {
   131		if err := fd.incref(false); err != nil {
   132			return err
   133		}
   134		defer fd.decref()
   135		err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_IPV6, syscall.IPV6_TCLASS, v)
   136		if err != nil {
   137			return os.NewSyscallError("setsockopt", err)
   138		}
   139		return nil
   140	}