Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/netstat/route.c
Show First 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | |||||
static struct timespec uptime; | static struct timespec uptime; | ||||
static const char *netname4(in_addr_t, in_addr_t); | static const char *netname4(in_addr_t, in_addr_t); | ||||
#ifdef INET6 | #ifdef INET6 | ||||
static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *); | static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *); | ||||
#endif | #endif | ||||
static void p_rtable_sysctl(int, int); | static void p_rtable_sysctl(int, int); | ||||
static void p_rtentry_sysctl(const char *name, struct rt_msghdr *); | static void p_rtentry_sysctl(const char *name, struct rt_msghdr *); | ||||
static void p_flags(int, const char *); | |||||
static void domask(char *, size_t, u_long); | static void domask(char *, size_t, u_long); | ||||
/* | /* | ||||
* Print routing tables. | * Print routing tables. | ||||
*/ | */ | ||||
void | void | ||||
routepr(int fibnum, int af) | routepr(int fibnum, int af) | ||||
Show All 20 Lines | routepr(int fibnum, int af) | ||||
if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) | if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) | ||||
err(EX_OSERR, "clock_gettime() failed"); | 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"); | ||||
if (!p_rtable_netlink(fibnum, af)) | |||||
p_rtable_sysctl(fibnum, af); | p_rtable_sysctl(fibnum, af); | ||||
xo_close_container("route-information"); | xo_close_container("route-information"); | ||||
} | } | ||||
/* | /* | ||||
* Print address family header before a section of the routing table. | * Print address family header before a section of the routing table. | ||||
*/ | */ | ||||
void | void | ||||
Show All 37 Lines | |||||
#else | #else | ||||
#define WID_DST_DEFAULT(af) \ | #define WID_DST_DEFAULT(af) \ | ||||
((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18) | ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18) | ||||
#define WID_GW_DEFAULT(af) \ | #define WID_GW_DEFAULT(af) \ | ||||
((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18) | ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18) | ||||
#define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8)) | #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 10 : 8)) | ||||
#endif /*INET6*/ | #endif /*INET6*/ | ||||
static int wid_dst; | struct _wid wid; | ||||
static int wid_gw; | |||||
static int wid_flags; | |||||
static int wid_pksent; | |||||
static int wid_mtu; | |||||
static int wid_if; | |||||
static int wid_expire; | |||||
/* | /* | ||||
* Print header for routing table columns. | * Print header for routing table columns. | ||||
*/ | */ | ||||
static void | void | ||||
pr_rthdr(int af1 __unused) | pr_rthdr(int af1 __unused) | ||||
{ | { | ||||
if (Wflag) { | if (Wflag) { | ||||
xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " | xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " | ||||
"{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n", | "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n", | ||||
wid_dst, wid_dst, "Destination", | wid.dst, wid.dst, "Destination", | ||||
wid_gw, wid_gw, "Gateway", | wid.gw, wid.gw, "Gateway", | ||||
wid_flags, wid_flags, "Flags", | wid.flags, wid.flags, "Flags", | ||||
wid_mtu, wid_mtu, "Nhop#", | wid.mtu, wid.mtu, "Nhop#", | ||||
wid_mtu, wid_mtu, "Mtu", | wid.mtu, wid.mtu, "Mtu", | ||||
wid_if, wid_if, "Netif", | wid.iface, wid.iface, "Netif", | ||||
wid_expire, "Expire"); | wid.expire, "Expire"); | ||||
} else { | } else { | ||||
xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " | xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " | ||||
"{T:/%*s}\n", | "{T:/%*s}\n", | ||||
wid_dst, wid_dst, "Destination", | wid.dst, wid.dst, "Destination", | ||||
wid_gw, wid_gw, "Gateway", | wid.gw, wid.gw, "Gateway", | ||||
wid_flags, wid_flags, "Flags", | wid.flags, wid.flags, "Flags", | ||||
wid_if, wid_if, "Netif", | wid.iface, wid.iface, "Netif", | ||||
wid_expire, "Expire"); | wid.expire, "Expire"); | ||||
} | } | ||||
} | } | ||||
void | |||||
set_wid(int fam) | |||||
{ | |||||
wid.dst = WID_DST_DEFAULT(fam); | |||||
wid.gw = WID_GW_DEFAULT(fam); | |||||
wid.flags = 6; | |||||
wid.pksent = 8; | |||||
wid.mtu = 6; | |||||
wid.iface = WID_IF_DEFAULT(fam); | |||||
wid.expire = 6; | |||||
} | |||||
static void | static void | ||||
p_rtable_sysctl(int fibnum, int af) | p_rtable_sysctl(int fibnum, int af) | ||||
{ | { | ||||
size_t needed; | size_t needed; | ||||
int mib[7]; | int mib[7]; | ||||
char *buf, *next, *lim; | char *buf, *next, *lim; | ||||
struct rt_msghdr *rtm; | struct rt_msghdr *rtm; | ||||
struct sockaddr *sa; | struct sockaddr *sa; | ||||
Show All 29 Lines | for (next = buf; next < lim; next += rtm->rtm_msglen) { | ||||
sa = (struct sockaddr *)(rtm + 1); | sa = (struct sockaddr *)(rtm + 1); | ||||
/* Only print family first time. */ | /* Only print family first time. */ | ||||
if (fam != sa->sa_family) { | if (fam != sa->sa_family) { | ||||
if (need_table_close) { | if (need_table_close) { | ||||
xo_close_list("rt-entry"); | xo_close_list("rt-entry"); | ||||
xo_close_instance("rt-family"); | xo_close_instance("rt-family"); | ||||
} | } | ||||
need_table_close = true; | need_table_close = true; | ||||
fam = sa->sa_family; | fam = sa->sa_family; | ||||
wid_dst = WID_DST_DEFAULT(fam); | set_wid(fam); | ||||
wid_gw = WID_GW_DEFAULT(fam); | |||||
wid_flags = 6; | |||||
wid_pksent = 8; | |||||
wid_mtu = 6; | |||||
wid_if = WID_IF_DEFAULT(fam); | |||||
wid_expire = 6; | |||||
xo_open_instance("rt-family"); | xo_open_instance("rt-family"); | ||||
pr_family(fam); | pr_family(fam); | ||||
xo_open_list("rt-entry"); | xo_open_list("rt-entry"); | ||||
pr_rthdr(fam); | pr_rthdr(fam); | ||||
} | } | ||||
p_rtentry_sysctl("rt-entry", rtm); | p_rtentry_sysctl("rt-entry", rtm); | ||||
} | } | ||||
Show All 20 Lines | for (i = 0; i < RTAX_MAX; i++) { | ||||
if (rtm->rtm_addrs & (1 << i)) { | if (rtm->rtm_addrs & (1 << i)) { | ||||
addr[i] = sa; | addr[i] = sa; | ||||
sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa)); | sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa)); | ||||
} | } | ||||
} | } | ||||
protrusion = p_sockaddr("destination", addr[RTAX_DST], | protrusion = p_sockaddr("destination", addr[RTAX_DST], | ||||
addr[RTAX_NETMASK], | addr[RTAX_NETMASK], | ||||
rtm->rtm_flags, wid_dst); | rtm->rtm_flags, wid.dst); | ||||
protrusion = p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST, | protrusion = p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST, | ||||
wid_gw - protrusion); | wid.gw - protrusion); | ||||
snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ", | snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ", | ||||
wid_flags - protrusion); | wid.flags - protrusion); | ||||
p_flags(rtm->rtm_flags, buffer); | p_flags(rtm->rtm_flags, buffer); | ||||
/* Output path weight as non-visual property */ | /* Output path weight as non-visual property */ | ||||
xo_emit("{e:weight/%u}", rtm->rtm_rmx.rmx_weight); | xo_emit("{e:weight/%u}", rtm->rtm_rmx.rmx_weight); | ||||
if (Wflag) { | if (Wflag) { | ||||
/* XXX: use=0? */ | /* XXX: use=0? */ | ||||
xo_emit("{t:nhop/%*lu} ", wid_mtu, rtm->rtm_rmx.rmx_nhidx); | xo_emit("{t:nhop/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_nhidx); | ||||
if (rtm->rtm_rmx.rmx_mtu != 0) | if (rtm->rtm_rmx.rmx_mtu != 0) | ||||
xo_emit("{t:mtu/%*lu} ", wid_mtu, rtm->rtm_rmx.rmx_mtu); | xo_emit("{t:mtu/%*lu} ", wid.mtu, rtm->rtm_rmx.rmx_mtu); | ||||
else | else | ||||
xo_emit("{P:/%*s} ", wid_mtu, ""); | xo_emit("{P:/%*s} ", wid.mtu, ""); | ||||
} | } | ||||
memset(prettyname, 0, sizeof(prettyname)); | memset(prettyname, 0, sizeof(prettyname)); | ||||
if (rtm->rtm_index < ifmap_size) { | if (rtm->rtm_index < ifmap_size) { | ||||
strlcpy(prettyname, ifmap[rtm->rtm_index].ifname, | strlcpy(prettyname, ifmap[rtm->rtm_index].ifname, | ||||
sizeof(prettyname)); | sizeof(prettyname)); | ||||
if (*prettyname == '\0') | if (*prettyname == '\0') | ||||
strlcpy(prettyname, "---", sizeof(prettyname)); | strlcpy(prettyname, "---", sizeof(prettyname)); | ||||
} | } | ||||
if (Wflag) | if (Wflag) | ||||
xo_emit("{t:interface-name/%*s}", wid_if, prettyname); | xo_emit("{t:interface-name/%*s}", wid.iface, prettyname); | ||||
else | else | ||||
xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, | xo_emit("{t:interface-name/%*.*s}", wid.iface, wid.iface, | ||||
prettyname); | prettyname); | ||||
if (rtm->rtm_rmx.rmx_expire) { | if (rtm->rtm_rmx.rmx_expire) { | ||||
time_t expire_time; | time_t expire_time; | ||||
if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0) | if ((expire_time = rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0) | ||||
xo_emit(" {:expire-time/%*d}", wid_expire, | xo_emit(" {:expire-time/%*d}", wid.expire, | ||||
(int)expire_time); | (int)expire_time); | ||||
} | } | ||||
xo_emit("\n"); | xo_emit("\n"); | ||||
xo_close_instance(name); | xo_close_instance(name); | ||||
} | } | ||||
int | int | ||||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | default: | ||||
} | } | ||||
cp = buf; | cp = buf; | ||||
} | } | ||||
} | } | ||||
return (cp); | return (cp); | ||||
} | } | ||||
static void | void | ||||
p_flags(int f, const char *format) | p_flags(int f, const char *format) | ||||
{ | { | ||||
print_flags_generic(f, rt_bits, format, "flags_pretty"); | print_flags_generic(f, rt_bits, format, "flags_pretty"); | ||||
} | } | ||||
char * | char * | ||||
▲ Show 20 Lines • Show All 244 Lines • Show Last 20 Lines |