Source File
ifreq_linux.go
Belonging Package
golang.org/x/sys/unix
// Copyright 2021 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.//go:build linux// +build linuxpackage uniximport ()// Helpers for dealing with ifreq since it contains a union and thus requires a// lot of unsafe.Pointer casts to use properly.// An Ifreq is a type-safe wrapper around the raw ifreq struct. An Ifreq// contains an interface name and a union of arbitrary data which can be// accessed using the Ifreq's methods. To create an Ifreq, use the NewIfreq// function.//// Use the Name method to access the stored interface name. The union data// fields can be get and set using the following methods:// - Uint16/SetUint16: flags// - Uint32/SetUint32: ifindex, metric, mtutype Ifreq struct{ raw ifreq }// NewIfreq creates an Ifreq with the input network interface name after// validating the name does not exceed IFNAMSIZ-1 (trailing NULL required)// bytes.func ( string) (*Ifreq, error) {// Leave room for terminating NULL byte.if len() >= IFNAMSIZ {return nil, EINVAL}var ifreqcopy(.Ifrn[:], )return &Ifreq{raw: }, nil}// TODO(mdlayher): get/set methods for hardware address sockaddr, char array, etc.// Name returns the interface name associated with the Ifreq.func ( *Ifreq) () string {return ByteSliceToString(.raw.Ifrn[:])}// According to netdevice(7), only AF_INET addresses are returned for numerous// sockaddr ioctls. For convenience, we expose these as Inet4Addr since the Port// field and other data is always empty.// Inet4Addr returns the Ifreq union data from an embedded sockaddr as a C// in_addr/Go []byte (4-byte IPv4 address) value. If the sockaddr family is not// AF_INET, an error is returned.func ( *Ifreq) () ([]byte, error) {:= *(*RawSockaddrInet4)(unsafe.Pointer(&.raw.Ifru[:SizeofSockaddrInet4][0]))if .Family != AF_INET {// Cannot safely interpret raw.Addr bytes as an IPv4 address.return nil, EINVAL}return .Addr[:], nil}// SetInet4Addr sets a C in_addr/Go []byte (4-byte IPv4 address) value in an// embedded sockaddr within the Ifreq's union data. v must be 4 bytes in length// or an error will be returned.func ( *Ifreq) ( []byte) error {if len() != 4 {return EINVAL}var [4]bytecopy([:], ).clear()*(*RawSockaddrInet4)(unsafe.Pointer(&.raw.Ifru[:SizeofSockaddrInet4][0]),) = RawSockaddrInet4{// Always set IP family as ioctls would require it anyway.Family: AF_INET,Addr: ,}return nil}// Uint16 returns the Ifreq union data as a C short/Go uint16 value.func ( *Ifreq) () uint16 {return *(*uint16)(unsafe.Pointer(&.raw.Ifru[:2][0]))}// SetUint16 sets a C short/Go uint16 value as the Ifreq's union data.func ( *Ifreq) ( uint16) {.clear()*(*uint16)(unsafe.Pointer(&.raw.Ifru[:2][0])) =}// Uint32 returns the Ifreq union data as a C int/Go uint32 value.func ( *Ifreq) () uint32 {return *(*uint32)(unsafe.Pointer(&.raw.Ifru[:4][0]))}// SetUint32 sets a C int/Go uint32 value as the Ifreq's union data.func ( *Ifreq) ( uint32) {.clear()*(*uint32)(unsafe.Pointer(&.raw.Ifru[:4][0])) =}// clear zeroes the ifreq's union field to prevent trailing garbage data from// being sent to the kernel if an ifreq is reused.func ( *Ifreq) () {for := range .raw.Ifru {.raw.Ifru[] = 0}}// TODO(mdlayher): export as IfreqData? For now we can provide helpers such as// IoctlGetEthtoolDrvinfo which use these APIs under the hood.// An ifreqData is an Ifreq which carries pointer data. To produce an ifreqData,// use the Ifreq.withData method.type ifreqData struct {name [IFNAMSIZ]byte// A type separate from ifreq is required in order to comply with the// unsafe.Pointer rules since the "pointer-ness" of data would not be// preserved if it were cast into the byte array of a raw ifreq.data unsafe.Pointer// Pad to the same size as ifreq._ [len(ifreq{}.Ifru) - SizeofPtr]byte}// withData produces an ifreqData with the pointer p set for ioctls which require// arbitrary pointer data.func ( Ifreq) ( unsafe.Pointer) ifreqData {return ifreqData{name: .raw.Ifrn,data: ,}}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |