Skip to content

Commit a82c058

Browse files
authored
fix: allow named pipe protocol support for ARM64 Windows (#232)
* fix: allow named pipe protocol support for ARM64 Windows * Revert "fix(namedpipe): Restrict package to supported platforms (#298)" This reverts commit 0997918. * fix: skip pipe protocol check test when not on Windows Signed-off-by: Niall Newman <nn@turacolabs.com> --------- Signed-off-by: Niall Newman <nn@turacolabs.com>
1 parent 2bc1143 commit a82c058

7 files changed

Lines changed: 187 additions & 22 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//go:build windows
2+
// +build windows
3+
4+
// go build mksyscall_windows.go && ./mksyscall_windows npipe_windows.go
5+
// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
6+
7+
package npipe
8+
9+
import "unsafe"
10+
import "syscall"
11+
12+
var (
13+
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
14+
15+
procCreateNamedPipeW = modkernel32.NewProc("CreateNamedPipeW")
16+
procConnectNamedPipe = modkernel32.NewProc("ConnectNamedPipe")
17+
procDisconnectNamedPipe = modkernel32.NewProc("DisconnectNamedPipe")
18+
procWaitNamedPipeW = modkernel32.NewProc("WaitNamedPipeW")
19+
procCreateEventW = modkernel32.NewProc("CreateEventW")
20+
procGetOverlappedResult = modkernel32.NewProc("GetOverlappedResult")
21+
procCancelIoEx = modkernel32.NewProc("CancelIoEx")
22+
)
23+
24+
func createNamedPipe(name *uint16, openMode uint32, pipeMode uint32, maxInstances uint32, outBufSize uint32, inBufSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) {
25+
r0, _, e1 := syscall.Syscall9(procCreateNamedPipeW.Addr(), 8, uintptr(unsafe.Pointer(name)), uintptr(openMode), uintptr(pipeMode), uintptr(maxInstances), uintptr(outBufSize), uintptr(inBufSize), uintptr(defaultTimeout), uintptr(unsafe.Pointer(sa)), 0)
26+
handle = syscall.Handle(r0)
27+
if handle == syscall.InvalidHandle {
28+
if e1 != 0 {
29+
err = error(e1)
30+
} else {
31+
err = syscall.EINVAL
32+
}
33+
}
34+
return
35+
}
36+
37+
func cancelIoEx(handle syscall.Handle, overlapped *syscall.Overlapped) (err error) {
38+
r1, _, e1 := syscall.Syscall(procCancelIoEx.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), 0)
39+
if r1 == 0 {
40+
if e1 != 0 {
41+
err = error(e1)
42+
} else {
43+
err = syscall.EINVAL
44+
}
45+
}
46+
return
47+
}
48+
49+
func connectNamedPipe(handle syscall.Handle, overlapped *syscall.Overlapped) (err error) {
50+
r1, _, e1 := syscall.Syscall(procConnectNamedPipe.Addr(), 2, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), 0)
51+
if r1 == 0 {
52+
if e1 != 0 {
53+
err = error(e1)
54+
} else {
55+
err = syscall.EINVAL
56+
}
57+
}
58+
return
59+
}
60+
61+
func disconnectNamedPipe(handle syscall.Handle) (err error) {
62+
r1, _, e1 := syscall.Syscall(procDisconnectNamedPipe.Addr(), 1, uintptr(handle), 0, 0)
63+
if r1 == 0 {
64+
if e1 != 0 {
65+
err = error(e1)
66+
} else {
67+
err = syscall.EINVAL
68+
}
69+
}
70+
return
71+
}
72+
73+
func waitNamedPipe(name *uint16, timeout uint32) (err error) {
74+
r1, _, e1 := syscall.Syscall(procWaitNamedPipeW.Addr(), 2, uintptr(unsafe.Pointer(name)), uintptr(timeout), 0)
75+
if r1 == 0 {
76+
if e1 != 0 {
77+
err = error(e1)
78+
} else {
79+
err = syscall.EINVAL
80+
}
81+
}
82+
return
83+
}
84+
85+
func createEvent(sa *syscall.SecurityAttributes, manualReset bool, initialState bool, name *uint16) (handle syscall.Handle, err error) {
86+
var _p0 uint32
87+
if manualReset {
88+
_p0 = 1
89+
} else {
90+
_p0 = 0
91+
}
92+
var _p1 uint32
93+
if initialState {
94+
_p1 = 1
95+
} else {
96+
_p1 = 0
97+
}
98+
r0, _, e1 := syscall.Syscall6(procCreateEventW.Addr(), 4, uintptr(unsafe.Pointer(sa)), uintptr(_p0), uintptr(_p1), uintptr(unsafe.Pointer(name)), 0, 0)
99+
handle = syscall.Handle(r0)
100+
if handle == syscall.InvalidHandle {
101+
if e1 != 0 {
102+
err = error(e1)
103+
} else {
104+
err = syscall.EINVAL
105+
}
106+
}
107+
return
108+
}
109+
110+
func getOverlappedResult(handle syscall.Handle, overlapped *syscall.Overlapped, transferred *uint32, wait bool) (err error) {
111+
var _p0 uint32
112+
if wait {
113+
_p0 = 1
114+
} else {
115+
_p0 = 0
116+
}
117+
r1, _, e1 := syscall.Syscall6(procGetOverlappedResult.Addr(), 4, uintptr(handle), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(transferred)), uintptr(_p0), 0, 0)
118+
if r1 == 0 {
119+
if e1 != 0 {
120+
err = error(e1)
121+
} else {
122+
err = syscall.EINVAL
123+
}
124+
}
125+
return
126+
}

namedpipe/namedpipe.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package namedpipe
2+
3+
import (
4+
"runtime"
5+
6+
"github.com/microsoft/go-mssqldb/msdsn"
7+
)
8+
9+
type namedPipeDialer struct{}
10+
11+
var dialer namedPipeDialer = namedPipeDialer{}
12+
13+
func init() {
14+
if runtime.GOOS == "windows" {
15+
msdsn.ProtocolParsers = append(msdsn.ProtocolParsers, dialer)
16+
msdsn.ProtocolDialers["np"] = dialer
17+
}
18+
}

namedpipe/namedpipe_others.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

namedpipe/namedpipe_windows.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build windows && (amd64 || 386)
2-
31
package namedpipe
42

53
import (
@@ -13,18 +11,16 @@ import (
1311
"github.com/microsoft/go-mssqldb/msdsn"
1412
)
1513

14+
type namedPipeData struct {
15+
PipeName string
16+
}
17+
1618
var azureDomains = []string{
1719
".database.windows.net",
1820
".database.chinacloudapi.cn",
1921
".database.usgovcloudapi.net",
2022
}
2123

22-
type namedPipeData struct {
23-
PipeName string
24-
}
25-
26-
type namedPipeDialer struct{}
27-
2824
func (n namedPipeDialer) ParseServer(server string, p *msdsn.Config) error {
2925
if p.Port > 0 {
3026
return fmt.Errorf("Named pipes disallowed due to port being specified")
@@ -111,10 +107,3 @@ func (n namedPipeDialer) CallBrowser(p *msdsn.Config) bool {
111107
_, ok := p.ProtocolParameters[n.Protocol()]
112108
return !ok
113109
}
114-
115-
func init() {
116-
dialer := namedPipeDialer{}
117-
118-
msdsn.ProtocolParsers = append(msdsn.ProtocolParsers, dialer)
119-
msdsn.ProtocolDialers["np"] = dialer
120-
}

namedpipe/namepipe_windows_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build windows && (amd64 || 386)
2-
31
package namedpipe
42

53
import (

namedpipe/stub.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package namedpipe
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"net"
10+
11+
"github.com/microsoft/go-mssqldb/msdsn"
12+
)
13+
14+
func (n namedPipeDialer) ParseServer(server string, p *msdsn.Config) error {
15+
return fmt.Errorf("Named pipe connections are not supported on this operating system")
16+
}
17+
18+
func (n namedPipeDialer) Protocol() string {
19+
return "np"
20+
}
21+
22+
func (n namedPipeDialer) Hidden() bool {
23+
return false
24+
}
25+
26+
func (n namedPipeDialer) ParseBrowserData(data msdsn.BrowserData, p *msdsn.Config) error {
27+
return fmt.Errorf("Named pipe connections are not supported on this operating system")
28+
}
29+
30+
func (n namedPipeDialer) DialConnection(ctx context.Context, p *msdsn.Config) (conn net.Conn, err error) {
31+
32+
return nil, fmt.Errorf("Named pipe connections are not supported on this operating system")
33+
}
34+
35+
func (n namedPipeDialer) CallBrowser(p *msdsn.Config) bool {
36+
return false
37+
}

namedpipe_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
)
1414

1515
func TestNamedPipeProtocolInstalled(t *testing.T) {
16-
if runtime.GOOS != "windows" || !(runtime.GOARCH == "amd64" || runtime.GOARCH == "386") {
17-
t.Skip("Skipping tests for unsupported platforms...")
16+
if runtime.GOOS != "windows" {
17+
t.Skip("Named pipes only registered on Windows")
1818
}
1919
for _, p := range msdsn.ProtocolParsers {
2020
if p.Protocol() == "np" {

0 commit comments

Comments
 (0)