-
Notifications
You must be signed in to change notification settings - Fork 9
/
arpcmd.c
232 lines (218 loc) · 4.83 KB
/
arpcmd.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* ARP commands
* Copyright 1991, Phil Karn, KA9Q
*/
#include <stdio.h>
#include <ctype.h>
#include "global.h"
#include "mbuf.h"
#include "timer.h"
#include "enet.h"
#include "ax25.h"
#include "arp.h"
#include "netuser.h"
#include "cmdparse.h"
#include "commands.h"
static int doarpadd(int argc,char *argv[],void *p);
static int doarpdrop(int argc,char *argv[],void *p);
static int doarpflush(int argc,char *argv[],void *p);
static void dumparp(void);
static struct cmds Arpcmds[] = {
"add", doarpadd, 0, 4,
"arp add <hostid> ether|ax25|netrom|arcnet <ether addr|callsign>",
"drop", doarpdrop, 0, 3,
"arp drop <hostid> ether|ax25|netrom|arcnet",
"flush", doarpflush, 0, 0,
NULL,
"publish", doarpadd, 0, 4,
"arp publish <hostid> ether|ax25|netrom|arcnet <ether addr|callsign>",
NULL,
};
char *Arptypes[] = {
"NET/ROM",
"10 Mb Ethernet",
"3 Mb Ethernet",
"AX.25",
"Pronet",
"Chaos",
"",
"Arcnet",
"Appletalk"
};
int
doarp(argc,argv,p)
int argc;
char *argv[];
void *p;
{
if(argc < 2){
dumparp();
return 0;
}
return subcmd(Arpcmds,argc,argv,p);
}
static
doarpadd(argc,argv,p)
int argc;
char *argv[];
void *p;
{
uint16 hardware;
int32 addr;
uint8 *hwaddr;
struct arp_tab *ap;
struct arp_type *at;
int pub = 0;
if(argv[0][0] == 'p') /* Is this entry published? */
pub = 1;
if((addr = resolve(argv[1])) == 0){
printf(Badhost,argv[1]);
return 1;
}
/* This is a kludge. It really ought to be table driven */
switch(tolower(argv[2][0])){
case 'n': /* Net/Rom pseudo-type */
hardware = ARP_NETROM;
break;
case 'e': /* "ether" */
hardware = ARP_ETHER;
break;
case 'a': /* "ax25" */
switch(tolower(argv[2][1])) {
case 'x':
hardware = ARP_AX25;
break;
case 'r':
hardware = ARP_ARCNET;
break;
default:
printf("unknown hardware type \"%s\"\n",argv[2]);
return -1;
}
break;
case 'm': /* "mac appletalk" */
hardware = ARP_APPLETALK;
break;
default:
printf("unknown hardware type \"%s\"\n",argv[2]);
return -1;
}
/* If an entry already exists, clear it */
if((ap = arp_lookup(hardware,addr)) != NULL)
arp_drop(ap);
at = &Arp_type[hardware];
if(at->scan == NULL){
printf("Attach device first\n");
return 1;
}
/* Allocate buffer for hardware address and fill with remaining args */
hwaddr = mallocw(at->hwalen);
/* Destination address */
(*at->scan)(hwaddr,argv[3]);
ap = arp_add(addr,hardware,hwaddr,pub); /* Put in table */
free(hwaddr); /* Clean up */
stop_timer(&ap->timer); /* Make entry permanent */
set_timer(&ap->timer,0L);
return 0;
}
/* Remove an ARP entry */
static
doarpdrop(argc,argv,p)
int argc;
char *argv[];
void *p;
{
uint16 hardware;
int32 addr;
struct arp_tab *ap;
if((addr = resolve(argv[1])) == 0){
printf(Badhost,argv[1]);
return 1;
}
/* This is a kludge. It really ought to be table driven */
switch(tolower(argv[2][0])){
case 'n':
hardware = ARP_NETROM;
break;
case 'e': /* "ether" */
hardware = ARP_ETHER;
break;
case 'a': /* "ax25" */
switch(tolower(argv[2][1])) {
case 'x':
hardware = ARP_AX25;
break;
case 'r':
hardware = ARP_ARCNET;
break;
default:
hardware = 0;
break;
}
break;
case 'm': /* "mac appletalk" */
hardware = ARP_APPLETALK;
break;
default:
hardware = 0;
break;
}
if((ap = arp_lookup(hardware,addr)) == NULL)
return -1;
arp_drop(ap);
return 0;
}
/* Flush all automatic entries in the arp cache */
static int
doarpflush(argc,argv,p)
int argc;
char *argv[];
void *p;
{
register struct arp_tab *ap;
struct arp_tab *aptmp;
int i;
for(i=0;i<HASHMOD;i++){
for(ap = Arp_tab[i];ap != NULL;ap = aptmp){
aptmp = ap->next;
if(dur_timer(&ap->timer) != 0)
arp_drop(ap);
}
}
return 0;
}
/* Dump ARP table */
static void
dumparp()
{
register int i;
register struct arp_tab *ap;
char e[128];
printf("received %u badtype %u bogus addr %u reqst in %u replies %u reqst out %u\n",
Arp_stat.recv,Arp_stat.badtype,Arp_stat.badaddr,Arp_stat.inreq,
Arp_stat.replies,Arp_stat.outreq);
printf("IP addr Type Time Q Addr\n");
for(i=0;i<HASHMOD;i++){
for(ap = Arp_tab[i];ap != (struct arp_tab *)NULL;ap = ap->next){
printf("%-16s",inet_ntoa(ap->ip_addr));
printf("%-15s",smsg(Arptypes,NHWTYPES,ap->hardware));
printf("%-5ld",read_timer(&ap->timer)/1000L);
if(ap->state == ARP_PENDING)
printf("%-2u",len_q(ap->pending));
else
printf(" ");
if(ap->state == ARP_VALID){
if(Arp_type[ap->hardware].format != NULL){
(*Arp_type[ap->hardware].format)(e,ap->hw_addr);
} else {
e[0] = '\0';
}
printf("%s",e);
} else {
printf("[unknown]");
}
if(ap->pub)
printf(" (published)");
printf("\n");
}
}
}