Add a minimal option and gt_open_sock
This commit is contained in:
105
glorytun.c
105
glorytun.c
@@ -7,15 +7,67 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
|
||||
#define GT_NAME "glorytun"
|
||||
#define GT_BUFFER_SIZE (256*1024)
|
||||
|
||||
volatile sig_atomic_t running;
|
||||
|
||||
static int gt_open_sock (char *host, char *port, int listener)
|
||||
{
|
||||
struct addrinfo hints = {
|
||||
.ai_family = AF_UNSPEC,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
.ai_protocol = IPPROTO_TCP,
|
||||
.ai_flags = AI_PASSIVE,
|
||||
};
|
||||
|
||||
struct addrinfo *ai, *res = NULL;
|
||||
|
||||
if (getaddrinfo(host, port, &hints, &res)) {
|
||||
printf("host not found\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fd = -1;
|
||||
|
||||
for (ai=res; ai; ai=ai->ai_next) {
|
||||
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
||||
|
||||
if (fd==-1)
|
||||
continue;
|
||||
|
||||
int ret;
|
||||
|
||||
if (listener) {
|
||||
ret = bind(fd, ai->ai_addr, ai->ai_addrlen);
|
||||
if (!ret)
|
||||
ret = listen(fd, 1);
|
||||
} else {
|
||||
ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
|
||||
}
|
||||
|
||||
if (!ret)
|
||||
break;
|
||||
|
||||
if (errno)
|
||||
printf("socket: %m\n");
|
||||
|
||||
close(fd);
|
||||
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int gt_open_tun (char *name)
|
||||
{
|
||||
int fd = open("/dev/net/tun", O_RDWR);
|
||||
@@ -115,17 +167,62 @@ static inline int write_from_buffer (int fd, buffer_t *buffer)
|
||||
return 1;
|
||||
}
|
||||
|
||||
enum option_type {
|
||||
option_flag,
|
||||
option_string,
|
||||
};
|
||||
|
||||
struct option {
|
||||
char *name;
|
||||
void *data;
|
||||
enum option_type type;
|
||||
};
|
||||
|
||||
static void option (int argc, char **argv, int n, struct option *opt)
|
||||
{
|
||||
for (int i=0; i<argc; i++) {
|
||||
for (int k=0; k<n; k++) {
|
||||
if (str_cmp(opt[k].name, argv[i]))
|
||||
continue;
|
||||
switch (opt[k].type) {
|
||||
case option_flag:
|
||||
*(int *)opt[k].data = 1;
|
||||
break;
|
||||
case option_string:
|
||||
*(char **)opt[k].data = argv[++i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
gt_set_signal();
|
||||
|
||||
int tun_fd = gt_open_tun(GT_NAME);
|
||||
char *host = NULL;
|
||||
char *port = "5000";
|
||||
char *dev = "glorytun";
|
||||
int listener = 0;
|
||||
|
||||
if (tun_fd==-1)
|
||||
struct option opts[] = {
|
||||
{ "dev", &dev, option_string },
|
||||
{ "host", &host, option_string },
|
||||
{ "port", &port, option_string },
|
||||
{ "listener", &listener, option_flag },
|
||||
};
|
||||
|
||||
option(argc, argv, COUNT(opts), opts);
|
||||
|
||||
int tun_fd = gt_open_tun(dev);
|
||||
int sock_fd = gt_open_sock(host, port, listener);
|
||||
|
||||
if (tun_fd==-1 || sock_fd==-1)
|
||||
return 1;
|
||||
|
||||
struct pollfd fds[] = {
|
||||
{ .fd = tun_fd, .events = POLLIN },
|
||||
{ .fd = tun_fd, .events = POLLIN },
|
||||
{ .fd = sock_fd, .events = POLLIN },
|
||||
};
|
||||
|
||||
buffer_t input;
|
||||
|
||||
Reference in New Issue
Block a user