ipv4

package
v0.0.0-...-f5c58d3 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2016 License: BSD-2-Clause Imports: 13 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// ARPRequest for ARP requests.
	ARPRequest = 1
	// ARPReply for ARP replies.
	ARPReply = 2
)
View Source
const (
	// DefaultARPExpiration is the default expiration for entries in the ARP table.
	DefaultARPExpiration = 4 * time.Hour

	// DefaultARPCleanupInterval is the cleanup interval for the cache.
	DefaultARPCleanupInterval = DefaultARPExpiration

	// DefaultARPQueryInterval is the default query interval to use when sending
	// ARP requests.
	DefaultARPQueryInterval = 1 * time.Second

	// DefaultARPTimeout is the default number of ARP requests after which to
	// give up on an ARP request.
	DefaultARPTimeout = 3
)
View Source
const (
	// ProtocolICMP is used for the ICMP protocol.
	ProtocolICMP = 1

	// ProtocolUDP is used for the UDP protocol.
	ProtocolUDP = 17
)
View Source
const (
	// ARPHardwareEthernet as lower layer.
	ARPHardwareEthernet = 1
)
View Source
const (
	// ARPProtocolIPv4 as upper layer.
	ARPProtocolIPv4 = 0x800
)

Variables

View Source
var (
	// ErrInvalidIHL is a check error returned when the length of the Options
	// byte slice does not match the IHL value.
	ErrInvalidIHL = errors.New("IHL field does not match Options byte slice length")

	// ErrInvalidChecksum is a check error returned when header checksum is
	// incorrect.
	ErrInvalidChecksum = errors.New("Checksum field is incorrect")

	// ErrInvalidTotalLength is an error returned when the packet length is
	// too small.
	ErrInvalidTotalLength = errors.New("TotalLength field is too small")
)
View Source
var Broadcast = Address([4]byte{255, 255, 255, 255})

Broadcast is the IPv4 broadcast address.

View Source
var (
	// ErrARPTimeout occurs when no ARP reply is received for an ARP request.
	ErrARPTimeout = errors.New("ARP request timeout")
)
View Source
var (
	// ErrNoRouteToDestinationAddress is returned when there is no route
	// to the destination address.
	ErrNoRouteToDestinationAddress = errors.New("no route to destination address")
)

Functions

This section is empty.

Types

type ARP

type ARP interface {
	Resolve(address Address) (ethernet.MAC, error)
}

ARP represents an ARP layer that can convert IPv4 addresses to Ethernet addresses.

func NewARP

func NewARP(mac ethernet.MAC, ip Address, eth ethernet.Layer) ARP

NewARP will create a default ARP interface with the default configuration.

func NewCustomARP

func NewCustomARP(mac ethernet.MAC, ip Address, eth ethernet.Layer, expiration, cleanupInterval, queryInterval time.Duration, timeout int) ARP

NewCustomARP will create a default ARP interface with a custom configuration.

type ARPHardwareType

type ARPHardwareType uint16

ARPHardwareType is the network protocol of the ARP packet.

type ARPOperation

type ARPOperation uint16

ARPOperation is a type of ARP packet.

type ARPPacket

type ARPPacket struct {
	HardwareType          ARPHardwareType
	ProtocolType          ARPProtocolType
	HardwareAddressLength uint8
	ProtocolAddressLength uint8
	Operation             ARPOperation
	SenderHardwareAddress ethernet.MAC
	SenderProtocolAddress Address
	TargetHardwareAddress ethernet.MAC
	TargetProtocolAddress Address
}

ARPPacket represents an ARP packet

func NewARPPacket

func NewARPPacket(r io.Reader) (ARPPacket, error)

NewARPPacket reads an ARP packet from a reader.

func NewARPReply

func NewARPReply(senderMAC, targetMAC ethernet.MAC, senderIP, targetIP Address) ARPPacket

NewARPReply constructs an ARP reply packet.

func NewARPRequest

func NewARPRequest(senderMAC ethernet.MAC, senderIP, targetIP Address) ARPPacket

NewARPRequest creates an ARP request packet.

func (ARPPacket) Write

func (p ARPPacket) Write(w io.Writer) error

Write an ARP packet to a writer.

type ARPProtocolType

type ARPProtocolType uint16

ARPProtocolType is the internetwork protocol of the ARP packet.

type Address

type Address [4]byte

Address is an IPv4 address.

func NewAddress

func NewAddress(s string) (Address, bool)

NewAddress creates a new address from a string.

If the string is invalid, NewAddress returns false.

func (Address) And

func (a Address) And(b Address) Address

And will perform the logical AND operation on two IP addresses.

func (Address) Bytes

func (a Address) Bytes() []byte

Bytes copies an address to a new byte slice.

func (Address) Equals

func (a Address) Equals(b Address) bool

Equals will compare two IP addresses for equality.

func (Address) String

func (a Address) String() string
type Header struct {
	Version        uint8
	IHL            uint8
	ToS            uint8
	TotalLength    uint16
	Identification uint16
	Flags          uint8
	FragmentOffset uint16
	TTL            uint8
	Protocol       Protocol
	Checksum       uint16
	Source         Address
	Destination    Address
	Options        []byte
}

Header is the logical version of an IPv4 header.

func NewHeader

func NewHeader(r io.Reader) (Header, error)

NewHeader reads a header from a Reader.

func (Header) CalculateChecksum

func (h Header) CalculateChecksum() uint16

CalculateChecksum calculates the header checksum.

func (Header) Check

func (h Header) Check() error

Check checks whether the IPv4 header is valid.

The function fails if the checksum is not correct, or if the IHL field does not match the length of the Options byte slice.

This functions can return ErrInvalidIHL, ErrInvalidChecksum or ErrInvalidTotalLength.

func (Header) RawHeader

func (h Header) RawHeader() RawHeader

RawHeader converts the header to a RawHeader.

func (Header) Write

func (h Header) Write(w io.Writer) error

Write the header to a Writer.

type Layer

type Layer interface {
	Packets(p Protocol) <-chan Packet
	Send(t Packet) error
}

Layer is an IPv4 layer.

func NewLayer

func NewLayer(address Address, router Router, eth ethernet.Layer) Layer

NewLayer creates a new instance of the default IPv4 layer.

type Packet

type Packet struct {
	Header
	Payload []byte
}

Packet is an IPv4 packet.

func NewDefaultPacket

func NewDefaultPacket() Packet

NewDefaultPacket constructs a default IPv4 packet.

func NewPacket

func NewPacket(r io.Reader) (p Packet, err error)

NewPacket will read a packet from a reader.

The header will be checked using the Check() function. Only valid packets will be returned, unless err is not nil.

func NewPacketTo

func NewPacketTo(to Address, proto Protocol, payload []byte) Packet

NewPacketTo constructs a new packet with a destination.

func (Packet) String

func (packet Packet) String() string

func (Packet) Write

func (packet Packet) Write(w io.Writer) error

Write will write a packet to a writer.

type Protocol

type Protocol uint8

Protocol is an IPv4 protocol.

type RawHeader

type RawHeader struct {
	VersionIHL          uint8
	ToS                 uint8
	TotalLength         uint16
	Identification      uint16
	FlagsFragmentOffset uint16
	TTL                 uint8
	Protocol            Protocol
	Checksum            uint16
	Source              Address
	Destination         Address
}

RawHeader represents a raw IPv4 header.

This struct can be written and read with the binary package.

func NewRawHeader

func NewRawHeader(r io.Reader) (RawHeader, error)

NewRawHeader reads a new raw header from a reader.

func (RawHeader) Header

func (h RawHeader) Header() Header

Header converts the RawHeader to a logic Header.

func (RawHeader) Write

func (h RawHeader) Write(w io.Writer) error

Write the header to a Writer.

type Router

type Router interface {
	// Resolve will resolve the IPv4 address to the ethernet MAC address
	// of the next hop.
	//
	// See also ErrNoRouteToDestinationAddress.
	Resolve(address Address) (ethernet.MAC, error)
}

Router is an IPv4 router.

func NewRouter

func NewRouter(arp ARP, address, netmask Address, gateway *Address) Router

NewRouter creates a default router.

ARP is used to resolve local addresses. Otherwise, the MAC address of the gateway is returned.

Specifying a gateway is optional.

Jump to

Keyboard shortcuts

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