Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * arp.c - Manipulate the system ARP cache |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * Author: Fred N. van Kempen, <waltje at uwalt.nl.mugnet.org> |
| 11 | * Busybox port: Paul van Gool <pvangool at mimotech.com> |
| 12 | * |
| 13 | * modified for getopt32 by Arne Bernin <arne [at] alamut.de> |
| 14 | */ |
| 15 | |
| 16 | #include "busybox.h" |
| 17 | #include "inet_common.h" |
| 18 | |
| 19 | #include <arpa/inet.h> |
| 20 | #include <net/if.h> |
| 21 | #include <net/if_arp.h> |
| 22 | #include <netinet/ether.h> |
| 23 | #include <netpacket/packet.h> |
| 24 | |
| 25 | #define DEBUG 0 |
| 26 | |
| 27 | #define DFLT_AF "inet" |
| 28 | #define DFLT_HW "ether" |
| 29 | |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 30 | #define ARP_OPT_A (0x1) |
| 31 | #define ARP_OPT_p (0x2) |
| 32 | #define ARP_OPT_H (0x4) |
| 33 | #define ARP_OPT_t (0x8) |
| 34 | #define ARP_OPT_i (0x10) |
| 35 | #define ARP_OPT_a (0x20) |
| 36 | #define ARP_OPT_d (0x40) |
| 37 | #define ARP_OPT_n (0x80) /* do not resolve addresses */ |
| 38 | #define ARP_OPT_D (0x100) /* HW-address is devicename */ |
| 39 | #define ARP_OPT_s (0x200) |
| 40 | #define ARP_OPT_v (0x400 * DEBUG) /* debugging output flag */ |
| 41 | |
| 42 | |
| 43 | static const struct aftype *ap; /* current address family */ |
| 44 | static const struct hwtype *hw; /* current hardware type */ |
| 45 | static int sockfd; /* active socket descriptor */ |
| 46 | static smallint hw_set; /* flag if hw-type was set (-H) */ |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 47 | static const char *device = ""; /* current device */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 48 | |
| 49 | static const char *const options[] = { |
| 50 | "pub", |
| 51 | "priv", |
| 52 | "temp", |
| 53 | "trail", |
| 54 | "dontpub", |
| 55 | "auto", |
| 56 | "dev", |
| 57 | "netmask", |
| 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | /* Delete an entry from the ARP cache. */ |
| 62 | /* Called only from main, once */ |
| 63 | static int arp_del(char **args) |
| 64 | { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 65 | char *host; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 66 | struct arpreq req; |
| 67 | struct sockaddr sa; |
| 68 | int flags = 0; |
| 69 | int err; |
| 70 | |
| 71 | memset(&req, 0, sizeof(req)); |
| 72 | |
| 73 | /* Resolve the host name. */ |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 74 | host = *args; |
| 75 | if (ap->input(host, &sa) < 0) { |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 76 | bb_herror_msg_and_die("%s", host); |
| 77 | } |
| 78 | |
| 79 | /* If a host has more than one address, use the correct one! */ |
| 80 | memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr)); |
| 81 | |
| 82 | if (hw_set) |
| 83 | req.arp_ha.sa_family = hw->type; |
| 84 | |
| 85 | req.arp_flags = ATF_PERM; |
| 86 | args++; |
| 87 | while (*args != NULL) { |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 88 | switch (index_in_str_array(options, *args)) { |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 89 | case 0: /* "pub" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 90 | flags |= 1; |
| 91 | args++; |
| 92 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 93 | case 1: /* "priv" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 94 | flags |= 2; |
| 95 | args++; |
| 96 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 97 | case 2: /* "temp" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 98 | req.arp_flags &= ~ATF_PERM; |
| 99 | args++; |
| 100 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 101 | case 3: /* "trail" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 102 | req.arp_flags |= ATF_USETRAILERS; |
| 103 | args++; |
| 104 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 105 | case 4: /* "dontpub" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 106 | #ifdef HAVE_ATF_DONTPUB |
| 107 | req.arp_flags |= ATF_DONTPUB; |
| 108 | #else |
| 109 | bb_error_msg("feature ATF_DONTPUB is not supported"); |
| 110 | #endif |
| 111 | args++; |
| 112 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 113 | case 5: /* "auto" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 114 | #ifdef HAVE_ATF_MAGIC |
| 115 | req.arp_flags |= ATF_MAGIC; |
| 116 | #else |
| 117 | bb_error_msg("feature ATF_MAGIC is not supported"); |
| 118 | #endif |
| 119 | args++; |
| 120 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 121 | case 6: /* "dev" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 122 | if (*++args == NULL) |
| 123 | bb_show_usage(); |
| 124 | device = *args; |
| 125 | args++; |
| 126 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 127 | case 7: /* "netmask" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 128 | if (*++args == NULL) |
| 129 | bb_show_usage(); |
| 130 | if (strcmp(*args, "255.255.255.255") != 0) { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 131 | host = *args; |
| 132 | if (ap->input(host, &sa) < 0) { |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 133 | bb_herror_msg_and_die("%s", host); |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 134 | } |
| 135 | memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr)); |
| 136 | req.arp_flags |= ATF_NETMASK; |
| 137 | } |
| 138 | args++; |
| 139 | break; |
| 140 | default: |
| 141 | bb_show_usage(); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (flags == 0) |
| 146 | flags = 3; |
| 147 | |
| 148 | strncpy(req.arp_dev, device, sizeof(req.arp_dev)); |
| 149 | |
| 150 | err = -1; |
| 151 | |
| 152 | /* Call the kernel. */ |
| 153 | if (flags & 2) { |
| 154 | if (option_mask32 & ARP_OPT_v) |
| 155 | bb_error_msg("SIOCDARP(nopub)"); |
| 156 | err = ioctl(sockfd, SIOCDARP, &req); |
| 157 | if (err < 0) { |
| 158 | if (errno == ENXIO) { |
| 159 | if (flags & 1) |
| 160 | goto nopub; |
| 161 | printf("No ARP entry for %s\n", host); |
| 162 | return -1; |
| 163 | } |
| 164 | bb_perror_msg_and_die("SIOCDARP(priv)"); |
| 165 | } |
| 166 | } |
| 167 | if ((flags & 1) && err) { |
| 168 | nopub: |
| 169 | req.arp_flags |= ATF_PUBL; |
| 170 | if (option_mask32 & ARP_OPT_v) |
| 171 | bb_error_msg("SIOCDARP(pub)"); |
| 172 | if (ioctl(sockfd, SIOCDARP, &req) < 0) { |
| 173 | if (errno == ENXIO) { |
| 174 | printf("No ARP entry for %s\n", host); |
| 175 | return -1; |
| 176 | } |
| 177 | bb_perror_msg_and_die("SIOCDARP(pub)"); |
| 178 | } |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* Get the hardware address to a specified interface name */ |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 184 | static void arp_getdevhw(char *ifname, struct sockaddr *sa, |
| 185 | const struct hwtype *hwt) |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 186 | { |
| 187 | struct ifreq ifr; |
| 188 | const struct hwtype *xhw; |
| 189 | |
| 190 | strcpy(ifr.ifr_name, ifname); |
| 191 | if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) { |
| 192 | bb_perror_msg_and_die("cant get HW-Address for '%s'", ifname); |
| 193 | } |
| 194 | if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) { |
| 195 | bb_error_msg_and_die("protocol type mismatch"); |
| 196 | } |
| 197 | memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr)); |
| 198 | |
| 199 | if (option_mask32 & ARP_OPT_v) { |
| 200 | xhw = get_hwntype(ifr.ifr_hwaddr.sa_family); |
| 201 | if (!xhw || !xhw->print) { |
| 202 | xhw = get_hwntype(-1); |
| 203 | } |
| 204 | bb_error_msg("device '%s' has HW address %s '%s'", |
| 205 | ifname, xhw->name, |
| 206 | xhw->print((char *) &ifr.ifr_hwaddr.sa_data)); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* Set an entry in the ARP cache. */ |
| 211 | /* Called only from main, once */ |
| 212 | static int arp_set(char **args) |
| 213 | { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 214 | char *host; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 215 | struct arpreq req; |
| 216 | struct sockaddr sa; |
| 217 | int flags; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 218 | |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 219 | memset(&req, 0, sizeof(req)); |
| 220 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 221 | host = *args++; |
| 222 | if (ap->input(host, &sa) < 0) { |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 223 | bb_herror_msg_and_die("%s", host); |
| 224 | } |
| 225 | /* If a host has more than one address, use the correct one! */ |
| 226 | memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr)); |
| 227 | |
| 228 | /* Fetch the hardware address. */ |
| 229 | if (*args == NULL) { |
| 230 | bb_error_msg_and_die("need hardware address"); |
| 231 | } |
| 232 | if (option_mask32 & ARP_OPT_D) { |
| 233 | arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL); |
| 234 | } else { |
| 235 | if (hw->input(*args++, &req.arp_ha) < 0) { |
| 236 | bb_error_msg_and_die("invalid hardware address"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* Check out any modifiers. */ |
| 241 | flags = ATF_PERM | ATF_COM; |
| 242 | while (*args != NULL) { |
| 243 | switch (index_in_str_array(options, *args)) { |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 244 | case 0: /* "pub" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 245 | flags |= ATF_PUBL; |
| 246 | args++; |
| 247 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 248 | case 1: /* "priv" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 249 | flags &= ~ATF_PUBL; |
| 250 | args++; |
| 251 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 252 | case 2: /* "temp" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 253 | flags &= ~ATF_PERM; |
| 254 | args++; |
| 255 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 256 | case 3: /* "trail" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 257 | flags |= ATF_USETRAILERS; |
| 258 | args++; |
| 259 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 260 | case 4: /* "dontpub" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 261 | #ifdef HAVE_ATF_DONTPUB |
| 262 | flags |= ATF_DONTPUB; |
| 263 | #else |
| 264 | bb_error_msg("feature ATF_DONTPUB is not supported"); |
| 265 | #endif |
| 266 | args++; |
| 267 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 268 | case 5: /* "auto" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 269 | #ifdef HAVE_ATF_MAGIC |
| 270 | flags |= ATF_MAGIC; |
| 271 | #else |
| 272 | bb_error_msg("feature ATF_MAGIC is not supported"); |
| 273 | #endif |
| 274 | args++; |
| 275 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 276 | case 6: /* "dev" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 277 | if (*++args == NULL) |
| 278 | bb_show_usage(); |
| 279 | device = *args; |
| 280 | args++; |
| 281 | break; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 282 | case 7: /* "netmask" */ |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 283 | if (*++args == NULL) |
| 284 | bb_show_usage(); |
| 285 | if (strcmp(*args, "255.255.255.255") != 0) { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 286 | host = *args; |
| 287 | if (ap->input(host, &sa) < 0) { |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 288 | bb_herror_msg_and_die("%s", host); |
| 289 | } |
| 290 | memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr)); |
| 291 | flags |= ATF_NETMASK; |
| 292 | } |
| 293 | args++; |
| 294 | break; |
| 295 | default: |
| 296 | bb_show_usage(); |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* Fill in the remainder of the request. */ |
| 302 | req.arp_flags = flags; |
| 303 | |
| 304 | strncpy(req.arp_dev, device, sizeof(req.arp_dev)); |
| 305 | |
| 306 | /* Call the kernel. */ |
| 307 | if (option_mask32 & ARP_OPT_v) |
| 308 | bb_error_msg("SIOCSARP()"); |
| 309 | if (ioctl(sockfd, SIOCSARP, &req) < 0) { |
| 310 | bb_perror_msg_and_die("SIOCSARP"); |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* Print the contents of an ARP request block. */ |
| 317 | static void |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 318 | arp_disp(const char *name, char *ip, int type, int arp_flags, |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 319 | char *hwa, char *mask, char *dev) |
| 320 | { |
| 321 | const struct hwtype *xhw; |
| 322 | |
| 323 | xhw = get_hwntype(type); |
| 324 | if (xhw == NULL) |
| 325 | xhw = get_hwtype(DFLT_HW); |
| 326 | |
| 327 | printf("%s (%s) at ", name, ip); |
| 328 | |
| 329 | if (!(arp_flags & ATF_COM)) { |
| 330 | if (arp_flags & ATF_PUBL) |
| 331 | printf("* "); |
| 332 | else |
| 333 | printf("<incomplete> "); |
| 334 | } else { |
| 335 | printf("%s [%s] ", hwa, xhw->name); |
| 336 | } |
| 337 | |
| 338 | if (arp_flags & ATF_NETMASK) |
| 339 | printf("netmask %s ", mask); |
| 340 | |
| 341 | if (arp_flags & ATF_PERM) |
| 342 | printf("PERM "); |
| 343 | if (arp_flags & ATF_PUBL) |
| 344 | printf("PUP "); |
| 345 | #ifdef HAVE_ATF_MAGIC |
| 346 | if (arp_flags & ATF_MAGIC) |
| 347 | printf("AUTO "); |
| 348 | #endif |
| 349 | #ifdef HAVE_ATF_DONTPUB |
| 350 | if (arp_flags & ATF_DONTPUB) |
| 351 | printf("DONTPUB "); |
| 352 | #endif |
| 353 | if (arp_flags & ATF_USETRAILERS) |
| 354 | printf("TRAIL "); |
| 355 | |
| 356 | printf("on %s\n", dev); |
| 357 | } |
| 358 | |
| 359 | /* Display the contents of the ARP cache in the kernel. */ |
| 360 | /* Called only from main, once */ |
| 361 | static int arp_show(char *name) |
| 362 | { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 363 | const char *host; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 364 | const char *hostname; |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 365 | FILE *fp; |
| 366 | struct sockaddr sa; |
| 367 | int type, flags; |
Bernhard Reutner-Fischer | 42646c5 | 2007-01-07 22:12:35 +0000 | [diff] [blame] | 368 | int num; |
| 369 | unsigned entries = 0, shown = 0; |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 370 | char ip[128]; |
| 371 | char hwa[128]; |
| 372 | char mask[128]; |
| 373 | char line[128]; |
| 374 | char dev[128]; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 375 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 376 | host = NULL; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 377 | if (name != NULL) { |
| 378 | /* Resolve the host name. */ |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 379 | if (ap->input(name, &sa) < 0) { |
| 380 | bb_herror_msg_and_die("%s", name); |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 381 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 382 | host = xstrdup(ap->sprint(&sa, 1)); |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 383 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 384 | fp = xfopen("/proc/net/arp", "r"); |
| 385 | /* Bypass header -- read one line */ |
| 386 | fgets(line, sizeof(line), fp); |
| 387 | |
| 388 | /* Read the ARP cache entries. */ |
| 389 | while (fgets(line, sizeof(line), fp)) { |
| 390 | |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 391 | mask[0] = '-'; mask[1] = '\0'; |
| 392 | dev[0] = '-'; dev[1] = '\0'; |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 393 | /* All these strings can't overflow |
| 394 | * because fgets above reads limited amount of data */ |
| 395 | num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n", |
| 396 | ip, &type, &flags, hwa, mask, dev); |
| 397 | if (num < 4) |
| 398 | break; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 399 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 400 | entries++; |
| 401 | /* if the user specified hw-type differs, skip it */ |
| 402 | if (hw_set && (type != hw->type)) |
| 403 | continue; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 404 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 405 | /* if the user specified address differs, skip it */ |
| 406 | if (host && strcmp(ip, host) != 0) |
| 407 | continue; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 409 | /* if the user specified device differs, skip it */ |
| 410 | if (device[0] && strcmp(dev, device) != 0) |
| 411 | continue; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 412 | |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 413 | shown++; |
| 414 | /* This IS ugly but it works -be */ |
| 415 | hostname = "?"; |
| 416 | if (!(option_mask32 & ARP_OPT_n)) { |
| 417 | if (ap->input(ip, &sa) < 0) |
| 418 | hostname = ip; |
| 419 | else |
| 420 | hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000); |
| 421 | if (strcmp(hostname, ip) == 0) |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 422 | hostname = "?"; |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 423 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 424 | |
| 425 | arp_disp(hostname, ip, type, flags, hwa, mask, dev); |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 426 | } |
| 427 | if (option_mask32 & ARP_OPT_v) |
| 428 | printf("Entries: %d\tSkipped: %d\tFound: %d\n", |
| 429 | entries, entries - shown, shown); |
| 430 | |
| 431 | if (!shown) { |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 432 | if (hw_set || host || device[0]) |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 433 | printf("No match found in %d entries\n", entries); |
| 434 | } |
Denis Vlasenko | 7f2527e | 2007-03-14 22:11:20 +0000 | [diff] [blame] | 435 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 436 | free((char*)host); |
| 437 | fclose(fp); |
| 438 | } |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 442 | int arp_main(int argc, char **argv); |
Denis Vlasenko | 88e2b1c | 2007-01-07 19:35:11 +0000 | [diff] [blame] | 443 | int arp_main(int argc, char **argv) |
| 444 | { |
| 445 | char *hw_type; |
| 446 | char *protocol; |
| 447 | |
| 448 | /* Initialize variables... */ |
| 449 | ap = get_aftype(DFLT_AF); |
| 450 | if (!ap) |
| 451 | bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family"); |
| 452 | |
| 453 | getopt32(argc, argv, "A:p:H:t:i:adnDsv", &protocol, &protocol, |
| 454 | &hw_type, &hw_type, &device); |
| 455 | argv += optind; |
| 456 | if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) { |
| 457 | ap = get_aftype(protocol); |
| 458 | if (ap == NULL) |
| 459 | bb_error_msg_and_die("%s: unknown %s", protocol, "address family"); |
| 460 | } |
| 461 | if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) { |
| 462 | hw = get_hwtype(hw_type); |
| 463 | if (hw == NULL) |
| 464 | bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type"); |
| 465 | hw_set = 1; |
| 466 | } |
| 467 | //if (option_mask32 & ARP_OPT_i)... -i |
| 468 | |
| 469 | if (ap->af != AF_INET) { |
| 470 | bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name); |
| 471 | } |
| 472 | |
| 473 | /* If no hw type specified get default */ |
| 474 | if (!hw) { |
| 475 | hw = get_hwtype(DFLT_HW); |
| 476 | if (!hw) |
| 477 | bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type"); |
| 478 | } |
| 479 | |
| 480 | if (hw->alen <= 0) { |
| 481 | bb_error_msg_and_die("%s: %s without ARP support", |
| 482 | hw->name, "hardware type"); |
| 483 | } |
| 484 | sockfd = xsocket(AF_INET, SOCK_DGRAM, 0); |
| 485 | |
| 486 | /* Now see what we have to do here... */ |
| 487 | if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) { |
| 488 | if (argv[0] == NULL) |
| 489 | bb_error_msg_and_die("need host name"); |
| 490 | if (option_mask32 & ARP_OPT_s) |
| 491 | return arp_set(argv); |
| 492 | return arp_del(argv); |
| 493 | } |
| 494 | //if (option_mask32 & ARP_OPT_a) - default |
| 495 | return arp_show(argv[0]); |
| 496 | } |