Commit b435fd9e authored by Fong's avatar Fong
Browse files

fix stack overflow and indent code

parent 7ccd0b6c
...@@ -25,7 +25,7 @@ int verbose = 0; ...@@ -25,7 +25,7 @@ int verbose = 0;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
char* stun_server = stun_servers[0]; char* stun_server = stun_servers[0];
char* local_host = "0.0.0.0"; char local_ip[16] = "0.0.0.0";
uint16_t stun_port = DEFAULT_STUN_SERVER_PORT; uint16_t stun_port = DEFAULT_STUN_SERVER_PORT;
uint16_t local_port = DEFAULT_LOCAL_PORT; uint16_t local_port = DEFAULT_LOCAL_PORT;
char* punch_server = NULL; char* punch_server = NULL;
...@@ -34,7 +34,7 @@ int main(int argc, char** argv) ...@@ -34,7 +34,7 @@ int main(int argc, char** argv)
static char usage[] = "usage: [-h] [-H STUN_HOST] [-t ttl] [-P STUN_PORT] [-s punch server] [-d id] [-i SOURCE_IP] [-p SOURCE_PORT] [-v verbose]\n"; static char usage[] = "usage: [-h] [-H STUN_HOST] [-t ttl] [-P STUN_PORT] [-s punch server] [-d id] [-i SOURCE_IP] [-p SOURCE_PORT] [-v verbose]\n";
int opt; int opt;
while ((opt = getopt (argc, argv, "H:h:t:P:p:s:d:i")) != -1) while ((opt = getopt (argc, argv, "H:h:t:P:p:s:d:i:v")) != -1)
{ {
switch (opt) switch (opt)
{ {
...@@ -50,20 +50,21 @@ int main(int argc, char** argv) ...@@ -50,20 +50,21 @@ int main(int argc, char** argv)
case 'P': case 'P':
stun_port = atoi(optarg); stun_port = atoi(optarg);
break; break;
case 's':
punch_server = optarg;
break;
case 'p': case 'p':
local_port = atoi(optarg); local_port = atoi(optarg);
break; break;
case 's':
punch_server = optarg;
break;
case 'd': case 'd':
peer_id = atoi(optarg); peer_id = atoi(optarg);
break; break;
case 'i': case 'i':
local_host = optarg; strncpy(local_ip, optarg, 16);
break; break;
case 'v': case 'v':
verbose = 1; verbose = 1;
break;
case '?': case '?':
default: default:
printf("invalid option: %c\n", opt); printf("invalid option: %c\n", opt);
...@@ -77,7 +78,7 @@ int main(int argc, char** argv) ...@@ -77,7 +78,7 @@ int main(int argc, char** argv)
uint16_t ext_port = 0; uint16_t ext_port = 0;
// TODO we should try another STUN server if failed // 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); nat_type type = detect_nat_type(stun_server, stun_port, local_ip, local_port, ext_ip, &ext_port);
printf("NAT type: %s\n", get_nat_desc(type)); printf("NAT type: %s\n", get_nat_desc(type));
if (ext_port) { if (ext_port) {
......
...@@ -181,7 +181,8 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf ...@@ -181,7 +181,8 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf
verbose_log("%s, NAT flooding protection triggered, try %d times\n", strerror(errno), i); verbose_log("%s, NAT flooding protection triggered, try %d times\n", strerror(errno), i);
break; break;
} }
// sleep for a while to avoid flooding protection
usleep(1000 * 100);
++i; ++i;
} else { } else {
nums[i] = nums[1000]; nums[i] = nums[1000];
...@@ -255,11 +256,8 @@ static void* server_notify_handler(void* data) { ...@@ -255,11 +256,8 @@ static void* server_notify_handler(void* data) {
break; break;
} }
/* add socket to FD_SET and check if connected // wait for a while
* set both fields of the timeval structure to zero struct timeval tv = {0, 1000 * 100};
* to make select() return immediately
*/
struct timeval tv = {0, 0};
int fd = wait_for_peer(sock_array, ++i, &tv); int fd = wait_for_peer(sock_array, ++i, &tv);
if (fd > 0) { if (fd > 0) {
// connected // connected
...@@ -270,7 +268,8 @@ static void* server_notify_handler(void* data) { ...@@ -270,7 +268,8 @@ static void* server_notify_handler(void* data) {
} }
} }
struct timeval tv = {10, 0}; printf("holes punched, waiting for peer\n");
struct timeval tv = {100, 0};
int fd = wait_for_peer(sock_array, i, &tv); int fd = wait_for_peer(sock_array, i, &tv);
if (fd > 0) { if (fd > 0) {
on_connected(fd); on_connected(fd);
......
...@@ -89,8 +89,6 @@ static void gen_random_string(char *s, const int len) { ...@@ -89,8 +89,6 @@ static void gen_random_string(char *s, const int len) {
for (; i < len; ++i) { for (; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
} }
s[len] = 0;
} }
static int send_bind_request(int sock, const char* remote_host, uint16_t remote_port, uint32_t change_ip, uint32_t change_port, StunAtrAddress* addr_array) { static int send_bind_request(int sock, const char* remote_host, uint16_t remote_port, uint32_t change_ip, uint32_t change_port, StunAtrAddress* addr_array) {
...@@ -130,7 +128,7 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_ ...@@ -130,7 +128,7 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_
int retries; int retries;
for (retries = 0; retries < MAX_RETRIES_NUM; retries++) { for (retries = 0; retries < MAX_RETRIES_NUM; retries++) {
if (-1 == sendto(sock, buf, ptr - buf, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr))) { if (-1 == sendto(sock, buf, ptr - buf, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr))) {
// udp sendto barely failed // sendto() barely failed
free(buf); free(buf);
return -1; return -1;
...@@ -205,13 +203,6 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_ ...@@ -205,13 +203,6 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_
return -1; return -1;
} }
break; break;
case SourceAddress:
if (stun_parse_atr_addr( body, attrLen, addr_array + 2)) {
free(buf);
return -1;
}
break;
default: default:
// ignore // ignore
break; break;
...@@ -230,7 +221,7 @@ const char* get_nat_desc(nat_type type) { ...@@ -230,7 +221,7 @@ const char* get_nat_desc(nat_type type) {
return nat_types[type]; return nat_types[type];
} }
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) { nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char* local_ip, uint16_t local_port, char* ext_ip, uint16_t* ext_port) {
uint32_t mapped_ip = 0; uint32_t mapped_ip = 0;
uint16_t mapped_port = 0; uint16_t mapped_port = 0;
int s; int s;
...@@ -246,7 +237,7 @@ nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char* ...@@ -246,7 +237,7 @@ nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char*
struct sockaddr_in local_addr; struct sockaddr_in local_addr;
local_addr.sin_family = AF_INET; local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr(local_host); local_addr.sin_addr.s_addr = inet_addr(local_ip);
local_addr.sin_port = htons(local_port); local_addr.sin_port = htons(local_port);
if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr))) { if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr))) {
...@@ -276,18 +267,17 @@ nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char* ...@@ -276,18 +267,17 @@ nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char*
/* /*
* it's complicated to get the RECEIVER address of UDP packet, * it's complicated to get the RECEIVER address of UDP packet,
* For Linux, use the setsockopt() called IP_PKTINFO * For Linux/Windows, set IP_PKTINFO option to enable ancillary
* which will get you a parameter over recvmsg() called * message that contains a pktinfo structure that supplies
* IP_PKTINFO, which carries a struct in_pktinfo, * some information about the incoming packets
* which has a 4 byte IP address hiding in its ipi_addr field.
*/ */
/* TODO use getifaddrs() to get interface address, /* TODO use getifaddrs() to get interface address,
* then compare it with mapped address to determine * then compare it with mapped address to determine
* if it's open internet * if it's open Internet
*/ */
if (!strcmp(local_host, inet_ntoa(mapped_addr))) { if (!strcmp(local_ip, inet_ntoa(mapped_addr))) {
nat_type = OpenInternet; nat_type = OpenInternet;
goto cleanup_sock; goto cleanup_sock;
} else { } else {
......
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