Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
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
Operations
Operations
Metrics
Environments
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 Framework
Commits
6c41d375
Commit
6c41d375
authored
Aug 14, 2018
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Properly compute net address for subnets
parent
556cffa4
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
80 additions
and
4 deletions
+80
-4
libcaf_core/caf/detail/mask_bits.hpp
libcaf_core/caf/detail/mask_bits.hpp
+52
-0
libcaf_core/src/ipv4_subnet.cpp
libcaf_core/src/ipv4_subnet.cpp
+3
-1
libcaf_core/src/ipv6_subnet.cpp
libcaf_core/src/ipv6_subnet.cpp
+5
-3
libcaf_core/test/ipv4_subnet.cpp
libcaf_core/test/ipv4_subnet.cpp
+10
-0
libcaf_core/test/ipv6_subnet.cpp
libcaf_core/test/ipv6_subnet.cpp
+10
-0
No files found.
libcaf_core/caf/detail/mask_bits.hpp
0 → 100644
View file @
6c41d375
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <array>
#include <cstdint>
namespace
caf
{
namespace
detail
{
/// Sets all bits after bits_to_keep to 0.
template
<
size_t
NumBytes
>
void
mask_bits
(
std
::
array
<
uint8_t
,
NumBytes
>&
bytes
,
size_t
bits_to_keep
)
{
// Calculate how many bytes we keep.
auto
bytes_to_keep
=
bits_to_keep
/
8
;
if
(
bytes_to_keep
>=
NumBytes
)
return
;
// See whether we have an unclean cut, e.g. keeping 7 bits of a byte.
auto
byte_cutoff
=
bits_to_keep
%
8
;
auto
i
=
bytes
.
begin
()
+
bytes_to_keep
;
if
(
byte_cutoff
!=
0
)
{
static
constexpr
uint8_t
mask
[]
=
{
0x00
,
0x80
,
0xC0
,
0xE0
,
0xF0
,
0xF8
,
0xFC
,
0xFE
};
*
i
=
*
i
&
mask
[
byte_cutoff
];
++
i
;
}
// Zero remaining bytes.
for
(;
i
!=
bytes
.
end
();
++
i
)
*
i
=
0
;
}
}
// namespace detail
}
// namespace caf
libcaf_core/src/ipv4_subnet.cpp
View file @
6c41d375
...
@@ -18,6 +18,8 @@
...
@@ -18,6 +18,8 @@
#include "caf/ipv4_subnet.hpp"
#include "caf/ipv4_subnet.hpp"
#include "caf/detail/mask_bits.hpp"
namespace
caf
{
namespace
caf
{
// -- constructors, destructors, and assignment operators --------------------
// -- constructors, destructors, and assignment operators --------------------
...
@@ -29,7 +31,7 @@ ipv4_subnet::ipv4_subnet() : prefix_length_(0) {
...
@@ -29,7 +31,7 @@ ipv4_subnet::ipv4_subnet() : prefix_length_(0) {
ipv4_subnet
::
ipv4_subnet
(
ipv4_address
network_address
,
uint8_t
prefix_length
)
ipv4_subnet
::
ipv4_subnet
(
ipv4_address
network_address
,
uint8_t
prefix_length
)
:
address_
(
network_address
),
:
address_
(
network_address
),
prefix_length_
(
prefix_length
)
{
prefix_length_
(
prefix_length
)
{
// nop
detail
::
mask_bits
(
address_
.
bytes
(),
prefix_length_
);
}
}
// -- properties ---------------------------------------------------------------
// -- properties ---------------------------------------------------------------
...
...
libcaf_core/src/ipv6_subnet.cpp
View file @
6c41d375
...
@@ -18,6 +18,8 @@
...
@@ -18,6 +18,8 @@
#include "caf/ipv6_subnet.hpp"
#include "caf/ipv6_subnet.hpp"
#include "caf/detail/mask_bits.hpp"
namespace
caf
{
namespace
caf
{
// -- constructors, destructors, and assignment operators --------------------
// -- constructors, destructors, and assignment operators --------------------
...
@@ -29,19 +31,19 @@ ipv6_subnet::ipv6_subnet() : prefix_length_(0) {
...
@@ -29,19 +31,19 @@ ipv6_subnet::ipv6_subnet() : prefix_length_(0) {
ipv6_subnet
::
ipv6_subnet
(
ipv4_subnet
subnet
)
ipv6_subnet
::
ipv6_subnet
(
ipv4_subnet
subnet
)
:
address_
(
ipv6_address
{
subnet
.
network_address
()}),
:
address_
(
ipv6_address
{
subnet
.
network_address
()}),
prefix_length_
(
v4_offset
+
subnet
.
prefix_length
()){
prefix_length_
(
v4_offset
+
subnet
.
prefix_length
()){
// nop
detail
::
mask_bits
(
address_
.
bytes
(),
prefix_length_
);
}
}
ipv6_subnet
::
ipv6_subnet
(
ipv4_address
network_address
,
uint8_t
prefix_length
)
ipv6_subnet
::
ipv6_subnet
(
ipv4_address
network_address
,
uint8_t
prefix_length
)
:
address_
(
network_address
),
:
address_
(
network_address
),
prefix_length_
(
prefix_length
+
v4_offset
)
{
prefix_length_
(
prefix_length
+
v4_offset
)
{
// nop
detail
::
mask_bits
(
address_
.
bytes
(),
prefix_length_
);
}
}
ipv6_subnet
::
ipv6_subnet
(
ipv6_address
network_address
,
uint8_t
prefix_length
)
ipv6_subnet
::
ipv6_subnet
(
ipv6_address
network_address
,
uint8_t
prefix_length
)
:
address_
(
network_address
),
:
address_
(
network_address
),
prefix_length_
(
prefix_length
)
{
prefix_length_
(
prefix_length
)
{
// nop
detail
::
mask_bits
(
address_
.
bytes
(),
prefix_length_
);
}
}
// -- properties ---------------------------------------------------------------
// -- properties ---------------------------------------------------------------
...
...
libcaf_core/test/ipv4_subnet.cpp
View file @
6c41d375
...
@@ -44,6 +44,16 @@ CAF_TEST(constructing) {
...
@@ -44,6 +44,16 @@ CAF_TEST(constructing) {
CAF_CHECK_EQUAL
(
local
.
prefix_length
(),
8u
);
CAF_CHECK_EQUAL
(
local
.
prefix_length
(),
8u
);
}
}
CAF_TEST
(
equality
)
{
auto
a
=
addr
(
0xff
,
0xff
,
0xff
,
0xff
)
/
19
;
auto
b
=
addr
(
0xff
,
0xff
,
0xff
,
0xab
)
/
19
;
auto
net
=
addr
(
0xff
,
0xff
,
0xe0
,
0x00
);
CAF_CHECK_EQUAL
(
a
.
network_address
(),
net
);
CAF_CHECK_EQUAL
(
a
.
network_address
(),
b
.
network_address
());
CAF_CHECK_EQUAL
(
a
.
prefix_length
(),
b
.
prefix_length
());
CAF_CHECK_EQUAL
(
a
,
b
);
}
CAF_TEST
(
constains
)
{
CAF_TEST
(
constains
)
{
ipv4_subnet
local
{
addr
(
127
,
0
,
0
,
0
),
8
};
ipv4_subnet
local
{
addr
(
127
,
0
,
0
,
0
),
8
};
CAF_CHECK
(
local
.
contains
(
addr
(
127
,
0
,
0
,
1
)));
CAF_CHECK
(
local
.
contains
(
addr
(
127
,
0
,
0
,
1
)));
...
...
libcaf_core/test/ipv6_subnet.cpp
View file @
6c41d375
...
@@ -39,6 +39,16 @@ CAF_TEST(constructing) {
...
@@ -39,6 +39,16 @@ CAF_TEST(constructing) {
CAF_CHECK_EQUAL
(
zero
.
prefix_length
(),
128u
);
CAF_CHECK_EQUAL
(
zero
.
prefix_length
(),
128u
);
}
}
CAF_TEST
(
equality
)
{
auto
a
=
ipv6_address
{{
0xffff
,
0xffff
,
0xffff
},
{}}
/
27
;
auto
b
=
ipv6_address
{{
0xffff
,
0xffff
,
0xabab
},
{}}
/
27
;
auto
net
=
ipv6_address
{{
0xffff
,
0xffe0
},
{}};
CAF_CHECK_EQUAL
(
a
.
network_address
(),
net
);
CAF_CHECK_EQUAL
(
a
.
network_address
(),
b
.
network_address
());
CAF_CHECK_EQUAL
(
a
.
prefix_length
(),
b
.
prefix_length
());
CAF_CHECK_EQUAL
(
a
,
b
);
}
CAF_TEST
(
constains
)
{
CAF_TEST
(
constains
)
{
auto
local
=
ipv6_address
{{
0xbebe
,
0xbebe
},
{}}
/
32
;
auto
local
=
ipv6_address
{{
0xbebe
,
0xbebe
},
{}}
/
32
;
CAF_CHECK
(
local
.
contains
(
ipv6_address
({
0xbebe
,
0xbebe
,
0xbebe
},
{})));
CAF_CHECK
(
local
.
contains
(
ipv6_address
({
0xbebe
,
0xbebe
,
0xbebe
},
{})));
...
...
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