Commit 64dab16a authored by Fong's avatar Fong
Browse files

fix gaint bug caused by varible scope

parent d18fc297
...@@ -30,8 +30,9 @@ int main(int argc, char** argv) ...@@ -30,8 +30,9 @@ int main(int argc, char** argv)
uint16_t local_port = DEFAULT_LOCAL_PORT; uint16_t local_port = DEFAULT_LOCAL_PORT;
char* punch_server = NULL; char* punch_server = NULL;
uint32_t peer_id = 0; uint32_t peer_id = 0;
int ttl = 10;
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"; 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:P:p:s:d:i")) != -1) while ((opt = getopt (argc, argv, "H:h:P:p:s:d:i")) != -1)
{ {
...@@ -43,6 +44,9 @@ int main(int argc, char** argv) ...@@ -43,6 +44,9 @@ int main(int argc, char** argv)
case 'H': case 'H':
stun_server = optarg; stun_server = optarg;
break; break;
case 't':
ttl = atoi(optarg);
break;
case 'P': case 'P':
stun_port = atoi(optarg); stun_port = atoi(optarg);
break; break;
...@@ -98,6 +102,7 @@ int main(int argc, char** argv) ...@@ -98,6 +102,7 @@ int main(int argc, char** argv)
client c; client c;
c.type = type; c.type = type;
c.ttl = ttl;
if (enroll(self, server_addr, &c) < 0) { if (enroll(self, server_addr, &c) < 0) {
printf("failed to enroll\n"); printf("failed to enroll\n");
...@@ -105,7 +110,7 @@ int main(int argc, char** argv) ...@@ -105,7 +110,7 @@ int main(int argc, char** argv)
} }
printf("enroll successfully, ID: %d\n", c.id); printf("enroll successfully, ID: %d\n", c.id);
pthread_t tid = wait_for_command(c.sfd); pthread_t tid = wait_for_command(&c.sfd);
if (peer_id) { if (peer_id) {
printf("connecting to peer %d\n", peer_id); printf("connecting to peer %d\n", peer_id);
......
...@@ -36,15 +36,18 @@ static int get_peer_info(client* cli, uint32_t peer_id, struct peer_info *peer) ...@@ -36,15 +36,18 @@ static int get_peer_info(client* cli, uint32_t peer_id, struct peer_info *peer)
return -1; return -1;
} }
if (recv(cli->sfd, (void*)peer, sizeof(struct peer_info), 0) < 0) int n_bytes = recv(cli->sfd, (void*)peer, sizeof(struct peer_info), 0);
if (n_bytes <= 0) {
return -1; return -1;
} else if (n_bytes == 1) {
// offline
return 1;
} else {
peer->port = ntohs(peer->port);
peer->type = ntohs(peer->type);
peer->port = ntohs(peer->port); return 0;
peer->type = ntohs(peer->type); }
printf("peer %d: %s:%d, nat type: %s\n", peer_id, peer->ip, peer->port, get_nat_desc(peer->type));
return 0;
} }
static int send_dummy_udp_packet(int fd, struct sockaddr_in addr) { static int send_dummy_udp_packet(int fd, struct sockaddr_in addr) {
...@@ -57,7 +60,7 @@ static int send_dummy_udp_packet(int fd, struct sockaddr_in addr) { ...@@ -57,7 +60,7 @@ static int send_dummy_udp_packet(int fd, struct sockaddr_in addr) {
} }
static int punch_hole(struct sockaddr_in peer_addr) { static int punch_hole(struct sockaddr_in peer_addr, int ttl) {
int hole = socket(AF_INET, SOCK_DGRAM, 0); int hole = socket(AF_INET, SOCK_DGRAM, 0);
if (hole != -1) { if (hole != -1) {
...@@ -76,7 +79,6 @@ static int punch_hole(struct sockaddr_in peer_addr) { ...@@ -76,7 +79,6 @@ static int punch_hole(struct sockaddr_in peer_addr) {
/* TODO we can use traceroute to get the number of hops to the peer /* TODO we can use traceroute to get the number of hops to the peer
* to make sure this packet woudn't reach the peer but get through the NAT of itself * to make sure this packet woudn't reach the peer but get through the NAT of itself
*/ */
int ttl = 5;
setsockopt(hole, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); setsockopt(hole, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
// send short ttl packets to avoid triggering flooding protection of NAT in front of peer // send short ttl packets to avoid triggering flooding protection of NAT in front of peer
...@@ -174,9 +176,9 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf ...@@ -174,9 +176,9 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf
if (port != remote_peer.port) { // exclude the used one if (port != remote_peer.port) { // exclude the used one
peer_addr.sin_port = htons(port); peer_addr.sin_port = htons(port);
if ((holes[i] = punch_hole(peer_addr)) < 0) { if ((holes[i] = punch_hole(peer_addr, c->ttl)) < 0) {
// NAT in front of us wound't tolerate too many ports used by one application // NAT in front of us wound't tolerate too many ports used by one application
verbose_log("NAT flooding protection triggered, try %d times\n", i); verbose_log("%s, NAT flooding protection triggered, try %d times\n", strerror(errno), i);
break; break;
} }
...@@ -327,11 +329,11 @@ int enroll(struct peer_info self, struct sockaddr_in punch_server, client* c) { ...@@ -327,11 +329,11 @@ int enroll(struct peer_info self, struct sockaddr_in punch_server, client* c) {
return 0; return 0;
} }
pthread_t wait_for_command(int server_sock) pthread_t wait_for_command(int* server_sock)
{ {
// wait for command from punch server in another thread // wait for command from punch server in another thread
pthread_t thread_id; pthread_t thread_id;
pthread_create(&thread_id, NULL, server_notify_handler, (void*)&server_sock); pthread_create(&thread_id, NULL, server_notify_handler, (void*)server_sock);
return thread_id; return thread_id;
} }
...@@ -353,12 +355,16 @@ void on_connected(int sock) { ...@@ -353,12 +355,16 @@ void on_connected(int sock) {
int connect_to_peer(client* cli, uint32_t peer_id) { int connect_to_peer(client* cli, uint32_t peer_id) {
struct peer_info peer; struct peer_info peer;
if (get_peer_info(cli, peer_id, &peer) < 0) { int n = get_peer_info(cli, peer_id, &peer);
printf("peer %d offline\n", peer_id); if (n) {
verbose_log("get_peer_info() return %d\n", n);
printf("failed to get info of remote peer\n");
return -1; return -1;
} }
printf("peer %d: %s:%d, nat type: %s\n", peer_id, peer.ip, peer.port, get_nat_desc(peer.type));
// choose less restricted peer as initiator // choose less restricted peer as initiator
switch(peer.type) { switch(peer.type) {
case OpenInternet: case OpenInternet:
......
...@@ -7,10 +7,16 @@ struct client { ...@@ -7,10 +7,16 @@ struct client {
int sfd; int sfd;
uint32_t id; uint32_t id;
char buf[128]; char buf[128];
//use a stack-based buffer to prevent memory allocation every time
char* msg_buf; char* msg_buf;
nat_type type; nat_type type;
char ext_ip[16]; char ext_ip[16];
uint16_t ext_port; uint16_t ext_port;
// ttl of hole punching packets,
// it should be greater than the number of hops between host to NAT of own side
// and less than the number of hops between host to NAT of remote side,
// so that the hole punching packets just die in the way
int ttl;
}; };
struct peer_info { struct peer_info {
...@@ -27,6 +33,6 @@ enum msg_type { ...@@ -27,6 +33,6 @@ enum msg_type {
// public functions // public functions
int enroll(struct peer_info self, struct sockaddr_in punch_server, client* c); int enroll(struct peer_info self, struct sockaddr_in punch_server, client* c);
pthread_t wait_for_command(int server_sock); pthread_t wait_for_command(int* server_sock);
int connect_to_peer(client* cli, uint32_t peer_id); int connect_to_peer(client* cli, uint32_t peer_id);
void on_connected(int sock); void on_connected(int sock);
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