pcp

package module
v0.0.0-...-0d1bc4d Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2020 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go-PCP

This package implements the Port Control Protocol (PCP) version 2, as specified by RFC6887. PCP is intended to be the successor to NAT-PNP and UPnP.

The package may not yet be fully compliant; there are parts which are still to be implemented, and it needs testing, as I did not have a PCP server available when writing this, hence there may be bugs. I should be able to test it soon and fix any if they are present.

As it stands, progress is as follows:

  • Map Opcode implemented. Can Add/Delete/Refresh port mappings.
  • Peer Opcode implemented. Can Add/Delete/Refresh peer mappings.
  • Announce Opcode implemented.
  • Provide proper events to Event chan of client.
  • Implement PCP option support.
  • Properly document methods.
Known Bugs/Non-Compliant Features
  • Connection to PCP server is not tested. Currently the connection is just opened, and data sent to it. The connection needs to remain open to listen for announce events. During connection dialling, a test payload should be sent, and a timeout error returned if server is not available.

  • A number of the network traffic codes specified in the official specification have yet to be implemented. Currently the All, TCP & UDP codes are present. There is an IANA package that should make adding the remaining codes pretty simple.

  • PCP Options are not implemented in the methods currently. 90% of the code is there, but the methods do not provide any means to pass in PCP options.

  • The network receiving and processing of messages at the moment is not a great implementation, and may be buggy. I have yet to test. Possible sources may be incorrect padding of network packets. If someone wants to review the handleMessage method and improve it, I'd welcome changes. Same goes for the epochValid code, which I'm not sure is compliant.

  • The way mapping deletion is handled at the moment feels like a hack - due to the method the specification provides for deleting a mapping (send a request to set mapping lifetime to zero), the does this, then listens to the events channel of the client and removes a mapping from the client map when a matching message is received. If someone can think of a cleaner way to do this, I'm all ears.

  • Around line 76 of network.go, there is a string match for comparing the IP address of the client's gateway to that in the message received. A string match doesn't feel optimal. If someone knows of a better way, please feel free to correct. A simple byte comparison does not work due to the variable length of the arrays used to accommodate both IPv4 and IPv6 addresses as far as I can tell.

Documentation

Index

Constants

View Source
const (
	ActionReceivedAnnounce = iota
	ActionReceivedMapping
	ActionReceivedPeer
	ActionClose
)

Not the greatest naming, but will do for now.

View Source
const (
	DefaultLifetimeSeconds = 3600
)

Variables

View Source
var (
	ErrNoInternalAddress  = errors.New("no internal address")
	ErrNoExternalAddress  = errors.New("no external address")
	ErrGatewayNotFound    = errors.New("gateway not found")
	ErrPacketTooLarge     = errors.New("packet exceeds 1100 octet size limit")
	ErrUnsupportedVersion = errors.New("the specified version is not supported")
	ErrWrongPacketType    = errors.New("the packet is not of the correct type")
	ErrAddressMismatch    = errors.New("the sender and gateway addresses do not match")
	ErrPortNotSpecified   = errors.New("you must specify a port")
	ErrNonceGeneration    = errors.New("an error occured whilst generating nonce")
	ErrMapDataPayload     = errors.New("could not create the map data payload")
	ErrPeerDataPayload    = errors.New("could not create the peer data payload")
	ErrRequestDataPayload = errors.New("could not create the request data payload")
	ErrNetworkSend        = errors.New("an error occured whilst sending the network request")
	ErrNetworkTimeout     = errors.New("the connection timed out")
	ErrMappingNotFound    = errors.New("mapping not found")
	ErrNoAddress          = errors.New("no address specified")
)

Functions

This section is empty.

Types

type Action

type Action uint8

func (Action) String

func (a Action) String() string

type Client

type Client struct {
	GatewayAddr  net.IP
	Event        chan Event
	Mappings     map[uint16]PortMap
	PeerMappings map[uint16]PeerMap
	// contains filtered or unexported fields
}

func NewClient

func NewClient() (client *Client, err error)

Potentially add deviceAddr at a later stage

func (*Client) AddPeerMapping

func (c *Client) AddPeerMapping(protocol Protocol, internalPort, requestedExternalPort, remotePort uint16, requestedAddr, remoteAddr net.IP, lifetime uint32) (err error)

func (*Client) AddPortMapping

func (c *Client) AddPortMapping(protocol Protocol, internalPort, requestedExternalPort uint16, requestedAddr net.IP, lifetime uint32) (err error)

Need to add support for PCP options later.

func (*Client) Announce

func (c *Client) Announce() (err error)

func (*Client) Close

func (c *Client) Close()

Closes connection to PCP server and returns close event

func (*Client) DeletePeerMapping

func (c *Client) DeletePeerMapping(internalPort uint16) (err error)

func (*Client) DeletePortMapping

func (c *Client) DeletePortMapping(internalPort uint16) (err error)

func (*Client) GetExternalAddress

func (c *Client) GetExternalAddress() (addr net.IP, err error)

func (*Client) GetGatewayAddress

func (c *Client) GetGatewayAddress() (addr net.IP, err error)

func (*Client) GetInternalAddress

func (c *Client) GetInternalAddress() (addr net.IP, err error)

func (*Client) RefreshPeerMapping

func (c *Client) RefreshPeerMapping(internalPort uint16, lifetime uint32) (err error)

func (*Client) RefreshPortMapping

func (c *Client) RefreshPortMapping(internalPort uint16, lifetime uint32) (err error)

type ClientEpoch

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

type Event

type Event struct {
	Action Action
	Data   interface{}
}

type OpCode

type OpCode uint8
const (
	OpAnnounce OpCode = iota
	OpMap
	OpPeer
)

func (OpCode) String

func (o OpCode) String() string

type OpDataMap

type OpDataMap struct {
	Protocol     Protocol
	InternalPort uint16
	ExternalPort uint16 //This is only a suggestion in request. Server ultimately decides.
	ExternalIP   net.IP //Also only a suggestion
}

type OpDataPeer

type OpDataPeer struct {
	OpDataMap
	RemotePort uint16
	RemoteIP   net.IP
}

type OptionOpCode

type OptionOpCode uint8
const (
	OptionOpReserved OptionOpCode = iota
	OptionOpThirdParty
	OptionOpPreferFailure
	OptionOpFilter
	OptionOpNonce
	OptionOpAuthenticationTag
	OptionOpPaAuthenticationTag
	OptionOpEapPayload
	OptionOpPrf
	OptionOpMacAlgorithm
	OptionOpSessionLifetime
	OptionOpReceivedPak
	OptionOpIdIndicator
	OptionOpThirdPartyId
)

func (OptionOpCode) String

func (o OptionOpCode) String() string

type PCPOption

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

type PeerMap

type PeerMap struct {
	PortMap
	RemotePort uint16
	RemoteIP   net.IP
}

type PortMap

type PortMap struct {
	OpDataMap
	Active   bool
	Lifetime uint32
	Refresh  RefreshTime
}

type Protocol

type Protocol uint8

At a later date it would be worth integrating with the IANA package to be fully compliant. For now, just implement common protocols

const (
	ProtocolAll Protocol = 0
	ProtocolTCP Protocol = 6
	ProtocolUDP Protocol = 17
)

func (Protocol) String

func (p Protocol) String() string

type RefreshTime

type RefreshTime struct {
	Attempt int
	Time    int64
}

Potentially add in progress bool

type RequestPacket

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

type ResponsePacket

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

type ResultCode

type ResultCode uint8
const (
	ResultSuccess ResultCode = iota
	ResultUnsupportedVersion
	ResultNotAuthorised
	ResultMalformedRequest
	ResultUnsupportedOpcode
	ResultUnsupportedOption
	ResultMalformedOption
	ResultNetworkFailure
	ResultNoResources
	ResultUnsupportedProtocol
	ResultUserExceededQuota
	ResultCannotProvideExternal
	ResultAddressMismatch
	ResultExcessiveRemotePeers
)

func (ResultCode) String

func (r ResultCode) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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