summaryrefslogtreecommitdiffstats
path: root/listen/arpdump.c
blob: ef94c00032f3dfe201f1ca9dba2458351844793b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* @(#) $Header: /home/ax25-cvs/ax25-apps/listen/arpdump.c,v 1.2 2001/09/12 13:18:43 terry Exp $ */

/* ARP packet tracing routines
 * Copyright 1991 Phil Karn, KA9Q
 */
#include <stdio.h>
#include "listen.h"

#define	ARP_REQUEST	1
#define	ARP_REPLY	2
#define	REVARP_REQUEST	3
#define	REVARP_REPLY	4

#define	ARP_AX25	3

#define	AXALEN		7

#define	PID_IP		0xCC

void arp_dump(unsigned char *data, int length)
{
	int is_ip = 0;
	int hardware;
	int protocol;
	int hwlen;
	int pralen;
	int operation;
	unsigned char *shwaddr, *sprotaddr;
	unsigned char *thwaddr, *tprotaddr;
	char tmp[25];

	lprintf(T_PROTOCOL, "ARP: ");
	lprintf(T_IPHDR, "len %d", length);
	if (length < 16) {
		lprintf(T_ERROR, " bad packet\n");
		return;
	}

	hardware = get16(data + 0);
	protocol = get16(data + 2);
	hwlen = data[4];
	pralen = data[5];
	operation = get16(data + 6);

	if (hardware != ARP_AX25) {
		lprintf(T_IPHDR, " non-AX25 ARP packet\n");
		return;
	}

	lprintf(T_IPHDR, " hwtype AX25");

	/* Print hardware length only if it doesn't match
	 * the length in the known types table
	 */
	if (hwlen != AXALEN)
		lprintf(T_IPHDR, " hwlen %d", hwlen);

	if (protocol == PID_IP) {
		lprintf(T_IPHDR, " prot IP");
		is_ip = 1;
	} else {
		lprintf(T_IPHDR, " prot 0x%x prlen %d", protocol, pralen);
	}

	switch (operation) {
	case ARP_REQUEST:
		lprintf(T_IPHDR, " op REQUEST");
		break;
	case ARP_REPLY:
		lprintf(T_IPHDR, " op REPLY");
		break;
	case REVARP_REQUEST:
		lprintf(T_IPHDR, " op REVERSE REQUEST");
		break;
	case REVARP_REPLY:
		lprintf(T_IPHDR, " op REVERSE REPLY");
		break;
	default:
		lprintf(T_IPHDR, " op %d", operation);
		break;
	}

	shwaddr = data + 8;
	sprotaddr = shwaddr + hwlen;

	thwaddr = sprotaddr + pralen;
	tprotaddr = thwaddr + hwlen;

	lprintf(T_IPHDR, "\nsender");
	if (is_ip)
		lprintf(T_ADDR, " IPaddr %d.%d.%d.%d",
			sprotaddr[0], sprotaddr[1],
			sprotaddr[2], sprotaddr[3]);
	lprintf(T_IPHDR, " hwaddr %s\n", pax25(tmp, shwaddr));

	lprintf(T_IPHDR, "target");
	if (is_ip)
		lprintf(T_ADDR, " IPaddr %d.%d.%d.%d",
			tprotaddr[0], tprotaddr[1],
			tprotaddr[2], tprotaddr[3]);
	if (*thwaddr != 0)
		lprintf(T_ADDR, " hwaddr %s", pax25(tmp, thwaddr));
	lprintf(T_IPHDR, "\n");
}