summaryrefslogtreecommitdiffstats
path: root/src/datatype.c
diff options
context:
space:
mode:
authorJan Engelhardt <jengelh@inai.de>2020-02-07 12:43:21 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2020-02-07 17:56:43 +0100
commite527e901564f9cb1cb7a42ddc3e5d72236b770bc (patch)
tree9c9887b2ce68828b66630e7a516b63b402f08387 /src/datatype.c
parenta16920d3620d9b7130c994a26c0b86f8b357103e (diff)
src: compute mnemonic port name much easier
Signed-off-by: Jan Engelhardt <jengelh@inai.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/datatype.c')
-rw-r--r--src/datatype.c33
1 files changed, 6 insertions, 27 deletions
diff --git a/src/datatype.c b/src/datatype.c
index 189e1b48..095598d9 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -657,34 +657,13 @@ 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;
- }
+ uint16_t port = mpz_get_be16(expr->value);
+ const struct servent *s = getservbyport(port, NULL);
- /* ...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);
+ 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)