netboot: Index | Files

package tftp

import ""

Package tftp implements a read-only TFTP server.


Package Files

handlers.go tftp.go


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

type Handler Uses

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.

func ConstantHandler Uses

func ConstantHandler(bs []byte) Handler

ConstantHandler returns a Handler that serves bs for all requested paths.

func FilesystemHandler Uses

func FilesystemHandler(root string) (Handler, error)

FilesystemHandler returns a Handler that serves files in root.

type Server Uses

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 Uses

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 Uses

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.

Package tftp imports 11 packages (graph) and is imported by 3 packages. Updated 2019-09-16. Refresh now. Tools for package owners.