Skip to content

Commit

Permalink
Merge pull request #51 from darrenstahlmsft/NoCGO
Browse files Browse the repository at this point in the history
Remove cgo as a dependency added by #49
  • Loading branch information
jstarks authored May 8, 2017
2 parents 518e6eb + de045f7 commit d311c76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

package winio

// #include <stdlib.h>
import "C"

import (
"errors"
"io"
Expand All @@ -21,10 +18,12 @@ import (
//sys waitNamedPipe(name string, timeout uint32) (err error) = WaitNamedPipeW
//sys getNamedPipeInfo(pipe syscall.Handle, flags *uint32, outSize *uint32, inSize *uint32, maxInstances *uint32) (err error) = GetNamedPipeInfo
//sys getNamedPipeHandleState(pipe syscall.Handle, state *uint32, curInstances *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32, userName *uint16, maxUserNameSize uint32) (err error) = GetNamedPipeHandleStateW
//sys localAlloc(uFlags uint32, length uint32) (ptr uintptr) = LocalAlloc
//sys copyMemory(dst uintptr, src uintptr, length uint32) = RtlCopyMemory

type securityAttributes struct {
Length uint32
SecurityDescriptor unsafe.Pointer
SecurityDescriptor uintptr
InheritHandle uint32
}

Expand Down Expand Up @@ -237,8 +236,10 @@ func makeServerPipeHandle(path string, securityDescriptor []byte, c *PipeConfig,
sa := &securityAttributes{}
sa.Length = uint32(unsafe.Sizeof(*sa))
if securityDescriptor != nil {
sa.SecurityDescriptor = C.CBytes(securityDescriptor)
defer C.free(sa.SecurityDescriptor)
len := uint32(len(securityDescriptor))
sa.SecurityDescriptor = localAlloc(0, len)
defer localFree(sa.SecurityDescriptor)
copyMemory(sa.SecurityDescriptor, uintptr(unsafe.Pointer(&securityDescriptor[0])), len)
}
h, err := createNamedPipe(path, flags, mode, cPIPE_UNLIMITED_INSTANCES, uint32(c.OutputBufferSize), uint32(c.InputBufferSize), 0, sa)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions zsyscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ var (
procWaitNamedPipeW = modkernel32.NewProc("WaitNamedPipeW")
procGetNamedPipeInfo = modkernel32.NewProc("GetNamedPipeInfo")
procGetNamedPipeHandleStateW = modkernel32.NewProc("GetNamedPipeHandleStateW")
procLocalAlloc = modkernel32.NewProc("LocalAlloc")
procRtlCopyMemory = modkernel32.NewProc("RtlCopyMemory")
procLookupAccountNameW = modadvapi32.NewProc("LookupAccountNameW")
procConvertSidToStringSidW = modadvapi32.NewProc("ConvertSidToStringSidW")
procConvertStringSecurityDescriptorToSecurityDescriptorW = modadvapi32.NewProc("ConvertStringSecurityDescriptorToSecurityDescriptorW")
Expand Down Expand Up @@ -228,6 +230,17 @@ func getNamedPipeHandleState(pipe syscall.Handle, state *uint32, curInstances *u
return
}

func localAlloc(uFlags uint32, length uint32) (ptr uintptr) {
r0, _, _ := syscall.Syscall(procLocalAlloc.Addr(), 2, uintptr(uFlags), uintptr(length), 0)
ptr = uintptr(r0)
return
}

func copyMemory(dst uintptr, src uintptr, length uint32) {
syscall.Syscall(procRtlCopyMemory.Addr(), 3, uintptr(dst), uintptr(src), uintptr(length))
return
}

func lookupAccountName(systemName *uint16, accountName string, sid *byte, sidSize *uint32, refDomain *uint16, refDomainSize *uint32, sidNameUse *uint32) (err error) {
var _p0 *uint16
_p0, err = syscall.UTF16PtrFromString(accountName)
Expand Down

0 comments on commit d311c76

Please sign in to comment.