package winio
import "github.com/Microsoft/go-winio"
This package provides utilities for efficiently performing Win32 IO operations in Go. Currently, this package is provides support for genreal IO and management of
- named pipes
- files
- Hyper-V sockets
This code is similar to Go's net package, and uses IO completion ports to avoid blocking IO on system threads, allowing Go to reuse the thread to schedule other goroutines.
This limits support to Windows Vista and newer operating systems.
Additionally, this package provides support for:
- creating and managing GUIDs
- writing to ETW
- opening and manageing VHDs
- parsing Windows Image files
- auto-generating Win32 API code
Index ¶
- Constants
- Variables
- func DialPipe(path string, timeout *time.Duration) (net.Conn, error)
- func DialPipeAccess(ctx context.Context, path string, access uint32) (net.Conn, error)
- func DialPipeAccessImpLevel(ctx context.Context, path string, access uint32, impLevel PipeImpLevel) (net.Conn, error)
- func DialPipeContext(ctx context.Context, path string) (net.Conn, error)
- func DisableProcessPrivileges(names []string) error
- func EnableProcessPrivileges(names []string) error
- func EncodeExtendedAttributes(eas []ExtendedAttribute) ([]byte, error)
- func EncodeReparsePoint(rp *ReparsePoint) []byte
- func HvsockGUIDBroadcast() guid.GUID
- func HvsockGUIDChildren() guid.GUID
- func HvsockGUIDLoopback() guid.GUID
- func HvsockGUIDParent() guid.GUID
- func HvsockGUIDSiloHost() guid.GUID
- func HvsockGUIDWildcard() guid.GUID
- func ListenPipe(path string, c *PipeConfig) (net.Listener, error)
- func LookupNameBySid(sid string) (name string, err error)
- func LookupSidByName(name string) (sid string, err error)
- func MakeOpenFile(h syscall.Handle) (io.ReadWriteCloser, error)
- func NewOpenFile(h windows.Handle) (io.ReadWriteCloser, error)
- func OpenForBackup(path string, access uint32, share uint32, createmode uint32) (*os.File, error)
- func RunWithPrivilege(name string, fn func() error) error
- func RunWithPrivileges(names []string, fn func() error) error
- func SddlToSecurityDescriptor(sddl string) ([]byte, error)
- func SecurityDescriptorToSddl(sd []byte) (string, error)
- func SetFileBasicInfo(f *os.File, bi *FileBasicInfo) error
- func VsockServiceID(port uint32) guid.GUID
- type AccountLookupError
- type BackupFileReader
- func NewBackupFileReader(f *os.File, includeSecurity bool) *BackupFileReader
- func (r *BackupFileReader) Close() error
- func (r *BackupFileReader) Read(b []byte) (int, error)
- type BackupFileWriter
- func NewBackupFileWriter(f *os.File, includeSecurity bool) *BackupFileWriter
- func (w *BackupFileWriter) Close() error
- func (w *BackupFileWriter) Write(b []byte) (int, error)
- type BackupHeader
- type BackupStreamReader
- func NewBackupStreamReader(r io.Reader) *BackupStreamReader
- func (r *BackupStreamReader) Next() (*BackupHeader, error)
- func (r *BackupStreamReader) Read(b []byte) (int, error)
- type BackupStreamWriter
- func NewBackupStreamWriter(w io.Writer) *BackupStreamWriter
- func (w *BackupStreamWriter) Write(b []byte) (int, error)
- func (w *BackupStreamWriter) WriteHeader(hdr *BackupHeader) error
- type ExtendedAttribute
- type FileBasicInfo
- type FileIDInfo
- type FileStandardInfo
- type HvsockAddr
- type HvsockConn
- func Dial(ctx context.Context, addr *HvsockAddr) (conn *HvsockConn, err error)
- func (conn *HvsockConn) Close() error
- func (conn *HvsockConn) CloseRead() error
- func (conn *HvsockConn) CloseWrite() error
- func (conn *HvsockConn) IsClosed() bool
- func (conn *HvsockConn) LocalAddr() net.Addr
- func (conn *HvsockConn) Read(b []byte) (int, error)
- func (conn *HvsockConn) RemoteAddr() net.Addr
- func (conn *HvsockConn) SetDeadline(t time.Time) error
- func (conn *HvsockConn) SetReadDeadline(t time.Time) error
- func (conn *HvsockConn) SetWriteDeadline(t time.Time) error
- func (conn *HvsockConn) Write(b []byte) (int, error)
- type HvsockDialer
- type HvsockListener
- func ListenHvsock(addr *HvsockAddr) (_ *HvsockListener, err error)
- func (l *HvsockListener) Accept() (_ net.Conn, err error)
- func (l *HvsockListener) Addr() net.Addr
- func (l *HvsockListener) Close() error
- type PipeConfig
- type PipeConn
- type PipeImpLevel
- type PrivilegeError
- type ReparsePoint
- func DecodeReparsePoint(b []byte) (*ReparsePoint, error)
- func DecodeReparsePointData(tag uint32, b []byte) (*ReparsePoint, error)
- type SddlConversionError
- type UnsupportedReparsePointError
Constants ¶
const ( BackupData = uint32(iota + 1) BackupEaData BackupSecurity BackupAlternateData BackupLink BackupPropertyData BackupObjectId //revive:disable-line:var-naming ID, not Id BackupReparseData BackupSparseBlock BackupTxfsData )
const ( WRITE_DAC = windows.WRITE_DAC WRITE_OWNER = windows.WRITE_OWNER ACCESS_SYSTEM_SECURITY = windows.ACCESS_SYSTEM_SECURITY )
const ( PipeImpLevelAnonymous = PipeImpLevel(fs.SECURITY_ANONYMOUS) PipeImpLevelIdentification = PipeImpLevel(fs.SECURITY_IDENTIFICATION) PipeImpLevelImpersonation = PipeImpLevel(fs.SECURITY_IMPERSONATION) PipeImpLevelDelegation = PipeImpLevel(fs.SECURITY_DELEGATION) )
const ( //revive:disable-next-line:var-naming ALL_CAPS SE_PRIVILEGE_ENABLED = windows.SE_PRIVILEGE_ENABLED //revive:disable-next-line:var-naming ALL_CAPS ERROR_NOT_ALL_ASSIGNED windows.Errno = windows.ERROR_NOT_ALL_ASSIGNED SeBackupPrivilege = "SeBackupPrivilege" SeRestorePrivilege = "SeRestorePrivilege" SeSecurityPrivilege = "SeSecurityPrivilege" )
const ( StreamSparseAttributes = uint32(8) )
Variables ¶
var ( // ErrPipeListenerClosed is returned for pipe operations on listeners that have been closed. ErrPipeListenerClosed = net.ErrClosed )
Functions ¶
func DialPipe ¶
DialPipe connects to a named pipe by path, timing out if the connection takes longer than the specified duration. If timeout is nil, then we use a default timeout of 2 seconds. (We do not use WaitNamedPipe.)
func DialPipeAccess ¶
DialPipeAccess attempts to connect to a named pipe by `path` with `access` until `ctx` cancellation or timeout.
func DialPipeAccessImpLevel ¶
func DialPipeAccessImpLevel(ctx context.Context, path string, access uint32, impLevel PipeImpLevel) (net.Conn, error)
DialPipeAccessImpLevel attempts to connect to a named pipe by `path` with `access` at `impLevel` until `ctx` cancellation or timeout. The other DialPipe* implementations use PipeImpLevelAnonymous.
func DialPipeContext ¶
DialPipeContext attempts to connect to a named pipe by `path` until `ctx` cancellation or timeout.
func DisableProcessPrivileges ¶
DisableProcessPrivileges disables privileges globally for the process.
func EnableProcessPrivileges ¶
EnableProcessPrivileges enables privileges globally for the process.
func EncodeExtendedAttributes ¶
func EncodeExtendedAttributes(eas []ExtendedAttribute) ([]byte, error)
EncodeExtendedAttributes encodes a list of EAs into a FILE_FULL_EA_INFORMATION buffer for use with BackupWrite, ZwSetEaFile, etc.
func EncodeReparsePoint ¶
func EncodeReparsePoint(rp *ReparsePoint) []byte
EncodeReparsePoint encodes a Win32 REPARSE_DATA_BUFFER structure describing a symlink or mount point.
func HvsockGUIDBroadcast ¶
HvsockGUIDBroadcast is the wildcard VmId for broadcasting sends to all partitions.
func HvsockGUIDChildren ¶
HvsockGUIDChildren is the wildcard VmId for accepting connections from the connector's child partitions.
func HvsockGUIDLoopback ¶
HvsockGUIDLoopback is the Loopback VmId for accepting connections to the same partition as the connector.
func HvsockGUIDParent ¶
HvsockGUIDParent is the wildcard VmId for accepting connections from the connector's parent partition. Listening on this VmId accepts connection from:
- Inside silos: silo host partition.
- Inside hosted silo: host of the VM.
- Inside VM: VM host.
- Physical host: Not supported.
func HvsockGUIDSiloHost ¶
HvsockGUIDSiloHost is the address of a silo's host partition:
- The silo host of a hosted silo is the utility VM.
- The silo host of a silo on a physical host is the physical host.
func HvsockGUIDWildcard ¶
HvsockGUIDWildcard is the wildcard VmId for accepting connections from all partitions.
func ListenPipe ¶
func ListenPipe(path string, c *PipeConfig) (net.Listener, error)
ListenPipe creates a listener on a Windows named pipe path, e.g. \\.\pipe\mypipe. The pipe must not already exist.
func LookupNameBySid ¶
LookupNameBySid looks up the name of an account by SID
func LookupSidByName ¶
LookupSidByName looks up the SID of an account by name
func MakeOpenFile ¶
func MakeOpenFile(h syscall.Handle) (io.ReadWriteCloser, error)
Deprecated: use NewOpenFile instead.
func NewOpenFile ¶
func NewOpenFile(h windows.Handle) (io.ReadWriteCloser, error)
func OpenForBackup ¶
OpenForBackup opens a file or directory, potentially skipping access checks if the backup or restore privileges have been acquired.
If the file opened was a directory, it cannot be used with Readdir().
func RunWithPrivilege ¶
RunWithPrivilege enables a single privilege for a function call.
func RunWithPrivileges ¶
RunWithPrivileges enables privileges for a function call.
func SddlToSecurityDescriptor ¶
func SecurityDescriptorToSddl ¶
func SetFileBasicInfo ¶
func SetFileBasicInfo(f *os.File, bi *FileBasicInfo) error
SetFileBasicInfo sets times and attributes for a file.
func VsockServiceID ¶
VsockServiceID returns an hvsock service ID corresponding to the specified AF_VSOCK port.
Types ¶
type AccountLookupError ¶
func (*AccountLookupError) Error ¶
func (e *AccountLookupError) Error() string
func (*AccountLookupError) Unwrap ¶
func (e *AccountLookupError) Unwrap() error
type BackupFileReader ¶
type BackupFileReader struct {
// contains filtered or unexported fields
}
BackupFileReader provides an io.ReadCloser interface on top of the BackupRead Win32 API.
func NewBackupFileReader ¶
func NewBackupFileReader(f *os.File, includeSecurity bool) *BackupFileReader
NewBackupFileReader returns a new BackupFileReader from a file handle. If includeSecurity is true, Read will attempt to read the security descriptor of the file.
func (*BackupFileReader) Close ¶
func (r *BackupFileReader) Close() error
Close frees Win32 resources associated with the BackupFileReader. It does not close the underlying file.
func (*BackupFileReader) Read ¶
func (r *BackupFileReader) Read(b []byte) (int, error)
Read reads a backup stream from the file by calling the Win32 API BackupRead().
type BackupFileWriter ¶
type BackupFileWriter struct {
// contains filtered or unexported fields
}
BackupFileWriter provides an io.WriteCloser interface on top of the BackupWrite Win32 API.
func NewBackupFileWriter ¶
func NewBackupFileWriter(f *os.File, includeSecurity bool) *BackupFileWriter
NewBackupFileWriter returns a new BackupFileWriter from a file handle. If includeSecurity is true, Write() will attempt to restore the security descriptor from the stream.
func (*BackupFileWriter) Close ¶
func (w *BackupFileWriter) Close() error
Close frees Win32 resources associated with the BackupFileWriter. It does not close the underlying file.
func (*BackupFileWriter) Write ¶
func (w *BackupFileWriter) Write(b []byte) (int, error)
Write restores a portion of the file using the provided backup stream.
type BackupHeader ¶
type BackupHeader struct { //revive:disable-next-line:var-naming ID, not Id Id uint32 // The backup stream ID Attributes uint32 // Stream attributes Size int64 // The size of the stream in bytes Name string // The name of the stream (for BackupAlternateData only). Offset int64 // The offset of the stream in the file (for BackupSparseBlock only). }
BackupHeader represents a backup stream of a file.
type BackupStreamReader ¶
type BackupStreamReader struct {
// contains filtered or unexported fields
}
BackupStreamReader reads from a stream produced by the BackupRead Win32 API and produces a series of BackupHeader values.
func NewBackupStreamReader ¶
func NewBackupStreamReader(r io.Reader) *BackupStreamReader
NewBackupStreamReader produces a BackupStreamReader from any io.Reader.
func (*BackupStreamReader) Next ¶
func (r *BackupStreamReader) Next() (*BackupHeader, error)
Next returns the next backup stream and prepares for calls to Read(). It skips the remainder of the current stream if it was not completely read.
func (*BackupStreamReader) Read ¶
func (r *BackupStreamReader) Read(b []byte) (int, error)
Read reads from the current backup stream.
type BackupStreamWriter ¶
type BackupStreamWriter struct {
// contains filtered or unexported fields
}
BackupStreamWriter writes a stream compatible with the BackupWrite Win32 API.
func NewBackupStreamWriter ¶
func NewBackupStreamWriter(w io.Writer) *BackupStreamWriter
NewBackupStreamWriter produces a BackupStreamWriter on top of an io.Writer.
func (*BackupStreamWriter) Write ¶
func (w *BackupStreamWriter) Write(b []byte) (int, error)
Write writes to the current backup stream.
func (*BackupStreamWriter) WriteHeader ¶
func (w *BackupStreamWriter) WriteHeader(hdr *BackupHeader) error
WriteHeader writes the next backup stream header and prepares for calls to Write().
type ExtendedAttribute ¶
ExtendedAttribute represents a single Windows EA.
func DecodeExtendedAttributes ¶
func DecodeExtendedAttributes(b []byte) (eas []ExtendedAttribute, err error)
DecodeExtendedAttributes decodes a list of EAs from a FILE_FULL_EA_INFORMATION buffer retrieved from BackupRead, ZwQueryEaFile, etc.
type FileBasicInfo ¶
type FileBasicInfo struct { CreationTime, LastAccessTime, LastWriteTime, ChangeTime windows.Filetime FileAttributes uint32 // contains filtered or unexported fields }
FileBasicInfo contains file access time and file attributes information.
func GetFileBasicInfo ¶
func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error)
GetFileBasicInfo retrieves times and attributes for a file.
type FileIDInfo ¶
FileIDInfo contains the volume serial number and file ID for a file. This pair should be unique on a system.
func GetFileID ¶
func GetFileID(f *os.File) (*FileIDInfo, error)
GetFileID retrieves the unique (volume, file ID) pair for a file.
type FileStandardInfo ¶
type FileStandardInfo struct { AllocationSize, EndOfFile int64 NumberOfLinks uint32 DeletePending, Directory bool }
FileStandardInfo contains extended information for the file. FILE_STANDARD_INFO in WinBase.h https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_standard_info
func GetFileStandardInfo ¶
func GetFileStandardInfo(f *os.File) (*FileStandardInfo, error)
GetFileStandardInfo retrieves ended information for the file.
type HvsockAddr ¶
An HvsockAddr is an address for a AF_HYPERV socket.
func (*HvsockAddr) Network ¶
func (*HvsockAddr) Network() string
Network returns the address's network name, "hvsock".
func (*HvsockAddr) String ¶
func (addr *HvsockAddr) String() string
type HvsockConn ¶
type HvsockConn struct {
// contains filtered or unexported fields
}
HvsockConn is a connected socket of the AF_HYPERV address family.
func Dial ¶
func Dial(ctx context.Context, addr *HvsockAddr) (conn *HvsockConn, err error)
Dial the Hyper-V socket at addr.
See HvsockDialer.Dial for more information.
func (*HvsockConn) Close ¶
func (conn *HvsockConn) Close() error
Close closes the socket connection, failing any pending read or write calls.
func (*HvsockConn) CloseRead ¶
func (conn *HvsockConn) CloseRead() error
CloseRead shuts down the read end of the socket, preventing future read operations.
func (*HvsockConn) CloseWrite ¶
func (conn *HvsockConn) CloseWrite() error
CloseWrite shuts down the write end of the socket, preventing future write operations and notifying the other endpoint that no more data will be written.
func (*HvsockConn) IsClosed ¶
func (conn *HvsockConn) IsClosed() bool
func (*HvsockConn) LocalAddr ¶
func (conn *HvsockConn) LocalAddr() net.Addr
LocalAddr returns the local address of the connection.
func (*HvsockConn) Read ¶
func (conn *HvsockConn) Read(b []byte) (int, error)
func (*HvsockConn) RemoteAddr ¶
func (conn *HvsockConn) RemoteAddr() net.Addr
RemoteAddr returns the remote address of the connection.
func (*HvsockConn) SetDeadline ¶
func (conn *HvsockConn) SetDeadline(t time.Time) error
SetDeadline implements the net.Conn SetDeadline method.
func (*HvsockConn) SetReadDeadline ¶
func (conn *HvsockConn) SetReadDeadline(t time.Time) error
SetReadDeadline implements the net.Conn SetReadDeadline method.
func (*HvsockConn) SetWriteDeadline ¶
func (conn *HvsockConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements the net.Conn SetWriteDeadline method.
func (*HvsockConn) Write ¶
func (conn *HvsockConn) Write(b []byte) (int, error)
type HvsockDialer ¶
type HvsockDialer struct { // Deadline is the time the Dial operation must connect before erroring. Deadline time.Time // Retries is the number of additional connects to try if the connection times out, is refused, // or the host is unreachable Retries uint // RetryWait is the time to wait after a connection error to retry RetryWait time.Duration // contains filtered or unexported fields }
HvsockDialer configures and dials a Hyper-V Socket (ie, HvsockConn).
func (*HvsockDialer) Dial ¶
func (d *HvsockDialer) Dial(ctx context.Context, addr *HvsockAddr) (conn *HvsockConn, err error)
Dial attempts to connect to the Hyper-V socket at addr, and returns a connection if successful. Will attempt (HvsockDialer).Retries if dialing fails, waiting (HvsockDialer).RetryWait between retries.
Dialing can be cancelled either by providing (HvsockDialer).Deadline, or cancelling ctx.
type HvsockListener ¶
type HvsockListener struct {
// contains filtered or unexported fields
}
HvsockListener is a socket listener for the AF_HYPERV address family.
func ListenHvsock ¶
func ListenHvsock(addr *HvsockAddr) (_ *HvsockListener, err error)
ListenHvsock listens for connections on the specified hvsock address.
func (*HvsockListener) Accept ¶
func (l *HvsockListener) Accept() (_ net.Conn, err error)
Accept waits for the next connection and returns it.
func (*HvsockListener) Addr ¶
func (l *HvsockListener) Addr() net.Addr
Addr returns the listener's network address.
func (*HvsockListener) Close ¶
func (l *HvsockListener) Close() error
Close closes the listener, causing any pending Accept calls to fail.
type PipeConfig ¶
type PipeConfig struct { // SecurityDescriptor contains a Windows security descriptor in SDDL format. SecurityDescriptor string // MessageMode determines whether the pipe is in byte or message mode. In either // case the pipe is read in byte mode by default. The only practical difference in // this implementation is that CloseWrite() is only supported for message mode pipes; // CloseWrite() is implemented as a zero-byte write, but zero-byte writes are only // transferred to the reader (and returned as io.EOF in this implementation) // when the pipe is in message mode. MessageMode bool // InputBufferSize specifies the size of the input buffer, in bytes. InputBufferSize int32 // OutputBufferSize specifies the size of the output buffer, in bytes. OutputBufferSize int32 }
PipeConfig contain configuration for the pipe listener.
type PipeConn ¶
type PipeImpLevel ¶
type PipeImpLevel uint32
PipeImpLevel is an enumeration of impersonation levels that may be set when calling DialPipeAccessImpersonation.
type PrivilegeError ¶
type PrivilegeError struct {
// contains filtered or unexported fields
}
PrivilegeError represents an error enabling privileges.
func (*PrivilegeError) Error ¶
func (e *PrivilegeError) Error() string
type ReparsePoint ¶
ReparsePoint describes a Win32 symlink or mount point.
func DecodeReparsePoint ¶
func DecodeReparsePoint(b []byte) (*ReparsePoint, error)
DecodeReparsePoint decodes a Win32 REPARSE_DATA_BUFFER structure containing either a symlink or a mount point.
func DecodeReparsePointData ¶
func DecodeReparsePointData(tag uint32, b []byte) (*ReparsePoint, error)
type SddlConversionError ¶
func (*SddlConversionError) Error ¶
func (e *SddlConversionError) Error() string
func (*SddlConversionError) Unwrap ¶
func (e *SddlConversionError) Unwrap() error
type UnsupportedReparsePointError ¶
type UnsupportedReparsePointError struct { Tag uint32 }
UnsupportedReparsePointError is returned when trying to decode a non-symlink or mount point reparse point.
func (*UnsupportedReparsePointError) Error ¶
func (e *UnsupportedReparsePointError) Error() string
Source Files ¶
backup.go doc.go ea.go file.go fileinfo.go hvsock.go pipe.go privilege.go reparse.go sd.go syscall.go zsyscall_windows.go
Directories ¶
Path | Synopsis |
---|---|
backuptar | |
internal | |
pkg | |
pkg/bindfilter | |
pkg/etw | Package etw provides support for TraceLogging-based ETW (Event Tracing for Windows). |
pkg/etwlogrus | |
pkg/etw/sample | Shows a sample usage of the ETW logging package. |
pkg/fs | This package contains Win32 filesystem functionality. |
pkg/guid | Package guid provides a GUID type. |
pkg/process | |
pkg/security | |
tools | |
tools/etw-provider-gen | |
tools/mkwinsyscall | mkwinsyscall generates windows system call bodies |
vhd | |
wim | Package wim implements a WIM file parser. |
wim/lzx | Package lzx implements a decompressor for the the WIM variant of the LZX compression algorithm. |
wim/validate |
- Version
- v0.6.2 (latest)
- Published
- Apr 9, 2024
- Platform
- windows/amd64
- Imports
- 20 packages
- Last checked
- now –
Tools for package owners.