Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
e1901e97
Commit
e1901e97
authored
Dec 04, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
system/Resolver: sockaddr_to_string() returns std::string()
No GLib memory allocation.
parent
d6941503
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
71 deletions
+43
-71
ClientNew.cxx
src/ClientNew.cxx
+4
-13
ServerSocket.cxx
src/event/ServerSocket.cxx
+11
-22
HttpdOutputPlugin.cxx
src/output/HttpdOutputPlugin.cxx
+3
-8
Resolver.cxx
src/system/Resolver.cxx
+17
-11
Resolver.hxx
src/system/Resolver.hxx
+6
-7
run_resolver.cxx
test/run_resolver.cxx
+2
-10
No files found.
src/ClientNew.cxx
View file @
e1901e97
...
...
@@ -28,8 +28,6 @@
#include "util/Error.hxx"
#include "Log.hxx"
#include <glib.h>
#include <assert.h>
#ifdef WIN32
#include <winsock2.h>
...
...
@@ -63,15 +61,12 @@ client_new(EventLoop &loop, Partition &partition,
int
fd
,
const
struct
sockaddr
*
sa
,
size_t
sa_length
,
int
uid
)
{
static
unsigned
int
next_client_num
;
c
har
*
remote
;
c
onst
auto
remote
=
sockaddr_to_string
(
sa
,
sa_length
)
;
assert
(
fd
>=
0
);
#ifdef HAVE_LIBWRAP
if
(
sa
->
sa_family
!=
AF_UNIX
)
{
char
*
hostaddr
=
sockaddr_to_string
(
sa
,
sa_length
,
IgnoreError
());
// TODO: shall we obtain the program name from argv[0]?
const
char
*
progname
=
"mpd"
;
...
...
@@ -84,14 +79,11 @@ client_new(EventLoop &loop, Partition &partition,
/* tcp wrappers says no */
FormatWarning
(
client_domain
,
"libwrap refused connection (libwrap=%s) from %s"
,
progname
,
hostaddr
);
progname
,
remote
.
c_str
()
);
g_free
(
hostaddr
);
close_socket
(
fd
);
return
;
}
g_free
(
hostaddr
);
}
#endif
/* HAVE_WRAP */
...
...
@@ -109,9 +101,8 @@ client_new(EventLoop &loop, Partition &partition,
client_list
.
Add
(
*
client
);
remote
=
sockaddr_to_string
(
sa
,
sa_length
,
IgnoreError
());
FormatInfo
(
client_domain
,
"[%u] opened from %s"
,
client
->
num
,
remote
);
g_free
(
remote
);
FormatInfo
(
client_domain
,
"[%u] opened from %s"
,
client
->
num
,
remote
.
c_str
());
}
void
...
...
src/event/ServerSocket.cxx
View file @
e1901e97
...
...
@@ -107,7 +107,10 @@ public:
using
SocketMonitor
::
IsDefined
;
using
SocketMonitor
::
Close
;
char
*
ToString
()
const
;
gcc_pure
std
::
string
ToString
()
const
{
return
sockaddr_to_string
(
address
,
address_length
);
}
void
SetFD
(
int
_fd
)
{
SocketMonitor
::
Open
(
_fd
);
...
...
@@ -122,18 +125,6 @@ private:
static
constexpr
Domain
server_socket_domain
(
"server_socket"
);
/**
* Wraper for sockaddr_to_string() which never fails.
*/
char
*
OneServerSocket
::
ToString
()
const
{
char
*
p
=
sockaddr_to_string
(
address
,
address_length
,
IgnoreError
());
if
(
p
==
nullptr
)
p
=
g_strdup
(
"[unknown]"
);
return
p
;
}
static
int
get_remote_uid
(
int
fd
)
{
...
...
@@ -243,23 +234,21 @@ ServerSocket::Open(Error &error)
Error
error2
;
if
(
!
i
.
Open
(
error2
))
{
if
(
good
!=
nullptr
&&
good
->
GetSerial
()
==
i
.
GetSerial
())
{
c
har
*
address_string
=
i
.
ToString
();
c
har
*
good_string
=
good
->
ToString
();
c
onst
auto
address_string
=
i
.
ToString
();
c
onst
auto
good_string
=
good
->
ToString
();
FormatWarning
(
server_socket_domain
,
"bind to '%s' failed: %s "
"(continuing anyway, because "
"binding to '%s' succeeded)"
,
address_string
,
error2
.
GetMessage
(),
good_string
);
g_free
(
address_string
);
g_free
(
good_string
);
address_string
.
c_str
(),
error2
.
GetMessage
(),
good_string
.
c_str
());
}
else
if
(
bad
==
nullptr
)
{
bad
=
&
i
;
c
har
*
address_string
=
i
.
ToString
();
c
onst
auto
address_string
=
i
.
ToString
();
error2
.
FormatPrefix
(
"Failed to bind to '%s': "
,
address_string
);
g_free
(
address_string
);
address_string
.
c_str
());
last_error
=
std
::
move
(
error2
);
}
...
...
src/output/HttpdOutputPlugin.cxx
View file @
e1901e97
...
...
@@ -33,8 +33,6 @@
#include "util/Domain.hxx"
#include "Log.hxx"
#include <glib.h>
#include <assert.h>
#include <sys/types.h>
...
...
@@ -199,8 +197,8 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
#ifdef HAVE_LIBWRAP
if
(
address
.
sa_family
!=
AF_UNIX
)
{
c
har
*
hostaddr
=
sockaddr_to_string
(
&
address
,
address_length
,
IgnoreError
()
);
c
onst
auto
hostaddr
=
sockaddr_to_string
(
&
address
,
address_length
);
// TODO: shall we obtain the program name from argv[0]?
const
char
*
progname
=
"mpd"
;
...
...
@@ -213,13 +211,10 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
/* tcp wrappers says no */
FormatWarning
(
httpd_output_domain
,
"libwrap refused connection (libwrap=%s) from %s"
,
progname
,
hostaddr
);
g_free
(
hostaddr
);
progname
,
hostaddr
.
c_str
());
close_socket
(
fd
);
return
;
}
g_free
(
hostaddr
);
}
#else
(
void
)
address
;
...
...
src/system/Resolver.cxx
View file @
e1901e97
...
...
@@ -44,17 +44,17 @@
const
Domain
resolver_domain
(
"resolver"
);
char
*
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
,
Error
&
error
)
std
::
string
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
)
{
#ifdef HAVE_UN
if
(
sa
->
sa_family
==
AF_UNIX
)
{
/* return path of UNIX domain sockets */
const
sockaddr_un
&
s_un
=
*
(
const
sockaddr_un
*
)
sa
;
if
(
length
<
sizeof
(
s_un
)
||
s_un
.
sun_path
[
0
]
==
0
)
return
g_strdup
(
"local"
)
;
return
"local"
;
return
g_strdup
(
s_un
.
sun_path
)
;
return
s_un
.
sun_path
;
}
#endif
...
...
@@ -83,17 +83,23 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error)
ret
=
getnameinfo
(
sa
,
length
,
host
,
sizeof
(
host
),
serv
,
sizeof
(
serv
),
NI_NUMERICHOST
|
NI_NUMERICSERV
);
if
(
ret
!=
0
)
{
error
.
Set
(
resolver_domain
,
ret
,
gai_strerror
(
ret
));
return
NULL
;
}
if
(
ret
!=
0
)
return
"unknown"
;
#ifdef HAVE_IPV6
if
(
strchr
(
host
,
':'
)
!=
NULL
)
return
g_strconcat
(
"["
,
host
,
"]:"
,
serv
,
NULL
);
if
(
strchr
(
host
,
':'
)
!=
NULL
)
{
std
::
string
result
(
"["
);
result
.
append
(
host
);
result
.
append
(
"]:"
);
result
.
append
(
serv
);
return
result
;
}
#endif
return
g_strconcat
(
host
,
":"
,
serv
,
NULL
);
std
::
string
result
(
host
);
result
.
push_back
(
':'
);
result
.
append
(
serv
);
return
result
;
}
struct
addrinfo
*
...
...
src/system/Resolver.hxx
View file @
e1901e97
...
...
@@ -22,6 +22,8 @@
#include "Compiler.h"
#include <string>
#include <stddef.h>
struct
sockaddr
;
...
...
@@ -33,17 +35,14 @@ extern const Domain resolver_domain;
/**
* Converts the specified socket address into a string in the form
* "IP:PORT". The return value must be freed with g_free() when you
* don't need it anymore.
* "IP:PORT".
*
* @param sa the sockaddr struct
* @param length the length of #sa in bytes
* @param error location to store the error occurring, or NULL to
* ignore errors
*/
gcc_
malloc
char
*
sockaddr_to_string
(
const
sockaddr
*
sa
,
size_t
length
,
Error
&
error
);
gcc_
pure
std
::
string
sockaddr_to_string
(
const
sockaddr
*
sa
,
size_t
length
);
/**
* Resolve a specification in the form "host", "host:port",
...
...
test/run_resolver.cxx
View file @
e1901e97
...
...
@@ -51,16 +51,8 @@ int main(int argc, char **argv)
}
for
(
const
struct
addrinfo
*
i
=
ai
;
i
!=
NULL
;
i
=
i
->
ai_next
)
{
char
*
p
=
sockaddr_to_string
(
i
->
ai_addr
,
i
->
ai_addrlen
,
error
);
if
(
p
==
NULL
)
{
freeaddrinfo
(
ai
);
LogError
(
error
);
return
EXIT_FAILURE
;
}
g_print
(
"%s
\n
"
,
p
);
g_free
(
p
);
const
auto
s
=
sockaddr_to_string
(
i
->
ai_addr
,
i
->
ai_addrlen
);
g_print
(
"%s
\n
"
,
s
.
c_str
());
}
freeaddrinfo
(
ai
);
...
...
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