github.com/gopacket/gopacket@v1.1.0/afpacket/sockopt_linux.go (about)

     1  // Copyright 2012 Google, Inc. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  
     7  //go:build linux
     8  // +build linux
     9  
    10  package afpacket
    11  
    12  import (
    13  	"unsafe"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  // setsockopt provides access to the setsockopt syscall.
    19  func setsockopt(fd, level, name int, val unsafe.Pointer, vallen uintptr) error {
    20  	_, _, errno := unix.Syscall6(
    21  		unix.SYS_SETSOCKOPT,
    22  		uintptr(fd),
    23  		uintptr(level),
    24  		uintptr(name),
    25  		uintptr(val),
    26  		vallen,
    27  		0,
    28  	)
    29  	if errno != 0 {
    30  		return error(errno)
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  // getsockopt provides access to the getsockopt syscall.
    37  func getsockopt(fd, level, name int, val unsafe.Pointer, vallen uintptr) error {
    38  	_, _, errno := unix.Syscall6(
    39  		unix.SYS_GETSOCKOPT,
    40  		uintptr(fd),
    41  		uintptr(level),
    42  		uintptr(name),
    43  		uintptr(val),
    44  		vallen,
    45  		0,
    46  	)
    47  	if errno != 0 {
    48  		return error(errno)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // htons converts a short (uint16) from host-to-network byte order.
    55  // Thanks to mikioh for this neat trick:
    56  // https://github.com/mikioh/-stdyng/blob/master/afpacket.go
    57  func htons(i uint16) uint16 {
    58  	return (i<<8)&0xff00 | i>>8
    59  }