summaryrefslogtreecommitdiff
path: root/proto_arp.c
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2016-01-21 23:23:26 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-01-26 18:10:23 +0100
commitf6371fb6df916db984bdce57ddea15abe7f780be (patch)
tree2c3c9fb7f0760118e5e3847d26f5fd8967b4cddb /proto_arp.c
parentf634c7420a9345987001c0e965c4d79057197f6f (diff)
dissectors: arp: Print hardware & protocol addresses
Print sender/target MAC and/or IPv4 address if htype is Ethernet and/or ptype is IPv4, respectively. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> [tk: minor simplifications of the code] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'proto_arp.c')
-rw-r--r--proto_arp.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/proto_arp.c b/proto_arp.c
index 9121223..9f2a4e0 100644
--- a/proto_arp.c
+++ b/proto_arp.c
@@ -5,7 +5,9 @@
*/
#include <stdint.h>
+#include <arpa/inet.h>
#include <netinet/in.h> /* for ntohs() */
+#include <linux/if_ether.h>
#include "proto.h"
#include "protos.h"
@@ -42,6 +44,44 @@ struct arphdr {
#define ARPOP_InREPLY 9 /* InARP reply */
#define ARPOP_NAK 10 /* (ATM)ARP NAK */
+enum addr_direct {
+ ADDR_SENDER,
+ ADDR_TARGET,
+};
+
+static void arp_print_addrs(struct arphdr *arp, enum addr_direct addr_dir)
+{
+ char *dir = addr_dir == ADDR_SENDER ? "Sender" : "Target";
+ bool has_eth;
+ bool has_ip4;
+
+ has_eth = ntohs(arp->ar_hrd) == ARPHRD_ETHER;
+ has_ip4 = ntohs(arp->ar_pro) == ETH_P_IP;
+
+ if (has_eth) {
+ uint8_t *mac;
+
+ mac = addr_dir == ADDR_SENDER ? &arp->ar_sha[0] : &arp->ar_tha[0];
+
+ tprintf(", %s MAC (%.2x:%.2x:%.2x:%.2x:%.2x:%.2x)",
+ dir, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+ }
+
+ if (has_ip4) {
+ char ip_str[INET_ADDRSTRLEN];
+ uint32_t ip;
+
+ if (addr_dir == ADDR_SENDER)
+ ip = *(uint32_t *)&arp->ar_sip[0];
+ else
+ ip = *(uint32_t *)&arp->ar_tip[0];
+
+ inet_ntop(AF_INET, &ip, ip_str, sizeof(ip_str));
+
+ tprintf(", %s IP (%s)", dir, ip_str);
+ }
+}
+
static void arp(struct pkt_buff *pkt)
{
char *hrd;
@@ -115,6 +155,10 @@ static void arp(struct pkt_buff *pkt)
tprintf("HA Len (%u), ", arp->ar_hln);
tprintf("Proto Len (%u), ", arp->ar_pln);
tprintf("Opcode (%u => %s)", ntohs(arp->ar_op), opcode);
+
+ arp_print_addrs(arp, ADDR_SENDER);
+ arp_print_addrs(arp, ADDR_TARGET);
+
tprintf(" ]\n");
}