arp.c revision 73135
1/* 2 * Copyright (c) 1984, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Sun Microsystems, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static char const copyright[] = 39"@(#) Copyright (c) 1984, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94"; 46#endif 47static const char rcsid[] = 48 "$FreeBSD: head/usr.sbin/arp/arp.c 73135 2001-02-27 09:02:10Z ru $"; 49#endif /* not lint */ 50 51/* 52 * arp - display, set, and delete arp table entries 53 */ 54 55 56#include <sys/param.h> 57#include <sys/file.h> 58#include <sys/socket.h> 59#include <sys/sockio.h> 60#include <sys/sysctl.h> 61#include <sys/ioctl.h> 62#include <sys/time.h> 63 64#include <net/if.h> 65#include <net/if_dl.h> 66#include <net/if_types.h> 67#include <net/route.h> 68 69#include <netinet/in.h> 70#include <netinet/if_ether.h> 71 72#include <arpa/inet.h> 73 74#include <err.h> 75#include <errno.h> 76#include <netdb.h> 77#include <nlist.h> 78#include <paths.h> 79#include <stdio.h> 80#include <stdlib.h> 81#include <strings.h> 82#include <unistd.h> 83 84void search(u_long addr, void (*action)(struct sockaddr_dl *sdl, 85 struct sockaddr_inarp *sin, struct rt_msghdr *rtm)); 86void print_entry(struct sockaddr_dl *sdl, 87 struct sockaddr_inarp *sin, struct rt_msghdr *rtm); 88void nuke_entry(struct sockaddr_dl *sdl, 89 struct sockaddr_inarp *sin, struct rt_msghdr *rtm); 90int delete(char *host, char *info); 91void ether_print(u_char *cp); 92void usage(void); 93int set(int argc, char **argv); 94int get(char *host); 95int file(char *name); 96void getsocket(void); 97int my_ether_aton(char *a, u_char *n); 98int rtmsg(int cmd); 99int get_ether_addr(u_int32_t ipaddr, u_char *hwaddr); 100 101static int pid; 102static int nflag; /* no reverse dns lookups */ 103static int aflag; /* do it for all entries */ 104static int s = -1; 105 106/* which function we're supposed to do */ 107#define F_GET 1 108#define F_SET 2 109#define F_FILESET 3 110#define F_REPLACE 4 111#define F_DELETE 5 112 113#define ROUNDUP(a) \ 114 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 115#define SETFUNC(f) { if (func) usage(); func = (f); } 116 117int 118main(argc, argv) 119 int argc; 120 char **argv; 121{ 122 int ch, func = 0; 123 int rtn = 0; 124 125 pid = getpid(); 126 while ((ch = getopt(argc, argv, "andfsS")) != -1) 127 switch((char)ch) { 128 case 'a': 129 aflag = 1; 130 break; 131 case 'd': 132 SETFUNC(F_DELETE); 133 break; 134 case 'n': 135 nflag = 1; 136 break; 137 case 'S': 138 SETFUNC(F_REPLACE); 139 break; 140 case 's': 141 SETFUNC(F_SET); 142 break; 143 case 'f' : 144 SETFUNC(F_FILESET); 145 break; 146 case '?': 147 default: 148 usage(); 149 } 150 argc -= optind; 151 argv += optind; 152 153 if (!func) 154 func = F_GET; 155 switch (func) { 156 case F_GET: 157 if (aflag) { 158 if (argc != 0) 159 usage(); 160 search(0, print_entry); 161 } else { 162 if (argc != 1) 163 usage(); 164 get(argv[0]); 165 } 166 break; 167 case F_SET: 168 case F_REPLACE: 169 if (argc < 2 || argc > 5) 170 usage(); 171 if (func == F_REPLACE) 172 (void) delete(argv[0], NULL); 173 rtn = set(argc, argv) ? 1 : 0; 174 break; 175 case F_DELETE: 176 if (aflag) { 177 if (argc != 0) 178 usage(); 179 search(0, nuke_entry); 180 } else { 181 if (argc < 1 || argc > 2) 182 usage(); 183 rtn = delete(argv[0], argv[1]); 184 } 185 break; 186 case F_FILESET: 187 if (argc != 1) 188 usage(); 189 rtn = file(argv[0]); 190 break; 191 } 192 193 return(rtn); 194} 195 196/* 197 * Process a file to set standard arp entries 198 */ 199int 200file(char *name) 201{ 202 FILE *fp; 203 int i, retval; 204 char line[100], arg[5][50], *args[5]; 205 206 if ((fp = fopen(name, "r")) == NULL) 207 errx(1, "cannot open %s", name); 208 args[0] = &arg[0][0]; 209 args[1] = &arg[1][0]; 210 args[2] = &arg[2][0]; 211 args[3] = &arg[3][0]; 212 args[4] = &arg[4][0]; 213 retval = 0; 214 while(fgets(line, 100, fp) != NULL) { 215 i = sscanf(line, "%49s %49s %49s %49s %49s", arg[0], arg[1], 216 arg[2], arg[3], arg[4]); 217 if (i < 2) { 218 warnx("bad line: %s", line); 219 retval = 1; 220 continue; 221 } 222 if (set(i, args)) 223 retval = 1; 224 } 225 fclose(fp); 226 return (retval); 227} 228 229void 230getsocket(void) 231{ 232 if (s < 0) { 233 s = socket(PF_ROUTE, SOCK_RAW, 0); 234 if (s < 0) 235 err(1, "socket"); 236 } 237} 238 239struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}}; 240struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m; 241struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 242int expire_time, flags, export_only, doing_proxy, found_entry; 243struct { 244 struct rt_msghdr m_rtm; 245 char m_space[512]; 246} m_rtmsg; 247 248/* 249 * Set an individual arp entry 250 */ 251int 252set(int argc, char **argv) 253{ 254 struct hostent *hp; 255 register struct sockaddr_inarp *sin = &sin_m; 256 register struct sockaddr_dl *sdl; 257 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 258 u_char *ea; 259 char *host = argv[0], *eaddr = argv[1]; 260 261 getsocket(); 262 argc -= 2; 263 argv += 2; 264 sdl_m = blank_sdl; 265 sin_m = blank_sin; 266 sin->sin_addr.s_addr = inet_addr(host); 267 if (sin->sin_addr.s_addr == -1) { 268 if (!(hp = gethostbyname(host))) { 269 warnx("%s: %s", host, hstrerror(h_errno)); 270 return (1); 271 } 272 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, 273 sizeof sin->sin_addr); 274 } 275 doing_proxy = flags = export_only = expire_time = 0; 276 while (argc-- > 0) { 277 if (strncmp(argv[0], "temp", 4) == 0) { 278 struct timeval time; 279 gettimeofday(&time, 0); 280 expire_time = time.tv_sec + 20 * 60; 281 } 282 else if (strncmp(argv[0], "pub", 3) == 0) { 283 flags |= RTF_ANNOUNCE; 284 doing_proxy = SIN_PROXY; 285 } else if (strncmp(argv[0], "trail", 5) == 0) { 286 printf("%s: Sending trailers is no longer supported\n", 287 host); 288 } 289 argv++; 290 } 291 ea = (u_char *)LLADDR(&sdl_m); 292 if (doing_proxy && !strcmp(eaddr, "auto")) { 293 if (!get_ether_addr(sin->sin_addr.s_addr, ea)) { 294 printf("no interface found for %s\n", 295 inet_ntoa(sin->sin_addr)); 296 return (1); 297 } 298 sdl_m.sdl_alen = 6; 299 } else { 300 if (my_ether_aton(eaddr, ea) == 0) 301 sdl_m.sdl_alen = 6; 302 } 303tryagain: 304 if (rtmsg(RTM_GET) < 0) { 305 warn("%s", host); 306 return (1); 307 } 308 sin = (struct sockaddr_inarp *)(rtm + 1); 309 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin); 310 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 311 if (sdl->sdl_family == AF_LINK && 312 (rtm->rtm_flags & RTF_LLINFO) && 313 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 314 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 315 case IFT_ISO88024: case IFT_ISO88025: 316 goto overwrite; 317 } 318 if (doing_proxy == 0) { 319 printf("set: can only proxy for %s\n", host); 320 return (1); 321 } 322 if (sin_m.sin_other & SIN_PROXY) { 323 printf("set: proxy entry exists for non 802 device\n"); 324 return(1); 325 } 326 sin_m.sin_other = SIN_PROXY; 327 export_only = 1; 328 goto tryagain; 329 } 330overwrite: 331 if (sdl->sdl_family != AF_LINK) { 332 printf("cannot intuit interface index and type for %s\n", host); 333 return (1); 334 } 335 sdl_m.sdl_type = sdl->sdl_type; 336 sdl_m.sdl_index = sdl->sdl_index; 337 return (rtmsg(RTM_ADD)); 338} 339 340/* 341 * Display an individual arp entry 342 */ 343int 344get(char *host) 345{ 346 struct hostent *hp; 347 struct sockaddr_inarp *sin = &sin_m; 348 349 sin_m = blank_sin; 350 sin->sin_addr.s_addr = inet_addr(host); 351 if (sin->sin_addr.s_addr == -1) { 352 if (!(hp = gethostbyname(host))) 353 errx(1, "%s: %s", host, hstrerror(h_errno)); 354 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, 355 sizeof sin->sin_addr); 356 } 357 search(sin->sin_addr.s_addr, print_entry); 358 if (found_entry == 0) { 359 printf("%s (%s) -- no entry\n", 360 host, inet_ntoa(sin->sin_addr)); 361 return(1); 362 } 363 return(0); 364} 365 366/* 367 * Delete an arp entry 368 */ 369int 370delete(char *host, char *info) 371{ 372 struct hostent *hp; 373 register struct sockaddr_inarp *sin = &sin_m; 374 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 375 struct sockaddr_dl *sdl; 376 377 getsocket(); 378 sin_m = blank_sin; 379 if (info && strncmp(info, "pro", 3) == 0) 380 sin_m.sin_other = SIN_PROXY; 381 sin->sin_addr.s_addr = inet_addr(host); 382 if (sin->sin_addr.s_addr == -1) { 383 if (!(hp = gethostbyname(host))) { 384 warnx("%s: %s", host, hstrerror(h_errno)); 385 return (1); 386 } 387 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, 388 sizeof sin->sin_addr); 389 } 390tryagain: 391 if (rtmsg(RTM_GET) < 0) { 392 warn("%s", host); 393 return (1); 394 } 395 sin = (struct sockaddr_inarp *)(rtm + 1); 396 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin); 397 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 398 if (sdl->sdl_family == AF_LINK && 399 (rtm->rtm_flags & RTF_LLINFO) && 400 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 401 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 402 case IFT_ISO88024: case IFT_ISO88025: 403 goto delete; 404 } 405 } 406 if (sin_m.sin_other & SIN_PROXY) { 407 fprintf(stderr, "delete: can't locate %s\n",host); 408 return (1); 409 } else { 410 sin_m.sin_other = SIN_PROXY; 411 goto tryagain; 412 } 413delete: 414 if (sdl->sdl_family != AF_LINK) { 415 printf("cannot locate %s\n", host); 416 return (1); 417 } 418 if (rtmsg(RTM_DELETE) == 0) { 419 printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr)); 420 return (0); 421 } 422 return (1); 423} 424 425/* 426 * Search the arp table and do some action on matching entries 427 */ 428void 429search(u_long addr, void (*action)(struct sockaddr_dl *sdl, 430 struct sockaddr_inarp *sin, struct rt_msghdr *rtm)) 431{ 432 int mib[6]; 433 size_t needed; 434 char *lim, *buf, *next; 435 struct rt_msghdr *rtm; 436 struct sockaddr_inarp *sin; 437 struct sockaddr_dl *sdl; 438 extern int h_errno; 439 440 mib[0] = CTL_NET; 441 mib[1] = PF_ROUTE; 442 mib[2] = 0; 443 mib[3] = AF_INET; 444 mib[4] = NET_RT_FLAGS; 445 mib[5] = RTF_LLINFO; 446 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 447 errx(1, "route-sysctl-estimate"); 448 if ((buf = malloc(needed)) == NULL) 449 errx(1, "malloc"); 450 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 451 errx(1, "actual retrieval of routing table"); 452 lim = buf + needed; 453 for (next = buf; next < lim; next += rtm->rtm_msglen) { 454 rtm = (struct rt_msghdr *)next; 455 sin = (struct sockaddr_inarp *)(rtm + 1); 456 (char *)sdl = (char *)sin + ROUNDUP(sin->sin_len); 457 if (addr) { 458 if (addr != sin->sin_addr.s_addr) 459 continue; 460 found_entry = 1; 461 } 462 (*action)(sdl, sin, rtm); 463 } 464} 465 466/* 467 * Display an arp entry 468 */ 469void 470print_entry(struct sockaddr_dl *sdl, 471 struct sockaddr_inarp *sin, struct rt_msghdr *rtm) 472{ 473 char *host; 474 extern int h_errno; 475 struct hostent *hp; 476 int seg; 477 478 if (nflag == 0) 479 hp = gethostbyaddr((caddr_t)&(sin->sin_addr), 480 sizeof sin->sin_addr, AF_INET); 481 else 482 hp = 0; 483 if (hp) 484 host = hp->h_name; 485 else { 486 host = "?"; 487 if (h_errno == TRY_AGAIN) 488 nflag = 1; 489 } 490 printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr)); 491 if (sdl->sdl_alen) 492 ether_print(LLADDR(sdl)); 493 else 494 printf("(incomplete)"); 495 if (rtm->rtm_rmx.rmx_expire == 0) 496 printf(" permanent"); 497 if (sin->sin_other & SIN_PROXY) 498 printf(" published (proxy only)"); 499 if (rtm->rtm_addrs & RTA_NETMASK) { 500 sin = (struct sockaddr_inarp *) 501 (ROUNDUP(sdl->sdl_len) + (char *)sdl); 502 if (sin->sin_addr.s_addr == 0xffffffff) 503 printf(" published"); 504 if (sin->sin_len != 8) 505 printf("(weird)"); 506 } 507 switch(sdl->sdl_type) { 508 case IFT_ETHER: 509 printf(" [ethernet]"); 510 break; 511 case IFT_ISO88025: 512 printf(" [token-ring]"); 513 break; 514 default: 515 } 516 if (sdl->sdl_rcf != NULL) { 517 printf(" rt=%x", ntohs(sdl->sdl_rcf)); 518 for (seg = 0; seg < ((((ntohs(sdl->sdl_rcf) & 0x1f00) >> 8) - 2 ) / 2); seg++) 519 printf(":%x", ntohs(sdl->sdl_route[seg])); 520 } 521 522 printf("\n"); 523 524} 525 526/* 527 * Nuke an arp entry 528 */ 529void 530nuke_entry(struct sockaddr_dl *sdl, 531 struct sockaddr_inarp *sin, struct rt_msghdr *rtm) 532{ 533 char ip[20]; 534 535 snprintf(ip, sizeof(ip), "%s", inet_ntoa(sin->sin_addr)); 536 delete(ip, NULL); 537} 538 539void 540ether_print(u_char *cp) 541{ 542 printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 543} 544 545int 546my_ether_aton(char *a, u_char *n) 547{ 548 int i, o[6]; 549 550 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 551 &o[3], &o[4], &o[5]); 552 if (i != 6) { 553 warnx("invalid Ethernet address '%s'", a); 554 return (1); 555 } 556 for (i=0; i<6; i++) 557 n[i] = o[i]; 558 return (0); 559} 560 561void 562usage(void) 563{ 564 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 565 "usage: arp [-n] hostname", 566 " arp [-n] -a", 567 " arp -d hostname [proxy]", 568 " arp -d -a", 569 " arp -s hostname ether_addr [temp] [pub]", 570 " arp -S hostname ether_addr [temp] [pub]", 571 " arp -f filename"); 572 exit(1); 573} 574 575int 576rtmsg(int cmd) 577{ 578 static int seq; 579 int rlen; 580 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 581 register char *cp = m_rtmsg.m_space; 582 register int l; 583 584 errno = 0; 585 if (cmd == RTM_DELETE) 586 goto doit; 587 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 588 rtm->rtm_flags = flags; 589 rtm->rtm_version = RTM_VERSION; 590 591 switch (cmd) { 592 default: 593 errx(1, "internal wrong cmd"); 594 case RTM_ADD: 595 rtm->rtm_addrs |= RTA_GATEWAY; 596 rtm->rtm_rmx.rmx_expire = expire_time; 597 rtm->rtm_inits = RTV_EXPIRE; 598 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 599 sin_m.sin_other = 0; 600 if (doing_proxy) { 601 if (export_only) 602 sin_m.sin_other = SIN_PROXY; 603 else { 604 rtm->rtm_addrs |= RTA_NETMASK; 605 rtm->rtm_flags &= ~RTF_HOST; 606 } 607 } 608 /* FALLTHROUGH */ 609 case RTM_GET: 610 rtm->rtm_addrs |= RTA_DST; 611 } 612#define NEXTADDR(w, s) \ 613 if (rtm->rtm_addrs & (w)) { \ 614 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));} 615 616 NEXTADDR(RTA_DST, sin_m); 617 NEXTADDR(RTA_GATEWAY, sdl_m); 618 NEXTADDR(RTA_NETMASK, so_mask); 619 620 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 621doit: 622 l = rtm->rtm_msglen; 623 rtm->rtm_seq = ++seq; 624 rtm->rtm_type = cmd; 625 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 626 if (errno != ESRCH || cmd != RTM_DELETE) { 627 warn("writing to routing socket"); 628 return (-1); 629 } 630 } 631 do { 632 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 633 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 634 if (l < 0) 635 warn("read from routing socket"); 636 return (0); 637} 638 639/* 640 * get_ether_addr - get the hardware address of an interface on the 641 * the same subnet as ipaddr. 642 */ 643#define MAX_IFS 32 644 645int 646get_ether_addr(u_int32_t ipaddr, u_char *hwaddr) 647{ 648 struct ifreq *ifr, *ifend, *ifp; 649 u_int32_t ina, mask; 650 struct sockaddr_dl *dla; 651 struct ifreq ifreq; 652 struct ifconf ifc; 653 struct ifreq ifs[MAX_IFS]; 654 int s; 655 656 s = socket(AF_INET, SOCK_DGRAM, 0); 657 if (s < 0) 658 err(1, "socket"); 659 660 ifc.ifc_len = sizeof(ifs); 661 ifc.ifc_req = ifs; 662 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { 663 warnx("ioctl(SIOCGIFCONF)"); 664 close(s); 665 return 0; 666 } 667 668 /* 669 * Scan through looking for an interface with an Internet 670 * address on the same subnet as `ipaddr'. 671 */ 672 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); 673 for (ifr = ifc.ifc_req; ifr < ifend; ) { 674 if (ifr->ifr_addr.sa_family == AF_INET) { 675 ina = ((struct sockaddr_in *) 676 &ifr->ifr_addr)->sin_addr.s_addr; 677 strncpy(ifreq.ifr_name, ifr->ifr_name, 678 sizeof(ifreq.ifr_name)); 679 /* 680 * Check that the interface is up, 681 * and not point-to-point or loopback. 682 */ 683 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) 684 continue; 685 if ((ifreq.ifr_flags & 686 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 687 IFF_LOOPBACK|IFF_NOARP)) 688 != (IFF_UP|IFF_BROADCAST)) 689 goto nextif; 690 /* 691 * Get its netmask and check that it's on 692 * the right subnet. 693 */ 694 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0) 695 continue; 696 mask = ((struct sockaddr_in *) 697 &ifreq.ifr_addr)->sin_addr.s_addr; 698 if ((ipaddr & mask) != (ina & mask)) 699 goto nextif; 700 break; 701 } 702nextif: 703 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr 704 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr))); 705 } 706 707 if (ifr >= ifend) { 708 close(s); 709 return 0; 710 } 711 712 /* 713 * Now scan through again looking for a link-level address 714 * for this interface. 715 */ 716 ifp = ifr; 717 for (ifr = ifc.ifc_req; ifr < ifend; ) { 718 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 719 && ifr->ifr_addr.sa_family == AF_LINK) { 720 /* 721 * Found the link-level address - copy it out 722 */ 723 dla = (struct sockaddr_dl *) &ifr->ifr_addr; 724 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 725 close (s); 726 printf("using interface %s for proxy with address ", 727 ifp->ifr_name); 728 ether_print(hwaddr); 729 printf("\n"); 730 return dla->sdl_alen; 731 } 732 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr 733 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr))); 734 } 735 return 0; 736} 737