Merge pull request #59 from electronjoe/graceful-SIGTERM-handling
[netsniff-ng-old.git] / xio.c
blobc78257827c0a1299c1531dc03ac4b9ae01010668
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #define _GNU_SOURCE
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <syslog.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <linux/if.h>
22 #include <linux/if_tun.h>
24 #include "die.h"
25 #include "xio.h"
26 #include "xutils.h"
28 int open_or_die(const char *file, int flags)
30 int ret = open(file, flags);
31 if (ret < 0)
32 panic("Cannot open file %s! %s.\n", file, strerror(errno));
34 return ret;
37 int open_or_die_m(const char *file, int flags, mode_t mode)
39 int ret = open(file, flags, mode);
40 if (ret < 0)
41 panic("Cannot open or create file %s! %s.", file, strerror(errno));
42 return ret;
45 void create_or_die(const char *file, mode_t mode)
47 int fd = open_or_die_m(file, O_WRONLY | O_CREAT, mode);
48 close(fd);
51 void pipe_or_die(int pipefd[2], int flags)
53 int ret = pipe2(pipefd, flags);
54 if (ret < 0)
55 panic("Cannot create pipe2 event fd! %s.\n", strerror(errno));
58 int tun_open_or_die(char *name, int type)
60 int fd, ret;
61 short flags;
62 struct ifreq ifr;
64 if (!name)
65 panic("No name provided for tundev!\n");
67 fd = open_or_die("/dev/net/tun", O_RDWR);
69 memset(&ifr, 0, sizeof(ifr));
70 ifr.ifr_flags = type;
71 strlcpy(ifr.ifr_name, name, IFNAMSIZ);
73 ret = ioctl(fd, TUNSETIFF, &ifr);
74 if (ret < 0)
75 panic("ioctl screwed up! %s.\n", strerror(errno));
77 ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
78 if (ret < 0)
79 panic("fctnl screwed up! %s.\n", strerror(errno));
81 flags = device_get_flags(name);
82 flags |= IFF_UP | IFF_RUNNING;
83 device_set_flags(name, flags);
85 return fd;
88 ssize_t read_or_die(int fd, void *buf, size_t len)
90 ssize_t ret = read(fd, buf, len);
91 if (ret < 0) {
92 if (errno == EPIPE)
93 die();
94 panic("Cannot read from descriptor! %s.\n", strerror(errno));
97 return ret;
100 ssize_t write_or_die(int fd, const void *buf, size_t len)
102 ssize_t ret = write(fd, buf, len);
103 if (ret < 0) {
104 if (errno == EPIPE)
105 die();
106 panic("Cannot write to descriptor! %s.", strerror(errno));
109 return ret;
112 extern volatile sig_atomic_t sigint;
114 ssize_t read_exact(int fd, void *buf, size_t len, int mayexit)
116 ssize_t num = 0, written;
118 while (len > 0 && !sigint) {
119 if ((written = read(fd, buf, len)) < 0) {
120 if (errno == EAGAIN && num > 0)
121 continue;
122 if (mayexit)
123 return -1;
124 else
125 continue;
127 if (!written)
128 return 0;
129 len -= written;
130 buf += written;
131 num += written;
134 return num;
137 ssize_t write_exact(int fd, void *buf, size_t len, int mayexit)
139 ssize_t num = 0, written;
141 while (len > 0 && !sigint) {
142 if ((written = write(fd, buf, len)) < 0) {
143 if (errno == EAGAIN && num > 0)
144 continue;
145 if (mayexit)
146 return -1;
147 else
148 continue;
150 if (!written)
151 return 0;
152 len -= written;
153 buf += written;
154 num += written;
157 return num;
160 static int fd_rnd = -1;
162 static void randombytes(unsigned char *x, unsigned long long xlen)
164 int ret;
166 if (fd_rnd == -1) {
167 for (;;) {
168 fd_rnd = open("/dev/urandom", O_RDONLY);
169 if (fd_rnd != -1)
170 break;
171 sleep(1);
175 while (xlen > 0) {
176 if (xlen < 1048576)
177 ret = xlen;
178 else
179 ret = 1048576;
181 ret = read(fd_rnd, x, ret);
182 if (ret < 1) {
183 sleep(1);
184 continue;
187 x += ret;
188 xlen -= ret;
192 /* Note: it's not really secure, but the name only suggests it's better to use
193 * than rand(3) when transferring bytes over the network in non-security
194 * critical structure members. secrand() is only used to fill up salts actually.
196 int secrand(void)
198 int ret;
200 randombytes((void *) &ret, sizeof(ret));
202 return ret;
205 static char const *priov[] = {
206 [LOG_EMERG] = "EMERG:",
207 [LOG_ALERT] = "ALERT:",
208 [LOG_CRIT] = "CRIT:",
209 [LOG_ERR] = "ERR:",
210 [LOG_WARNING] = "WARNING:",
211 [LOG_NOTICE] = "NOTICE:",
212 [LOG_INFO] = "INFO:",
213 [LOG_DEBUG] = "DEBUG:",
216 static ssize_t cookie_writer(void *cookie, char const *data, size_t leng)
218 int prio = LOG_DEBUG, len;
220 do {
221 len = strlen(priov[prio]);
222 } while (memcmp(data, priov[prio], len) && --prio >= 0);
224 if (prio < 0) {
225 prio = LOG_INFO;
226 } else {
227 data += len;
228 leng -= len;
231 while (*data == ' ') {
232 ++data;
233 --leng;
236 syslog(prio, "%.*s", (int) leng, data);
238 return leng;
241 static cookie_io_functions_t cookie_log = {
242 .write = cookie_writer,
245 void to_std_log(FILE **fp)
247 setvbuf(*fp = fopencookie(NULL, "w", cookie_log), NULL, _IOLBF, 0);