summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-12-28 02:39:57 +0000
committerChristian Pointner <equinox@anytun.org>2008-12-28 02:39:57 +0000
commit03e65a0b2401f63c8b10a1395def5c7859280aba (patch)
tree1f1643f21653cfca58f3aa5b48871ac963114cdd /src
parentfixed memory error und udp_close (diff)
added enpoint to string functions
Diffstat (limited to 'src')
-rw-r--r--src/uanytun.c4
-rw-r--r--src/udp.c47
-rw-r--r--src/udp.h6
3 files changed, 54 insertions, 3 deletions
diff --git a/src/uanytun.c b/src/uanytun.c
index 27d1880..881ba5b 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -71,6 +71,10 @@ int main(int argc, char* argv[])
exit(-1);
}
+ udp_set_remote(sock, "anycast.anytun.org", "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];
diff --git a/src/udp.c b/src/udp.c
index b5701ad..70375c2 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -96,7 +96,6 @@ void udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* por
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
- hints.ai_flags |= AI_CANONNAME;
int errcode = getaddrinfo(remote_addr, port, &hints, &res);
if (errcode != 0) {
@@ -118,7 +117,51 @@ void udp_close(udp_socket_t** sock)
free(*sock);
*sock = NULL;
}
-
+
+char* udp_endpoint_to_string(struct sockaddr_storage ss)
+{
+ void* ptr;
+ u_int16_t port;
+ char* addrstr;
+
+ switch (((struct sockaddr *)&ss)->sa_family)
+ {
+ case AF_INET:
+ ptr = &((struct sockaddr_in *)&ss)->sin_addr;
+ port = ntohs(((struct sockaddr_in *)&ss)->sin_port);
+ addrstr = malloc(INET_ADDRSTRLEN+1);
+ break;
+ case AF_INET6:
+ ptr = &((struct sockaddr_in6 *)&ss)->sin6_addr;
+ port = ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
+ addrstr = malloc(INET6_ADDRSTRLEN+1);
+ break;
+ default:
+ return "";
+ }
+ inet_ntop (((struct sockaddr *)&ss)->sa_family, ptr, addrstr, 100);
+ char* ret;
+ asprintf(&ret, "%s:%d", addrstr, port);
+ free(addrstr);
+ return ret;
+}
+
+char* udp_get_local_end_string(udp_socket_t* sock)
+{
+ if(!sock)
+ return "";
+
+ return udp_endpoint_to_string(sock->remote_end_);
+}
+
+char* udp_get_remote_end_string(udp_socket_t* sock)
+{
+ if(!sock)
+ return "";
+
+ 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_)
{
if(!sock || !remote_end_)
diff --git a/src/udp.h b/src/udp.h
index 91867b2..848673c 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -47,7 +47,11 @@ typedef struct udp_socket_struct udp_socket_t;
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_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_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len);