ip/l2tp: add JSON support

Convert ip l2tp to use JSON output routines.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Stephen Hemminger 2018-03-27 18:07:45 -07:00 committed by David Ahern
parent 1f483fc618
commit 98453b6580

View File

@ -204,15 +204,22 @@ static int delete_session(struct l2tp_parm *p)
return 0; return 0;
} }
static void print_cookie(char *name, const uint8_t *cookie, int len) static void print_cookie(const char *name, const char *fmt,
const uint8_t *cookie, int len)
{ {
printf(" %s %02x%02x%02x%02x", name, char abuf[32];
cookie[0], cookie[1], size_t n;
cookie[2], cookie[3]);
n = snprintf(abuf, sizeof(abuf),
"%02x%02x%02x%02x",
cookie[0], cookie[1], cookie[2], cookie[3]);
if (len == 8) if (len == 8)
printf("%02x%02x%02x%02x", snprintf(abuf + n, sizeof(abuf) - n,
cookie[4], cookie[5], "%02x%02x%02x%02x",
cookie[6], cookie[7]); cookie[4], cookie[5],
cookie[6], cookie[7]);
print_string(PRINT_ANY, name, fmt, abuf);
} }
static void print_tunnel(const struct l2tp_data *data) static void print_tunnel(const struct l2tp_data *data)
@ -220,74 +227,115 @@ static void print_tunnel(const struct l2tp_data *data)
const struct l2tp_parm *p = &data->config; const struct l2tp_parm *p = &data->config;
char buf[INET6_ADDRSTRLEN]; char buf[INET6_ADDRSTRLEN];
printf("Tunnel %u, encap %s\n", open_json_object(NULL);
p->tunnel_id, print_uint(PRINT_ANY, "tunnel_id", "Tunnel %u,", p->tunnel_id);
p->encap == L2TP_ENCAPTYPE_UDP ? "UDP" : print_string(PRINT_ANY, "encap", " encap %s",
p->encap == L2TP_ENCAPTYPE_IP ? "IP" : "??"); p->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
printf(" From %s ", p->encap == L2TP_ENCAPTYPE_IP ? "IP" : "??");
inet_ntop(p->local_ip.family, p->local_ip.data, print_string(PRINT_FP, NULL, "%s", _SL_);
buf, sizeof(buf)));
printf("to %s\n", print_string(PRINT_ANY, "local", " From %s ",
inet_ntop(p->peer_ip.family, p->peer_ip.data, inet_ntop(p->local_ip.family, p->local_ip.data,
buf, sizeof(buf))); buf, sizeof(buf)));
printf(" Peer tunnel %u\n", print_string(PRINT_ANY, "peer", "to %s",
p->peer_tunnel_id); inet_ntop(p->peer_ip.family, p->peer_ip.data,
buf, sizeof(buf)));
print_string(PRINT_FP, NULL, "%s", _SL_);
print_uint(PRINT_ANY, "peer_tunnel", " Peer tunnel %u",
p->peer_tunnel_id);
print_string(PRINT_FP, NULL, "%s", _SL_);
if (p->encap == L2TP_ENCAPTYPE_UDP) { if (p->encap == L2TP_ENCAPTYPE_UDP) {
printf(" UDP source / dest ports: %hu/%hu\n", print_string(PRINT_FP, NULL,
p->local_udp_port, p->peer_udp_port); " UDP source / dest ports:", NULL);
print_uint(PRINT_ANY, "local_port", " %hu",
p->local_udp_port);
print_uint(PRINT_ANY, "peer_port", "/%hu",
p->peer_udp_port);
print_string(PRINT_FP, NULL, "%s", _SL_);
switch (p->local_ip.family) { switch (p->local_ip.family) {
case AF_INET: case AF_INET:
printf(" UDP checksum: %s\n", print_bool(PRINT_JSON, "checksum",
p->udp_csum ? "enabled" : "disabled"); NULL, p->udp_csum);
print_string(PRINT_FP, NULL,
" UDP checksum: %s\n",
p->udp_csum ? "enabled" : "disabled");
break; break;
case AF_INET6: case AF_INET6:
printf(" UDP checksum: %s%s%s%s\n", if (is_json_context()) {
p->udp6_csum_tx && p->udp6_csum_rx print_bool(PRINT_JSON, "checksum_tx",
? "enabled" : "", NULL, p->udp6_csum_tx);
p->udp6_csum_tx && !p->udp6_csum_rx
? "tx" : "", print_bool(PRINT_JSON, "checksum_rx",
!p->udp6_csum_tx && p->udp6_csum_rx NULL, p->udp6_csum_tx);
? "rx" : "", } else {
!p->udp6_csum_tx && !p->udp6_csum_rx printf(" UDP checksum: %s%s%s%s\n",
? "disabled" : ""); p->udp6_csum_tx && p->udp6_csum_rx
? "enabled" : "",
p->udp6_csum_tx && !p->udp6_csum_rx
? "tx" : "",
!p->udp6_csum_tx && p->udp6_csum_rx
? "rx" : "",
!p->udp6_csum_tx && !p->udp6_csum_rx
? "disabled" : "");
}
break; break;
} }
} }
close_json_object();
} }
static void print_session(struct l2tp_data *data) static void print_session(struct l2tp_data *data)
{ {
struct l2tp_parm *p = &data->config; struct l2tp_parm *p = &data->config;
printf("Session %u in tunnel %u\n", open_json_object(NULL);
p->session_id, p->tunnel_id);
printf(" Peer session %u, tunnel %u\n",
p->peer_session_id, p->peer_tunnel_id);
if (p->ifname != NULL) print_uint(PRINT_ANY, "session_id", "Session %u", p->session_id);
printf(" interface name: %s\n", p->ifname); print_uint(PRINT_ANY, "tunnel_id", " in tunnel %u", p->tunnel_id);
print_string(PRINT_FP, NULL, "%s", _SL_);
print_uint(PRINT_ANY, "peer_session_id",
" Peer session %u,", p->peer_session_id);
print_uint(PRINT_ANY, "peer_tunnel_id",
" tunnel %u", p->peer_tunnel_id);
print_string(PRINT_FP, NULL, "%s", _SL_);
if (p->ifname != NULL) {
print_color_string(PRINT_ANY, COLOR_IFNAME,
"interface", " interface name: %s" , p->ifname);
print_string(PRINT_FP, NULL, "%s", _SL_);
}
print_uint(PRINT_ANY, "offset", " offset %u,", p->offset);
print_uint(PRINT_ANY, "peer_offset", " peer offset %u\n", p->peer_offset);
printf(" offset %u, peer offset %u\n",
p->offset, p->peer_offset);
if (p->cookie_len > 0) if (p->cookie_len > 0)
print_cookie("cookie", p->cookie, p->cookie_len); print_cookie("cookie", "cookie",
p->cookie, p->cookie_len);
if (p->peer_cookie_len > 0) if (p->peer_cookie_len > 0)
print_cookie("peer cookie", p->peer_cookie, p->peer_cookie_len); print_cookie("peer_cookie", "peer cookie",
p->peer_cookie, p->peer_cookie_len);
if (p->reorder_timeout != 0) if (p->reorder_timeout != 0)
printf(" reorder timeout: %u\n", p->reorder_timeout); print_uint(PRINT_ANY, "reorder_timeout",
else " reorder timeout: %u", p->reorder_timeout);
printf("\n");
if (p->send_seq || p->recv_seq) { if (p->send_seq || p->recv_seq) {
printf(" sequence numbering:"); print_string(PRINT_FP, NULL, "%s sequence numbering:", _SL_);
if (p->send_seq) if (p->send_seq)
printf(" send"); print_null(PRINT_ANY, "send_seq", " send", NULL);
if (p->recv_seq) if (p->recv_seq)
printf(" recv"); print_null(PRINT_ANY, "recv_seq", " recv", NULL);
printf("\n");
} }
print_string(PRINT_FP, NULL, "\n", NULL);
close_json_object();
} }
static int get_response(struct nlmsghdr *n, void *arg) static int get_response(struct nlmsghdr *n, void *arg)
@ -435,10 +483,13 @@ static int get_session(struct l2tp_data *p)
if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0) if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0)
return -2; return -2;
new_json_obj(json);
if (rtnl_dump_filter(&genl_rth, session_nlmsg, p) < 0) { if (rtnl_dump_filter(&genl_rth, session_nlmsg, p) < 0) {
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
exit(1); exit(1);
} }
delete_json_obj();
fflush(stdout);
return 0; return 0;
} }
@ -468,10 +519,13 @@ static int get_tunnel(struct l2tp_data *p)
if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0) if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0)
return -2; return -2;
new_json_obj(json);
if (rtnl_dump_filter(&genl_rth, tunnel_nlmsg, p) < 0) { if (rtnl_dump_filter(&genl_rth, tunnel_nlmsg, p) < 0) {
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
exit(1); exit(1);
} }
delete_json_obj();
fflush(stdout);
return 0; return 0;
} }