Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
4bbac29d
Commit
4bbac29d
authored
Dec 28, 2018
by
hydratim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for v4-only and improved tests
parent
ac7e6118
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
48 deletions
+33
-48
include/pistache/listener.h
include/pistache/listener.h
+2
-0
src/server/listener.cc
src/server/listener.cc
+23
-0
tests/listener_test.cc
tests/listener_test.cc
+8
-48
No files found.
include/pistache/listener.h
View file @
4bbac29d
...
...
@@ -49,6 +49,8 @@ public:
int
backlog
=
Const
::
MaxBacklog
);
void
setHandler
(
const
std
::
shared_ptr
<
Handler
>&
handler
);
static
bool
systemSupportsIpv6
();
void
bind
();
void
bind
(
const
Address
&
address
);
...
...
src/server/listener.cc
View file @
4bbac29d
...
...
@@ -14,6 +14,8 @@
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
...
...
@@ -145,6 +147,27 @@ Listener::pinWorker(size_t worker, const CpuSet& set)
#endif
}
bool
Listener
::
systemSupportsIpv6
(){
struct
ifaddrs
*
ifaddr
,
*
ifa
;
int
family
,
n
;
bool
supportsIpv6
=
false
;
if
(
getifaddrs
(
&
ifaddr
)
==
-
1
)
{
throw
std
::
runtime_error
(
"Call to getifaddrs() failed"
);
}
for
(
ifa
=
ifaddr
,
n
=
0
;
ifa
!=
NULL
;
ifa
=
ifa
->
ifa_next
,
n
++
)
{
if
(
ifa
->
ifa_addr
==
NULL
)
continue
;
family
=
ifa
->
ifa_addr
->
sa_family
;
if
(
family
==
AF_INET6
)
continue
;
}
freeifaddrs
(
ifaddr
);
return
supportsIpv6
;
}
void
Listener
::
bind
()
{
bind
(
addr_
);
...
...
tests/listener_test.cc
View file @
4bbac29d
...
...
@@ -123,57 +123,17 @@ TEST(listener_test, listener_bind_port_free) {
ASSERT_TRUE
(
true
);
}
TEST
(
listener_test
,
listener_bind_v4_port
)
{
uint16_t
port_nb
;
// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper
s
=
bind_free_port
();
port_nb
=
s
.
port
();
}
if
(
port_nb
==
0
)
{
FAIL
()
<<
"Could not find a free port. Abort test.
\n
"
;
}
Pistache
::
Port
port
(
port_nb
);
Pistache
::
Address
address
(
Pistache
::
Ipv4
::
loopback
(),
port
);
TEST
(
listener_test
,
listener_bind_ephemeral_v6_port
)
{
Pistache
::
Port
port
(
0
);
Pistache
::
Address
address
(
Pistache
::
Ipv6
::
any
(),
port
);
Pistache
::
Tcp
::
Listener
listener
;
if
(
not
listener
.
systemSupportsIpv6
())
{
Pistache
::
Flags
<
Pistache
::
Tcp
::
Options
>
options
;
listener
.
init
(
1
,
options
);
listener
.
setHandler
(
Pistache
::
Http
::
make_handler
<
DummyHandler
>
());
listener
.
bind
(
address
);
ASSERT_TRUE
(
true
);
}
TEST
(
listener_test
,
listener_bind_v6_port
)
{
uint16_t
port_nb
;
// This is just done to get the value of a free port. The socket will be closed
// after the closing curly bracket and the port will be free again (SO_REUSEADDR option).
// In theory, it is possible that some application grab this port before we bind it again...
{
SocketWrapper
s
=
bind_free_port
();
port_nb
=
s
.
port
();
}
if
(
port_nb
==
0
)
{
FAIL
()
<<
"Could not find a free port. Abort test.
\n
"
;
}
Pistache
::
Port
port
(
port_nb
);
Pistache
::
Address
address
(
Pistache
::
Ipv6
::
loopback
(),
port
);
Pistache
::
Tcp
::
Listener
listener
;
Pistache
::
Flags
<
Pistache
::
Tcp
::
Options
>
options
;
listener
.
init
(
1
,
options
);
listener
.
setHandler
(
Pistache
::
Http
::
make_handler
<
DummyHandler
>
());
listener
.
bind
(
address
);
ASSERT_TRUE
(
true
);
}
...
...
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