Add a buffer_t and fill it from tun
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,4 +1,4 @@
|
|||||||
CFLAGS=-std=c99 -fsanitize=address -fno-omit-frame-pointer
|
CFLAGS=-std=c99 -g -fsanitize=address -fno-omit-frame-pointer
|
||||||
|
|
||||||
glorytun:
|
glorytun:
|
||||||
|
|
||||||
|
|||||||
95
common.h
95
common.h
@@ -4,22 +4,113 @@
|
|||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#define COUNT(x) (sizeof(x)/sizeof(x[0]))
|
#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)
|
if (!dst || !src)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
size_t i;
|
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] = src[i];
|
||||||
|
|
||||||
dst[i] = 0;
|
dst[i] = 0;
|
||||||
|
|
||||||
return i;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
60
glorytun.c
60
glorytun.c
@@ -42,7 +42,8 @@ static int gt_open_tun (char *name)
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void gt_sa_stop (int sig) {
|
static void gt_sa_stop (int sig)
|
||||||
|
{
|
||||||
switch (sig) {
|
switch (sig) {
|
||||||
case SIGINT:
|
case SIGINT:
|
||||||
case SIGTERM:
|
case SIGTERM:
|
||||||
@@ -65,6 +66,54 @@ static int gt_set_signal (void)
|
|||||||
sigaction(SIGPIPE, &sa, NULL);
|
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)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
gt_set_signal();
|
gt_set_signal();
|
||||||
@@ -78,6 +127,9 @@ int main (int argc, char **argv)
|
|||||||
{ .fd = tun_fd, .events = POLLIN },
|
{ .fd = tun_fd, .events = POLLIN },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
buffer_t input;
|
||||||
|
buffer_setup(&input, NULL, 256*1024);
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
int ret = poll(fds, COUNT(fds), 0);
|
int ret = poll(fds, COUNT(fds), 0);
|
||||||
|
|
||||||
@@ -92,9 +144,13 @@ int main (int argc, char **argv)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (fds[0].revents & POLLIN) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user