unix: add more illumos ioctl wrappers

Expose Strioctl and Lifreq structs
Add helpers for working with them
Add additional wrapper functions

This work is in support of wireGuard/wireguard-go#39

Change-Id: I7a4d919f986ec977e2cd393eaf237d8c43bbc1cb
Reviewed-on: https://go-review.googlesource.com/c/sys/+/302831
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Trust: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Nahum Shalman
2021-03-16 16:49:22 +00:00
committed by Tobias Klauser
parent 09eb48e85f
commit c94f62235c
4 changed files with 80 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2009 The Go Authors. All rights reserved.
// 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.
@@ -10,6 +10,8 @@
package unix
import (
"fmt"
"runtime"
"unsafe"
)
@@ -127,3 +129,50 @@ func Getmsg(fd int, cl []byte, data []byte) (retCl []byte, retData []byte, flags
}
return retCl, retData, flags, nil
}
func IoctlSetIntRetInt(fd int, req uint, arg int) (int, error) {
return ioctlRet(fd, req, uintptr(arg))
}
func IoctlSetString(fd int, req uint, val string) error {
bs := make([]byte, len(val)+1)
copy(bs[:len(bs)-1], val)
err := ioctl(fd, req, uintptr(unsafe.Pointer(&bs[0])))
runtime.KeepAlive(&bs[0])
return err
}
// Lifreq Helpers
func (l *Lifreq) SetName(name string) error {
if len(name) >= len(l.Name) {
return fmt.Errorf("name cannot be more than %d characters", len(l.Name)-1)
}
for i := range name {
l.Name[i] = int8(name[i])
}
return nil
}
func (l *Lifreq) SetLifruInt(d int) {
*(*int)(unsafe.Pointer(&l.Lifru[0])) = d
}
func (l *Lifreq) GetLifruInt() int {
return *(*int)(unsafe.Pointer(&l.Lifru[0]))
}
func IoctlLifreq(fd int, req uint, l *Lifreq) error {
return ioctl(fd, req, uintptr(unsafe.Pointer(l)))
}
// Strioctl Helpers
func (s *Strioctl) SetInt(i int) {
s.Len = int32(unsafe.Sizeof(i))
s.Dp = (*int8)(unsafe.Pointer(&i))
}
func IoctlSetStrioctlRetInt(fd int, req uint, s *Strioctl) (int, error) {
return ioctlRet(fd, req, uintptr(unsafe.Pointer(s)))
}

View File

@@ -0,0 +1,26 @@
// 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 illumos
// +build illumos
package unix_test
import (
"testing"
"golang.org/x/sys/unix"
)
func TestLifreqSetName(t *testing.T) {
var l unix.Lifreq
err := l.SetName("12345678901234356789012345678901234567890")
if err == nil {
t.Fatal(`Lifreq.SetName should reject names that are too long`)
}
err = l.SetName("tun0")
if err != nil {
t.Errorf(`Lifreq.SetName("tun0") failed: %v`, err)
}
}

View File

@@ -42,6 +42,6 @@ const (
type strbuf C.struct_strbuf
type strioctl C.struct_strioctl
type Strioctl C.struct_strioctl
type lifreq C.struct_lifreq
type Lifreq C.struct_lifreq

View File

@@ -25,14 +25,14 @@ type strbuf struct {
Buf *int8
}
type strioctl struct {
type Strioctl struct {
Cmd int32
Timout int32
Len int32
Dp *int8
}
type lifreq struct {
type Lifreq struct {
Name [32]int8
Lifru1 [4]byte
Type uint32