var ( // ErrInvalidHardwareAddr is returned when one or more invalid hardware // addresses are passed to NewPacket. ErrInvalidHardwareAddr = errors.New("invalid hardware address") // ErrInvalidIP is returned when one or more invalid IPv4 addresses are // passed to NewPacket. ErrInvalidIP = errors.New("invalid IPv4 address") )
A Client is an ARP client, which can be used to send and receive ARP packets.
type Client struct {
// contains filtered or unexported fields
}
func Dial(ifi *net.Interface) (*Client, error)
Dial creates a new Client using the specified network interface. Dial retrieves the IPv4 address of the interface and binds a raw socket to send and receive ARP packets.
func New(ifi *net.Interface, p net.PacketConn) (*Client, error)
New creates a new Client using the specified network interface and net.PacketConn. This allows the caller to define exactly how they bind to the net.PacketConn. This is most useful to define what protocol to pass to socket(7).
In most cases, callers would be better off calling Dial.
func (c *Client) Close() error
Close closes the Client's raw socket and stops sending and receiving ARP packets.
func (c Client) HardwareAddr() net.HardwareAddr
HardwareAddr fetches the hardware address for the interface associated with the connection.
func (c *Client) Read() (*Packet, *ethernet.Frame, error)
Read reads a single ARP packet and returns it, together with its ethernet frame.
func (c *Client) Reply(req *Packet, hwAddr net.HardwareAddr, ip netip.Addr) error
Reply constructs and sends a reply to an ARP request. On the ARP layer, it will be addressed to the sender address of the packet. On the ethernet layer, it will be sent to the actual remote address from which the request was received.
For more fine-grained control, use WriteTo to write a custom response.
func (c *Client) Request(ip netip.Addr) error
Request sends an ARP request, asking for the hardware address associated with an IPv4 address. The response, if any, can be read with the Read method.
Unlike Resolve, which provides an easier interface for getting the hardware address, Request allows sending many requests in a row, retrieving the responses afterwards.
func (c *Client) Resolve(ip netip.Addr) (net.HardwareAddr, error)
Resolve performs an ARP request, attempting to retrieve the hardware address of a machine using its IPv4 address. Resolve must not be used concurrently with Read. If you're using Read (usually in a loop), you need to use Request instead. Resolve may read more than one message if it receives messages unrelated to the request.
func (c *Client) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines associated with the connection.
func (c *Client) SetReadDeadline(t time.Time) error
SetReadDeadline sets the deadline for future raw socket read calls. If the deadline is reached, a raw socket read will fail with a timeout (see type net.Error) instead of blocking. A zero value for t means a raw socket read will not time out.
func (c *Client) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the deadline for future raw socket write calls. If the deadline is reached, a raw socket write will fail with a timeout (see type net.Error) instead of blocking. A zero value for t means a raw socket write will not time out. Even if a write times out, it may return n > 0, indicating that some of the data was successfully written.
func (c *Client) WriteTo(p *Packet, addr net.HardwareAddr) error
WriteTo writes a single ARP packet to addr. Note that addr should, but doesn't have to, match the target hardware address of the ARP packet.
An Operation is an ARP operation, such as request or reply.
type Operation uint16
Operation constants which indicate an ARP request or reply.
const ( OperationRequest Operation = 1 OperationReply Operation = 2 )
func (i Operation) String() string
A Packet is a raw ARP packet, as described in RFC 826.
type Packet struct { // HardwareType specifies an IANA-assigned hardware type, as described // in RFC 826. HardwareType uint16 // ProtocolType specifies the internetwork protocol for which the ARP // request is intended. Typically, this is the IPv4 EtherType. ProtocolType uint16 // HardwareAddrLength specifies the length of the sender and target // hardware addresses included in a Packet. HardwareAddrLength uint8 // IPLength specifies the length of the sender and target IPv4 addresses // included in a Packet. IPLength uint8 // Operation specifies the ARP operation being performed, such as request // or reply. Operation Operation // SenderHardwareAddr specifies the hardware address of the sender of this // Packet. SenderHardwareAddr net.HardwareAddr // SenderIP specifies the IPv4 address of the sender of this Packet. SenderIP netip.Addr // TargetHardwareAddr specifies the hardware address of the target of this // Packet. TargetHardwareAddr net.HardwareAddr // TargetIP specifies the IPv4 address of the target of this Packet. TargetIP netip.Addr }
func NewPacket(op Operation, srcHW net.HardwareAddr, srcIP netip.Addr, dstHW net.HardwareAddr, dstIP netip.Addr) (*Packet, error)
NewPacket creates a new Packet from an input Operation and hardware/IPv4 address values for both a sender and target.
If either hardware address is less than 6 bytes in length, or there is a length mismatch between the two, ErrInvalidHardwareAddr is returned.
If either IP address is not an IPv4 address, or there is a length mismatch between the two, ErrInvalidIP is returned.
func (p *Packet) MarshalBinary() ([]byte, error)
MarshalBinary allocates a byte slice containing the data from a Packet.
MarshalBinary never returns an error.
func (p *Packet) UnmarshalBinary(b []byte) error
UnmarshalBinary unmarshals a raw byte slice into a Packet.