mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-08-29 13:26:51 -04:00
* Add lint and go generate stages to CI
Add CI step to verify `go generate` was run on repo.
Add linter stage to CI along with linter config file,
`.golangci.yml`.
Will likely prefer revive over static-check.
Updated README Contributing section on linting requirements.
Added sequence ordering to make sure lint and go generate stages run
before tests and build.
This way, build and tests are not run on code that could potentially:
1. not build due to `gofmt` issues;
2. contain bugs;
3. have to be re-submitted after issues are fixed; or
4. contain outdated Win32 syscall or other auto-generated files.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* Fixed linter issues
Code changes to satisfy linters:
- Ran `gofmt -s -w` on repo.
- Broke up long lines.
- When possible, changed names with incorrect initialism formatting
- Added exceptions for exported variables.
- Added exceptions for ALL_CAPS_WITH_UNDERSCORES code.
- Switched to using `windows` or `syscall` definitions if possible;
especially if some constants were unused.
- Added `_ =` to satisfy error linter, and acknowledge that errors are
being ignored.
- Switched to using `errors.Is` and `As` in places, elsewhere added
exceptions if error value was known to be `syscall.Errno`.
- Removed bare returns.
- Prevented variables from being overshadowed in certain places
(ignoring cases of overshadowing `err`).
- Renamed variables and functions (eg, `len`, `eventMetadata.bytes`) to
prevent shadowing pre-built functions and imported pacakges.
- Removed unused method receivers.
- Added exceptions to certain unused (unexported) constants and
functions.
- Deleted unused `once` from `pkg/etw.providerMap`.
- Renamed `noop.go` files to `main_other.go` or `doc.go`, to better fit
style recommendations.
- Added exceptions for non-secure use of SHA1 and weak crypto
libraries.
- Replaced `ioutil` with `io` and `os` (and `t.TempDir` in tests).
- Added fully exhaustive checks for `switch` statements in `pkg/etw`.
- Defined constant strings for `tools/mkwinsyscall`.
- Removed unnecessary conversions.
- Made sure `context.Cancel` was called.
Additionally, added `//go:build windows" constraints on files with
unexported code, since linter will complain about unused code on
non-Windows platforms.
Added a stub `main() {}` for `mkwinsyscall` for non-Windows builds, just in
case `//go:generate` directives are added to OS-agnostic files.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* PR: spelling, constants, fuzzing
Moved HVSocket fuzzing tests to separate file with go 1.18 build
constraint.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
180 lines
4.9 KiB
Go
180 lines
4.9 KiB
Go
//go:build windows
|
|
|
|
package socket
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"github.com/Microsoft/go-winio/pkg/guid"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go socket.go
|
|
|
|
//sys getsockname(s windows.Handle, name unsafe.Pointer, namelen *int32) (err error) [failretval==socketError] = ws2_32.getsockname
|
|
//sys getpeername(s windows.Handle, name unsafe.Pointer, namelen *int32) (err error) [failretval==socketError] = ws2_32.getpeername
|
|
//sys bind(s windows.Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socketError] = ws2_32.bind
|
|
|
|
const socketError = uintptr(^uint32(0))
|
|
|
|
var (
|
|
// todo(helsaawy): create custom error types to store the desired vs actual size and addr family?
|
|
|
|
ErrBufferSize = errors.New("buffer size")
|
|
ErrAddrFamily = errors.New("address family")
|
|
ErrInvalidPointer = errors.New("invalid pointer")
|
|
ErrSocketClosed = fmt.Errorf("socket closed: %w", net.ErrClosed)
|
|
)
|
|
|
|
// todo(helsaawy): replace these with generics, ie: GetSockName[S RawSockaddr](s windows.Handle) (S, error)
|
|
|
|
// GetSockName writes the local address of socket s to the [RawSockaddr] rsa.
|
|
// If rsa is not large enough, the [windows.WSAEFAULT] is returned.
|
|
func GetSockName(s windows.Handle, rsa RawSockaddr) error {
|
|
ptr, l, err := rsa.Sockaddr()
|
|
if err != nil {
|
|
return fmt.Errorf("could not retrieve socket pointer and size: %w", err)
|
|
}
|
|
|
|
// although getsockname returns WSAEFAULT if the buffer is too small, it does not set
|
|
// &l to the correct size, so--apart from doubling the buffer repeatedly--there is no remedy
|
|
return getsockname(s, ptr, &l)
|
|
}
|
|
|
|
// GetPeerName returns the remote address the socket is connected to.
|
|
//
|
|
// See [GetSockName] for more information.
|
|
func GetPeerName(s windows.Handle, rsa RawSockaddr) error {
|
|
ptr, l, err := rsa.Sockaddr()
|
|
if err != nil {
|
|
return fmt.Errorf("could not retrieve socket pointer and size: %w", err)
|
|
}
|
|
|
|
return getpeername(s, ptr, &l)
|
|
}
|
|
|
|
func Bind(s windows.Handle, rsa RawSockaddr) (err error) {
|
|
ptr, l, err := rsa.Sockaddr()
|
|
if err != nil {
|
|
return fmt.Errorf("could not retrieve socket pointer and size: %w", err)
|
|
}
|
|
|
|
return bind(s, ptr, l)
|
|
}
|
|
|
|
// "golang.org/x/sys/windows".ConnectEx and .Bind only accept internal implementations of the
|
|
// their sockaddr interface, so they cannot be used with HvsockAddr
|
|
// Replicate functionality here from
|
|
// https://cs.opensource.google/go/x/sys/+/master:windows/syscall_windows.go
|
|
|
|
// The function pointers to `AcceptEx`, `ConnectEx` and `GetAcceptExSockaddrs` must be loaded at
|
|
// runtime via a WSAIoctl call:
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/Mswsock/nc-mswsock-lpfn_connectex#remarks
|
|
|
|
type runtimeFunc struct {
|
|
id guid.GUID
|
|
once sync.Once
|
|
addr uintptr
|
|
err error
|
|
}
|
|
|
|
func (f *runtimeFunc) Load() error {
|
|
f.once.Do(func() {
|
|
var s windows.Handle
|
|
s, f.err = windows.Socket(windows.AF_INET, windows.SOCK_STREAM, windows.IPPROTO_TCP)
|
|
if f.err != nil {
|
|
return
|
|
}
|
|
defer windows.CloseHandle(s) //nolint:errcheck
|
|
|
|
var n uint32
|
|
f.err = windows.WSAIoctl(s,
|
|
windows.SIO_GET_EXTENSION_FUNCTION_POINTER,
|
|
(*byte)(unsafe.Pointer(&f.id)),
|
|
uint32(unsafe.Sizeof(f.id)),
|
|
(*byte)(unsafe.Pointer(&f.addr)),
|
|
uint32(unsafe.Sizeof(f.addr)),
|
|
&n,
|
|
nil, //overlapped
|
|
0, //completionRoutine
|
|
)
|
|
})
|
|
return f.err
|
|
}
|
|
|
|
var (
|
|
// todo: add `AcceptEx` and `GetAcceptExSockaddrs`
|
|
WSAID_CONNECTEX = guid.GUID{ //revive:disable-line:var-naming ALL_CAPS
|
|
Data1: 0x25a207b9,
|
|
Data2: 0xddf3,
|
|
Data3: 0x4660,
|
|
Data4: [8]byte{0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e},
|
|
}
|
|
|
|
connectExFunc = runtimeFunc{id: WSAID_CONNECTEX}
|
|
)
|
|
|
|
func ConnectEx(
|
|
fd windows.Handle,
|
|
rsa RawSockaddr,
|
|
sendBuf *byte,
|
|
sendDataLen uint32,
|
|
bytesSent *uint32,
|
|
overlapped *windows.Overlapped,
|
|
) error {
|
|
if err := connectExFunc.Load(); err != nil {
|
|
return fmt.Errorf("failed to load ConnectEx function pointer: %w", err)
|
|
}
|
|
ptr, n, err := rsa.Sockaddr()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return connectEx(fd, ptr, n, sendBuf, sendDataLen, bytesSent, overlapped)
|
|
}
|
|
|
|
// BOOL LpfnConnectex(
|
|
// [in] SOCKET s,
|
|
// [in] const sockaddr *name,
|
|
// [in] int namelen,
|
|
// [in, optional] PVOID lpSendBuffer,
|
|
// [in] DWORD dwSendDataLength,
|
|
// [out] LPDWORD lpdwBytesSent,
|
|
// [in] LPOVERLAPPED lpOverlapped
|
|
// )
|
|
|
|
func connectEx(
|
|
s windows.Handle,
|
|
name unsafe.Pointer,
|
|
namelen int32,
|
|
sendBuf *byte,
|
|
sendDataLen uint32,
|
|
bytesSent *uint32,
|
|
overlapped *windows.Overlapped,
|
|
) (err error) {
|
|
// todo: after upgrading to 1.18, switch from syscall.Syscall9 to syscall.SyscallN
|
|
r1, _, e1 := syscall.Syscall9(connectExFunc.addr,
|
|
7,
|
|
uintptr(s),
|
|
uintptr(name),
|
|
uintptr(namelen),
|
|
uintptr(unsafe.Pointer(sendBuf)),
|
|
uintptr(sendDataLen),
|
|
uintptr(unsafe.Pointer(bytesSent)),
|
|
uintptr(unsafe.Pointer(overlapped)),
|
|
0,
|
|
0)
|
|
if r1 == 0 {
|
|
if e1 != 0 {
|
|
err = error(e1)
|
|
} else {
|
|
err = syscall.EINVAL
|
|
}
|
|
}
|
|
return err
|
|
}
|