]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: compute mnemonic port name much easier
authorJan Engelhardt <jengelh@inai.de>
Fri, 7 Feb 2020 11:43:21 +0000 (12:43 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 7 Feb 2020 16:56:43 +0000 (17:56 +0100)
Signed-off-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/datatype.c
src/json.c

index 189e1b482c29f1a7eed03e5b79f60d6ef1f31a4f..095598d9c60e74c1e2a7075773e5b54bb3a65ef0 100644 (file)
@@ -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)
index 1906e7db7c1957b2289136621b4ea511e3bcbaf8..86028959b8a32b9086908a52b37c3c318b43d34f 100644 (file)
@@ -1021,23 +1021,14 @@ 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));
+           (s = getservbyport(port, NULL)) == NULL)
+               return json_integer(ntohs(port));
 
-       return json_string(buf);
+       return json_string(s->s_name);
 }
 
 json_t *mark_type_json(const struct expr *expr, struct output_ctx *octx)