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)
server_addr
.
sin_port
=
htons
(
DEFAULT_SERVER_PORT
);
client
c
;
if
(
init
(
self
,
server_addr
,
&
c
))
{
printf
(
"
init
failed
\n
"
);
if
(
enroll
(
self
,
server_addr
,
&
c
)
<
0
)
{
printf
(
"failed
to enroll
\n
"
);
return
-
1
;
}
printf
(
"enroll successfully, ID: %d
\n
"
,
c
.
id
);
pthread_t
tid
=
wait_for_command
(
c
.
sfd
);
if
(
peer_id
)
{
printf
(
"connecting to peer %d
\n
"
,
peer_id
);
...
...
@@ -112,8 +115,6 @@ int main(int argc, char** argv)
}
}
for
(;
;)
{
}
pthread_join
(
tid
,
NULL
);
return
0
;
}
nat_traversal.c
View file @
b8ac64f0
...
...
@@ -22,8 +22,8 @@
// file scope variables
static
int
nums
[
MAX_PORT
-
MIN_PORT
];
static
int
send_to_punch_server
(
client
*
c
,
int
flags
)
{
int
n
=
send
(
c
->
sfd
,
c
->
buf
,
c
->
msg_buf
-
c
->
buf
,
flags
);
static
int
send_to_punch_server
(
client
*
c
)
{
int
n
=
send
(
c
->
sfd
,
c
->
buf
,
c
->
msg_buf
-
c
->
buf
,
0
);
c
->
msg_buf
=
c
->
buf
;
return
n
;
...
...
@@ -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
)
{
cli
->
msg_buf
=
encode16
(
cli
->
msg_buf
,
GetPeerInfo
);
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
;
}
...
...
@@ -192,7 +192,7 @@ static int connect_to_symmetric_nat(client* c, uint32_t peer_id, struct peer_inf
// hole punched, notify peer
c
->
msg_buf
=
encode16
(
c
->
msg_buf
,
NotifyPeer
);
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
};
int
fd
=
wait_for_peer
(
holes
,
i
,
&
timeout
);
...
...
@@ -284,7 +284,7 @@ static void* server_notify_handler(void* data) {
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
;
for
(
i
=
0
,
temp
=
MIN_PORT
;
temp
<=
MAX_PORT
;
i
++
,
temp
++
)
{
nums
[
i
]
=
temp
;
...
...
@@ -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
.
type
);
if
(
-
1
==
send_to_punch_server
(
c
,
0
))
{
printf
(
"failed to enroll
\n
"
);
if
(
-
1
==
send_to_punch_server
(
c
))
{
return
-
1
;
}
...
...
@@ -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
));
int
n
=
recv
(
server_sock
,
&
peer_id
,
sizeof
(
uint32_t
),
0
);
if
(
n
!=
sizeof
(
uint32_t
))
{
printf
(
"failed to enroll
\n
"
);
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_create
(
&
thread_id
,
NULL
,
server_notify_handler
,
(
void
*
)
&
server_sock
);
//pthread_join(thread_id, NULL);
return
0
;
return
thread_id
;
}
void
on_connected
(
int
sock
)
{
...
...
nat_traversal.h
View file @
b8ac64f0
...
...
@@ -26,6 +26,7 @@ enum msg_type {
};
// 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
);
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