-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.c
60 lines (41 loc) · 970 Bytes
/
socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Tool creating a socket and displaying the identity of the
network namespace to which it belongs to
*/
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/sockios.h> // For SIOCGSKNS
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "util.h"
int main(void)
{
int sd;
int fd;
struct stat st;
int rc;
// Create a socket
sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sd < 0) {
ERR("socket(): '%m' (%d)\n", errno);
return 1;
}
// Get a file descriptor on its network_ns
fd = ioctl(sd, SIOCGSKNS);
if (fd < 0) {
ERR("ioctl(SIOCGSKNS): '%m' (%d)\n", errno);
return 1;
}
// Get the information about the network_ns
rc = fstat(fd, &st);
if (rc < 0) {
ERR("fstat(): '%m' (%d)\n", errno);
return 1;
}
printf("Network namespace is [Device,Inode]: [%lu,%lu]\n", st.st_dev, st.st_ino);
close(sd);
close(fd);
return 0;
} // main