Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/dumpon/dumpon.c
Show First 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | |||||
static int verbose; | static int verbose; | ||||
static void _Noreturn | static void _Noreturn | ||||
usage(void) | usage(void) | ||||
{ | { | ||||
fprintf(stderr, | fprintf(stderr, | ||||
"usage: dumpon [-v] [-k <pubkey>] [-Zz] <device>\n" | "usage: dumpon [-v] [-k <pubkey>] [-Zz] <device>\n" | ||||
" dumpon [-v] [-k <pubkey>] [-Zz]\n" | " dumpon [-v] [-k <pubkey>] [-Zz]\n" | ||||
" [-g <gateway>|default] -s <server> -c <client> <iface>\n" | " [-g <gateway>] -s <server> -c <client> <iface>\n" | ||||
" dumpon [-v] off\n" | " dumpon [-v] off\n" | ||||
" dumpon [-v] -l\n"); | " dumpon [-v] -l\n"); | ||||
exit(EX_USAGE); | exit(EX_USAGE); | ||||
} | } | ||||
/* | /* | ||||
* Look for a default route on the specified interface. | * Look for a default route on the specified interface. | ||||
*/ | */ | ||||
static char * | static char * | ||||
find_gateway(const char *ifname) | find_gateway(const char *ifname) | ||||
{ | { | ||||
struct ifaddrs *ifa, *ifap; | struct ifaddrs *ifa, *ifap; | ||||
struct rt_msghdr *rtm; | struct rt_msghdr *rtm; | ||||
struct sockaddr *sa; | struct sockaddr *sa; | ||||
struct sockaddr_dl *sdl; | struct sockaddr_dl *sdl; | ||||
struct sockaddr_in *dst, *mask, *gw; | struct sockaddr_in *dst, *mask, *gw; | ||||
char *buf, *next, *ret; | char *buf, *next, *ret; | ||||
size_t sz; | size_t sz; | ||||
int error, i, ifindex, mib[7]; | int error, i, ifindex, mib[7]; | ||||
ret = NULL; | |||||
/* First look up the interface index. */ | /* First look up the interface index. */ | ||||
if (getifaddrs(&ifap) != 0) | if (getifaddrs(&ifap) != 0) | ||||
err(EX_OSERR, "getifaddrs"); | err(EX_OSERR, "getifaddrs"); | ||||
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { | for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { | ||||
if (ifa->ifa_addr->sa_family != AF_LINK) | if (ifa->ifa_addr->sa_family != AF_LINK) | ||||
continue; | continue; | ||||
if (strcmp(ifa->ifa_name, ifname) == 0) { | if (strcmp(ifa->ifa_name, ifname) == 0) { | ||||
sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr; | sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr; | ||||
Show All 21 Lines | for (;;) { | ||||
error = sysctl(mib, nitems(mib), buf, &sz, NULL, 0); | error = sysctl(mib, nitems(mib), buf, &sz, NULL, 0); | ||||
if (error == 0) | if (error == 0) | ||||
break; | break; | ||||
if (errno != ENOMEM) | if (errno != ENOMEM) | ||||
err(EX_OSERR, "sysctl(NET_RT_DUMP)"); | err(EX_OSERR, "sysctl(NET_RT_DUMP)"); | ||||
free(buf); | free(buf); | ||||
} | } | ||||
ret = NULL; | |||||
for (next = buf; next < buf + sz; next += rtm->rtm_msglen) { | for (next = buf; next < buf + sz; next += rtm->rtm_msglen) { | ||||
rtm = (struct rt_msghdr *)(void *)next; | rtm = (struct rt_msghdr *)(void *)next; | ||||
if (rtm->rtm_version != RTM_VERSION) | if (rtm->rtm_version != RTM_VERSION) | ||||
continue; | continue; | ||||
if ((rtm->rtm_flags & RTF_GATEWAY) == 0 || | if ((rtm->rtm_flags & RTF_GATEWAY) == 0 || | ||||
rtm->rtm_index != ifindex) | rtm->rtm_index != ifindex) | ||||
continue; | continue; | ||||
▲ Show 20 Lines • Show All 312 Lines • ▼ Show 20 Lines | if (netdump) { | ||||
if (strlcpy(ndconf.ndc_iface, argv[0], | if (strlcpy(ndconf.ndc_iface, argv[0], | ||||
sizeof(ndconf.ndc_iface)) >= sizeof(ndconf.ndc_iface)) | sizeof(ndconf.ndc_iface)) >= sizeof(ndconf.ndc_iface)) | ||||
errx(EX_USAGE, "invalid interface name '%s'", argv[0]); | errx(EX_USAGE, "invalid interface name '%s'", argv[0]); | ||||
if (inet_aton(server, &ndconf.ndc_server) == 0) | if (inet_aton(server, &ndconf.ndc_server) == 0) | ||||
errx(EX_USAGE, "invalid server address '%s'", server); | errx(EX_USAGE, "invalid server address '%s'", server); | ||||
if (inet_aton(client, &ndconf.ndc_client) == 0) | if (inet_aton(client, &ndconf.ndc_client) == 0) | ||||
errx(EX_USAGE, "invalid client address '%s'", client); | errx(EX_USAGE, "invalid client address '%s'", client); | ||||
if (gateway == NULL) | gateway = find_gateway(argv[0]); | ||||
if (gateway == NULL) { | |||||
if (verbose) | |||||
printf("failed to look up gateway for %s\n", | |||||
server); | |||||
gateway = server; | gateway = server; | ||||
else if (strcmp(gateway, "default") == 0 && | } | ||||
(gateway = find_gateway(argv[0])) == NULL) | |||||
errx(EX_NOHOST, | |||||
"failed to look up next-hop router for %s", server); | |||||
if (inet_aton(gateway, &ndconf.ndc_gateway) == 0) | if (inet_aton(gateway, &ndconf.ndc_gateway) == 0) | ||||
errx(EX_USAGE, "invalid gateway address '%s'", gateway); | errx(EX_USAGE, "invalid gateway address '%s'", gateway); | ||||
#ifdef HAVE_CRYPTO | #ifdef HAVE_CRYPTO | ||||
if (pubkeyfile != NULL) | if (pubkeyfile != NULL) | ||||
genkey(pubkeyfile, kdap); | genkey(pubkeyfile, kdap); | ||||
#endif | #endif | ||||
error = ioctl(fd, NETDUMPSCONF, &ndconf); | error = ioctl(fd, NETDUMPSCONF, &ndconf); | ||||
Show All 28 Lines |