diff --git a/glorytun.c b/glorytun.c index cfe3c8e..5a11d56 100644 --- a/glorytun.c +++ b/glorytun.c @@ -7,15 +7,67 @@ #include #include #include +#include +#include +#include #include #include -#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