Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/netstat/route.c
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | |||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sysexits.h> | #include <sysexits.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <err.h> | |||||
#include <libxo/xo.h> | #include <libxo/xo.h> | ||||
#include "netstat.h" | #include "netstat.h" | ||||
#include "common.h" | #include "common.h" | ||||
#include "nl_defs.h" | #include "nl_defs.h" | ||||
/* | /* | ||||
* Definitions for showing gateway flags. | * Definitions for showing gateway flags. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | routepr(int fibnum, int af) | ||||
intsize = sizeof(int); | intsize = sizeof(int); | ||||
if (fibnum == -1 && | if (fibnum == -1 && | ||||
sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) | sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) | ||||
fibnum = 0; | fibnum = 0; | ||||
if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) | if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) | ||||
numfibs = 1; | numfibs = 1; | ||||
if (fibnum < 0 || fibnum > numfibs - 1) | if (fibnum < 0 || fibnum > numfibs - 1) | ||||
errx(EX_USAGE, "%d: invalid fib", fibnum); | xo_errx(EX_USAGE, "%d: invalid fib", fibnum); | ||||
/* | /* | ||||
* Since kernel & userland use different timebase | * Since kernel & userland use different timebase | ||||
* (time_uptime vs time_second) and we are reading kernel memory | * (time_uptime vs time_second) and we are reading kernel memory | ||||
* directly we should do rt_expire --> expire_time conversion. | * directly we should do rt_expire --> expire_time conversion. | ||||
*/ | */ | ||||
if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) | if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) | ||||
err(EX_OSERR, "clock_gettime() failed"); | xo_err(EX_OSERR, "clock_gettime() failed"); | ||||
xo_open_container("route-information"); | xo_open_container("route-information"); | ||||
xo_emit("{T:Routing tables}"); | xo_emit("{T:Routing tables}"); | ||||
if (fibnum) | if (fibnum) | ||||
xo_emit(" ({L:fib}: {:fib/%d})", fibnum); | xo_emit(" ({L:fib}: {:fib/%d})", fibnum); | ||||
xo_emit("\n"); | xo_emit("\n"); | ||||
#ifdef WITHOUT_NETLINK | #ifdef WITHOUT_NETLINK | ||||
p_rtable_sysctl(fibnum, af); | p_rtable_sysctl(fibnum, af); | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | p_rtable_sysctl(int fibnum, int af) | ||||
mib[0] = CTL_NET; | mib[0] = CTL_NET; | ||||
mib[1] = PF_ROUTE; | mib[1] = PF_ROUTE; | ||||
mib[2] = 0; | mib[2] = 0; | ||||
mib[3] = af; | mib[3] = af; | ||||
mib[4] = NET_RT_DUMP; | mib[4] = NET_RT_DUMP; | ||||
mib[5] = 0; | mib[5] = 0; | ||||
mib[6] = fibnum; | mib[6] = fibnum; | ||||
if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) | if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) | ||||
err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af, | xo_err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af, | ||||
fibnum); | fibnum); | ||||
if ((buf = malloc(needed)) == NULL) | if ((buf = malloc(needed)) == NULL) | ||||
errx(2, "malloc(%lu)", (unsigned long)needed); | xo_errx(EX_OSERR, "malloc(%lu)", (unsigned long)needed); | ||||
if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) | if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) | ||||
err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum); | xo_err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d", af, fibnum); | ||||
lim = buf + needed; | lim = buf + needed; | ||||
xo_open_container("route-table"); | xo_open_container("route-table"); | ||||
xo_open_list("rt-family"); | xo_open_list("rt-family"); | ||||
for (next = buf; next < lim; next += rtm->rtm_msglen) { | for (next = buf; next < lim; next += rtm->rtm_msglen) { | ||||
rtm = (struct rt_msghdr *)next; | rtm = (struct rt_msghdr *)next; | ||||
if (rtm->rtm_version != RTM_VERSION) | if (rtm->rtm_version != RTM_VERSION) | ||||
continue; | continue; | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 451 Lines • Show Last 20 Lines |