Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
nat_traversal
Commits
b8ac64f0
Commit
b8ac64f0
authored
May 17, 2017
by
Fong
Browse files
remove infinite loop
parent
2003e1dc
Changes
3
Hide whitespace changes
Inline
Side-by-side
main.c
View file @
b8ac64f0
...
@@ -97,11 +97,14 @@ int main(int argc, char** argv)
...
@@ -97,11 +97,14 @@ int main(int argc, char** argv)
server_addr
.
sin_port
=
htons
(
DEFAULT_SERVER_PORT
);
server_addr
.
sin_port
=
htons
(
DEFAULT_SERVER_PORT
);
client
c
;
client
c
;
if
(
init
(
self
,
server_addr
,
&
c
))
{
if
(
enroll
(
self
,
server_addr
,
&
c
)
<
0
)
{
printf
(
"
init
failed
\n
"
);
printf
(
"failed
to enroll
\n
"
);
return
-
1
;
return
-
1
;
}
}
printf
(
"enroll successfully, ID: %d
\n
"
,
c
.
id
);
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
);
...
@@ -112,8 +115,6 @@ int main(int argc, char** argv)
...
@@ -112,8 +115,6 @@ int main(int argc, char** argv)
}
}
}
}
for
(;
;)
{
pthread_join
(
tid
,
NULL
);
}
return
0
;
return
0
;
}
}
nat_traversal.c
View file @
b8ac64f0
...
@@ -22,8 +22,8 @@
...
@@ -22,8 +22,8 @@
// file scope variables
// file scope variables
static
int
nums
[
MAX_PORT
-
MIN_PORT
];
static
int
nums
[
MAX_PORT
-
MIN_PORT
];
static
int
send_to_punch_server
(
client
*
c
,
int
flags
)
{
static
int
send_to_punch_server
(
client
*
c
)
{
int
n
=
send
(
c
->
sfd
,
c
->
buf
,
c
->
msg_buf
-
c
->
buf
,
flags
);
int
n
=
send
(
c
->
sfd
,
c
->
buf
,
c
->
msg_buf
-
c
->
buf
,
0
);
c
->
msg_buf
=
c
->
buf
;
c
->
msg_buf
=
c
->
buf
;
return
n
;
return
n
;
...
@@ -32,7 +32,7 @@ static int send_to_punch_server(client* c, int flags) {
...
@@ -32,7 +32,7 @@ static int send_to_punch_server(client* c, int flags) {
static
int
get_peer_info
(
client
*
cli
,
uint32_t
peer_id
,
struct
peer_info
*
peer
)
{
static
int
get_peer_info
(
client
*
cli
,
uint32_t
peer_id
,
struct
peer_info
*
peer
)
{
cli
->
msg_buf
=
encode16
(
cli
->
msg_buf
,
GetPeerInfo
);
cli
->
msg_buf
=
encode16
(
cli
->
msg_buf
,
GetPeerInfo
);
cli
->
msg_buf
=
encode32
(
cli
->
msg_buf
,
peer_id
);
cli
->
msg_buf
=
encode32
(
cli
->
msg_buf
,
peer_id
);
if
(
-
1
==
send_to_punch_server
(
cli
,
0
))
{
if
(
-
1
==
send_to_punch_server
(
cli
))
{
return
-
1
;
return
-
1
;
}
}
...
@@ -192,7 +192,7 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf
...
@@ -192,7 +192,7 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf
// hole punched, notify peer
// hole punched, notify peer
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
NotifyPeer
);
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
NotifyPeer
);
c
->
msg_buf
=
encode32
(
c
->
msg_buf
,
peer_id
);
c
->
msg_buf
=
encode32
(
c
->
msg_buf
,
peer_id
);
send_to_punch_server
(
c
,
0
);
send_to_punch_server
(
c
);
struct
timeval
timeout
=
{
10
,
0
};
struct
timeval
timeout
=
{
10
,
0
};
int
fd
=
wait_for_peer
(
holes
,
i
,
&
timeout
);
int
fd
=
wait_for_peer
(
holes
,
i
,
&
timeout
);
...
@@ -284,7 +284,7 @@ static void* server_notify_handler(void* data) {
...
@@ -284,7 +284,7 @@ static void* server_notify_handler(void* data) {
return
NULL
;
return
NULL
;
}
}
int
init
(
struct
peer_info
self
,
struct
sockaddr_in
punch_server
,
client
*
c
)
{
int
enroll
(
struct
peer_info
self
,
struct
sockaddr_in
punch_server
,
client
*
c
)
{
int
i
,
temp
;
int
i
,
temp
;
for
(
i
=
0
,
temp
=
MIN_PORT
;
temp
<=
MAX_PORT
;
i
++
,
temp
++
)
{
for
(
i
=
0
,
temp
=
MIN_PORT
;
temp
<=
MAX_PORT
;
i
++
,
temp
++
)
{
nums
[
i
]
=
temp
;
nums
[
i
]
=
temp
;
...
@@ -305,8 +305,7 @@ int init(struct peer_info self, struct sockaddr_in punch_server, client* c) {
...
@@ -305,8 +305,7 @@ int init(struct peer_info self, struct sockaddr_in punch_server, client* c) {
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
self
.
port
);
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
self
.
port
);
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
self
.
type
);
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
self
.
type
);
if
(
-
1
==
send_to_punch_server
(
c
,
0
))
{
if
(
-
1
==
send_to_punch_server
(
c
))
{
printf
(
"failed to enroll
\n
"
);
return
-
1
;
return
-
1
;
}
}
...
@@ -318,19 +317,21 @@ int init(struct peer_info self, struct sockaddr_in punch_server, client* c) {
...
@@ -318,19 +317,21 @@ int init(struct peer_info self, struct sockaddr_in punch_server, client* c) {
setsockopt
(
server_sock
,
SOL_SOCKET
,
SO_RCVTIMEO
,
(
const
char
*
)
&
tv
,
sizeof
(
tv
));
setsockopt
(
server_sock
,
SOL_SOCKET
,
SO_RCVTIMEO
,
(
const
char
*
)
&
tv
,
sizeof
(
tv
));
int
n
=
recv
(
server_sock
,
&
peer_id
,
sizeof
(
uint32_t
),
0
);
int
n
=
recv
(
server_sock
,
&
peer_id
,
sizeof
(
uint32_t
),
0
);
if
(
n
!=
sizeof
(
uint32_t
))
{
if
(
n
!=
sizeof
(
uint32_t
))
{
printf
(
"failed to enroll
\n
"
);
return
-
1
;
return
-
1
;
}
}
printf
(
"enroll successfully, ID: %d
\n
"
,
ntohl
(
peer_id
)
)
;
c
->
id
=
ntohl
(
peer_id
);
// wait for message from punch server in another thread
return
0
;
}
pthread_t
wait_for_command
(
int
server_sock
)
{
// 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
);
//pthread_join(thread_id, NULL);
return
thread_id
;
return
0
;
}
}
void
on_connected
(
int
sock
)
{
void
on_connected
(
int
sock
)
{
...
...
nat_traversal.h
View file @
b8ac64f0
...
@@ -26,6 +26,7 @@ enum msg_type {
...
@@ -26,6 +26,7 @@ enum msg_type {
};
};
// public functions
// public functions
int
init
(
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
);
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
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment