Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
actor-incubator
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
cpp-libs
actor-incubator
Commits
4be5d1d9
Commit
4be5d1d9
authored
Aug 30, 2019
by
Jakob Otto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rework conversion functions
parent
99b787bf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
39 deletions
+34
-39
libcaf_net/caf/detail/convert_ip_endpoint.hpp
libcaf_net/caf/detail/convert_ip_endpoint.hpp
+2
-6
libcaf_net/src/convert_ip_endpoint.cpp
libcaf_net/src/convert_ip_endpoint.cpp
+29
-30
libcaf_net/test/convert_ip_endpoint.cpp
libcaf_net/test/convert_ip_endpoint.cpp
+3
-3
No files found.
libcaf_net/caf/detail/convert_ip_endpoint.hpp
View file @
4be5d1d9
...
...
@@ -24,13 +24,9 @@
namespace
caf
{
namespace
detail
{
sockaddr_
in6
to_sockaddr
(
const
ipv6
_endpoint
&
ep
);
sockaddr_
storage
to_sockaddr
(
const
ip
_endpoint
&
ep
);
ipv6_endpoint
to_ip_endpoint
(
const
sockaddr_in6
&
addr
);
sockaddr_in
to_sockaddr
(
const
ipv4_endpoint
&
ep
);
ipv4_endpoint
to_ip_endpoint
(
const
sockaddr_in
&
addr
);
ip_endpoint
to_ip_endpoint
(
const
sockaddr_storage
&
addr
);
}
// namespace detail
}
// namespace caf
libcaf_net/src/convert_ip_endpoint.cpp
View file @
4be5d1d9
...
...
@@ -19,42 +19,41 @@
#include "caf/detail/convert_ip_endpoint.hpp"
#include "caf/ipv4_endpoint.hpp"
#include "caf/ipv6_endpoint.hpp"
#include <iostream>
namespace
caf
{
namespace
detail
{
sockaddr_in6
to_sockaddr
(
const
ipv6_endpoint
&
ep
)
{
sockaddr_in6
addr
=
{};
addr
.
sin6_family
=
AF_INET6
;
addr
.
sin6_port
=
ntohs
(
ep
.
port
());
memcpy
(
&
addr
.
sin6_addr
,
ep
.
address
().
bytes
().
data
(),
ep
.
address
().
size
());
return
addr
;
sockaddr_storage
to_sockaddr
(
const
ip_endpoint
&
ep
)
{
sockaddr_storage
sockaddr
=
{};
if
(
ep
.
address
().
embeds_v4
())
{
auto
sockaddr4
=
reinterpret_cast
<
sockaddr_in
*>
(
&
sockaddr
);
sockaddr4
->
sin_family
=
AF_INET
;
sockaddr4
->
sin_port
=
ntohs
(
ep
.
port
());
sockaddr4
->
sin_addr
.
s_addr
=
ep
.
address
().
embedded_v4
().
bits
();
}
else
{
auto
sockaddr6
=
reinterpret_cast
<
sockaddr_in6
*>
(
&
sockaddr
);
sockaddr6
->
sin6_family
=
AF_INET6
;
sockaddr6
->
sin6_port
=
ntohs
(
ep
.
port
());
memcpy
(
&
sockaddr6
->
sin6_addr
,
ep
.
address
().
bytes
().
data
(),
ep
.
address
().
bytes
().
size
());
}
return
sockaddr
;
}
ipv6_endpoint
to_ip_endpoint
(
const
sockaddr_in6
&
addr
)
{
ip_endpoint
ep
;
ep
.
port
(
htons
(
addr
.
sin6_port
));
ipv6_address
ip_addr
;
memcpy
(
ip_addr
.
bytes
().
data
(),
&
addr
.
sin6_addr
,
ip_addr
.
size
());
ep
.
address
(
ip_addr
);
return
ep
;
}
sockaddr_in
to_sockaddr
(
const
ipv4_endpoint
&
ep
)
{
sockaddr_in
addr
=
{};
addr
.
sin_family
=
AF_INET
;
addr
.
sin_port
=
ntohs
(
ep
.
port
());
memcpy
(
&
addr
.
sin_addr
,
ep
.
address
().
bytes
().
data
(),
ep
.
address
().
size
());
return
addr
;
}
ipv4_endpoint
to_ip_endpoint
(
const
sockaddr_in
&
addr
)
{
ipv4_endpoint
ep
;
ep
.
port
(
htons
(
addr
.
sin_port
));
ipv4_address
ip_addr
;
memcpy
(
ip_addr
.
bytes
().
data
(),
&
addr
.
sin_addr
,
ip_addr
.
size
());
ep
.
address
(
ip_addr
);
return
ep
;
ip_endpoint
to_ip_endpoint
(
const
sockaddr_storage
&
addr
)
{
if
(
addr
.
ss_family
==
AF_INET
)
{
auto
sockaddr4
=
reinterpret_cast
<
const
sockaddr_in
*>
(
&
addr
);
ipv4_address
ipv4_addr
;
memcpy
(
ipv4_addr
.
data
().
data
(),
&
sockaddr4
->
sin_addr
,
ipv4_addr
.
size
());
return
{
ipv4_addr
,
htons
(
sockaddr4
->
sin_port
)};
}
else
{
auto
sockaddr6
=
reinterpret_cast
<
const
sockaddr_in6
*>
(
&
addr
);
ipv6_address
ipv6_addr
;
memcpy
(
ipv6_addr
.
bytes
().
data
(),
&
sockaddr6
->
sin6_addr
,
ipv6_addr
.
bytes
().
size
());
return
{
ipv6_addr
,
htons
(
sockaddr6
->
sin6_port
)};
}
}
}
// namespace detail
...
...
libcaf_net/test/convert_ip_endpoint.cpp
View file @
4be5d1d9
...
...
@@ -36,7 +36,7 @@ CAF_TEST(sockaddr_in6 roundtrip) {
source_addr
.
sin6_family
=
AF_INET6
;
source_addr
.
sin6_port
=
htons
(
23
);
source_addr
.
sin6_addr
=
in6addr_loopback
;
auto
ep
=
to_ip_endpoint
(
source_addr
);
auto
ep
=
to_ip_endpoint
(
reinterpret_cast
<
sockaddr_storage
&>
(
source_addr
)
);
auto
dest_addr
=
to_sockaddr
(
ep
);
CAF_CHECK_EQUAL
(
memcmp
(
&
source_addr
,
&
dest_addr
,
sizeof
(
sockaddr_in6
)),
0
);
}
...
...
@@ -55,13 +55,13 @@ CAF_TEST(sockaddr_in4 roundtrip) {
source_addr
.
sin_family
=
AF_INET
;
source_addr
.
sin_port
=
htons
(
23
);
source_addr
.
sin_addr
.
s_addr
=
INADDR_LOOPBACK
;
auto
ep
=
to_ip_endpoint
(
source_addr
);
auto
ep
=
to_ip_endpoint
(
reinterpret_cast
<
sockaddr_storage
&>
(
source_addr
)
);
auto
dest_addr
=
to_sockaddr
(
ep
);
CAF_CHECK_EQUAL
(
memcmp
(
&
source_addr
,
&
dest_addr
,
sizeof
(
sockaddr_in
)),
0
);
}
CAF_TEST
(
ipv4_endpoint
roundtrip
)
{
ipv
4
_endpoint
source_ep
;
ipv
6
_endpoint
source_ep
;
if
(
auto
err
=
detail
::
parse
(
"127.0.0.1:55555"
,
source_ep
))
CAF_FAIL
(
"unable to parse input: "
<<
err
);
auto
addr
=
to_sockaddr
(
source_ep
);
...
...
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