From: Jan Engelhardt <jengelh@inai.de>
To: pablo@netfilter.org
Cc: netfilter-devel@vger.kernel.org
Subject: [PATCH nft] src: compute mnemonic port name much easier
Date: Fri, 7 Feb 2020 12:43:21 +0100 [thread overview]
Message-ID: <20200207114321.29709-1-jengelh@inai.de> (raw)
Signed-off-by: Jan Engelhardt <jengelh@inai.de>
---
src/datatype.c | 34 ++++++----------------------------
src/json.c | 20 +++++---------------
2 files changed, 11 insertions(+), 43 deletions(-)
diff --git a/src/datatype.c b/src/datatype.c
index 189e1b48..e4ef51e8 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -657,34 +657,12 @@ const struct datatype inet_protocol_type = {
static void inet_service_print(const struct expr *expr, struct output_ctx *octx)
{
- struct sockaddr_in sin = { .sin_family = AF_INET };
- char buf[NI_MAXSERV];
- uint16_t port;
- int err;
-
- sin.sin_port = mpz_get_be16(expr->value);
- err = getnameinfo((struct sockaddr *)&sin, sizeof(sin), NULL, 0,
- buf, sizeof(buf), 0);
- if (err != 0) {
- nft_print(octx, "%u", ntohs(sin.sin_port));
- return;
- }
- port = atoi(buf);
- /* We got a TCP service name string, display it... */
- if (htons(port) != sin.sin_port) {
- nft_print(octx, "\"%s\"", buf);
- return;
- }
-
- /* ...otherwise, this might be a UDP service name. */
- err = getnameinfo((struct sockaddr *)&sin, sizeof(sin), NULL, 0,
- buf, sizeof(buf), NI_DGRAM);
- if (err != 0) {
- /* No service name, display numeric value. */
- nft_print(octx, "%u", ntohs(sin.sin_port));
- return;
- }
- nft_print(octx, "\"%s\"", buf);
+ uint16_t port = mpz_get_be16(expr->value);
+ const struct servent *s = getservbyport(port, NULL);
+ if (s == NULL)
+ nft_print(octx, "%hu", ntohs(port));
+ else
+ nft_print(octx, "\"%s\"", s->s_name);
}
void inet_service_type_print(const struct expr *expr, struct output_ctx *octx)
diff --git a/src/json.c b/src/json.c
index 1906e7db..7be13e6e 100644
--- a/src/json.c
+++ b/src/json.c
@@ -1021,23 +1021,13 @@ json_t *inet_protocol_type_json(const struct expr *expr,
json_t *inet_service_type_json(const struct expr *expr, struct output_ctx *octx)
{
- struct sockaddr_in sin = {
- .sin_family = AF_INET,
- .sin_port = mpz_get_be16(expr->value),
- };
- char buf[NI_MAXSERV];
+ uint16_t port = mpz_get_be16(expr->value);
+ const struct servent *s = NULL;
if (!nft_output_service(octx) ||
- 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);
+ (s = getservbyport(port, NULL)) == NULL)
+ return json_integer(ntohs(port));
+ return json_string(s->s_name);
}
json_t *mark_type_json(const struct expr *expr, struct output_ctx *octx)
--
2.25.0
next reply other threads:[~2020-02-07 11:43 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-07 11:43 Jan Engelhardt [this message]
2020-02-07 16:48 ` [PATCH nft] src: compute mnemonic port name much easier Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200207114321.29709-1-jengelh@inai.de \
--to=jengelh@inai.de \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).