tftp

package
v0.3.11-8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package tftp implements a read-only TFTP server.

Index

Constants

View Source
const (
	// DefaultWriteTimeout is the duration a client has to acknowledge
	// a data packet from the server. This can be overridden by
	// setting Server.WriteTimeout.
	DefaultWriteTimeout = 2 * time.Second
	// DefaultWriteAttempts is the maximum number of times a single
	// packet will be (re)sent before timing out a client. This can be
	// overridden by setting Server.WriteAttempts.
	DefaultWriteAttempts = 5
	// DefaultBlockSize is the maximum block size used to send data to
	// clients. The server will respect a request for a smaller block
	// size, but requests for larger block sizes will be clamped to
	// DefaultBlockSize. This can be overridden by setting
	// Server.MaxBlockSize.
	DefaultBlockSize = 1450
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(path string, clientAddr net.Addr) (file io.ReadCloser, size int64, err error)

A Handler provides bytes for a file.

If size is non-zero, it must be equal to the number of bytes in file. The server will offer the "tsize" extension to clients that request it.

Note that some clients (particularly firmware TFTP clients) can be very capricious about servers not supporting all the options that they request, so passing a size of 0 may cause TFTP transfers to fail for some clients.

type Server

type Server struct {
	Handler Handler // handler to invoke for requests

	// WriteTimeout sets the duration to wait for the client to
	// acknowledge a data packet. Defaults to DefaultWriteTimeout.
	WriteTimeout time.Duration
	// WriteAttempts sets how many times a packet will be (re)sent
	// before timing out the client and aborting the transfer. If 0,
	// uses DefaultWriteAttempts.
	WriteAttempts int
	// MaxBlockSize sets the maximum block size used for file
	// transfers. If 0, uses DefaultBlockSize.
	MaxBlockSize int64

	// InfoLog specifies an optional logger for informational
	// messages. If nil, informational messages are suppressed.
	InfoLog func(msg string)
	// TransferLog specifies an optional logger for completed
	// transfers. A successful transfer is logged with err == nil. If
	// nil, transfer logs are suppressed.
	TransferLog func(clientAddr net.Addr, path string, err error)

	// Dial specifies a function to use when setting up a "connected"
	// UDP socket to a TFTP client. While this is mostly here for
	// testing, it can also be used to implement advanced relay
	// functionality (e.g. serving TFTP through SOCKS). If nil,
	// net.Dial is used.
	Dial func(network, addr string) (net.Conn, error)
}

A Server defines parameters for running a TFTP server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(addr string) error

ListenAndServe listens on the UDP network address addr and then calls Serve to handle TFTP requests. If addr is blank, ":69" is used.

func (*Server) Serve

func (s *Server) Serve(l net.PacketConn) error

Serve accepts requests on listener l, creating a new transfer goroutine for each. The transfer goroutines use s.Handler to get bytes, and transfers them to the client.

Jump to

Keyboard shortcuts

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