Created
September 5, 2016 10:22
-
-
Save vfreex/d45f3e1229eeb583fb3d7281ca58bf6b to your computer and use it in GitHub Desktop.
namespaces_netlink_test.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <asm/types.h> | |
#include <sys/socket.h> | |
#include <linux/netlink.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <sched.h> | |
int main() | |
{ | |
pid_t pid; | |
//int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_USERSOCK); | |
unshare(CLONE_NEWPID); | |
pid = fork(); | |
if (pid == -1) { | |
perror("fork error"); | |
exit(-1); | |
} | |
if (pid == 0) { | |
int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_USERSOCK); | |
unshare(CLONE_NEWNS|CLONE_NEWNET); | |
//system("mount -t none none /proc --make-private && mount -t proc proc /proc && ps"); | |
printf("child proc, pid=%lu\n", (long)getpid()); | |
if (sock == -1) { | |
perror("socket error"); | |
return -1; | |
} | |
unshare(CLONE_NEWNET); | |
struct sockaddr_nl sa; | |
memset(&sa, 0, sizeof(sa)); | |
sa.nl_family = AF_NETLINK; | |
sa.nl_groups = 0; | |
int ret; | |
printf("child proc: binding\n"); | |
ret = bind(sock, (struct sockaddr *)&sa, sizeof(sa)); | |
if (ret == -1) { | |
perror("bind error\n"); | |
return -1; | |
} | |
int len; | |
ret = getsockname(sock, (struct sockaddr *)&sa, &len); | |
if (ret == -1) { | |
perror("getsockname error\n"); | |
return -1; | |
} | |
printf("bind nl_pid=%lu\n", (long)sa.nl_pid); | |
struct sockaddr_nl sa_remote; | |
char buffer[4096]; | |
struct iovec iov = { buffer, sizeof(buffer) }; | |
struct msghdr msg = { &sa_remote, sizeof(sa_remote), &iov, 1, NULL, 0, 0 }; | |
struct nlmsghdr *nh; | |
printf("waiting...\n"); | |
len = recvmsg(sock, &msg, 0); | |
for (nh = (struct nlmsghdr *) buffer; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) { | |
printf("type=%d\n", nh->nlmsg_type); | |
const char *payload = buffer + sizeof(struct nlmsghdr); | |
printf("payload: %s\n", payload); | |
} | |
return 0; | |
} | |
printf("parent proc\n"); | |
char buffer[4096]; | |
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_USERSOCK); | |
if (fd == -1) { | |
perror("socket error"); | |
return -1; | |
} | |
struct nlmsghdr *nh = (struct nlmsghdr *)buffer; | |
nh->nlmsg_pid = pid; | |
nh->nlmsg_len = 100; | |
struct sockaddr_nl sa; | |
memset(&sa, 0, sizeof(sa)); | |
sa.nl_family = AF_NETLINK; | |
sa.nl_groups = 0; | |
int ret = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); | |
if (ret == -1) { | |
perror("bind error"); | |
return -1; | |
} | |
struct sockaddr_nl sa_remote; | |
memset(&sa_remote, 0, sizeof(sa_remote)); | |
sa_remote.nl_family = AF_NETLINK; | |
sa_remote.nl_groups = 0; | |
sa_remote.nl_pid = pid; | |
struct iovec iov = { nh, nh->nlmsg_len }; | |
struct msghdr msg = { &sa_remote, sizeof(sa_remote), &iov, 1, NULL, 0, 0 }; | |
nh->nlmsg_pid = 0; | |
nh->nlmsg_seq = 1234; | |
nh->nlmsg_flags |= NLM_F_ACK; | |
nh->nlmsg_type = 12345; | |
char *payload = buffer + sizeof(struct nlmsghdr); | |
strcpy(payload, "Hello world"); | |
sleep(1); | |
printf("prepare to send\n"); | |
int len = sendmsg(fd, &msg, 0); | |
if (len == -1) { | |
perror("sendmsg error"); | |
return -1; | |
} | |
printf("sent %d\n", len); | |
printf("waitpid\n"); | |
waitpid(pid, NULL, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment