udp

package
v1.55.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MPL-2.0 Imports: 15 Imported by: 7

Documentation

Index

Constants

View Source
const (
	AddrFamilyIpv4 = iota + 1
	AddrFamilyIpv6
)
View Source
const ConnectRequestConnectionId = 0x41727101980
View Source
const ConnectionIdMissmatchNul = "Connection ID missmatch.\x00"

Variables

This section is empty.

Functions

func GetNodeAddrsCompactMarshaler added in v1.49.0

func GetNodeAddrsCompactMarshaler(nas []krpc.NodeAddr, family AddrFamily) encoding.BinaryMarshaler

Returns a marshaler for the given node addrs for the specified family.

func Read

func Read(r io.Reader, data interface{}) error

func Write

func Write(w io.Writer, data interface{}) error

This is for fixed-size, builtin types only I think.

Types

type Action

type Action int32
const (
	ActionConnect Action = iota
	ActionAnnounce
	ActionScrape
	ActionError
)

type AddrFamily added in v1.49.0

type AddrFamily int

Discriminates behaviours based on address family in use.

type AnnounceEvent

type AnnounceEvent int32

func (AnnounceEvent) String

func (e AnnounceEvent) String() string

func (*AnnounceEvent) UnmarshalText added in v1.48.0

func (me *AnnounceEvent) UnmarshalText(text []byte) error

type AnnounceRequest

type AnnounceRequest struct {
	InfoHash   [20]byte
	PeerId     [20]byte
	Downloaded int64
	Left       int64 // If less than 0, math.MaxInt64 will be used for HTTP trackers instead.
	Uploaded   int64
	// Apparently this is optional. None can be used for announces done at
	// regular intervals.
	Event     AnnounceEvent
	IPAddress uint32
	Key       int32
	NumWant   int32 // How many peer addresses are desired. -1 for default.
	Port      uint16

} // 82 bytes

Marshalled as binary by the UDP client, so be careful making changes.

type AnnounceResponseHeader

type AnnounceResponseHeader struct {
	Interval int32
	Leechers int32
	Seeders  int32
}

type AnnounceResponsePeers

type AnnounceResponsePeers interface {
	encoding.BinaryUnmarshaler
	NodeAddrs() []krpc.NodeAddr
}

type Client

type Client struct {
	Dispatcher *Dispatcher
	Writer     io.Writer
	// contains filtered or unexported fields
}

Client interacts with UDP trackers via its Writer and Dispatcher. It has no knowledge of connection specifics.

func (*Client) Announce

func (cl *Client) Announce(
	ctx context.Context, req AnnounceRequest, opts Options,

	ipv6 func(net.Addr) bool,
) (
	respHdr AnnounceResponseHeader,

	peers AnnounceResponsePeers,
	err error,
)

func (*Client) Scrape

func (cl *Client) Scrape(
	ctx context.Context, ihs []InfoHash,
) (
	out ScrapeResponse, err error,
)

There's no way to pass options in a scrape, since we don't when the request body ends.

type ConnClient

type ConnClient struct {
	Client Client
	// contains filtered or unexported fields
}

Manages a Client with a specific connection.

func NewConnClient

func NewConnClient(opts NewConnClientOpts) (cc *ConnClient, err error)

func (*ConnClient) Announce

func (cc *ConnClient) Announce(
	ctx context.Context, req AnnounceRequest, opts Options,
) (
	h AnnounceResponseHeader, nas AnnounceResponsePeers, err error,
)

func (*ConnClient) Close

func (cc *ConnClient) Close() error

func (*ConnClient) LocalAddr added in v1.41.0

func (cc *ConnClient) LocalAddr() net.Addr

type ConnectionId

type ConnectionId = uint64

type ConnectionRequest

type ConnectionRequest struct {
	ConnectionId  ConnectionId
	Action        Action
	TransactionId TransactionId
}

type ConnectionResponse

type ConnectionResponse struct {
	ConnectionId ConnectionId
}

type DispatchedResponse

type DispatchedResponse struct {
	Header ResponseHeader
	// Response payload, after the header.
	Body []byte
	// Response source address
	Addr net.Addr
}

type Dispatcher

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

Maintains a mapping of transaction IDs to handlers.

func (*Dispatcher) Dispatch

func (me *Dispatcher) Dispatch(b []byte, addr net.Addr) error

The caller owns b.

func (*Dispatcher) NewTransaction

func (me *Dispatcher) NewTransaction(h TransactionResponseHandler) Transaction

type ErrorResponse added in v1.49.0

type ErrorResponse struct {
	Message string
}

func (ErrorResponse) Error added in v1.49.0

func (me ErrorResponse) Error() string

type InfoHash

type InfoHash = [20]byte

type NewConnClientOpts

type NewConnClientOpts struct {
	// The network to operate to use, such as "udp4", "udp", "udp6".
	Network string
	// Tracker address
	Host string
	// If non-nil, forces either IPv4 or IPv6 in the UDP tracker wire protocol.
	Ipv6 *bool
	// Logger to use for internal errors.
	Logger log.Logger
	// Custom function to use as a substitute for net.ListenPacket
	ListenPacket listenPacketFunc
}

type Options

type Options struct {
	RequestUri string
}

func (Options) Encode

func (opts Options) Encode() (ret []byte)

type RequestHeader

type RequestHeader struct {
	ConnectionId  ConnectionId
	Action        Action
	TransactionId TransactionId

} // 16 bytes

type ResponseHeader

type ResponseHeader struct {
	Action        Action
	TransactionId TransactionId
}

type ScrapeInfohashResult

type ScrapeInfohashResult struct {
	// I'm not sure why the fields are named differently for HTTP scrapes.
	// https://www.bittorrent.org/beps/bep_0048.html
	Seeders   int32 `bencode:"complete"`
	Completed int32 `bencode:"downloaded"`
	Leechers  int32 `bencode:"incomplete"`
}

type ScrapeRequest

type ScrapeRequest []InfoHash

type ScrapeResponse

type ScrapeResponse []ScrapeInfohashResult

type Transaction

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

func (*Transaction) End

func (t *Transaction) End()

func (*Transaction) Id

func (t *Transaction) Id() TransactionId

type TransactionId

type TransactionId = int32

func RandomTransactionId

func RandomTransactionId() TransactionId

type TransactionResponseHandler

type TransactionResponseHandler func(dr DispatchedResponse)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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