From 221033af170eca8d553999701125c428c68070a3 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 28 Dec 2008 03:49:14 +0000 Subject: some cleanup added remote endpoint auto detection small bugfixes --- src/uanytun.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src/uanytun.c') diff --git a/src/uanytun.c b/src/uanytun.c index ae052f8..567a103 100644 --- a/src/uanytun.c +++ b/src/uanytun.c @@ -36,6 +36,7 @@ #include #include +#include #include "log.h" #include "signal.h" @@ -45,6 +46,32 @@ #include "daemon.h" #include "sysexec.h" +void main_loop(tun_device_t* dev, udp_socket_t* sock) +{ + log_printf(INFO, "entering main loop"); + + u_int8_t buf[1600]; + int len = 0; + unsigned int cnt = 0; + while(cnt < 5) { + udp_endpoint_t remote; + len = udp_read(sock, buf, 1600, &remote); + + if(memcmp(&remote, &(sock->remote_end_), sizeof(remote))) { + memcpy(&(sock->remote_end_), &remote, sizeof(remote)); + char* addrstring = udp_endpoint_to_string(remote); + log_printf(NOTICE, "autodetected remote host changed %s", addrstring); + free(addrstring); + } + + printf("read %d bytes from socket\n", len); + + udp_write(sock, buf, len); + cnt++; + } +} + + int main(int argc, char* argv[]) { log_init("uanytun", DAEMON); @@ -70,23 +97,17 @@ int main(int argc, char* argv[]) log_printf(ERR, "error on udp_init"); exit(-1); } + char* local_string = udp_get_local_end_string(sock); + log_printf(INFO, "listening on: %s", local_string); + free(local_string); udp_set_remote(sock, "1.2.3.4", "4444"); char* remote_string = udp_get_remote_end_string(sock); log_printf(INFO, "set remote end to: %s", remote_string); free(remote_string); - log_printf(INFO, "entering main loop"); - u_int8_t buf[1600]; - int len = 0; - unsigned int cnt = 0; - while(cnt < 5) { - struct sockaddr_storage remote; - len = udp_read(sock, buf, 1600, &remote); - printf("read %d bytes from socket\n", len); - udp_write(sock, buf, len); - cnt++; - } + main_loop(dev, sock); + tun_close(&dev); udp_close(&sock); -- cgit v1.2.3