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;
int main(int argc, char** argv)
{
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 local_port = DEFAULT_LOCAL_PORT;
char* punch_server = NULL;
......@@ -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";
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)
{
......@@ -50,20 +50,21 @@ int main(int argc, char** argv)
case 'P':
stun_port = atoi(optarg);
break;
case 's':
punch_server = optarg;
break;
case 'p':
local_port = atoi(optarg);
break;
case 's':
punch_server = optarg;
break;
case 'd':
peer_id = atoi(optarg);
break;
case 'i':
local_host = optarg;
strncpy(local_ip, optarg, 16);
break;
case 'v':
verbose = 1;
break;
case '?':
default:
printf("invalid option: %c\n", opt);
......@@ -77,7 +78,7 @@ int main(int argc, char** argv)
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);
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));
if (ext_port) {
......
......@@ -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);
break;
}
// sleep for a while to avoid flooding protection
usleep(1000 * 100);
++i;
} else {
nums[i] = nums[1000];
......@@ -255,11 +256,8 @@ static void* server_notify_handler(void* data) {
break;
}
/* add socket to FD_SET and check if connected
* set both fields of the timeval structure to zero
* to make select() return immediately
*/
struct timeval tv = {0, 0};
// wait for a while
struct timeval tv = {0, 1000 * 100};
int fd = wait_for_peer(sock_array, ++i, &tv);
if (fd > 0) {
// connected
......@@ -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);
if (fd > 0) {
on_connected(fd);
......
......@@ -89,8 +89,6 @@ static void gen_random_string(char *s, const int len) {
for (; i < len; ++i) {
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) {
......@@ -130,7 +128,7 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_
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
// sendto() barely failed
free(buf);
return -1;
......@@ -205,13 +203,6 @@ static int send_bind_request(int sock, const char* remote_host, uint16_t remote_
return -1;
}
break;
case SourceAddress:
if (stun_parse_atr_addr( body, attrLen, addr_array + 2)) {
free(buf);
return -1;
}
break;
default:
// ignore
break;
......@@ -230,7 +221,7 @@ const char* get_nat_desc(nat_type 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;
uint16_t mapped_port = 0;
int s;
......@@ -246,7 +237,7 @@ nat_type detect_nat_type(const char* stun_host, uint16_t stun_port, const char*
struct sockaddr_in local_addr;
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);
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*
/*
* it's complicated to get the RECEIVER address of UDP packet,
* For Linux, use the setsockopt() called IP_PKTINFO
* which will get you a parameter over recvmsg() called
* IP_PKTINFO, which carries a struct in_pktinfo,
* which has a 4 byte IP address hiding in its ipi_addr field.
* For Linux/Windows, set IP_PKTINFO option to enable ancillary
* message that contains a pktinfo structure that supplies
* some information about the incoming packets
*/
/* TODO use getifaddrs() to get interface address,
* 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;
goto cleanup_sock;
} 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