From 43c513ea604c77e71333d8307bdb9b710b091721 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Tue, 9 Apr 2024 06:55:39 +0100 Subject: Add netlink socket --- md/notes/kernel/netlink_socket.md | 50 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'md/notes/kernel/netlink_socket.md') diff --git a/md/notes/kernel/netlink_socket.md b/md/notes/kernel/netlink_socket.md index 01e9043..e022e52 100644 --- a/md/notes/kernel/netlink_socket.md +++ b/md/notes/kernel/netlink_socket.md @@ -3,11 +3,11 @@ keywords: kernel,linux,netlink,socket # Kernel compile "Hello world" -Compile minimal linux kernel module. +As base start from minimal kernel module ## Files -You need to create to files __Makefile__ and __hello_world.c__. +You need to create to files __Makefile__ and __netlink_socket.c__. __Makefile__ ```Makefile @@ -206,51 +206,51 @@ create utility that connects and interacts with netlink socket. int main(int argc, char **argv) { - struct sockaddr_nl src_addr; - struct sockaddr_nl dest_addr; - struct nlmsghdr *nlh; - struct msghdr msg; - struct iovec iov; - int sock_fd; - int ret; + struct sockaddr_nl src_addr; + struct sockaddr_nl dest_addr; + struct nlmsghdr *nlh; + struct msghdr msg; + struct iovec iov; + int sock_fd; + int ret; - sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_PROTO); + sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_PROTO); if (sock_fd < 0) { printf("socket creation for NETLINK failed...\n"); return -1; } - memset(&src_addr, 0, sizeof(src_addr)); + memset(&src_addr, 0, sizeof(src_addr)); src_addr.nl_family = AF_NETLINK; src_addr.nl_pid = getpid(); bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr)); - nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(PAYLOAD_SIZE)); + nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(PAYLOAD_SIZE)); memset(nlh, 0, NLMSG_SPACE(PAYLOAD_SIZE)); nlh->nlmsg_len = NLMSG_SPACE(PAYLOAD_SIZE); nlh->nlmsg_pid = getpid(); nlh->nlmsg_flags = 0; - strcpy(NLMSG_DATA(nlh), "Test test Test!"); + strcpy(NLMSG_DATA(nlh), "Test test Test!"); - memset(&dest_addr, 0, sizeof(dest_addr)); + memset(&dest_addr, 0, sizeof(dest_addr)); dest_addr.nl_family = AF_NETLINK; dest_addr.nl_pid = 0; // Linux Kernel PID dest_addr.nl_groups = 0; - memset(&iov, 0, sizeof(iov)); - iov.iov_base = (void *)nlh; - iov.iov_len = nlh->nlmsg_len; + memset(&iov, 0, sizeof(iov)); + iov.iov_base = (void *)nlh; + iov.iov_len = nlh->nlmsg_len; - memset(&msg, 0, sizeof(msg)); - msg.msg_name = (void *)&dest_addr; - msg.msg_namelen = sizeof(dest_addr); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; + memset(&msg, 0, sizeof(msg)); + msg.msg_name = (void *)&dest_addr; + msg.msg_namelen = sizeof(dest_addr); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; - ret = sendmsg(sock_fd, &msg, 0); + ret = sendmsg(sock_fd, &msg, 0); printf("Message sent, payload: %s\n", (char *)NLMSG_DATA(nlh)); // Clear netlink packet payload: @@ -260,9 +260,9 @@ int main(int argc, char **argv) { ret = recvmsg(sock_fd, &msg, 0); printf("Message received, payload: %s\n", (char *)NLMSG_DATA(nlh)); - close(sock_fd); + close(sock_fd); - return 0; + return 0; } ``` -- cgit v1.2.3