6 #include <sys/socket.h>
9 #include <linux/if_tun.h>
16 int open_or_die(const char *file
, int flags
)
18 int ret
= open(file
, flags
);
20 panic("Cannot open file %s! %s.\n", file
, strerror(errno
));
25 int open_or_die_m(const char *file
, int flags
, mode_t mode
)
27 int ret
= open(file
, flags
, mode
);
29 panic("Cannot open or create file %s! %s.", file
, strerror(errno
));
33 void create_or_die(const char *file
, mode_t mode
)
35 int fd
= open_or_die_m(file
, O_WRONLY
| O_CREAT
, mode
);
39 void pipe_or_die(int pipefd
[2], int flags
)
41 int ret
= pipe2(pipefd
, flags
);
43 panic("Cannot create pipe2 event fd! %s.\n", strerror(errno
));
46 int tun_open_or_die(char *name
, int type
)
53 panic("No name provided for tundev!\n");
55 fd
= open_or_die("/dev/net/tun", O_RDWR
);
57 memset(&ifr
, 0, sizeof(ifr
));
59 strlcpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
61 ret
= ioctl(fd
, TUNSETIFF
, &ifr
);
63 panic("ioctl screwed up! %s.\n", strerror(errno
));
65 ret
= fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
67 panic("fctnl screwed up! %s.\n", strerror(errno
));
69 flags
= device_get_flags(name
);
70 flags
|= IFF_UP
| IFF_RUNNING
;
71 device_set_flags(name
, flags
);
76 ssize_t
read_or_die(int fd
, void *buf
, size_t len
)
78 ssize_t ret
= read(fd
, buf
, len
);
82 panic("Cannot read from descriptor! %s.\n", strerror(errno
));
88 ssize_t
write_or_die(int fd
, const void *buf
, size_t len
)
90 ssize_t ret
= write(fd
, buf
, len
);
94 panic("Cannot write to descriptor! %s.", strerror(errno
));