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