Documentation ¶
Overview ¶
Package tftp provides TFTP client and server implementations.
Index ¶
- Constants
- Variables
- func ErrorCause(err error) error
- func IsOptionParsingError(err error) bool
- func IsRemoteError(err error) bool
- func IsUnexpectedDatagram(err error) bool
- type Client
- type ClientOpt
- type ErrorCode
- type ReadHandler
- type ReadHandlerFunc
- type ReadRequest
- type ReadWriteHandler
- type Response
- type Server
- type ServerOpt
- type TransferMode
- type WriteHandler
- type WriteHandlerFunc
- type WriteRequest
Constants ¶
const ( // ErrCodeNotDefined - Not defined, see error message (if any). ErrCodeNotDefined ErrorCode = 0x0 // ErrCodeFileNotFound - File not found. ErrCodeFileNotFound ErrorCode = 0x1 // ErrCodeAccessViolation - Access violation. ErrCodeAccessViolation ErrorCode = 0x2 // ErrCodeDiskFull - Disk full or allocation exceeded. ErrCodeDiskFull ErrorCode = 0x3 // ErrCodeIllegalOperation - Illegal TFTP operation. ErrCodeIllegalOperation ErrorCode = 0x4 // ErrCodeUnknownTransferID - Unknown transfer ID. ErrCodeUnknownTransferID ErrorCode = 0x5 // ErrCodeFileAlreadyExists - File already exists. ErrCodeFileAlreadyExists ErrorCode = 0x6 // ErrCodeNoSuchUser - No such user. ErrCodeNoSuchUser ErrorCode = 0x7 // ModeNetASCII is the string for netascii transfer mode ModeNetASCII TransferMode = "netascii" // ModeOctet is the string for octet/binary transfer mode ModeOctet TransferMode = "octet" )
Variables ¶
var ( // ErrInvalidURL indicates that the URL passed to Get or Put is invalid. ErrInvalidURL = errors.New("invalid URL") // ErrInvalidHostIP indicates an empty or invalid host. ErrInvalidHostIP = errors.New("invalid host/IP") // ErrInvalidFile indicates an empty or invalid file. ErrInvalidFile = errors.New("invalid file") // ErrSizeNotReceived indicates tsize was not negotiated. ErrSizeNotReceived = errors.New("size not received") // ErrAddressNotAvailable indicates the server address was requested before // the server had been started. ErrAddressNotAvailable = errors.New("address not available until server has been started") // ErrNoRegisteredHandlers indicates no handlers were registered before starting the server. ErrNoRegisteredHandlers = errors.New("no handlers registered") // ErrInvalidNetwork indicates that a network other than udp, udp4, or udp6 was configured. ErrInvalidNetwork = errors.New("invalid network: must be udp, udp4, or udp6") // ErrInvalidBlocksize indicates that a blocksize outside the range 8 to 65464 was configured. ErrInvalidBlocksize = errors.New("invalid blocksize: must be between 8 and 65464") // ErrInvalidTimeout indicates that a timeout outside the range 1 to 255 was configured. ErrInvalidTimeout = errors.New("invalid timeout: must be between 1 and 255") // ErrInvalidWindowsize indicates that a windowsize outside the range 1 to 65535 was configured. ErrInvalidWindowsize = errors.New("invalid windowsize: must be between 1 and 65535") // ErrInvalidMode indicates that a mode other than ModeNetASCII or ModeOctet was configured. ErrInvalidMode = errors.New("invalid transfer mode: must be ModeNetASCII or ModeOctet") // ErrInvalidRetransmit indicates that the retransmit limit was configured with a negative value. ErrInvalidRetransmit = errors.New("invalid retransmit: cannot be negative") // ErrMaxRetries indicates that the maximum number of retries has been reached. ErrMaxRetries = errors.New("max retries reached") )
Functions ¶
func ErrorCause ¶
ErrorCause extracts the original error from an error wrapped by tftp.
func IsOptionParsingError ¶
IsOptionParsingError allows a consumer to check if an error was induced during option parsing.
func IsRemoteError ¶
IsRemoteError allows a consumer to check if an error was an error by the remote client/server.
func IsUnexpectedDatagram ¶
IsUnexpectedDatagram allows a consumer to check if an error is an unexpected datagram.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client makes requests to a server.
func NewClient ¶
NewClient returns a configured Client.
Any number of ClientOpts can be provided to modify the default client behavior.
type ClientOpt ¶
ClientOpt is a function that configures a Client.
func ClientBlocksize ¶
ClientBlocksize configures the number of data bytes that will be send in each datagram. Valid range is 8 to 65464.
Default: 512.
func ClientMode ¶
func ClientMode(mode TransferMode) ClientOpt
ClientMode configures the mode.
Valid options are ModeNetASCII and ModeOctet. Default is ModeNetASCII.
func ClientRetransmit ¶
ClientRetransmit configures the per-packet retransmission limit for all requests.
Default: 10.
func ClientTimeout ¶
ClientTimeout configures the number of seconds to wait before resending an unacknowledged datagram. Valid range is 1 to 255.
Default: 1.
func ClientTransferSize ¶
ClientTransferSize requests for the server to send the file size before sending.
Default: enabled.
func ClientWindowsize ¶
ClientWindowsize configures the number of datagrams that will be transmitted before needing an acknowledgement.
Default: 1.
type ReadHandler ¶
type ReadHandler interface {
ServeTFTP(ReadRequest)
}
ReadHandler responds to a TFTP read request.
type ReadHandlerFunc ¶
type ReadHandlerFunc func(ReadRequest)
ReadHandlerFunc is an adapter type to allow a function to serve as a ReadHandler.
func (ReadHandlerFunc) ServeTFTP ¶
func (h ReadHandlerFunc) ServeTFTP(w ReadRequest)
ServeTFTP calls the ReadHandlerFunc function.
type ReadRequest ¶
type ReadRequest interface { // Addr is the network address of the client. Addr() *net.UDPAddr // Name is the file name requested by the client. Name() string // Write write's data to the client. Write([]byte) (int, error) // WriteError sends an error to the client and terminates the // connection. WriteError can only be called once. Write cannot // be called after an error has been written. WriteError(ErrorCode, string) // WriteSize sets the transfer size (tsize) value to be sent to // the client. It must be called before any calls to Write. WriteSize(int64) // TransferMode returns the TFTP transfer mode requested by the client. TransferMode() TransferMode }
ReadRequest is provided to a ReadHandler's ServeTFTP method.
type ReadWriteHandler ¶
type ReadWriteHandler interface { ReadHandler WriteHandler }
ReadWriteHandler combines ReadHandler and WriteHandler.
func FileServer ¶
func FileServer(dir string) ReadWriteHandler
FileServer creates a handler for sending and reciving files on the filesystem.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is an io.Reader for receiving files from a TFTP server.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server contains the configuration to run a TFTP server.
A ReadHandler, WriteHandler, or both can be registered to the server. If one of the handlers isn't registered, the server will return errors to clients attempting to use them.
func NewServer ¶
NewServer returns a configured Server.
Addr is the network address to listen on and is in the form "host:port". If a no host is given the server will listen on all interfaces.
Any number of ServerOpts can be provided to configure optional values.
func (*Server) Addr ¶
Addr is the network address of the server. It is available after the server has been started.
func (*Server) ListenAndServe ¶
ListenAndServe starts a configured server.
func (*Server) ReadHandler ¶
func (s *Server) ReadHandler(rh ReadHandler)
ReadHandler registers a ReadHandler for the server.
func (*Server) WriteHandler ¶
func (s *Server) WriteHandler(wh WriteHandler)
WriteHandler registers a WriteHandler for the server.
type ServerOpt ¶
ServerOpt is a function that configures a Server.
func ServerNet ¶
ServerNet configures the network a server listens on. Must be one of: udp, udp4, udp6.
Default: udp.
func ServerRetransmit ¶
ServerRetransmit configures the per-packet retransmission limit for all requests.
Default: 10.
func ServerSinglePort ¶
ServerSinglePort enables the server to service all requests via a single port rather than the standard TFTP behavior of each client communicating on a separate port.
This is an experimental feature.
Default is disabled.
type WriteHandler ¶
type WriteHandler interface {
ReceiveTFTP(WriteRequest)
}
WriteHandler responds to a TFTP write request.
type WriteHandlerFunc ¶
type WriteHandlerFunc func(WriteRequest)
WriteHandlerFunc is an adapter type to allow a function to serve as a WriteHandler.
func (WriteHandlerFunc) ReceiveTFTP ¶
func (h WriteHandlerFunc) ReceiveTFTP(w WriteRequest)
ReceiveTFTP calls the WriteHandlerFunc function.
type WriteRequest ¶
type WriteRequest interface { // Addr is the network address of the client. Addr() *net.UDPAddr // Name is the file name provided by the client. Name() string // Read reads the request data from the client. Read([]byte) (int, error) // Size returns the transfer size (tsize) as provided by the client. // If the tsize option was not negotiated, an error will be returned. Size() (int64, error) // WriteError sends an error to the client and terminates the // connection. WriteError can only be called once. Read cannot // be called after an error has been written. WriteError(ErrorCode, string) // TransferMode returns the TFTP transfer mode requested by the client. TransferMode() TransferMode }
WriteRequest is provided to a WriteHandler's ReceiveTFTP method.