Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
6d53b2f3
Commit
6d53b2f3
authored
Sep 18, 2014
by
antirez
Browse files
anetPeerToString() refactoring and more explicit checks.
Related to PR #2010.
parent
6d699ea4
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/anet.c
View file @
6d53b2f3
...
@@ -529,12 +529,9 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
...
@@ -529,12 +529,9 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
struct
sockaddr_storage
sa
;
struct
sockaddr_storage
sa
;
socklen_t
salen
=
sizeof
(
sa
);
socklen_t
salen
=
sizeof
(
sa
);
if
(
getpeername
(
fd
,(
struct
sockaddr
*
)
&
sa
,
&
salen
)
==
-
1
)
{
if
(
getpeername
(
fd
,(
struct
sockaddr
*
)
&
sa
,
&
salen
)
==
-
1
)
goto
error
;
if
(
port
)
*
port
=
0
;
if
(
ip_len
==
0
)
goto
error
;
ip
[
0
]
=
'?'
;
ip
[
1
]
=
'\0'
;
return
-
1
;
}
if
(
sa
.
ss_family
==
AF_INET
)
{
if
(
sa
.
ss_family
==
AF_INET
)
{
struct
sockaddr_in
*
s
=
(
struct
sockaddr_in
*
)
&
sa
;
struct
sockaddr_in
*
s
=
(
struct
sockaddr_in
*
)
&
sa
;
if
(
ip
)
inet_ntop
(
AF_INET
,(
void
*
)
&
(
s
->
sin_addr
),
ip
,
ip_len
);
if
(
ip
)
inet_ntop
(
AF_INET
,(
void
*
)
&
(
s
->
sin_addr
),
ip
,
ip_len
);
...
@@ -543,11 +540,25 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
...
@@ -543,11 +540,25 @@ int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
struct
sockaddr_in6
*
s
=
(
struct
sockaddr_in6
*
)
&
sa
;
struct
sockaddr_in6
*
s
=
(
struct
sockaddr_in6
*
)
&
sa
;
if
(
ip
)
inet_ntop
(
AF_INET6
,(
void
*
)
&
(
s
->
sin6_addr
),
ip
,
ip_len
);
if
(
ip
)
inet_ntop
(
AF_INET6
,(
void
*
)
&
(
s
->
sin6_addr
),
ip
,
ip_len
);
if
(
port
)
*
port
=
ntohs
(
s
->
sin6_port
);
if
(
port
)
*
port
=
ntohs
(
s
->
sin6_port
);
}
else
{
}
else
if
(
sa
.
ss_family
==
AF_UNIX
)
{
if
(
ip
)
ip
[
0
]
=
'\0'
;
if
(
ip
)
strncpy
(
ip
,
"unixsocket"
,
ip_len
)
;
if
(
port
)
*
port
=
0
;
if
(
port
)
*
port
=
0
;
}
else
{
goto
error
;
}
}
return
0
;
return
0
;
error:
if
(
ip
)
{
if
(
ip_len
>=
2
)
{
ip
[
0
]
=
'?'
;
ip
[
1
]
=
'\0'
;
}
else
if
(
ip_len
==
1
)
{
ip
[
0
]
=
'\0'
;
}
}
if
(
port
)
*
port
=
0
;
return
-
1
;
}
}
int
anetSockName
(
int
fd
,
char
*
ip
,
size_t
ip_len
,
int
*
port
)
{
int
anetSockName
(
int
fd
,
char
*
ip
,
size_t
ip_len
,
int
*
port
)
{
...
...
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