Skip to content

Commit

Permalink
move code to go-windows
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Jan 6, 2020
1 parent ffd89d3 commit f6f8006
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 148 deletions.
24 changes: 9 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
[![Build Status](https://dev.azure.com/oneeyedelf1/wasm-imagemagick/_apis/build/status/KnicKnic.go-failovercluster-api?branchName=master)](https://dev.azure.com/oneeyedelf1/wasm-imagemagick/_build/latest?definitionId=4&branchName=master)
[![Build Status](https://dev.azure.com/oneeyedelf1/powershell.native/_apis/build/status/KnicKnic.go-windows?branchName=master)](https://dev.azure.com/oneeyedelf1/powershell.native/_build/latest?definitionId=5&branchName=master)

# Goal

To create bindings to allow you a go application to call Microsoft Windows Server Failover Cluster Api.
To create bindings to allow you a go application to call various Microsoft Windows Server Apis.

Currently uses syscall to wrap the c clusapi.dll and resutils.dll code.

## Completed
## Contents (pkg/...)

Parts of the following cluster api sets
1. Cluster
1. Resource
1. Registry
1. Crypto

## TODO

* add comments for public functions
* Write more tests
* complete more wrappers for functions
* [Cluster](pkg/cluster/Readme.md)
* Microsoft Windows Failover Cluster bindings
* [kernel32](pkg/kernel32)
* LocalAlloc & LocalFree
* [ntdll](pkg/ntdll)
* memcpy
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module github.com/KnicKnic/go-failovercluster-api
module github.com/KnicKnic/go-windows

go 1.13

require (
github.com/stretchr/testify v1.4.0
golang.org/dl v0.0.0-20191205014302-95494741406c // indirect
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/dl v0.0.0-20191205014302-95494741406c/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
19 changes: 19 additions & 0 deletions pkg/cluster/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Goal

To create bindings to allow you a go application to call Microsoft Windows Server Failover Cluster Api.

Currently uses syscall to wrap the c clusapi.dll and resutils.dll code.

## Completed

Parts of the following cluster api sets
1. Cluster
1. Resource
1. Registry
1. Crypto

## TODO

* add comments for public functions
* Write more tests
* complete more wrappers for functions
2 changes: 1 addition & 1 deletion pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"syscall"
"unsafe"

"github.com/KnicKnic/go-failovercluster-api/pkg/errors"
"github.com/KnicKnic/go-windows/pkg/errors"
"golang.org/x/sys/windows"
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/cluster/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"syscall"
"unsafe"

"github.com/KnicKnic/go-failovercluster-api/pkg/errors"
"github.com/KnicKnic/go-failovercluster-api/pkg/kernel32"
"github.com/KnicKnic/go-failovercluster-api/pkg/ntdll"
"github.com/KnicKnic/go-windows/pkg/errors"
"github.com/KnicKnic/go-windows/pkg/kernel32"
"github.com/KnicKnic/go-windows/pkg/ntdll"
"golang.org/x/sys/windows"
)

Expand Down
10 changes: 5 additions & 5 deletions pkg/cluster/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"syscall"
"unsafe"

"github.com/KnicKnic/go-failovercluster-api/pkg/errors"
"github.com/KnicKnic/go-failovercluster-api/pkg/memory"
"github.com/KnicKnic/go-windows/pkg/errors"
"github.com/KnicKnic/go-windows/pkg/util/guid"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ func (handle KeyHandle) SetByteValue(value string, data []byte) error {
}

// SetGuidValue sets a value on a key
func (handle KeyHandle) SetGuidValue(value string, guid memory.GUID) error {
func (handle KeyHandle) SetGuidValue(value string, guid guid.GUID) error {
data, err := guid.ToByte()
if err != nil {
return err
Expand Down Expand Up @@ -214,7 +214,7 @@ func (handle KeyHandle) QueryByteValue(valueName string) (data []byte, err error
}

// QueryGuidValue returns syscall.ERROR_FILE_NOT_FOUND if value does not exist
func (handle KeyHandle) QueryGuidValue(valueName string) (data memory.GUID, err error) {
func (handle KeyHandle) QueryGuidValue(valueName string) (data guid.GUID, err error) {
dataBuf, err := handle.QueryByteValue(valueName)
if err != nil {
return
Expand All @@ -224,6 +224,6 @@ func (handle KeyHandle) QueryGuidValue(valueName string) (data memory.GUID, err
return
}

data, err = memory.GuidFromBytes(dataBuf)
data, err = guid.FromBytes(dataBuf)
return
}
4 changes: 2 additions & 2 deletions pkg/cluster/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"syscall"
"testing"

"github.com/KnicKnic/go-failovercluster-api/pkg/memory"
"github.com/KnicKnic/go-windows/pkg/util/guid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func TestQueryInvalidValue(t *testing.T) {
assert.Equal(t, syscall.ERROR_FILE_NOT_FOUND, err)

myGuidStr := "206994D6-C7B7-ABDB-D89E-AB9CBF3853C4"
myGuid, err := memory.GuidFromString(myGuidStr)
myGuid, err := guid.FromString(myGuidStr)
err = key.SetGuidValue("test_guid_value", myGuid)
assert.Nil(t, err)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"syscall"
"unsafe"

"github.com/KnicKnic/go-failovercluster-api/pkg/errors"
"github.com/KnicKnic/go-windows/pkg/errors"
"golang.org/x/sys/windows"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel32/kernel32.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kernel32
import (
"syscall"

"github.com/KnicKnic/go-failovercluster-api/pkg/errors"
"github.com/KnicKnic/go-windows/pkg/errors"
"golang.org/x/sys/windows"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/ntdll/ntdll.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"syscall"
"unsafe"

"github.com/KnicKnic/go-failovercluster-api/pkg/kernel32"
"github.com/KnicKnic/go-windows/pkg/kernel32"
"golang.org/x/sys/windows"
)

Expand Down
34 changes: 17 additions & 17 deletions pkg/memory/convert.go → pkg/util/convert.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package memory

import (
"encoding/binary"
"unsafe"
)

func Uint32ToByte(data uint32) (output []byte) {
output = make([]byte, int(unsafe.Sizeof(data)))
binary.LittleEndian.PutUint32(output, data)
return
}
func Uint64ToByte(data uint64) (output []byte) {
output = make([]byte, int(unsafe.Sizeof(data)))
binary.LittleEndian.PutUint64(output, data)
return
}
package util

import (
"encoding/binary"
"unsafe"
)

func Uint32ToByte(data uint32) (output []byte) {
output = make([]byte, int(unsafe.Sizeof(data)))
binary.LittleEndian.PutUint32(output, data)
return
}
func Uint64ToByte(data uint64) (output []byte) {
output = make([]byte, int(unsafe.Sizeof(data)))
binary.LittleEndian.PutUint64(output, data)
return
}
12 changes: 6 additions & 6 deletions pkg/memory/convert_test.go → pkg/util/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package memory

// func TestGuidToByte(t *testing.T) {

// syscall.GUID
// }
package util

// func TestGuidToByte(t *testing.T) {

// syscall.GUID
// }
124 changes: 62 additions & 62 deletions pkg/memory/guid.go → pkg/util/guid/guid.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
package memory

import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
)

type GUID struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]byte
}

func (guid GUID) String() string {
return fmt.Sprintf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7])
}
func GuidFromString(g string) (guid GUID, err error) {
_, err = fmt.Sscanf(g, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
&guid.Data1, &guid.Data2, &guid.Data3,
&guid.Data4[0], &guid.Data4[1], &guid.Data4[2], &guid.Data4[3],
&guid.Data4[4], &guid.Data4[5], &guid.Data4[6], &guid.Data4[7])
return
}

func GenerateGuidByte() (data []byte, err error) {

data = make([]byte, 16)
_, err = rand.Read(data)
return
}

func GenerateGuid() (guid GUID, err error) {
data, err := GenerateGuidByte()
if err != nil {
return
}
guid, err = GuidFromBytes(data)
return
}

func (data GUID) ToByte() (output []byte, err error) {

buf := new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, data)
if err != nil {
return
}
output = buf.Bytes()
return
}
func GuidFromBytes(data []byte) (guid GUID, err error) {

buf := bytes.NewReader(data)
err = binary.Read(buf, binary.LittleEndian, &guid)
return
}
package guid

import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
)

type GUID struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]byte
}

func (guid GUID) String() string {
return fmt.Sprintf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7])
}
func FromString(g string) (guid GUID, err error) {
_, err = fmt.Sscanf(g, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
&guid.Data1, &guid.Data2, &guid.Data3,
&guid.Data4[0], &guid.Data4[1], &guid.Data4[2], &guid.Data4[3],
&guid.Data4[4], &guid.Data4[5], &guid.Data4[6], &guid.Data4[7])
return
}

func GenerateBytes() (data []byte, err error) {

data = make([]byte, 16)
_, err = rand.Read(data)
return
}

func Generate() (guid GUID, err error) {
data, err := GenerateBytes()
if err != nil {
return
}
guid, err = FromBytes(data)
return
}

func (data GUID) ToByte() (output []byte, err error) {

buf := new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, data)
if err != nil {
return
}
output = buf.Bytes()
return
}
func FromBytes(data []byte) (guid GUID, err error) {

buf := bytes.NewReader(data)
err = binary.Read(buf, binary.LittleEndian, &guid)
return
}
Loading

0 comments on commit f6f8006

Please sign in to comment.