2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
16 #include <sys/types.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
22 #include <linux/if_tun.h>
28 int open_or_die(const char *file
, int flags
)
30 int ret
= open(file
, flags
);
32 panic("Cannot open file!\n");
37 int open_or_die_m(const char *file
, int flags
, mode_t mode
)
39 int ret
= open(file
, flags
, mode
);
41 panic("Cannot open or create file!");
45 void create_or_die(const char *file
, mode_t mode
)
47 int fd
= open_or_die_m(file
, O_WRONLY
| O_CREAT
, mode
);
51 void pipe_or_die(int pipefd
[2], int flags
)
53 int ret
= pipe2(pipefd
, flags
);
55 panic("Cannot create pipe2 event fd!\n");
58 int tun_open_or_die(char *name
, int type
)
63 fd
= open_or_die("/dev/net/tun", O_RDWR
);
65 memset(&ifr
, 0, sizeof(ifr
));
69 strlcpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
71 ret
= ioctl(fd
, TUNSETIFF
, &ifr
);
73 panic("ioctl screwed up!\n");
75 ret
= fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
77 panic("fctnl screwed up!\n");
82 ssize_t
read_or_die(int fd
, void *buf
, size_t len
)
84 ssize_t ret
= read(fd
, buf
, len
);
88 panic("Cannot read from descriptor!\n");
94 ssize_t
write_or_die(int fd
, const void *buf
, size_t len
)
96 ssize_t ret
= write(fd
, buf
, len
);
100 panic("Cannot write to descriptor!");
106 extern volatile sig_atomic_t sigint
;
108 ssize_t
read_exact(int fd
, void *buf
, size_t len
, int mayexit
)
110 register ssize_t num
= 0, written
;
112 while (len
> 0 && !sigint
) {
113 if ((written
= read(fd
, buf
, len
)) < 0) {
114 if (errno
== EAGAIN
&& num
> 0)
131 ssize_t
write_exact(int fd
, void *buf
, size_t len
, int mayexit
)
133 register ssize_t num
= 0, written
;
135 while (len
> 0 && !sigint
) {
136 if ((written
= write(fd
, buf
, len
)) < 0) {
137 if (errno
== EAGAIN
&& num
> 0)