netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeremy Sowden <jeremy@azazel.net>
To: Jan Engelhardt <jengelh@inai.de>
Cc: Netfilter Devel <netfilter-devel@vger.kernel.org>
Subject: [PATCH xtables-addons v2 05/13] pknock: pknlusr: use NLMSG macros and proper types, rather than arithmetic on char pointers.
Date: Sun, 25 Oct 2020 13:15:51 +0000	[thread overview]
Message-ID: <20201025131559.920038-7-jeremy@azazel.net> (raw)
In-Reply-To: <20201025131559.920038-1-jeremy@azazel.net>

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 extensions/pknock/pknlusr.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/extensions/pknock/pknlusr.c b/extensions/pknock/pknlusr.c
index ed741599558b..252fd42ffecd 100644
--- a/extensions/pknock/pknlusr.c
+++ b/extensions/pknock/pknlusr.c
@@ -20,8 +20,10 @@ int main(void)
 	struct sockaddr_nl local_addr = { .nl_family = AF_NETLINK };
 	int sock_fd;
 
-	int buf_size;
-	unsigned char *buf;
+	size_t nlmsg_size;
+	struct nlmgrhdr *nlmsg;
+	struct cn_msg *cn_msg;
+	struct xt_pknock_nl_msg *pknock_msg;
 
 	sock_fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
 
@@ -40,38 +42,38 @@ int main(void)
 		return 1;
 	}
 
-	buf_size = sizeof(struct xt_pknock_nl_msg) + sizeof(struct cn_msg) + sizeof(struct nlmsghdr);
-	buf = malloc(buf_size);
+	nlmsg_size = NLMSG_SPACE(sizeof(*cn_msg) + sizeof(*pknock_msg));
+	nlmsg = malloc(nlmsg_size);
 
-	if (!buf) {
+	if (!nlmsg) {
 		perror("malloc()");
 		return 1;
 	}
 
 	while(1) {
 
-		struct xt_pknock_nl_msg *nlmsg;
-
 		const char *ip;
 		char ipbuf[48];
 
-		memset(buf, 0, buf_size);
+		memset(nlmsg, 0, nlmsg_size);
 
-		status = recv(sock_fd, buf, buf_size, 0);
+		status = recv(sock_fd, nlmsg, nlmsg_size, 0);
 
 		if (status <= 0) {
 			perror("recv()");
 			return 1;
 		}
-		nlmsg = (struct xt_pknock_nl_msg *)(buf + sizeof(struct cn_msg) + sizeof(struct nlmsghdr));
-		ip = inet_ntop(AF_INET, &nlmsg->peer_ip, ipbuf, sizeof(ipbuf));
-		printf("rule_name: %s - ip %s\n", nlmsg->rule_name, ip);
+
+		cn_msg = NLMSG_DATA(nlmsg);
+		pknock_msg = (struct xt_pknock_nl_msg *)(cn_msg->data);
+		ip = inet_ntop(AF_INET, &pknock_msg->peer_ip, ipbuf, sizeof(ipbuf));
+		printf("rule_name: %s - ip %s\n", pknock_msg->rule_name, ip);
 
 	}
 
 	close(sock_fd);
 
-	free(buf);
+	free(nlmsg);
 
 	return 0;
 }
-- 
2.28.0


  parent reply	other threads:[~2020-10-25 13:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-25 13:15 [PATCH xtables-addons v2 00/13] pknlusr improvements Jeremy Sowden
2020-10-25 13:15 ` Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 01/13] pknock: pknlusr: ensure man-page is included by `make dist` Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 02/13] pknock: pknlusr: remove dest_addr and rename src_addr Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 03/13] pknock: pknlusr: tighten up variable scopes Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 04/13] pknock: pknlusr: tidy up initialization of local address Jeremy Sowden
2020-10-25 13:15 ` Jeremy Sowden [this message]
2020-10-25 13:15 ` [PATCH xtables-addons v2 06/13] pknock: pknlusr: use macro to define inet_ntop buffer size Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 07/13] pknock: pknlusr: don't treat recv return value of zero as an error Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 08/13] pknock: pknlusr: always close socket Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 09/13] pknock: pknlusr: fix hard-coded netlink multicast group ID Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 10/13] pknock: xt_pknock: use IS_ENABLED Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 11/13] pknock: xt_pknock: use kzalloc Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 12/13] pknock: xt_pknock: use `pr_err` Jeremy Sowden
2020-10-25 13:15 ` [PATCH xtables-addons v2 13/13] pknock: xt_pknock: remove DEBUG definition and disable debug output Jeremy Sowden
2020-10-25 14:18 ` [PATCH xtables-addons v2 00/13] pknlusr improvements Jan Engelhardt

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=20201025131559.920038-7-jeremy@azazel.net \
    --to=jeremy@azazel.net \
    --cc=jengelh@inai.de \
    --cc=netfilter-devel@vger.kernel.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).