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
4e30e747
Commit
4e30e747
authored
Feb 10, 2015
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/SocketAddress: light wrapper for struct sockaddr
parent
42890b9a
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
201 additions
and
67 deletions
+201
-67
Makefile.am
Makefile.am
+1
-0
Listen.cxx
src/Listen.cxx
+3
-3
Client.hxx
src/client/Client.hxx
+2
-2
ClientNew.cxx
src/client/ClientNew.cxx
+4
-3
ServerSocket.cxx
src/event/ServerSocket.cxx
+21
-22
ServerSocket.hxx
src/event/ServerSocket.hxx
+3
-4
Resolver.cxx
src/net/Resolver.cxx
+12
-9
Resolver.hxx
src/net/Resolver.hxx
+2
-2
SocketAddress.cxx
src/net/SocketAddress.cxx
+38
-0
SocketAddress.hxx
src/net/SocketAddress.hxx
+103
-0
SocketUtil.cxx
src/net/SocketUtil.cxx
+3
-9
SocketUtil.hxx
src/net/SocketUtil.hxx
+2
-4
HttpdInternal.hxx
src/output/plugins/httpd/HttpdInternal.hxx
+1
-2
HttpdOutputPlugin.cxx
src/output/plugins/httpd/HttpdOutputPlugin.cxx
+4
-6
run_resolver.cxx
test/run_resolver.cxx
+2
-1
No files found.
Makefile.am
View file @
4e30e747
...
@@ -421,6 +421,7 @@ libthread_a_SOURCES = \
...
@@ -421,6 +421,7 @@ libthread_a_SOURCES = \
libnet_a_SOURCES
=
\
libnet_a_SOURCES
=
\
src/net/Resolver.cxx src/net/Resolver.hxx
\
src/net/Resolver.cxx src/net/Resolver.hxx
\
src/net/SocketAddress.cxx src/net/SocketAddress.hxx
\
src/net/SocketUtil.cxx src/net/SocketUtil.hxx
\
src/net/SocketUtil.cxx src/net/SocketUtil.hxx
\
src/net/SocketError.cxx src/net/SocketError.hxx
src/net/SocketError.cxx src/net/SocketError.hxx
...
...
src/Listen.cxx
View file @
4e30e747
...
@@ -23,6 +23,7 @@
...
@@ -23,6 +23,7 @@
#include "config/Param.hxx"
#include "config/Param.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
#include "config/ConfigOption.hxx"
#include "net/SocketAddress.hxx"
#include "event/ServerSocket.hxx"
#include "event/ServerSocket.hxx"
#include "util/Error.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Domain.hxx"
...
@@ -48,10 +49,9 @@ public:
...
@@ -48,10 +49,9 @@ public:
:
ServerSocket
(
_loop
),
partition
(
_partition
)
{}
:
ServerSocket
(
_loop
),
partition
(
_partition
)
{}
private
:
private
:
void
OnAccept
(
int
fd
,
const
sockaddr
&
address
,
void
OnAccept
(
int
fd
,
SocketAddress
address
,
int
uid
)
override
{
size_t
address_length
,
int
uid
)
override
{
client_new
(
GetEventLoop
(),
partition
,
client_new
(
GetEventLoop
(),
partition
,
fd
,
&
address
,
address_length
,
uid
);
fd
,
address
,
uid
);
}
}
};
};
...
...
src/client/Client.hxx
View file @
4e30e747
...
@@ -36,7 +36,7 @@
...
@@ -36,7 +36,7 @@
#include <stddef.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdarg.h>
struct
sockaddr
;
class
SocketAddress
;
class
EventLoop
;
class
EventLoop
;
class
Path
;
class
Path
;
struct
Partition
;
struct
Partition
;
...
@@ -204,7 +204,7 @@ void client_manager_init(void);
...
@@ -204,7 +204,7 @@ void client_manager_init(void);
void
void
client_new
(
EventLoop
&
loop
,
Partition
&
partition
,
client_new
(
EventLoop
&
loop
,
Partition
&
partition
,
int
fd
,
const
sockaddr
*
sa
,
size_t
sa_length
,
int
uid
);
int
fd
,
SocketAddress
address
,
int
uid
);
/**
/**
* Write a C string to the client.
* Write a C string to the client.
...
...
src/client/ClientNew.cxx
View file @
4e30e747
...
@@ -23,6 +23,7 @@
...
@@ -23,6 +23,7 @@
#include "Partition.hxx"
#include "Partition.hxx"
#include "Instance.hxx"
#include "Instance.hxx"
#include "system/fd_util.h"
#include "system/fd_util.h"
#include "net/SocketAddress.hxx"
#include "net/Resolver.hxx"
#include "net/Resolver.hxx"
#include "Permission.hxx"
#include "Permission.hxx"
#include "util/Error.hxx"
#include "util/Error.hxx"
...
@@ -58,15 +59,15 @@ Client::Client(EventLoop &_loop, Partition &_partition,
...
@@ -58,15 +59,15 @@ Client::Client(EventLoop &_loop, Partition &_partition,
void
void
client_new
(
EventLoop
&
loop
,
Partition
&
partition
,
client_new
(
EventLoop
&
loop
,
Partition
&
partition
,
int
fd
,
const
struct
sockaddr
*
sa
,
size_t
sa_length
,
int
uid
)
int
fd
,
SocketAddress
address
,
int
uid
)
{
{
static
unsigned
int
next_client_num
;
static
unsigned
int
next_client_num
;
const
auto
remote
=
sockaddr_to_string
(
sa
,
sa_length
);
const
auto
remote
=
sockaddr_to_string
(
address
);
assert
(
fd
>=
0
);
assert
(
fd
>=
0
);
#ifdef HAVE_LIBWRAP
#ifdef HAVE_LIBWRAP
if
(
sa
->
sa_family
!=
AF_UNIX
)
{
if
(
address
.
GetFamily
()
!=
AF_UNIX
)
{
// 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"
;
...
...
src/event/ServerSocket.cxx
View file @
4e30e747
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include "config.h"
#include "config.h"
#include "ServerSocket.hxx"
#include "ServerSocket.hxx"
#include "net/SocketAddress.hxx"
#include "net/SocketUtil.hxx"
#include "net/SocketUtil.hxx"
#include "net/SocketError.hxx"
#include "net/SocketError.hxx"
#include "net/Resolver.hxx"
#include "net/Resolver.hxx"
...
@@ -59,29 +60,28 @@ class OneServerSocket final : private SocketMonitor {
...
@@ -59,29 +60,28 @@ class OneServerSocket final : private SocketMonitor {
AllocatedPath
path
;
AllocatedPath
path
;
size_t
address_length
;
SocketAddress
address
;
struct
sockaddr
*
address
;
public
:
public
:
OneServerSocket
(
EventLoop
&
_loop
,
ServerSocket
&
_parent
,
OneServerSocket
(
EventLoop
&
_loop
,
ServerSocket
&
_parent
,
unsigned
_serial
,
unsigned
_serial
,
const
struct
sockaddr
*
_address
,
SocketAddress
_address
)
size_t
_address_length
)
:
SocketMonitor
(
_loop
),
:
SocketMonitor
(
_loop
),
parent
(
_parent
),
serial
(
_serial
),
parent
(
_parent
),
serial
(
_serial
),
path
(
AllocatedPath
::
Null
()),
path
(
AllocatedPath
::
Null
()),
address_length
(
_address_length
),
address
((
sockaddr
*
)
xmemdup
(
_address
.
GetAddress
(),
address
((
sockaddr
*
)
xmemdup
(
_address
,
_address_length
))
_address
.
GetSize
()),
_address
.
GetSize
())
{
{
assert
(
_address
!=
nullptr
);
assert
(
!
_address
.
IsNull
()
);
assert
(
_address
_length
>
0
);
assert
(
_address
.
GetSize
()
>
0
);
}
}
OneServerSocket
(
const
OneServerSocket
&
other
)
=
delete
;
OneServerSocket
(
const
OneServerSocket
&
other
)
=
delete
;
OneServerSocket
&
operator
=
(
const
OneServerSocket
&
other
)
=
delete
;
OneServerSocket
&
operator
=
(
const
OneServerSocket
&
other
)
=
delete
;
~
OneServerSocket
()
{
~
OneServerSocket
()
{
free
(
address
);
free
(
const_cast
<
struct
sockaddr
*>
(
address
.
GetAddress
())
);
if
(
IsDefined
())
if
(
IsDefined
())
Close
();
Close
();
...
@@ -104,7 +104,7 @@ public:
...
@@ -104,7 +104,7 @@ public:
gcc_pure
gcc_pure
std
::
string
ToString
()
const
{
std
::
string
ToString
()
const
{
return
sockaddr_to_string
(
address
,
address_length
);
return
sockaddr_to_string
(
address
);
}
}
void
SetFD
(
int
_fd
)
{
void
SetFD
(
int
_fd
)
{
...
@@ -168,8 +168,8 @@ OneServerSocket::Accept()
...
@@ -168,8 +168,8 @@ OneServerSocket::Accept()
}
}
parent
.
OnAccept
(
peer_fd
,
parent
.
OnAccept
(
peer_fd
,
(
const
sockaddr
&
)
peer_address
,
{
(
const
sockaddr
*
)
&
peer_address
,
socklen_t
(
peer_address_length
)
}
,
peer_address_length
,
get_remote_uid
(
peer_fd
));
get_remote_uid
(
peer_fd
));
}
}
bool
bool
...
@@ -184,9 +184,9 @@ OneServerSocket::Open(Error &error)
...
@@ -184,9 +184,9 @@ OneServerSocket::Open(Error &error)
{
{
assert
(
!
IsDefined
());
assert
(
!
IsDefined
());
int
_fd
=
socket_bind_listen
(
address
->
sa_family
,
int
_fd
=
socket_bind_listen
(
address
.
GetFamily
()
,
SOCK_STREAM
,
0
,
SOCK_STREAM
,
0
,
address
,
address_length
,
5
,
address
,
5
,
error
);
error
);
if
(
_fd
<
0
)
if
(
_fd
<
0
)
return
false
;
return
false
;
...
@@ -280,10 +280,10 @@ ServerSocket::Close()
...
@@ -280,10 +280,10 @@ ServerSocket::Close()
}
}
OneServerSocket
&
OneServerSocket
&
ServerSocket
::
AddAddress
(
const
sockaddr
&
address
,
size_t
address_length
)
ServerSocket
::
AddAddress
(
SocketAddress
address
)
{
{
sockets
.
emplace_back
(
loop
,
*
this
,
next_serial
,
sockets
.
emplace_back
(
loop
,
*
this
,
next_serial
,
&
address
,
address_length
);
address
);
return
sockets
.
back
();
return
sockets
.
back
();
}
}
...
@@ -302,8 +302,7 @@ ServerSocket::AddFD(int fd, Error &error)
...
@@ -302,8 +302,7 @@ ServerSocket::AddFD(int fd, Error &error)
return
false
;
return
false
;
}
}
OneServerSocket
&
s
=
AddAddress
((
const
sockaddr
&
)
address
,
OneServerSocket
&
s
=
AddAddress
({(
const
sockaddr
*
)
&
address
,
address_length
});
address_length
);
s
.
SetFD
(
fd
);
s
.
SetFD
(
fd
);
return
true
;
return
true
;
...
@@ -320,7 +319,7 @@ ServerSocket::AddPortIPv4(unsigned port)
...
@@ -320,7 +319,7 @@ ServerSocket::AddPortIPv4(unsigned port)
sin
.
sin_family
=
AF_INET
;
sin
.
sin_family
=
AF_INET
;
sin
.
sin_addr
.
s_addr
=
INADDR_ANY
;
sin
.
sin_addr
.
s_addr
=
INADDR_ANY
;
AddAddress
(
(
const
sockaddr
&
)
sin
,
sizeof
(
sin
)
);
AddAddress
(
{(
const
sockaddr
*
)
&
sin
,
sizeof
(
sin
)}
);
}
}
#ifdef HAVE_IPV6
#ifdef HAVE_IPV6
...
@@ -333,7 +332,7 @@ ServerSocket::AddPortIPv6(unsigned port)
...
@@ -333,7 +332,7 @@ ServerSocket::AddPortIPv6(unsigned port)
sin
.
sin6_port
=
htons
(
port
);
sin
.
sin6_port
=
htons
(
port
);
sin
.
sin6_family
=
AF_INET6
;
sin
.
sin6_family
=
AF_INET6
;
AddAddress
(
(
const
sockaddr
&
)
sin
,
sizeof
(
sin
)
);
AddAddress
(
{(
const
sockaddr
*
)
&
sin
,
sizeof
(
sin
)}
);
}
}
/**
/**
...
@@ -392,7 +391,7 @@ ServerSocket::AddHost(const char *hostname, unsigned port, Error &error)
...
@@ -392,7 +391,7 @@ ServerSocket::AddHost(const char *hostname, unsigned port, Error &error)
return
false
;
return
false
;
for
(
const
struct
addrinfo
*
i
=
ai
;
i
!=
nullptr
;
i
=
i
->
ai_next
)
for
(
const
struct
addrinfo
*
i
=
ai
;
i
!=
nullptr
;
i
=
i
->
ai_next
)
AddAddress
(
*
i
->
ai_addr
,
i
->
ai_addrlen
);
AddAddress
(
SocketAddress
(
i
->
ai_addr
,
i
->
ai_addrlen
)
);
freeaddrinfo
(
ai
);
freeaddrinfo
(
ai
);
...
@@ -426,7 +425,7 @@ ServerSocket::AddPath(AllocatedPath &&path, Error &error)
...
@@ -426,7 +425,7 @@ ServerSocket::AddPath(AllocatedPath &&path, Error &error)
s_un
.
sun_family
=
AF_UNIX
;
s_un
.
sun_family
=
AF_UNIX
;
memcpy
(
s_un
.
sun_path
,
path
.
c_str
(),
path_length
+
1
);
memcpy
(
s_un
.
sun_path
,
path
.
c_str
(),
path_length
+
1
);
OneServerSocket
&
s
=
AddAddress
(
(
const
sockaddr
&
)
s_un
,
sizeof
(
s_un
)
);
OneServerSocket
&
s
=
AddAddress
(
{(
const
sockaddr
*
)
&
s_un
,
sizeof
(
s_un
)}
);
s
.
SetPath
(
std
::
move
(
path
));
s
.
SetPath
(
std
::
move
(
path
));
return
true
;
return
true
;
...
...
src/event/ServerSocket.hxx
View file @
4e30e747
...
@@ -24,7 +24,7 @@
...
@@ -24,7 +24,7 @@
#include <stddef.h>
#include <stddef.h>
struct
sockaddr
;
class
SocketAddress
;
class
EventLoop
;
class
EventLoop
;
class
Error
;
class
Error
;
class
AllocatedPath
;
class
AllocatedPath
;
...
@@ -51,7 +51,7 @@ public:
...
@@ -51,7 +51,7 @@ public:
}
}
private
:
private
:
OneServerSocket
&
AddAddress
(
const
sockaddr
&
address
,
size_t
length
);
OneServerSocket
&
AddAddress
(
SocketAddress
address
);
/**
/**
* Add a listener on a port on all IPv4 interfaces.
* Add a listener on a port on all IPv4 interfaces.
...
@@ -111,8 +111,7 @@ public:
...
@@ -111,8 +111,7 @@ public:
void
Close
();
void
Close
();
protected
:
protected
:
virtual
void
OnAccept
(
int
fd
,
const
sockaddr
&
address
,
virtual
void
OnAccept
(
int
fd
,
SocketAddress
address
,
int
uid
)
=
0
;
size_t
address_length
,
int
uid
)
=
0
;
};
};
#endif
#endif
src/net/Resolver.cxx
View file @
4e30e747
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include "config.h"
#include "config.h"
#include "Resolver.hxx"
#include "Resolver.hxx"
#include "SocketAddress.hxx"
#include "util/Error.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Domain.hxx"
...
@@ -43,13 +44,14 @@
...
@@ -43,13 +44,14 @@
const
Domain
resolver_domain
(
"resolver"
);
const
Domain
resolver_domain
(
"resolver"
);
std
::
string
std
::
string
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
)
sockaddr_to_string
(
SocketAddress
address
)
{
{
#ifdef HAVE_UN
#ifdef HAVE_UN
if
(
sa
->
sa_family
==
AF_UNIX
)
{
if
(
address
.
GetFamily
()
==
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
*
)
if
(
length
<
sizeof
(
s_un
)
||
s_un
.
sun_path
[
0
]
==
0
)
address
.
GetAddress
();
if
(
address
.
GetSize
()
<
sizeof
(
s_un
)
||
s_un
.
sun_path
[
0
]
==
0
)
return
"local"
;
return
"local"
;
return
s_un
.
sun_path
;
return
s_un
.
sun_path
;
...
@@ -57,14 +59,15 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length)
...
@@ -57,14 +59,15 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length)
#endif
#endif
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
const
struct
sockaddr_in6
*
a6
=
(
const
struct
sockaddr_in6
*
)
sa
;
const
struct
sockaddr_in6
*
a6
=
(
const
struct
sockaddr_in6
*
)
address
.
GetAddress
();
struct
sockaddr_in
a4
;
struct
sockaddr_in
a4
;
#endif
#endif
int
ret
;
int
ret
;
char
host
[
NI_MAXHOST
],
serv
[
NI_MAXSERV
];
char
host
[
NI_MAXHOST
],
serv
[
NI_MAXSERV
];
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
if
(
sa
->
sa_family
==
AF_INET6
&&
if
(
address
.
GetFamily
()
==
AF_INET6
&&
IN6_IS_ADDR_V4MAPPED
(
&
a6
->
sin6_addr
))
{
IN6_IS_ADDR_V4MAPPED
(
&
a6
->
sin6_addr
))
{
/* convert "::ffff:127.0.0.1" to "127.0.0.1" */
/* convert "::ffff:127.0.0.1" to "127.0.0.1" */
...
@@ -74,12 +77,12 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length)
...
@@ -74,12 +77,12 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length)
sizeof
(
a4
.
sin_addr
));
sizeof
(
a4
.
sin_addr
));
a4
.
sin_port
=
a6
->
sin6_port
;
a4
.
sin_port
=
a6
->
sin6_port
;
sa
=
(
const
struct
sockaddr
*
)
&
a4
;
address
=
{
(
const
struct
sockaddr
*
)
&
a4
,
sizeof
(
a4
)
};
length
=
sizeof
(
a4
);
}
}
#endif
#endif
ret
=
getnameinfo
(
sa
,
length
,
host
,
sizeof
(
host
),
serv
,
sizeof
(
serv
),
ret
=
getnameinfo
(
address
.
GetAddress
(),
address
.
GetSize
(),
host
,
sizeof
(
host
),
serv
,
sizeof
(
serv
),
NI_NUMERICHOST
|
NI_NUMERICSERV
);
NI_NUMERICHOST
|
NI_NUMERICSERV
);
if
(
ret
!=
0
)
if
(
ret
!=
0
)
return
"unknown"
;
return
"unknown"
;
...
...
src/net/Resolver.hxx
View file @
4e30e747
...
@@ -26,10 +26,10 @@
...
@@ -26,10 +26,10 @@
#include <stddef.h>
#include <stddef.h>
struct
sockaddr
;
struct
addrinfo
;
struct
addrinfo
;
class
Error
;
class
Error
;
class
Domain
;
class
Domain
;
class
SocketAddress
;
extern
const
Domain
resolver_domain
;
extern
const
Domain
resolver_domain
;
...
@@ -42,7 +42,7 @@ extern const Domain resolver_domain;
...
@@ -42,7 +42,7 @@ extern const Domain resolver_domain;
*/
*/
gcc_pure
gcc_pure
std
::
string
std
::
string
sockaddr_to_string
(
const
sockaddr
*
sa
,
size_t
length
);
sockaddr_to_string
(
SocketAddress
address
);
/**
/**
* Resolve a specification in the form "host", "host:port",
* Resolve a specification in the form "host", "host:port",
...
...
src/net/SocketAddress.cxx
0 → 100644
View file @
4e30e747
/*
* Copyright (C) 2012-2015 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "SocketAddress.hxx"
#include <string.h>
bool
SocketAddress
::
operator
==
(
SocketAddress
other
)
const
{
return
size
==
other
.
size
&&
memcmp
(
address
,
other
.
address
,
size
)
==
0
;
}
src/net/SocketAddress.hxx
0 → 100644
View file @
4e30e747
/*
* Copyright (C) 2012-2015 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SOCKET_ADDRESS_HXX
#define SOCKET_ADDRESS_HXX
#include "Compiler.h"
#include <cstddef>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
/**
* An OO wrapper for struct sockaddr.
*/
class
SocketAddress
{
public
:
#ifdef WIN32
typedef
int
size_type
;
#else
typedef
socklen_t
size_type
;
#endif
private
:
const
struct
sockaddr
*
address
;
size_type
size
;
public
:
SocketAddress
()
=
default
;
constexpr
SocketAddress
(
std
::
nullptr_t
)
:
address
(
nullptr
),
size
(
0
)
{}
constexpr
SocketAddress
(
const
struct
sockaddr
*
_address
,
size_type
_size
)
:
address
(
_address
),
size
(
_size
)
{}
static
constexpr
SocketAddress
Null
()
{
return
nullptr
;
}
constexpr
bool
IsNull
()
const
{
return
address
==
nullptr
;
}
const
struct
sockaddr
*
GetAddress
()
const
{
return
address
;
}
constexpr
size_type
GetSize
()
const
{
return
size
;
}
constexpr
int
GetFamily
()
const
{
return
address
->
sa_family
;
}
/**
* Does the object have a well-defined address? Check !IsNull()
* before calling this method.
*/
bool
IsDefined
()
const
{
return
GetFamily
()
!=
AF_UNSPEC
;
}
gcc_pure
bool
operator
==
(
const
SocketAddress
other
)
const
;
bool
operator
!=
(
const
SocketAddress
other
)
const
{
return
!
(
*
this
==
other
);
}
};
#endif
src/net/SocketUtil.cxx
View file @
4e30e747
...
@@ -19,25 +19,19 @@
...
@@ -19,25 +19,19 @@
#include "config.h"
#include "config.h"
#include "SocketUtil.hxx"
#include "SocketUtil.hxx"
#include "SocketAddress.hxx"
#include "SocketError.hxx"
#include "SocketError.hxx"
#include "system/fd_util.h"
#include "system/fd_util.h"
#include <unistd.h>
#include <unistd.h>
#ifndef WIN32
#include <sys/socket.h>
#else
#include <ws2tcpip.h>
#include <winsock.h>
#endif
#ifdef HAVE_IPV6
#ifdef HAVE_IPV6
#include <string.h>
#include <string.h>
#endif
#endif
int
int
socket_bind_listen
(
int
domain
,
int
type
,
int
protocol
,
socket_bind_listen
(
int
domain
,
int
type
,
int
protocol
,
const
struct
sockaddr
*
address
,
size_t
address_length
,
SocketAddress
address
,
int
backlog
,
int
backlog
,
Error
&
error
)
Error
&
error
)
{
{
...
@@ -60,7 +54,7 @@ socket_bind_listen(int domain, int type, int protocol,
...
@@ -60,7 +54,7 @@ socket_bind_listen(int domain, int type, int protocol,
return
-
1
;
return
-
1
;
}
}
ret
=
bind
(
fd
,
address
,
address_length
);
ret
=
bind
(
fd
,
address
.
GetAddress
(),
address
.
GetSize
()
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
SetSocketError
(
error
);
SetSocketError
(
error
);
close_socket
(
fd
);
close_socket
(
fd
);
...
...
src/net/SocketUtil.hxx
View file @
4e30e747
...
@@ -26,9 +26,7 @@
...
@@ -26,9 +26,7 @@
#ifndef MPD_SOCKET_UTIL_HXX
#ifndef MPD_SOCKET_UTIL_HXX
#define MPD_SOCKET_UTIL_HXX
#define MPD_SOCKET_UTIL_HXX
#include <stddef.h>
class
SocketAddress
;
struct
sockaddr
;
class
Error
;
class
Error
;
/**
/**
...
@@ -47,7 +45,7 @@ class Error;
...
@@ -47,7 +45,7 @@ class Error;
*/
*/
int
int
socket_bind_listen
(
int
domain
,
int
type
,
int
protocol
,
socket_bind_listen
(
int
domain
,
int
type
,
int
protocol
,
const
struct
sockaddr
*
address
,
size_t
address_length
,
SocketAddress
address
,
int
backlog
,
int
backlog
,
Error
&
error
);
Error
&
error
);
...
...
src/output/plugins/httpd/HttpdInternal.hxx
View file @
4e30e747
...
@@ -259,8 +259,7 @@ public:
...
@@ -259,8 +259,7 @@ public:
private
:
private
:
virtual
void
RunDeferred
()
override
;
virtual
void
RunDeferred
()
override
;
virtual
void
OnAccept
(
int
fd
,
const
sockaddr
&
address
,
void
OnAccept
(
int
fd
,
SocketAddress
address
,
int
uid
)
override
;
size_t
address_length
,
int
uid
)
override
;
};
};
extern
const
class
Domain
httpd_output_domain
;
extern
const
class
Domain
httpd_output_domain
;
...
...
src/output/plugins/httpd/HttpdOutputPlugin.cxx
View file @
4e30e747
...
@@ -26,6 +26,7 @@
...
@@ -26,6 +26,7 @@
#include "encoder/EncoderPlugin.hxx"
#include "encoder/EncoderPlugin.hxx"
#include "encoder/EncoderList.hxx"
#include "encoder/EncoderList.hxx"
#include "net/Resolver.hxx"
#include "net/Resolver.hxx"
#include "net/SocketAddress.hxx"
#include "Page.hxx"
#include "Page.hxx"
#include "IcyMetaDataServer.hxx"
#include "IcyMetaDataServer.hxx"
#include "system/fd_util.h"
#include "system/fd_util.h"
...
@@ -201,16 +202,14 @@ HttpdOutput::RunDeferred()
...
@@ -201,16 +202,14 @@ HttpdOutput::RunDeferred()
}
}
void
void
HttpdOutput
::
OnAccept
(
int
fd
,
const
sockaddr
&
address
,
HttpdOutput
::
OnAccept
(
int
fd
,
SocketAddress
address
,
gcc_unused
int
uid
)
size_t
address_length
,
gcc_unused
int
uid
)
{
{
/* the listener socket has become readable - a client has
/* the listener socket has become readable - a client has
connected */
connected */
#ifdef HAVE_LIBWRAP
#ifdef HAVE_LIBWRAP
if
(
address
.
sa_family
!=
AF_UNIX
)
{
if
(
address
.
GetFamily
()
!=
AF_UNIX
)
{
const
auto
hostaddr
=
sockaddr_to_string
(
&
address
,
const
auto
hostaddr
=
sockaddr_to_string
(
address
);
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"
;
...
@@ -230,7 +229,6 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
...
@@ -230,7 +229,6 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
}
}
#else
#else
(
void
)
address
;
(
void
)
address
;
(
void
)
address_length
;
#endif
/* HAVE_WRAP */
#endif
/* HAVE_WRAP */
const
ScopeLock
protect
(
mutex
);
const
ScopeLock
protect
(
mutex
);
...
...
test/run_resolver.cxx
View file @
4e30e747
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
#include "config.h"
#include "config.h"
#include "net/Resolver.hxx"
#include "net/Resolver.hxx"
#include "net/SocketAddress.hxx"
#include "util/Error.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
#include "Log.hxx"
...
@@ -50,7 +51,7 @@ int main(int argc, char **argv)
...
@@ -50,7 +51,7 @@ 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
)
{
const
auto
s
=
sockaddr_to_string
(
i
->
ai_addr
,
i
->
ai_addrlen
);
const
auto
s
=
sockaddr_to_string
(
{
i
->
ai_addr
,
i
->
ai_addrlen
}
);
printf
(
"%s
\n
"
,
s
.
c_str
());
printf
(
"%s
\n
"
,
s
.
c_str
());
}
}
...
...
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