summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2018-09-11 22:14:25 +0200
committerFlorian Westphal <fw@strlen.de>2018-09-13 10:42:11 +0200
commit758f8bd61bcad7809d776881bc1f8de0b2fab2e2 (patch)
tree39f6778cda7f776c3c67bdc4ad7ccb70141cccc4 /src
parent5fb4942292c30be7225c4bbbc04fb3eecbf514fd (diff)
json: Make inet_service_type_json() respect literal level
This brings inet_service_type_json() on par with inet_service_type_print(). Despite datatype_print()'s ability to use the 'print' callback, a dedicated 'json' callback is required to make port numbers appear as numbers in JSON output instead of strings. Therefore go with a bit of code duplication here. Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'src')
-rw-r--r--src/json.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/json.c b/src/json.c
index bb1d0f2d..c0fc4226 100644
--- a/src/json.c
+++ b/src/json.c
@@ -857,7 +857,23 @@ json_t *inet_protocol_type_json(const struct expr *expr,
json_t *inet_service_type_json(const struct expr *expr, struct output_ctx *octx)
{
- return json_integer(ntohs(mpz_get_be16(expr->value)));
+ struct sockaddr_in sin = {
+ .sin_family = AF_INET,
+ .sin_port = mpz_get_be16(expr->value),
+ };
+ char buf[NI_MAXSERV];
+
+ if (octx->literal < NFT_LITERAL_PORT ||
+ getnameinfo((struct sockaddr *)&sin, sizeof(sin),
+ NULL, 0, buf, sizeof(buf), 0))
+ return json_integer(ntohs(sin.sin_port));
+
+ if (htons(atoi(buf)) == sin.sin_port ||
+ getnameinfo((struct sockaddr *)&sin, sizeof(sin),
+ NULL, 0, buf, sizeof(buf), NI_DGRAM))
+ return json_integer(ntohs(sin.sin_port));
+
+ return json_string(buf);
}
json_t *mark_type_json(const struct expr *expr, struct output_ctx *octx)