arp.c (596e374dac03cbfa29cc70e81d77bde8c888d0e4) arp.c (66658902ed53889b4a9ca81dc5b14399052c8ab9)
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

--- 442 unchanged lines hidden (view full) ---

451/*
452 * Search the arp table and do some action on matching entries
453 */
454static int
455search(u_long addr, action_fn *action)
456{
457 int mib[6];
458 size_t needed;
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

--- 442 unchanged lines hidden (view full) ---

451/*
452 * Search the arp table and do some action on matching entries
453 */
454static int
455search(u_long addr, action_fn *action)
456{
457 int mib[6];
458 size_t needed;
459 char *lim, *buf, *next;
459 char *lim, *buf, *newbuf, *next;
460 struct rt_msghdr *rtm;
461 struct sockaddr_inarp *sin2;
462 struct sockaddr_dl *sdl;
463 char ifname[IF_NAMESIZE];
460 struct rt_msghdr *rtm;
461 struct sockaddr_inarp *sin2;
462 struct sockaddr_dl *sdl;
463 char ifname[IF_NAMESIZE];
464 int found_entry = 0;
464 int st, found_entry = 0;
465
466 mib[0] = CTL_NET;
467 mib[1] = PF_ROUTE;
468 mib[2] = 0;
469 mib[3] = AF_INET;
470 mib[4] = NET_RT_FLAGS;
471 mib[5] = RTF_LLINFO;
472 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
473 err(1, "route-sysctl-estimate");
474 if (needed == 0) /* empty table */
475 return 0;
465
466 mib[0] = CTL_NET;
467 mib[1] = PF_ROUTE;
468 mib[2] = 0;
469 mib[3] = AF_INET;
470 mib[4] = NET_RT_FLAGS;
471 mib[5] = RTF_LLINFO;
472 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
473 err(1, "route-sysctl-estimate");
474 if (needed == 0) /* empty table */
475 return 0;
476 if ((buf = malloc(needed)) == NULL)
477 err(1, "malloc");
478 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
476 buf = NULL;
477 do {
478 needed += needed / 2;
479 newbuf = realloc(buf, needed);
480 if (newbuf == NULL) {
481 if (buf != NULL)
482 free(buf);
483 errx(1, "could not reallocate memory");
484 }
485 buf = newbuf;
486 st = sysctl(mib, 6, buf, &needed, NULL, 0);
487 } while (st == -1 && errno == ENOMEM);
488 if (st == -1)
479 err(1, "actual retrieval of routing table");
480 lim = buf + needed;
481 for (next = buf; next < lim; next += rtm->rtm_msglen) {
482 rtm = (struct rt_msghdr *)next;
483 sin2 = (struct sockaddr_inarp *)(rtm + 1);
484 sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
485 if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
486 strcmp(ifname, rifname))

--- 309 unchanged lines hidden ---
489 err(1, "actual retrieval of routing table");
490 lim = buf + needed;
491 for (next = buf; next < lim; next += rtm->rtm_msglen) {
492 rtm = (struct rt_msghdr *)next;
493 sin2 = (struct sockaddr_inarp *)(rtm + 1);
494 sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
495 if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
496 strcmp(ifname, rifname))

--- 309 unchanged lines hidden ---