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
4c6ae4e9
Commit
4c6ae4e9
authored
Aug 20, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/IPv4Address: add various helper methods
parent
9cb9154b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
0 deletions
+43
-0
IPv4Address.hxx
src/net/IPv4Address.hxx
+43
-0
No files found.
src/net/IPv4Address.hxx
View file @
4c6ae4e9
...
...
@@ -158,6 +158,26 @@ public:
return
ConstructInAddr
(
INADDR_LOOPBACK
);
}
/**
* Generate a (net-)mask with the specified prefix length.
*/
static
constexpr
IPv4Address
MaskFromPrefix
(
unsigned
prefix_length
)
noexcept
{
return
Construct
(
prefix_length
==
0
?
0
:
(
~
uint32_t
(
0
))
<<
(
32
-
prefix_length
),
~
uint16_t
(
0
));
}
/**
* Return a downcasted reference to the address. This call is
* only legal after verifying SocketAddress::GetFamily().
*/
static
constexpr
const
IPv4Address
&
Cast
(
const
SocketAddress
src
)
noexcept
{
/* this reinterpret_cast works because this class is
just a wrapper for struct sockaddr_in6 */
return
*
(
const
IPv4Address
*
)(
const
void
*
)
src
.
GetAddress
();
}
constexpr
operator
SocketAddress
()
const
noexcept
{
return
SocketAddress
((
const
struct
sockaddr
*
)
&
address
,
sizeof
(
address
));
...
...
@@ -188,6 +208,29 @@ public:
constexpr
const
struct
in_addr
&
GetAddress
()
const
noexcept
{
return
address
.
sin_addr
;
}
/**
* @return the 32 bit IP address in network byte order
*/
constexpr
uint32_t
GetNumericAddressBE
()
const
noexcept
{
return
GetAddress
().
s_addr
;
}
/**
* @return the 32 bit IP address in host byte order
*/
constexpr
uint32_t
GetNumericAddress
()
const
noexcept
{
return
FromBE32
(
GetNumericAddressBE
());
}
/**
* Bit-wise AND of two addresses. This is useful for netmask
* calculations.
*/
constexpr
IPv4Address
operator
&
(
const
IPv4Address
&
other
)
const
noexcept
{
return
IPv4Address
(
ConstructInAddrBE
(
GetNumericAddressBE
()
&
other
.
GetNumericAddressBE
()),
GetPort
()
&
other
.
GetPort
());
}
};
#endif
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