npipe

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 8 Imported by: 0

README

npipe

This project is a fork of https://github.com/natefinch/npipe

===== Package npipe provides a pure Go wrapper around Windows named pipes.

Windows named pipe documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365780

npipe provides an interface based on stdlib's net package, with Dial, Listen, and Accept functions, as well as associated implementations of net.Conn and net.Listener. It supports rpc over the connection.

Notes
  • Deadlines for reading/writing to the connection are only functional in Windows Vista/Server 2008 and above, due to limitations with the Windows API.

  • The pipes support byte mode only (no support for message mode)

Examples

The Dial function connects a client to a named pipe:

conn, err := npipe.Dial(`\\.\pipe\mypipename`)
if err != nil {
	<handle error>
}
fmt.Fprintf(conn, "Hi server!\n")
msg, err := bufio.NewReader(conn).ReadString('\n')
...

The Listen function creates servers:

ln, err := npipe.Listen(`\\.\pipe\mypipename`)
if err != nil {
	// handle error
}
for {
	conn, err := ln.Accept()
	if err != nil {
		// handle error
		continue
	}
	go handleConnection(conn)
}

Variables

var ErrClosed = PipeError{"Pipe has been closed.", false}

ErrClosed is the error returned by PipeListener.Accept when Close is called on the PipeListener.

type PipeAddr

type PipeAddr string

PipeAddr represents the address of a named pipe.

func (PipeAddr) Network
func (a PipeAddr) Network() string

Network returns the address's network name, "pipe".

func (PipeAddr) String
func (a PipeAddr) String() string

String returns the address of the pipe

type PipeConn

type PipeConn struct {
    // contains filtered or unexported fields
}

PipeConn is the implementation of the net.Conn interface for named pipe connections.

func Dial
func Dial(address string) (*PipeConn, error)

Dial connects to a named pipe with the given address. If the specified pipe is not available, it will wait indefinitely for the pipe to become available.

The address must be of the form \.\pipe<name> for local pipes and \\pipe<name> for remote pipes.

Dial will return a PipeError if you pass in a badly formatted pipe name.

Examples:

// local pipe
conn, err := Dial(`\\.\pipe\mypipename`)

// remote pipe
conn, err := Dial(`\\othercomp\pipe\mypipename`)
func DialTimeout
func DialTimeout(address string, timeout time.Duration) (*PipeConn, error)

DialTimeout acts like Dial, but will time out after the duration of timeout

func (*PipeConn) Close
func (c *PipeConn) Close() error

Close closes the connection.

func (*PipeConn) LocalAddr
func (c *PipeConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*PipeConn) Read
func (c *PipeConn) Read(b []byte) (int, error)

Read implements the net.Conn Read method.

func (*PipeConn) RemoteAddr
func (c *PipeConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*PipeConn) SetDeadline
func (c *PipeConn) SetDeadline(t time.Time) error

SetDeadline implements the net.Conn SetDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above

func (*PipeConn) SetReadDeadline
func (c *PipeConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.Conn SetReadDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above

func (*PipeConn) SetWriteDeadline
func (c *PipeConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.Conn SetWriteDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above

func (*PipeConn) Write
func (c *PipeConn) Write(b []byte) (int, error)

Write implements the net.Conn Write method.

type PipeError

type PipeError struct {
    // contains filtered or unexported fields
}

PipeError is an error related to a call to a pipe

func (PipeError) Error
func (e PipeError) Error() string

Error implements the error interface

func (PipeError) Temporary
func (e PipeError) Temporary() bool

Temporary implements net.AddrError.Temporary()

func (PipeError) Timeout
func (e PipeError) Timeout() bool

Timeout implements net.AddrError.Timeout()

type PipeListener

type PipeListener struct {
    // contains filtered or unexported fields
}

PipeListener is a named pipe listener. Clients should typically use variables of type net.Listener instead of assuming named pipe.

func Listen
func Listen(address string) (*PipeListener, error)

Listen returns a new PipeListener that will listen on a pipe with the given address. The address must be of the form \.\pipe<name>

Listen will return a PipeError for an incorrectly formatted pipe name.

func (*PipeListener) Accept
func (l *PipeListener) Accept() (net.Conn, error)

Accept implements the Accept method in the net.Listener interface; it waits for the next call and returns a generic net.Conn.

func (*PipeListener) AcceptPipe
func (l *PipeListener) AcceptPipe() (*PipeConn, error)

AcceptPipe accepts the next incoming call and returns the new connection.

func (*PipeListener) Addr
func (l *PipeListener) Addr() net.Addr

Addr returns the listener's network address, a PipeAddr.

func (*PipeListener) Close
func (l *PipeListener) Close() error

Close stops listening on the address. Already Accepted connections are not closed.

Documentation

Rendered for windows/amd64

Overview

Package npipe provides wrapper functions to more easily interact with Windows named pipes

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosed = PipeError{"Pipe has been closed.", false}

ErrClosed is the error returned by PipeListener.Accept when Close is called on the PipeListener.

Functions

func ValidatePipeAddress

func ValidatePipeAddress(address string) error

ValidatePipeAddress validates that a proper Windows named pipe path was passed in (e.g., \\.\pipe\srvsvc)

Types

type PipeAddr

type PipeAddr string

PipeAddr represents the address of a named pipe.

func (PipeAddr) Network

func (a PipeAddr) Network() string

Network returns the address's network name, "pipe".

func (PipeAddr) String

func (a PipeAddr) String() string

String returns the address of the pipe

type PipeConn

type PipeConn struct {
	// contains filtered or unexported fields
}

PipeConn is the implementation of the net.Conn interface for named pipe connections.

func Dial

func Dial(address string) (*PipeConn, error)

Dial connects to a named pipe with the given address. If the specified pipe is not available, it will wait indefinitely for the pipe to become available.

The address must be of the form \\.\\pipe\<name> for local pipes and \\<computer>\pipe\<name> for remote pipes.

Dial will return a PipeError if you pass in a badly formatted pipe name.

Examples:

// local pipe
conn, err := Dial(`\\.\pipe\mypipename`)

// remote pipe
conn, err := Dial(`\\othercomp\pipe\mypipename`)
Example

Use Dial to connect to a server and read messages from it.

package main

import (
	"bufio"
	"fmt"

	"github.com/Ne0nd0g/npipe"
)

func main() {
	conn, err := npipe.Dial(`\\.\pipe\mypipe`)
	if err != nil {
		// handle error
	}
	if _, err := fmt.Fprintln(conn, "Hi server!"); err != nil {
		// handle error
	}
	r := bufio.NewReader(conn)
	msg, err := r.ReadString('\n')
	if err != nil {
		// handle error
	}
	fmt.Println(msg)
}
Output:

func DialTimeout

func DialTimeout(address string, timeout time.Duration) (*PipeConn, error)

DialTimeout acts like Dial, but will time out after the duration of timeout

func (*PipeConn) Close

func (c *PipeConn) Close() error

Close closes the connection.

func (*PipeConn) LocalAddr

func (c *PipeConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*PipeConn) Read

func (c *PipeConn) Read(b []byte) (int, error)

Read implements the net.Conn Read method.

func (*PipeConn) RemoteAddr

func (c *PipeConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*PipeConn) SetDeadline

func (c *PipeConn) SetDeadline(t time.Time) error

SetDeadline implements the net.Conn SetDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above

func (*PipeConn) SetReadDeadline

func (c *PipeConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.Conn SetReadDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above

func (*PipeConn) SetWriteDeadline

func (c *PipeConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.Conn SetWriteDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above

func (*PipeConn) Write

func (c *PipeConn) Write(b []byte) (int, error)

Write implements the net.Conn Write method.

type PipeError

type PipeError struct {
	// contains filtered or unexported fields
}

PipeError is an error related to a call to a pipe

func (PipeError) Error

func (e PipeError) Error() string

Error implements the error interface

func (PipeError) Temporary

func (e PipeError) Temporary() bool

Temporary implements net.AddrError.Temporary()

func (PipeError) Timeout

func (e PipeError) Timeout() bool

Timeout implements net.AddrError.Timeout()

type PipeListener

type PipeListener struct {
	// contains filtered or unexported fields
}

PipeListener is a named pipe listener. Clients should typically use variables of type net.Listener instead of assuming named pipe.

func Listen

func Listen(address string) (*PipeListener, error)

Listen returns a new PipeListener that will listen on a pipe with the given address The address must be of the form \\.\pipe\<name> A PipeError for an incorrectly formatted pipe name

Example

Use Listen to start a server, and accept connections with Accept().

package main

import (
	"bufio"
	"fmt"
	"net"

	"github.com/Ne0nd0g/npipe"
)

func main() {
	ln, err := npipe.Listen(`\\.\pipe\mypipe`)
	if err != nil {
		// handle error
	}

	for {
		conn, err := ln.Accept()
		if err != nil {
			// handle error
			continue
		}

		// handle connection like any other net.Conn
		go func(conn net.Conn) {
			r := bufio.NewReader(conn)
			msg, err := r.ReadString('\n')
			if err != nil {
				// handle error
				return
			}
			fmt.Println(msg)
		}(conn)
	}
}
Output:

func NewPipeListener

func NewPipeListener(name string, openMode, pipeMode, maxInstances, outBuffer, inBuffer, timeout uint32, sa *windows.SecurityAttributes) (*PipeListener, error)

NewPipeListener is a factory that creates and returns a pointer to a PipeListener https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createnamedpipew HANDLE CreateNamedPipeW(

[in]           LPCWSTR               lpName,
[in]           DWORD                 dwOpenMode,
[in]           DWORD                 dwPipeMode,
[in]           DWORD                 nMaxInstances,
[in]           DWORD                 nOutBufferSize,
[in]           DWORD                 nInBufferSize,
[in]           DWORD                 nDefaultTimeOut,
[in, optional] LPSECURITY_ATTRIBUTES lpSecurityAttributes

);

func NewPipeListenerQuick

func NewPipeListenerQuick(name string, first bool) (*PipeListener, error)

NewPipeListenerQuick creates a Windows named pipe in a default configuration where The pipe mode will be type BYTE An unlimited number of instances can be created for this pipe The In and Out buffer size will be 512 bytes The default timeout is set to zero which ends up being 50 milliseconds The default Security Descriptor is full control to the LocalSystem account, administrators, and the creator owner

Read access is granted to members of the "Everyone" group and the "anonymous" account.

This function replaced the "createPipe" function from the npipe package before it was forked

func (*PipeListener) Accept

func (l *PipeListener) Accept() (net.Conn, error)

Accept implements the Accept method in the net.Listener interface; it waits for the next call and returns a generic net.Conn.

func (*PipeListener) AcceptPipe

func (l *PipeListener) AcceptPipe() (*PipeConn, error)

AcceptPipe accepts the next incoming call and returns the new connection. It might return an error if a client connected and immediately cancelled the connection.

func (*PipeListener) Addr

func (l *PipeListener) Addr() net.Addr

Addr returns the listener's network address, a PipeAddr.

func (*PipeListener) Close

func (l *PipeListener) Close() error

Close stops listening on the address. Already Accepted connections are not closed.

func (*PipeListener) Handle

func (l *PipeListener) Handle() windows.Handle

Handle returns the Windows Handle to

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL