Add a buffer_t and fill it from tun

This commit is contained in:
angt
2015-10-20 17:37:43 +02:00
parent 64316dd724
commit 8ad524a0ef
3 changed files with 152 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
CFLAGS=-std=c99 -fsanitize=address -fno-omit-frame-pointer
CFLAGS=-std=c99 -g -fsanitize=address -fno-omit-frame-pointer
glorytun:

View File

@@ -4,22 +4,113 @@
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#define COUNT(x) (sizeof(x)/sizeof(x[0]))
static inline size_t str_cpy (char *dst, char *src, size_t n)
#define ALIGN_SIZE (1<<4)
#define ALIGN_MASK (ALIGN_SIZE-1)
#define ALIGN(x) (((x)+ALIGN_MASK)&~ALIGN_MASK)
#define ALIGN_DOWN(x) ((x)&~ALIGN_MASK)
#define PALIGN(x) ((void *)ALIGN((size_t)(x)))
#define PALIGN_DOWN(x) ((void *)ALIGN_DOWN((size_t)(x)))
static inline void byte_set (void *dst, const char value, size_t size)
{
if (!dst)
return;
char *restrict d = dst;
while (size--)
*d++ = value;
}
static inline void byte_copy (void *dst, const void *src, size_t size)
{
if (!dst || !src)
return;
char *restrict d = dst;
const char *restrict s = src;
while (size--)
*d++ = *s++;
}
static inline size_t str_cpy (char *dst, const char *src, size_t len)
{
if (!dst || !src)
return 0;
size_t i;
for (i=0; i<n && src[i]; i++)
for (i=0; i<len && src[i]; i++)
dst[i] = src[i];
dst[i] = 0;
return i;
}
typedef struct buffer buffer_t;
struct buffer {
uint8_t *data;
uint8_t *read;
uint8_t *write;
uint8_t *end;
};
static inline void buffer_setup (buffer_t *buffer, void *data, size_t size)
{
if (!data)
data = malloc(ALIGN(size));
buffer->data = data;
buffer->read = data;
buffer->write = data;
buffer->end = data;
buffer->end += size;
}
static inline void buffer_format (buffer_t *buffer)
{
buffer->write = buffer->data;
buffer->read = buffer->data;
}
static inline size_t buffer_size (buffer_t *buffer)
{
return buffer->end-buffer->data;
}
static inline size_t buffer_write_size (buffer_t *buffer)
{
return buffer->end-buffer->write;
}
static inline size_t buffer_read_size (buffer_t *buffer)
{
return buffer->write-buffer->read;
}
static inline void buffer_shift (buffer_t *buffer)
{
if (buffer->read==buffer->write) {
buffer_format(buffer);
} else {
const uint8_t *src = PALIGN_DOWN(buffer->read);
const size_t size = ALIGN(buffer->write-src);
if (buffer->data+size<src) {
byte_copy(buffer->data, src, size);
buffer->read -= src-buffer->data;
buffer->write -= src-buffer->data;
}
}
}

View File

@@ -42,7 +42,8 @@ static int gt_open_tun (char *name)
return fd;
}
static void gt_sa_stop (int sig) {
static void gt_sa_stop (int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
@@ -65,6 +66,54 @@ static int gt_set_signal (void)
sigaction(SIGPIPE, &sa, NULL);
}
static inline int buffer_read_fd (buffer_t *buffer, int fd)
{
buffer_shift(buffer);
size_t size = buffer_write_size(buffer);
if (!size)
return -1;
ssize_t ret = read(fd, buffer->write, size);
if (ret==-1) {
if (errno==EAGAIN || errno==EINTR)
return -1;
if (errno)
printf("read: %m\n");
return 0;
}
buffer->write += ret;
return 1;
}
static inline int buffer_write_fd (buffer_t *buffer, int fd)
{
size_t size = buffer_read_size(buffer);
if (!size)
return -1;
ssize_t ret = write(fd, buffer->read, size);
if (ret==-1) {
if (errno==EAGAIN || errno==EINTR)
return -1;
if (errno)
printf("read: %m\n");
return 0;
}
buffer->read += ret;
buffer_shift(buffer);
return 1;
}
int main (int argc, char **argv)
{
gt_set_signal();
@@ -78,6 +127,9 @@ int main (int argc, char **argv)
{ .fd = tun_fd, .events = POLLIN },
};
buffer_t input;
buffer_setup(&input, NULL, 256*1024);
while (running) {
int ret = poll(fds, COUNT(fds), 0);
@@ -92,9 +144,13 @@ int main (int argc, char **argv)
continue;
if (fds[0].revents & POLLIN) {
printf("POLLIN!\n");
int read_ret = buffer_read_fd(&input, fds[0].fd);
printf("read %zu\n", buffer_read_size(&input));
buffer_format(&input);
}
}
free(input.data);
return 0;
}