static void hsr_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { SPRINT_BUF(b1); if (!tb) return; if (tb[IFLA_HSR_SLAVE1] && RTA_PAYLOAD(tb[IFLA_HSR_SLAVE1]) < sizeof(__u32)) return; if (tb[IFLA_HSR_SLAVE2] && RTA_PAYLOAD(tb[IFLA_HSR_SLAVE2]) < sizeof(__u32)) return; if (tb[IFLA_HSR_SEQ_NR] && RTA_PAYLOAD(tb[IFLA_HSR_SEQ_NR]) < sizeof(__u16)) return; if (tb[IFLA_HSR_SUPERVISION_ADDR] && RTA_PAYLOAD(tb[IFLA_HSR_SUPERVISION_ADDR]) < ETH_ALEN) return; if (tb[IFLA_HSR_SLAVE1]) print_string(PRINT_ANY, "slave1", "slave1 %s ", ll_index_to_name(rta_getattr_u32(tb[IFLA_HSR_SLAVE1]))); else print_null(PRINT_ANY, "slave1", "slave1 %s ", "<none>"); if (tb[IFLA_HSR_SLAVE2]) print_string(PRINT_ANY, "slave2", "slave2 %s ", ll_index_to_name(rta_getattr_u32(tb[IFLA_HSR_SLAVE2]))); else print_null(PRINT_ANY, "slave2", "slave2 %s ", "<none>"); if (tb[IFLA_HSR_SEQ_NR]) print_int(PRINT_ANY, "seq_nr", "sequence %d ", rta_getattr_u16(tb[IFLA_HSR_SEQ_NR])); if (tb[IFLA_HSR_SUPERVISION_ADDR]) print_string(PRINT_ANY, "supervision_addr", "supervision %s ", ll_addr_n2a(RTA_DATA(tb[IFLA_HSR_SUPERVISION_ADDR]), RTA_PAYLOAD(tb[IFLA_HSR_SUPERVISION_ADDR]), ARPHRD_VOID, b1, sizeof(b1))); }
static char* print_value(cjson* item, int depth, int fmt) { char* out; switch (item->type) { case JSON_NULL: out = print_null(); break; case JSON_TRUE: out = print_true(); break; case JSON_FALSE: out = print_false(); break; case JSON_STRING: out = print_string(item->valuestring); break; case JSON_NUMBER: out = print_number(item); break; case JSON_ARRAY: out = print_array(item, depth, fmt); break; case JSON_OBJECT: out = print_object(item, depth, fmt); break; } return out; }
unsigned int print_name_and_link(const char *fmt, const char *name, struct rtattr *tb[]) { const char *link = NULL; unsigned int m_flag = 0; SPRINT_BUF(b1); if (tb[IFLA_LINK]) { int iflink = rta_getattr_u32(tb[IFLA_LINK]); if (iflink) { if (tb[IFLA_LINK_NETNSID]) { if (is_json_context()) { print_int(PRINT_JSON, "link_index", NULL, iflink); } else { link = ll_idx_n2a(iflink); } } else { link = ll_index_to_name(iflink); if (is_json_context()) { print_string(PRINT_JSON, "link", NULL, link); link = NULL; } m_flag = ll_index_to_flags(iflink); m_flag = !(m_flag & IFF_UP); } } else { if (is_json_context()) print_null(PRINT_JSON, "link", NULL, NULL); else link = "NONE"; } if (link) { snprintf(b1, sizeof(b1), "%s@%s", name, link); name = b1; } } print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt, name); return m_flag; }
void print_ptr_rec(ptr x) { /*printf("%u\n", x);*/ if (is_fixnum(x)) { printf("%d", to_fixnum(x)); } else if (x == bool_f) { printf("#f"); } else if (x == bool_t) { printf("#t"); } else if (is_null(x)) { print_null(); } else if (is_char(x)) { printf("%s", beautify(to_char(x))); } else if (is_pair(x)) { printf("("); print_pair(x); printf(")"); } else { printf("#<unknown 0x%08x>", x); } }
int print_neigh(struct nlmsghdr *n, void *arg) { FILE *fp = (FILE *)arg; struct ndmsg *r = NLMSG_DATA(n); int len = n->nlmsg_len; struct rtattr *tb[NDA_MAX+1]; static int logit = 1; __u8 protocol = 0; if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH && n->nlmsg_type != RTM_GETNEIGH) { fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n", n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); return 0; } len -= NLMSG_LENGTH(sizeof(*r)); if (len < 0) { fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); return -1; } if (filter.flushb && n->nlmsg_type != RTM_NEWNEIGH) return 0; if (filter.family && filter.family != r->ndm_family) return 0; if (filter.index && filter.index != r->ndm_ifindex) return 0; if (!(filter.state&r->ndm_state) && !(r->ndm_flags & NTF_PROXY) && !(r->ndm_flags & NTF_EXT_LEARNED) && (r->ndm_state || !(filter.state&0x100)) && (r->ndm_family != AF_DECnet)) return 0; if (filter.master && !(n->nlmsg_flags & NLM_F_DUMP_FILTERED)) { if (logit) { logit = 0; fprintf(fp, "\nWARNING: Kernel does not support filtering by master device\n\n"); } } parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); if (inet_addr_match_rta(&filter.pfx, tb[NDA_DST])) return 0; if (tb[NDA_PROTOCOL]) protocol = rta_getattr_u8(tb[NDA_PROTOCOL]); if (filter.protocol && filter.protocol != protocol) return 0; if (filter.unused_only && tb[NDA_CACHEINFO]) { struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); if (ci->ndm_refcnt) return 0; } if (filter.flushb) { struct nlmsghdr *fn; if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { if (flush_update()) return -1; } fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp)); memcpy(fn, n, n->nlmsg_len); fn->nlmsg_type = RTM_DELNEIGH; fn->nlmsg_flags = NLM_F_REQUEST; fn->nlmsg_seq = ++rth.seq; filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb; filter.flushed++; if (show_stats < 2) return 0; } open_json_object(NULL); if (n->nlmsg_type == RTM_DELNEIGH) print_bool(PRINT_ANY, "deleted", "Deleted ", true); else if (n->nlmsg_type == RTM_GETNEIGH) print_null(PRINT_ANY, "miss", "%s ", "miss"); if (tb[NDA_DST]) { const char *dst; int family = r->ndm_family; if (family == AF_BRIDGE) { if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr)) family = AF_INET6; else family = AF_INET; } dst = format_host_rta(family, tb[NDA_DST]); print_color_string(PRINT_ANY, ifa_family_color(family), "dst", "%s ", dst); } if (!filter.index && r->ndm_ifindex) { if (!is_json_context()) fprintf(fp, "dev "); print_color_string(PRINT_ANY, COLOR_IFNAME, "dev", "%s ", ll_index_to_name(r->ndm_ifindex)); } if (tb[NDA_LLADDR]) { const char *lladdr; SPRINT_BUF(b1); lladdr = ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]), RTA_PAYLOAD(tb[NDA_LLADDR]), ll_index_to_type(r->ndm_ifindex), b1, sizeof(b1)); if (!is_json_context()) fprintf(fp, "lladdr "); print_color_string(PRINT_ANY, COLOR_MAC, "lladdr", "%s", lladdr); } if (r->ndm_flags & NTF_ROUTER) print_null(PRINT_ANY, "router", " %s", "router"); if (r->ndm_flags & NTF_PROXY) print_null(PRINT_ANY, "proxy", " %s", "proxy"); if (r->ndm_flags & NTF_EXT_LEARNED) print_null(PRINT_ANY, "extern_learn", " %s ", "extern_learn"); if (show_stats) { if (tb[NDA_CACHEINFO]) print_cacheinfo(RTA_DATA(tb[NDA_CACHEINFO])); if (tb[NDA_PROBES]) print_uint(PRINT_ANY, "probes", " probes %u", rta_getattr_u32(tb[NDA_PROBES])); } if (r->ndm_state) print_neigh_state(r->ndm_state); if (protocol) { SPRINT_BUF(b1); print_string(PRINT_ANY, "protocol", " proto %s ", rtnl_rtprot_n2a(protocol, b1, sizeof(b1))); } print_string(PRINT_FP, NULL, "\n", ""); close_json_object(); fflush(stdout); return 0; }
static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) { if (!tb) return; if (!tb[IFLA_GRE_COLLECT_METADATA]) gre_print_direct_opt(f, tb); else print_bool(PRINT_ANY, "external", "external ", true); if (tb[IFLA_GRE_IGNORE_DF] && rta_getattr_u8(tb[IFLA_GRE_IGNORE_DF])) print_bool(PRINT_ANY, "ignore_df", "ignore-df ", true); if (tb[IFLA_GRE_ERSPAN_INDEX]) { __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]); fprintf(f, "erspan_index %u ", erspan_idx); } if (tb[IFLA_GRE_ENCAP_TYPE] && rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) { __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]); __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]); __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]); __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]); open_json_object("encap"); print_string(PRINT_FP, NULL, "encap ", NULL); switch (type) { case TUNNEL_ENCAP_FOU: print_string(PRINT_ANY, "type", "%s ", "fou"); break; case TUNNEL_ENCAP_GUE: print_string(PRINT_ANY, "type", "%s ", "gue"); break; default: print_null(PRINT_ANY, "type", "%s ", "unknown"); break; } if (is_json_context()) { print_uint(PRINT_JSON, "sport", NULL, sport ? ntohs(sport) : 0); print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); print_bool(PRINT_JSON, "csum", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM); print_bool(PRINT_JSON, "csum6", NULL, flags & TUNNEL_ENCAP_FLAG_CSUM6); print_bool(PRINT_JSON, "remcsum", NULL, flags & TUNNEL_ENCAP_FLAG_REMCSUM); close_json_object(); } else { if (sport == 0) fputs("encap-sport auto ", f); else fprintf(f, "encap-sport %u", ntohs(sport)); fprintf(f, "encap-dport %u ", ntohs(dport)); if (flags & TUNNEL_ENCAP_FLAG_CSUM) fputs("encap-csum ", f); else fputs("noencap-csum ", f); if (flags & TUNNEL_ENCAP_FLAG_CSUM6) fputs("encap-csum6 ", f); else fputs("noencap-csum6 ", f); if (flags & TUNNEL_ENCAP_FLAG_REMCSUM) fputs("encap-remcsum ", f); else fputs("noencap-remcsum ", f); } } }