tftp

package
v0.0.0-...-42e8fca Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Pages 131 - 132 Listing 6-9: Server type implementation.

Pages 120-121 Listing 6-1: Types and codes used by the TFTP server.

Index

Constants

View Source
const (
	// TFTP limits datagram packets to 516 bytes or fewer to avoid
	// fragmentation.
	// We define two constant size.
	// The maximum block si ze is the datagram size minus a 4-byte header.
	DatagramSize = 516              // the maximum supported datagram size
	BlockSize    = DatagramSize - 4 // the DatagramSize minus a 4-byte header
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Ack

type Ack uint16 // Acknowledgement packet represented by a 16-bit, unsigned integer.

Pages 128 - 129 Listing 6-6: Acknowledgement type implementation.

func (Ack) MarshalBinary

func (a Ack) MarshalBinary() ([]byte, error)

func (*Ack) UnmarshalBinary

func (a *Ack) UnmarshalBinary(p []byte) error

type Data

type Data struct {
	Block   uint16
	Payload io.Reader
}

Page 126 Listing 6-4: Date type and its binary marshaling method. Data struct keeps track of the current block number and the data source.

func (*Data) MarshalBinary

func (d *Data) MarshalBinary() ([]byte, error)

MarshalBinary will return 516 bytes per call at most by relying on the io.CopyN function and the BlockSize constant.

func (*Data) UnmarshalBinary

func (d *Data) UnmarshalBinary(p []byte) error

Page 127 Listing 6-5: Data type implementation.

type Err

type Err struct {
	Error   ErrCode
	Message string
}

Page 129 - 130 Listing 6-7: Error type used for conveying errors between the client and server.

func (Err) MarshalBinary

func (e Err) MarshalBinary() ([]byte, error)

func (*Err) UnmarshalBinary

func (e *Err) UnmarshalBinary(p []byte) error

Page 130 Listing 6-8: Error type's binary unmarshaler implementation.

type ErrCode

type ErrCode uint16

We define a series of unsigned 16-bit integer error codes per the RFC. Although we don't use all error codes in our server since it only allows downloads, a client could return these error codes in lieu of an acknowledgement packet.

const (
	ErrUnknown ErrCode = iota
	ErrNotFound
	ErrAccessViolation
	ErrDiskFull
	ErrIllegalOp
	ErrUnknownID
	ErrFileExists
	ErrNoUser
)

type OpCode

type OpCode uint16

The first two bytes of a TFTP packet's header is an operation code. Each operation code is a 2-byte, unsigned integer.

const (
	OpRRQ OpCode = iota + 1

	OpData
	OpAck
	OpErr
)

Our server supports four operations: A read request (RRQ), a data operation, an acknowledgement, and an error. Since our server is read-only, we skip the write request (WRQ) definition.

type ReadReq

type ReadReq struct {
	Filename string
	Mode     string
}

The struct representing the read request needs to keep track of the filename and the mode.

func (ReadReq) MarshalBinary

func (q ReadReq) MarshalBinary() ([]byte, error)

Although not used by our server, a client would make use of this method.

func (*ReadReq) UnmarshalBinary

func (q *ReadReq) UnmarshalBinary(p []byte) error

Pages 123-124 Listing 6-3: Read request type implementation. This rounds out the read request's implementation by defining a method that allows the server to unmarshal a read request from a byte slice, typically read from a network connection with a client.

type Server

type Server struct {
	Payload []byte        // the payload served for all read requests
	Retries uint8         // the number of times to retry a failed transmission
	Timeout time.Duration // the duration to wait for an acknowledgement
}

func (Server) ListenAndServe

func (s Server) ListenAndServe(addr string) error

func (*Server) Serve

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

Directories

Path Synopsis
Page 135 Listing 6-11: Command line TFTP server implementation.
Page 135 Listing 6-11: Command line TFTP server implementation.

Jump to

Keyboard shortcuts

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