summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-12-28 03:49:14 +0000
committerChristian Pointner <equinox@anytun.org>2008-12-28 03:49:14 +0000
commit221033af170eca8d553999701125c428c68070a3 (patch)
tree80b08210f2566c2ddef7c6c9b236900e1e5817f0
parentfixed addrstr length error (diff)
some cleanup
added remote endpoint auto detection small bugfixes
-rw-r--r--src/uanytun.c43
-rw-r--r--src/udp.c21
-rw-r--r--src/udp.h10
3 files changed, 48 insertions, 26 deletions
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 <stdlib.h>
#include <stdio.h>
+#include <string.h>
#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);
diff --git a/src/udp.c b/src/udp.c
index fdf59b0..a6ffabf 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -50,6 +50,8 @@ void udp_init(udp_socket_t** sock, const char* local_addr, const char* port)
*sock = malloc(sizeof(udp_socket_t));
if(!*sock)
return;
+ memset(&((*sock)->local_end_), 0, sizeof((*sock)->local_end_));
+ memset(&((*sock)->remote_end_), 0, sizeof((*sock)->local_end_));
struct addrinfo hints, *res;
@@ -66,7 +68,6 @@ void udp_init(udp_socket_t** sock, const char* local_addr, const char* port)
return;
}
- memset(&((*sock)->remote_end_), 0, sizeof((*sock)->local_end_));
memcpy(&((*sock)->local_end_), res->ai_addr, sizeof(*(res->ai_addr)));
(*sock)->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
if((*sock)->fd_ < 0) {
@@ -119,30 +120,30 @@ void udp_close(udp_socket_t** sock)
*sock = NULL;
}
-char* udp_endpoint_to_string(struct sockaddr_storage ss)
+char* udp_endpoint_to_string(udp_endpoint_t e)
{
void* ptr;
u_int16_t port;
size_t addrstr_len = 0;
char* addrstr;
- switch (((struct sockaddr *)&ss)->sa_family)
+ switch (((struct sockaddr *)&e)->sa_family)
{
case AF_INET:
- ptr = &((struct sockaddr_in *)&ss)->sin_addr;
- port = ntohs(((struct sockaddr_in *)&ss)->sin_port);
+ ptr = &((struct sockaddr_in *)&e)->sin_addr;
+ port = ntohs(((struct sockaddr_in *)&e)->sin_port);
addrstr_len = INET_ADDRSTRLEN + 1;
break;
case AF_INET6:
- ptr = &((struct sockaddr_in6 *)&ss)->sin6_addr;
- port = ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
+ ptr = &((struct sockaddr_in6 *)&e)->sin6_addr;
+ port = ntohs(((struct sockaddr_in6 *)&e)->sin6_port);
addrstr_len = INET6_ADDRSTRLEN + 1;
break;
default:
return "";
}
addrstr = malloc(addrstr_len);
- inet_ntop (((struct sockaddr *)&ss)->sa_family, ptr, addrstr, addrstr_len);
+ inet_ntop (((struct sockaddr *)&e)->sa_family, ptr, addrstr, addrstr_len);
char* ret;
asprintf(&ret, "%s:%d", addrstr, port);
free(addrstr);
@@ -154,7 +155,7 @@ char* udp_get_local_end_string(udp_socket_t* sock)
if(!sock)
return "";
- return udp_endpoint_to_string(sock->remote_end_);
+ return udp_endpoint_to_string(sock->local_end_);
}
char* udp_get_remote_end_string(udp_socket_t* sock)
@@ -165,7 +166,7 @@ char* udp_get_remote_end_string(udp_socket_t* sock)
return udp_endpoint_to_string(sock->remote_end_);
}
-int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, struct sockaddr_storage* remote_end)
+int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end)
{
if(!sock || !remote_end)
return -1;
diff --git a/src/udp.h b/src/udp.h
index 096f655..e7d4875 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -37,10 +37,11 @@
#include <sys/socket.h>
+typedef struct sockaddr_storage udp_endpoint_t;
struct udp_socket_struct {
int fd_;
- struct sockaddr_storage local_end_;
- struct sockaddr_storage remote_end_;
+ udp_endpoint_t local_end_;
+ udp_endpoint_t remote_end_;
};
typedef struct udp_socket_struct udp_socket_t;
@@ -48,12 +49,11 @@ void udp_init(udp_socket_t** sock, const char* local_addr, const char* port);
void udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port);
void udp_close(udp_socket_t** sock);
-char* udp_endpoint_to_string(struct sockaddr_storage ss);
+char* udp_endpoint_to_string(udp_endpoint_t e);
char* udp_get_local_end_string(udp_socket_t* sock);
char* udp_get_remote_end_string(udp_socket_t* sock);
-int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, struct sockaddr_storage* remote_end);
+int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end);
int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len);
-
#endif