Debian's patch for better device name/lock handling
[oss-qm-packages.git] / rarp.c
blob8ad7998af3e5f5c2d0f19f85170a27cc191de757
1 /*
2 * rarp This file contains an implementation of the command
3 * that maintains the kernel's RARP cache. It is derived
4 * from Fred N. van Kempen's arp command.
6 * Version: $Id: rarp.c,v 1.5 1999/01/05 20:53:02 philip Exp $
8 * Usage: rarp -d hostname Delete entry
9 * rarp -s hostname ethernet_address Add entry
10 * rarp -a Print entries
11 * rarp -f Add frop /etc/ethers
13 * Rewritten: Phil Blundell <Philip.Blundell@pobox.com> 1997-08-03
14 * gettext instead of catgets: Arnaldo Carvalho de Melo <acme@conectiva.com.br> 1998-06-29
15 * 1998-01-01 Bernd Eckenfels reorganised usage()
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <net/if.h>
25 #include <net/if_arp.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <netdb.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <getopt.h>
35 #define DFLT_HW "ether"
37 #include "config.h"
38 #include "intl.h"
39 #include "net-support.h"
40 #include "version.h"
41 #include "pathnames.h"
43 static char no_rarp_message[] = N_("This kernel does not support RARP.\n");
45 static char version_string[] = RELEASE "\nrarp 1.02 (1998-06-30)\n";
47 static struct hwtype *hardware = NULL;
49 /* Delete an entry from the RARP cache. */
50 static int rarp_delete(int fd, struct hostent *hp)
52 struct arpreq req;
53 struct sockaddr_in *si;
54 unsigned int found = 0;
55 char **addr;
57 /* The host can have more than one address, so we loop on them. */
58 for (addr = hp->h_addr_list; *addr != NULL; addr++) {
59 memset((char *) &req, 0, sizeof(req));
60 si = (struct sockaddr_in *) &req.arp_pa;
61 si->sin_family = hp->h_addrtype;
62 memcpy((char *) &si->sin_addr, *addr, hp->h_length);
64 /* Call the kernel. */
65 if (ioctl(fd, SIOCDRARP, &req) == 0) {
66 found++;
67 } else {
68 switch (errno) {
69 case ENXIO:
70 break;
71 case ENODEV:
72 fputs(_(no_rarp_message), stderr);
73 return 1;
74 default:
75 perror("SIOCDRARP");
76 return 1;
81 if (found == 0)
82 printf(_("no RARP entry for %s.\n"), hp->h_name);
83 return 0;
87 /* Set an entry in the RARP cache. */
88 static int rarp_set(int fd, struct hostent *hp, char *hw_addr)
90 struct arpreq req;
91 struct sockaddr_in *si;
92 struct sockaddr sap;
94 if (hardware->input(hw_addr, &sap)) {
95 fprintf(stderr, _("%s: bad hardware address\n"), hw_addr);
96 return 1;
98 /* Clear and fill in the request block. */
99 memset((char *) &req, 0, sizeof(req));
100 si = (struct sockaddr_in *) &req.arp_pa;
101 si->sin_family = hp->h_addrtype;
102 memcpy((char *) &si->sin_addr, hp->h_addr_list[0], hp->h_length);
103 req.arp_ha.sa_family = hardware->type;
104 memcpy(req.arp_ha.sa_data, sap.sa_data, hardware->alen);
106 /* Call the kernel. */
107 if (ioctl(fd, SIOCSRARP, &req) < 0) {
108 if (errno == ENODEV)
109 fputs(_(no_rarp_message), stderr);
110 else
111 perror("SIOCSRARP");
112 return 1;
114 return 0;
117 /* Process an EtherFile */
118 static int rarp_file(int fd, const char *name)
120 char buff[1024];
121 char *host, *addr;
122 int linenr;
123 FILE *fp;
124 struct hostent *hp;
126 if ((fp = fopen(name, "r")) == NULL) {
127 fprintf(stderr, _("rarp: cannot open file %s:%s.\n"), name, strerror(errno));
128 return -1;
130 /* Read the lines in the file. */
131 linenr = 0;
132 while (fgets(buff, sizeof(buff), fp)) {
133 ++linenr;
134 if (buff[0] == '#' || buff[0] == '\0')
135 continue;
136 if ((addr = strtok(buff, "\n \t")) == NULL)
137 continue;
138 if ((host = strtok(NULL, "\n \t")) == NULL) {
139 fprintf(stderr, _("rarp: format error at %s:%u\n"), name, linenr);
140 continue;
142 if ((hp = gethostbyname(host)) == NULL) {
143 fprintf(stderr, _("rarp: %s: unknown host\n"), host);
145 if (rarp_set(fd, hp, addr) != 0) {
146 fprintf(stderr, _("rarp: cannot set entry from %s:%u\n"), name, linenr);
150 (void) fclose(fp);
151 return 0;
154 static int display_cache(void)
156 FILE *fd = fopen(_PATH_PROCNET_RARP, "r");
157 char buffer[256];
158 if (fd == NULL) {
159 if (errno == ENOENT)
160 fputs(_(no_rarp_message), stderr);
161 else
162 perror(_PATH_PROCNET_RARP);
163 return 1;
165 while (feof(fd) == 0) {
166 if (fgets(buffer, 255, fd))
167 fputs(buffer, stdout);
169 fclose(fd);
170 return 0;
173 static void usage(void)
175 fprintf(stderr, _("Usage: rarp -a list entries in cache.\n"));
176 fprintf(stderr, _(" rarp -d <hostname> delete entry from cache.\n"));
177 fprintf(stderr, _(" rarp [<HW>] -s <hostname> <hwaddr> add entry to cache.\n"));
178 fprintf(stderr, _(" rarp -f add entries from /etc/ethers.\n"));
179 fprintf(stderr, _(" rarp -V display program version.\n\n"));
181 fprintf(stderr, _(" <HW>=Use '-H <hw>' to specify hardware address type. Default: %s\n"), DFLT_HW);
182 fprintf(stderr, _(" List of possible hardware types (which support ARP):\n"));
183 print_hwlist(1); /* 1 = ARPable */
184 exit(E_USAGE);
187 #define MODE_DISPLAY 1
188 #define MODE_DELETE 2
189 #define MODE_SET 3
190 #define MODE_ETHERS 4
192 static struct option longopts[] =
194 {"version", 0, NULL, 'V'},
195 {"verbose", 0, NULL, 'v'},
196 {"list", 0, NULL, 'a'},
197 {"set", 0, NULL, 's'},
198 {"delete", 0, NULL, 'd'},
199 {"help", 0, NULL, 'h'},
200 {NULL, 0, NULL, 0}
203 int main(int argc, char **argv)
205 int result = 0, mode = 0, c, nargs = 0, verbose = 0;
206 char *args[3];
207 struct hostent *hp;
208 int fd;
210 #if I18N
211 bindtextdomain("net-tools", "/usr/share/locale");
212 textdomain("net-tools");
213 #endif
215 /* Get a default hardware type. */
216 hardware = get_hwtype(DFLT_HW);
218 do {
219 c = getopt_long(argc, argv, "-ht:aHdsVvf", longopts, NULL);
220 switch (c) {
221 case EOF:
222 break;
223 case 'h':
224 usage();
225 case 'V':
226 fprintf(stderr, version_string);
227 exit(E_VERSION);
228 break;
229 case 'v':
230 verbose++;
231 break;
232 case 'a':
233 case 's':
234 case 'd':
235 if (mode) {
236 fprintf(stderr, _("%s: illegal option mix.\n"), argv[0]);
237 usage();
238 } else {
239 mode = (c == 'a' ? MODE_DISPLAY : (c == 'd' ? MODE_DELETE : MODE_SET));
241 break;
242 case 'f':
243 mode = MODE_ETHERS;
244 break;
245 case 'H':
246 case 't':
247 if (optarg) {
248 hardware = get_hwtype(optarg);
249 } else {
250 usage();
252 break;
253 case 1:
254 if (nargs == 2) {
255 usage();
256 exit(1);
257 } else {
258 args[nargs++] = optarg;
260 break;
261 default:
262 usage();
264 } while (c != EOF);
266 if (hardware == NULL) {
267 fprintf(stderr, _("rarp: %s: unknown hardware type.\n"), optarg);
268 exit(1);
270 switch (mode) {
271 case 0:
272 usage();
274 case MODE_DISPLAY:
275 if (nargs != (mode - 1)) {
276 usage();
278 result = display_cache();
279 break;
281 case MODE_DELETE:
282 case MODE_SET:
283 if (nargs != (mode - 1)) {
284 usage();
286 if ((hp = gethostbyname(args[0])) == NULL) {
287 fprintf(stderr, _("rarp: %s: unknown host\n"), args[0]);
288 exit(1);
290 if (fd = socket(PF_INET, SOCK_DGRAM, 0), fd < 0) {
291 perror("socket");
292 exit(1);
294 result = (mode == MODE_DELETE) ? rarp_delete(fd, hp) : rarp_set(fd, hp, args[1]);
295 close(fd);
296 break;
298 case MODE_ETHERS:
299 if (nargs != 0 && nargs != 1)
300 usage();
301 if (fd = socket(PF_INET, SOCK_DGRAM, 0), fd < 0) {
302 perror("socket");
303 exit(1);
305 result = rarp_file(fd, nargs ? args[0] : _PATH_ETHERS);
306 close(fd);
309 exit(result);