Commit f16627e1 authored by Fong's avatar Fong
Browse files

fix endianness of in_addr

parent 62b2ef30
......@@ -3,6 +3,7 @@ CFLAGS = -g -Wall
all: nat_traversal
# clang warn about unused argument, it requires -pthread when compiling but not when linking
nat_traversal: main.o nat_traversal.o nat_type.o
$(CC) $(CFLAGS) -o nat_traversal main.o nat_traversal.o nat_type.o -pthread
......
......@@ -12,13 +12,16 @@
#define DEFAULT_SERVER_PORT 9988
#define MSG_BUF_SIZE 512
// use some public stun servers to detect port allocation rule
// use public stun servers to detect port allocation rule
static char *stun_servers[] = {
"stun.ideasip.com",
"stun.ekiga.net",
"203.183.172.196"
};
// definition checked against extern declaration
int verbose = 0;
int main(int argc, char** argv)
{
char* stun_server = stun_servers[0];
......@@ -28,7 +31,7 @@ int main(int argc, char** argv)
char* punch_server = NULL;
uint32_t peer_id = 0;
static char usage[] = "usage: [-h] [-H STUN_HOST] [-P STUN_PORT] [-s punch server] [-d id] [-i SOURCE_IP] [-p SOURCE_PORT]\n";
static char usage[] = "usage: [-h] [-H STUN_HOST] [-P STUN_PORT] [-s punch server] [-d id] [-i SOURCE_IP] [-p SOURCE_PORT] [-v verbose]\n";
int opt;
while ((opt = getopt (argc, argv, "H:h:P:p:s:d:i")) != -1)
{
......@@ -55,6 +58,8 @@ int main(int argc, char** argv)
case 'i':
local_host = optarg;
break;
case 'v':
verbose = 1;
case '?':
default:
printf("invalid option: %c\n", opt);
......@@ -67,9 +72,9 @@ int main(int argc, char** argv)
char ext_ip[16] = {0};
uint16_t ext_port = 0;
// TODO we should try another STUN server if failed
nat_type type = detect_nat_type(stun_server, stun_port, local_host, local_port, ext_ip, &ext_port);
// TODO log
printf("NAT type: %s\n", get_nat_desc(type));
if (ext_port) {
printf("external address: %s:%d\n", ext_ip, ext_port);
......@@ -89,10 +94,6 @@ int main(int argc, char** argv)
server_addr.sin_port = htons(DEFAULT_SERVER_PORT);
client c;
// test
c.type = SymmetricNAT;
if (init(self, server_addr, &c)) {
printf("init failed\n");
......@@ -100,8 +101,9 @@ int main(int argc, char** argv)
}
if (peer_id) {
printf("connecting to peer %d", peer_id);
if (connect_to_peer(&c, peer_id) < 0) {
printf("failed to connect\n");
printf("failed to connect to peer %d\n", peer_id);
return -1;
}
......
......@@ -11,6 +11,8 @@
#include "nat_type.h"
#define MAX_RETRIES_NUM 3
static const char* nat_types[] = {
"blocked",
"open internet",
......@@ -125,7 +127,10 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_
memcpy(&remote_addr.sin_addr.s_addr, server->h_addr_list[0], server->h_length);
remote_addr.sin_port = htons(remote_port);
int retries;
for (retries = 0; retries < MAX_RETRIES_NUM; retries++) {
if (-1 == sendto(sock, buf, ptr - buf, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr))) {
// udp sendto barely failed
free(buf);
return -1;
......@@ -134,15 +139,25 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_
socklen_t fromlen = sizeof remote_addr;
struct timeval tv;
tv.tv_sec = 5;
tv.tv_sec = 3;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
if (recvfrom(sock, buf, 512, 0, (struct sockaddr *)&remote_addr, &fromlen) <= 0) {
if (errno != EAGAIN || errno != EWOULDBLOCK) {
free(buf);
return -1;
}
//timout, retry
} else {
// got response
break;
}
}
if (retries == MAX_RETRIES_NUM)
return -1;
StunHeader reply_header;
memcpy(&reply_header, buf, sizeof(StunHeader));
......@@ -278,7 +293,7 @@ nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char*
} else {
if (changed_ip != 0 && changed_port != 0) {
if (send_bind_request(s, stun_host, stun_port, ChangeIpFlag, ChangePortFlag, bind_result)) {
struct in_addr addr = {changed_ip};
struct in_addr addr = {htonl(changed_ip)};
char* alt_host = inet_ntoa(addr);
memset(bind_result, 0, sizeof(StunAtrAddress) * 2);
......
......@@ -76,6 +76,12 @@ typedef struct
char* encode16(char* buf, uint16_t data);
char* encode32(char* buf, uint32_t data);
char* encode(char* buf, const char* data, unsigned int length);
extern int verbose;
#define verbose_log(format, ...) do { \
if (verbose) \
printf(format, ##__VA_ARGS__); \
} while(0)
nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char* local_host, uint16_t local_port, char* ext_ip, uint16_t* ext_port);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment