Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx> --- 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