summaryrefslogtreecommitdiff
path: root/md/notes/kernel/netlink_socket.md
diff options
context:
space:
mode:
authorArturs Artamonovs <dos21h@gmail.com>2024-04-09 06:55:39 +0100
committerArturs Artamonovs <dos21h@gmail.com>2024-04-09 06:55:39 +0100
commit43c513ea604c77e71333d8307bdb9b710b091721 (patch)
tree27d5382aa1de245ff5028f6c558aa354d3bfa994 /md/notes/kernel/netlink_socket.md
parent3164a32dd4d5849fa19fe34a2c2eba03edf5f87e (diff)
downloadmd-content-43c513ea604c77e71333d8307bdb9b710b091721.tar.gz
md-content-43c513ea604c77e71333d8307bdb9b710b091721.zip
Add netlink socket
Diffstat (limited to 'md/notes/kernel/netlink_socket.md')
-rw-r--r--md/notes/kernel/netlink_socket.md50
1 files changed, 25 insertions, 25 deletions
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;
}
```