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
Show 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 @@
...
@@ -28,8 +28,6 @@
#include "util/Error.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
#include "Log.hxx"
#include <glib.h>
#include <assert.h>
#include <assert.h>
#ifdef WIN32
#ifdef WIN32
#include <winsock2.h>
#include <winsock2.h>
...
@@ -63,15 +61,12 @@ client_new(EventLoop &loop, Partition &partition,
...
@@ -63,15 +61,12 @@ client_new(EventLoop &loop, Partition &partition,
int
fd
,
const
struct
sockaddr
*
sa
,
size_t
sa_length
,
int
uid
)
int
fd
,
const
struct
sockaddr
*
sa
,
size_t
sa_length
,
int
uid
)
{
{
static
unsigned
int
next_client_num
;
static
unsigned
int
next_client_num
;
c
har
*
remote
;
c
onst
auto
remote
=
sockaddr_to_string
(
sa
,
sa_length
)
;
assert
(
fd
>=
0
);
assert
(
fd
>=
0
);
#ifdef HAVE_LIBWRAP
#ifdef HAVE_LIBWRAP
if
(
sa
->
sa_family
!=
AF_UNIX
)
{
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]?
// TODO: shall we obtain the program name from argv[0]?
const
char
*
progname
=
"mpd"
;
const
char
*
progname
=
"mpd"
;
...
@@ -84,14 +79,11 @@ client_new(EventLoop &loop, Partition &partition,
...
@@ -84,14 +79,11 @@ client_new(EventLoop &loop, Partition &partition,
/* tcp wrappers says no */
/* tcp wrappers says no */
FormatWarning
(
client_domain
,
FormatWarning
(
client_domain
,
"libwrap refused connection (libwrap=%s) from %s"
,
"libwrap refused connection (libwrap=%s) from %s"
,
progname
,
hostaddr
);
progname
,
remote
.
c_str
()
);
g_free
(
hostaddr
);
close_socket
(
fd
);
close_socket
(
fd
);
return
;
return
;
}
}
g_free
(
hostaddr
);
}
}
#endif
/* HAVE_WRAP */
#endif
/* HAVE_WRAP */
...
@@ -109,9 +101,8 @@ client_new(EventLoop &loop, Partition &partition,
...
@@ -109,9 +101,8 @@ client_new(EventLoop &loop, Partition &partition,
client_list
.
Add
(
*
client
);
client_list
.
Add
(
*
client
);
remote
=
sockaddr_to_string
(
sa
,
sa_length
,
IgnoreError
());
FormatInfo
(
client_domain
,
"[%u] opened from %s"
,
FormatInfo
(
client_domain
,
"[%u] opened from %s"
,
client
->
num
,
remote
);
client
->
num
,
remote
.
c_str
());
g_free
(
remote
);
}
}
void
void
...
...
src/event/ServerSocket.cxx
View file @
e1901e97
...
@@ -107,7 +107,10 @@ public:
...
@@ -107,7 +107,10 @@ public:
using
SocketMonitor
::
IsDefined
;
using
SocketMonitor
::
IsDefined
;
using
SocketMonitor
::
Close
;
using
SocketMonitor
::
Close
;
char
*
ToString
()
const
;
gcc_pure
std
::
string
ToString
()
const
{
return
sockaddr_to_string
(
address
,
address_length
);
}
void
SetFD
(
int
_fd
)
{
void
SetFD
(
int
_fd
)
{
SocketMonitor
::
Open
(
_fd
);
SocketMonitor
::
Open
(
_fd
);
...
@@ -122,18 +125,6 @@ private:
...
@@ -122,18 +125,6 @@ private:
static
constexpr
Domain
server_socket_domain
(
"server_socket"
);
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
static
int
get_remote_uid
(
int
fd
)
get_remote_uid
(
int
fd
)
{
{
...
@@ -243,23 +234,21 @@ ServerSocket::Open(Error &error)
...
@@ -243,23 +234,21 @@ ServerSocket::Open(Error &error)
Error
error2
;
Error
error2
;
if
(
!
i
.
Open
(
error2
))
{
if
(
!
i
.
Open
(
error2
))
{
if
(
good
!=
nullptr
&&
good
->
GetSerial
()
==
i
.
GetSerial
())
{
if
(
good
!=
nullptr
&&
good
->
GetSerial
()
==
i
.
GetSerial
())
{
c
har
*
address_string
=
i
.
ToString
();
c
onst
auto
address_string
=
i
.
ToString
();
c
har
*
good_string
=
good
->
ToString
();
c
onst
auto
good_string
=
good
->
ToString
();
FormatWarning
(
server_socket_domain
,
FormatWarning
(
server_socket_domain
,
"bind to '%s' failed: %s "
"bind to '%s' failed: %s "
"(continuing anyway, because "
"(continuing anyway, because "
"binding to '%s' succeeded)"
,
"binding to '%s' succeeded)"
,
address_string
,
error2
.
GetMessage
(),
address_string
.
c_str
(),
good_string
);
error2
.
GetMessage
(),
g_free
(
address_string
);
good_string
.
c_str
());
g_free
(
good_string
);
}
else
if
(
bad
==
nullptr
)
{
}
else
if
(
bad
==
nullptr
)
{
bad
=
&
i
;
bad
=
&
i
;
c
har
*
address_string
=
i
.
ToString
();
c
onst
auto
address_string
=
i
.
ToString
();
error2
.
FormatPrefix
(
"Failed to bind to '%s': "
,
error2
.
FormatPrefix
(
"Failed to bind to '%s': "
,
address_string
);
address_string
.
c_str
());
g_free
(
address_string
);
last_error
=
std
::
move
(
error2
);
last_error
=
std
::
move
(
error2
);
}
}
...
...
src/output/HttpdOutputPlugin.cxx
View file @
e1901e97
...
@@ -33,8 +33,6 @@
...
@@ -33,8 +33,6 @@
#include "util/Domain.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
#include "Log.hxx"
#include <glib.h>
#include <assert.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/types.h>
...
@@ -199,8 +197,8 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
...
@@ -199,8 +197,8 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
#ifdef HAVE_LIBWRAP
#ifdef HAVE_LIBWRAP
if
(
address
.
sa_family
!=
AF_UNIX
)
{
if
(
address
.
sa_family
!=
AF_UNIX
)
{
c
har
*
hostaddr
=
sockaddr_to_string
(
&
address
,
address_length
,
c
onst
auto
hostaddr
=
sockaddr_to_string
(
&
address
,
IgnoreError
()
);
address_length
);
// TODO: shall we obtain the program name from argv[0]?
// TODO: shall we obtain the program name from argv[0]?
const
char
*
progname
=
"mpd"
;
const
char
*
progname
=
"mpd"
;
...
@@ -213,13 +211,10 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
...
@@ -213,13 +211,10 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
/* tcp wrappers says no */
/* tcp wrappers says no */
FormatWarning
(
httpd_output_domain
,
FormatWarning
(
httpd_output_domain
,
"libwrap refused connection (libwrap=%s) from %s"
,
"libwrap refused connection (libwrap=%s) from %s"
,
progname
,
hostaddr
);
progname
,
hostaddr
.
c_str
());
g_free
(
hostaddr
);
close_socket
(
fd
);
close_socket
(
fd
);
return
;
return
;
}
}
g_free
(
hostaddr
);
}
}
#else
#else
(
void
)
address
;
(
void
)
address
;
...
...
src/system/Resolver.cxx
View file @
e1901e97
...
@@ -44,17 +44,17 @@
...
@@ -44,17 +44,17 @@
const
Domain
resolver_domain
(
"resolver"
);
const
Domain
resolver_domain
(
"resolver"
);
char
*
std
::
string
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
,
Error
&
error
)
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
)
{
{
#ifdef HAVE_UN
#ifdef HAVE_UN
if
(
sa
->
sa_family
==
AF_UNIX
)
{
if
(
sa
->
sa_family
==
AF_UNIX
)
{
/* return path of UNIX domain sockets */
/* return path of UNIX domain sockets */
const
sockaddr_un
&
s_un
=
*
(
const
sockaddr_un
*
)
sa
;
const
sockaddr_un
&
s_un
=
*
(
const
sockaddr_un
*
)
sa
;
if
(
length
<
sizeof
(
s_un
)
||
s_un
.
sun_path
[
0
]
==
0
)
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
#endif
...
@@ -83,17 +83,23 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error)
...
@@ -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
),
ret
=
getnameinfo
(
sa
,
length
,
host
,
sizeof
(
host
),
serv
,
sizeof
(
serv
),
NI_NUMERICHOST
|
NI_NUMERICSERV
);
NI_NUMERICHOST
|
NI_NUMERICSERV
);
if
(
ret
!=
0
)
{
if
(
ret
!=
0
)
error
.
Set
(
resolver_domain
,
ret
,
gai_strerror
(
ret
));
return
"unknown"
;
return
NULL
;
}
#ifdef HAVE_IPV6
#ifdef HAVE_IPV6
if
(
strchr
(
host
,
':'
)
!=
NULL
)
if
(
strchr
(
host
,
':'
)
!=
NULL
)
{
return
g_strconcat
(
"["
,
host
,
"]:"
,
serv
,
NULL
);
std
::
string
result
(
"["
);
result
.
append
(
host
);
result
.
append
(
"]:"
);
result
.
append
(
serv
);
return
result
;
}
#endif
#endif
return
g_strconcat
(
host
,
":"
,
serv
,
NULL
);
std
::
string
result
(
host
);
result
.
push_back
(
':'
);
result
.
append
(
serv
);
return
result
;
}
}
struct
addrinfo
*
struct
addrinfo
*
...
...
src/system/Resolver.hxx
View file @
e1901e97
...
@@ -22,6 +22,8 @@
...
@@ -22,6 +22,8 @@
#include "Compiler.h"
#include "Compiler.h"
#include <string>
#include <stddef.h>
#include <stddef.h>
struct
sockaddr
;
struct
sockaddr
;
...
@@ -33,17 +35,14 @@ extern const Domain resolver_domain;
...
@@ -33,17 +35,14 @@ extern const Domain resolver_domain;
/**
/**
* Converts the specified socket address into a string in the form
* Converts the specified socket address into a string in the form
* "IP:PORT". The return value must be freed with g_free() when you
* "IP:PORT".
* don't need it anymore.
*
*
* @param sa the sockaddr struct
* @param sa the sockaddr struct
* @param length the length of #sa in bytes
* @param length the length of #sa in bytes
* @param error location to store the error occurring, or NULL to
* ignore errors
*/
*/
gcc_
malloc
gcc_
pure
char
*
std
::
string
sockaddr_to_string
(
const
sockaddr
*
sa
,
size_t
length
,
Error
&
error
);
sockaddr_to_string
(
const
sockaddr
*
sa
,
size_t
length
);
/**
/**
* Resolve a specification in the form "host", "host:port",
* 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)
...
@@ -51,16 +51,8 @@ int main(int argc, char **argv)
}
}
for
(
const
struct
addrinfo
*
i
=
ai
;
i
!=
NULL
;
i
=
i
->
ai_next
)
{
for
(
const
struct
addrinfo
*
i
=
ai
;
i
!=
NULL
;
i
=
i
->
ai_next
)
{
char
*
p
=
sockaddr_to_string
(
i
->
ai_addr
,
i
->
ai_addrlen
,
const
auto
s
=
sockaddr_to_string
(
i
->
ai_addr
,
i
->
ai_addrlen
);
error
);
g_print
(
"%s
\n
"
,
s
.
c_str
());
if
(
p
==
NULL
)
{
freeaddrinfo
(
ai
);
LogError
(
error
);
return
EXIT_FAILURE
;
}
g_print
(
"%s
\n
"
,
p
);
g_free
(
p
);
}
}
freeaddrinfo
(
ai
);
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