Commit Graph

4 Commits

Author SHA1 Message Date
Kir Kolyshkin
dcacdad474 unix: use ByteSliceFromString in (*Ifreq).Name
CL 340370 introduced (*Ifreq).Name method which scans the Ifrn field
for \0 twice -- first explicitly, when by calling BytePtrToString.

It seems more straightforward to use ByteSliceToString instead. The
differences are:
 - simpler code;
 - no double scanning for \0;
 - in case there is no \0 in the Ifrn (unlikely), the full string
   (rather than an empty string) is now returned.

With the last item in mind, the test case with no \0 as input that
expects empty output fails, so drop it. Alternatively, we could test
that it returns the full string, but it will be essentially testing
ByteSliceToString, which is not the point.

Cc: Matt Layher <mdlayher@gmail.com>
Change-Id: I31d4f6dbe98aae5120f9b2246c93ceaa6a165395
Reviewed-on: https://go-review.googlesource.com/c/sys/+/407194
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
2022-05-19 14:10:25 +00:00
Matt Layher
41cdb8703e unix: add Ifreq.Inet4Addr methods for manipulating IPv4 addresses
ioctls such as SIOCGIFADDR deal with AF_INET sockaddr addresses, but none of
the fields aside from the embedded IPv4 address are used. To keep the interface
more simple, we directly expose Inet4Addr get and set methods which enable use
of these ioctls with the Ifreq wrapper.

Change-Id: Ia8b6ab9730f852cb99f4152e334a59d395476d2f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/343250
Trust: Matt Layher <mdlayher@gmail.com>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
2021-08-20 12:10:16 +00:00
Matt Layher
bfb29a6856 unix: export Ifreq and add IoctlIfreq function
This CL expands upon CL 340369 by exporting the Ifreq type along with methods
for setting and getting data to/from the ifreq union in a type-safe way.
ifreqData remains unexported as we can keep adding helpers similar to the
IoctlGetEthtoolDrvinfo to expose those operations in a less error-prone way.

A test is also added to verify interface index data using IoctlIfreq against
the modern rtnetlink API used by the standard library.

Change-Id: Ic6980cbcd3792cc341cd614061cce32fa1f851e7
Reviewed-on: https://go-review.googlesource.com/c/sys/+/340370
Trust: Matt Layher <mdlayher@gmail.com>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-08-17 19:03:40 +00:00
Matt Layher
e5e7981a10 unix: add ifreq and ifreqData helpers for Linux
ifreq is difficult to use in Go due to the union field in particular. This
situation is made worse due to the need to comply with Go's unsafe.Pointer
rules. This CL generates the raw ifreq type and also adds an ifreqData type
of the same size which is specialized for use with unsafe.Pointer.

We also replace the existing ifreqEthtool (which was not padded to the correct
size) with the new APIs and add a test to verify that IoctlGetEthtoolDrvinfo
functions properly by checking the name of the driver for each network interface.

Future uses of ifreq in package unix can expand upon this type with additional
getter and setter methods to deal with the unsafe casts to and from the union
byte array. We may also consider exporting ifreq and ifreqData if necessary.

Change-Id: Ibf73a10e774b4336815c674bb867bbb7ec1b9c71
Reviewed-on: https://go-review.googlesource.com/c/sys/+/340369
Run-TryBot: Matt Layher <mdlayher@gmail.com>
Trust: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-06 18:45:41 +00:00