6 #include <sys/socket.h>
9 #include <linux/if_tun.h>
17 int open_or_die(const char *file
, int flags
)
19 int ret
= open(file
, flags
);
21 panic("Cannot open file %s! %s.\n", file
, strerror(errno
));
26 int open_or_die_m(const char *file
, int flags
, mode_t mode
)
28 int ret
= open(file
, flags
, mode
);
30 panic("Cannot open or create file %s! %s.", file
, strerror(errno
));
34 void create_or_die(const char *file
, mode_t mode
)
36 int fd
= open_or_die_m(file
, O_WRONLY
| O_CREAT
, mode
);
40 void pipe_or_die(int pipefd
[2], int flags
)
42 int ret
= pipe2(pipefd
, flags
);
44 panic("Cannot create pipe2 event fd! %s.\n", strerror(errno
));
47 int tun_open_or_die(char *name
, int type
)
54 panic("No name provided for tundev!\n");
56 fd
= open_or_die("/dev/net/tun", O_RDWR
);
58 memset(&ifr
, 0, sizeof(ifr
));
60 strlcpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
62 ret
= ioctl(fd
, TUNSETIFF
, &ifr
);
64 panic("ioctl screwed up! %s.\n", strerror(errno
));
66 ret
= fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
68 panic("fctnl screwed up! %s.\n", strerror(errno
));
70 flags
= device_get_flags(name
);
71 flags
|= IFF_UP
| IFF_RUNNING
;
72 device_set_flags(name
, flags
);
77 ssize_t
read_or_die(int fd
, void *buf
, size_t len
)
79 ssize_t ret
= read(fd
, buf
, len
);
83 panic("Cannot read from descriptor! %s.\n", strerror(errno
));
89 ssize_t
write_or_die(int fd
, const void *buf
, size_t len
)
91 ssize_t ret
= write(fd
, buf
, len
);
95 panic("Cannot write to descriptor! %s.", strerror(errno
));