@@ -14,11 +14,13 @@ const headerSizeARP = 28
1414// associated with a given internet layer address, typically an IPv4 address.
1515// Defined in RFC 826.
1616type ARPPacket struct {
17- HardwareType uint16 // Network link protocol type.
18- ProtocolType uint16 // Internetwork protocol for which the ARP request is intended.
19- Hlen uint8 // Length (in octets) of a hardware address.
20- Plen uint8 // Length (in octets) of internetwork addresses.
21- Op uint16 // Specifies the operation that the sender is performing.
17+ HardwareType uint16 // Network link protocol type.
18+ ProtocolType uint16 // Internetwork protocol for which the ARP request is intended.
19+ ProtocolTypeDesc string // Internetwork protocol description.
20+ Hlen uint8 // Length (in octets) of a hardware address.
21+ Plen uint8 // Length (in octets) of internetwork addresses.
22+ Op uint16 // Specifies the operation that the sender is performing.
23+ OpDesc string // Operation description.
2224 // Media address of the sender. In an ARP request this field is used to indicate
2325 // the address of the host sending the request. In an ARP reply this field is used
2426 // to indicate the address of the host that the request was looking for.
@@ -31,7 +33,7 @@ type ARPPacket struct {
3133}
3234
3335func (ap * ARPPacket ) String () string {
34- return fmt .Sprintf (`ARP Packet:
36+ return fmt .Sprintf (`%s
3537- Hardware Type: %d
3638- Protocol Type: %s (%#04x)
3739- HLen: %d
@@ -42,12 +44,13 @@ func (ap *ARPPacket) String() string {
4244- Target MAC Address: %s
4345- Target IP Address: %s
4446` ,
47+ ap .Summary (),
4548 ap .HardwareType ,
46- ap .ptype () ,
49+ ap .ProtocolTypeDesc ,
4750 ap .ProtocolType ,
4851 ap .Hlen ,
4952 ap .Plen ,
50- ap .op () ,
53+ ap .OpDesc ,
5154 ap .Op ,
5255 ap .SenderMAC ,
5356 ap .SenderIP ,
@@ -56,16 +59,31 @@ func (ap *ARPPacket) String() string {
5659 )
5760}
5861
62+ func (ap * ARPPacket ) Summary () string {
63+ var message string
64+ switch ap .OpDesc {
65+ case "request" :
66+ message = fmt .Sprintf ("ARP Packet: (%s) Who has %s? Tell %s" , ap .OpDesc , ap .TargetIP , ap .SenderIP )
67+ case "reply" :
68+ message = fmt .Sprintf ("ARP Packet: (%s) %s at %s" , ap .OpDesc , ap .SenderIP , ap .SenderMAC )
69+ default :
70+ message = fmt .Sprintf ("ARP Packet: (%s)" , ap .OpDesc )
71+ }
72+ return message
73+ }
74+
5975// Parse parses the given ARP packet data into the ARPPacket struct.
6076func (ap * ARPPacket ) Parse (data []byte ) error {
6177 if len (data ) < headerSizeARP {
6278 return fmt .Errorf ("minimum header size for ARP is %d bytes, got %d bytes" , headerSizeARP , len (data ))
6379 }
6480 ap .HardwareType = binary .BigEndian .Uint16 (data [0 :2 ])
6581 ap .ProtocolType = binary .BigEndian .Uint16 (data [2 :4 ])
82+ ap .ProtocolTypeDesc = ap .ptype ()
6683 ap .Hlen = data [4 ]
6784 ap .Plen = data [5 ]
6885 ap .Op = binary .BigEndian .Uint16 (data [6 :8 ])
86+ ap .OpDesc = ap .op ()
6987 hoffset := 8 + ap .Hlen
7088 ap .SenderMAC = net .HardwareAddr (data [8 :hoffset ])
7189 poffset := hoffset + ap .Plen
0 commit comments