summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/uanytun.c9
-rw-r--r--src/udp.c10
-rw-r--r--src/udp.h2
3 files changed, 12 insertions, 9 deletions
diff --git a/src/uanytun.c b/src/uanytun.c
index 881ba5b..ae052f8 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -71,7 +71,7 @@ int main(int argc, char* argv[])
exit(-1);
}
- udp_set_remote(sock, "anycast.anytun.org", "4444");
+ 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);
@@ -81,9 +81,10 @@ int main(int argc, char* argv[])
int len = 0;
unsigned int cnt = 0;
while(cnt < 5) {
- len = tun_read(dev, buf, 1600);
- printf("read %d bytes from device\n", len);
-// tun_write(dev, buf, len);
+ 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++;
}
tun_close(&dev);
diff --git a/src/udp.c b/src/udp.c
index 70375c2..c2c7c79 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -66,6 +66,7 @@ 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) {
@@ -162,12 +163,13 @@ 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, struct sockaddr_storage* remote_end)
{
- if(!sock || !remote_end_)
+ if(!sock || !remote_end)
return -1;
- return 0;
+ int socklen = sizeof(*remote_end);
+ return recvfrom(sock->fd_, buf, len, 0, (struct sockaddr *)remote_end, &socklen);
}
int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len)
@@ -175,6 +177,6 @@ int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len)
if(!sock)
return -1;
- return 0;
+ return sendto(sock->fd_, buf, len, 0, (struct sockaddr *)&(sock->remote_end_), sizeof(sock->remote_end_));;
}
diff --git a/src/udp.h b/src/udp.h
index 848673c..096f655 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -52,7 +52,7 @@ 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_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);