arp working over network

This commit is contained in:
soypat
2025-02-18 18:48:55 -03:00
parent 6e9aafc1e0
commit 0606a04f45
11 changed files with 800 additions and 20 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ var (
)
// Operation represents the type of ARP packet, either request or reply/response.
type Operation uint8
type Operation uint16
const (
OpRequest Operation = 1 // request
+26 -2
View File
@@ -3,6 +3,9 @@ package arp
import (
"encoding/binary"
"errors"
"fmt"
"net"
"net/netip"
"github.com/soypat/lneto/lneto2"
)
@@ -61,10 +64,10 @@ func (afrm Frame) SetProtocol(Type lneto2.EtherType, length uint8) {
}
// Operation returns the ARP header operation field. See [Operation].
func (afrm Frame) Operation() Operation { return Operation(afrm.buf[6]) }
func (afrm Frame) Operation() Operation { return Operation(binary.BigEndian.Uint16(afrm.buf[6:8])) }
// SetOperation sets the ARP header operation field. See [Operation].
func (afrm Frame) SetOperation(b Operation) { afrm.buf[6] = uint8(b) }
func (afrm Frame) SetOperation(op Operation) { binary.BigEndian.PutUint16(afrm.buf[6:8], uint16(op)) }
// Sender returns the hardware (MAC) and protocol addresses of sender of ARP packet.
// In an ARP request MAC address is used to indicate
@@ -139,3 +142,24 @@ func (afrm Frame) ValidateSize(v *lneto2.Validator) {
v.AddError(errShortARP)
}
}
func (afrm Frame) String() string {
opstr := afrm.Operation().String()
hwt, _ := afrm.Hardware()
ptt, _ := afrm.Protocol()
sndhw, sndpt := afrm.Sender()
tgthw, tgtpt := afrm.Target()
var sndstr, tgtstr string
if ptt == lneto2.EtherTypeIPv4 || ptt == lneto2.EtherTypeIPv6 {
sender, _ := netip.AddrFromSlice(sndpt)
target, _ := netip.AddrFromSlice(tgtpt)
sndstr = sender.String()
tgtstr = target.String()
} else {
sndstr = net.HardwareAddr(sndpt).String()
tgtstr = net.HardwareAddr(tgtpt).String()
}
return fmt.Sprintf("ARP %s HW=(%d,SENDER=%s,TARGET=%s) PROTO=(%s,SENDER=%s,TARGET=%s)",
opstr, hwt, net.HardwareAddr(sndhw).String(), net.HardwareAddr(tgthw).String(),
ptt.String(), sndstr, tgtstr)
}
+25
View File
@@ -0,0 +1,25 @@
// Code generated by "stringer -type=Operation -linecomment -output stringers.go ."; DO NOT EDIT.
package arp
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[OpRequest-1]
_ = x[OpReply-2]
}
const _Operation_name = "requestreply"
var _Operation_index = [...]uint8{0, 7, 12}
func (i Operation) String() string {
i -= 1
if i >= Operation(len(_Operation_index)-1) {
return "Operation(" + strconv.FormatInt(int64(i+1), 10) + ")"
}
return _Operation_name[_Operation_index[i]:_Operation_index[i+1]]
}