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
273771ff
Commit
273771ff
authored
Nov 30, 2020
by
Max Kellermann
Committed by
Max Kellermann
Nov 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/SocketAddress: add CastTo()
parent
32ce9ce9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
44 additions
and
34 deletions
+44
-34
IPv4Address.cxx
src/net/IPv4Address.cxx
+4
-10
IPv4Address.hxx
src/net/IPv4Address.hxx
+1
-3
IPv6Address.cxx
src/net/IPv6Address.cxx
+4
-10
IPv6Address.hxx
src/net/IPv6Address.hxx
+1
-3
SocketAddress.cxx
src/net/SocketAddress.cxx
+4
-4
SocketAddress.hxx
src/net/SocketAddress.hxx
+14
-1
StaticSocketAddress.hxx
src/net/StaticSocketAddress.hxx
+13
-0
ToString.cxx
src/net/ToString.cxx
+2
-2
ToString.hxx
src/net/ToString.hxx
+1
-1
No files found.
src/net/IPv4Address.cxx
View file @
273771ff
...
...
@@ -31,15 +31,9 @@
#include <cassert>
static
const
struct
sockaddr_in
*
CastToIPv4
(
const
struct
sockaddr
*
p
)
noexcept
IPv4Address
::
IPv4Address
(
SocketAddress
src
)
noexcept
:
address
(
src
.
CastTo
<
struct
sockaddr_in
>
())
{
assert
(
p
->
sa_family
==
AF_INET
);
/* cast through void to work around the bogus alignment warning */
const
void
*
q
=
reinterpret_cast
<
const
void
*>
(
p
);
return
reinterpret_cast
<
const
struct
sockaddr_in
*>
(
q
);
assert
(
!
src
.
IsNull
());
assert
(
src
.
GetFamily
()
==
AF_INET
);
}
IPv4Address
::
IPv4Address
(
SocketAddress
src
)
noexcept
:
address
(
*
CastToIPv4
(
src
.
GetAddress
()))
{}
src/net/IPv4Address.hxx
View file @
273771ff
...
...
@@ -171,9 +171,7 @@ public:
* 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_in */
return
*
(
const
IPv4Address
*
)(
const
void
*
)
src
.
GetAddress
();
return
Cast
(
src
.
CastTo
<
struct
sockaddr_in
>
());
}
constexpr
operator
SocketAddress
()
const
noexcept
{
...
...
src/net/IPv6Address.cxx
View file @
273771ff
...
...
@@ -34,19 +34,13 @@
#include <string.h>
static
const
struct
sockaddr_in6
*
CastToIPv6
(
const
struct
sockaddr
*
p
)
noexcept
IPv6Address
::
IPv6Address
(
SocketAddress
src
)
noexcept
:
address
(
src
.
CastTo
<
struct
sockaddr_in6
>
())
{
assert
(
p
->
sa_family
==
AF_INET6
);
/* cast through void to work around the bogus alignment warning */
const
void
*
q
=
reinterpret_cast
<
const
void
*>
(
p
);
return
reinterpret_cast
<
const
struct
sockaddr_in6
*>
(
q
);
assert
(
!
src
.
IsNull
());
assert
(
src
.
GetFamily
()
==
AF_INET6
);
}
IPv6Address
::
IPv6Address
(
SocketAddress
src
)
noexcept
:
address
(
*
CastToIPv6
(
src
.
GetAddress
()))
{}
bool
IPv6Address
::
IsAny
()
const
noexcept
{
...
...
src/net/IPv6Address.hxx
View file @
273771ff
...
...
@@ -140,9 +140,7 @@ public:
* only legal after verifying SocketAddress::GetFamily().
*/
static
constexpr
const
IPv6Address
&
Cast
(
const
SocketAddress
src
)
noexcept
{
/* this reinterpret_cast works because this class is
just a wrapper for struct sockaddr_in6 */
return
*
(
const
IPv6Address
*
)(
const
void
*
)
src
.
GetAddress
();
return
Cast
(
src
.
CastTo
<
struct
sockaddr_in6
>
());
}
constexpr
operator
SocketAddress
()
const
noexcept
{
...
...
src/net/SocketAddress.cxx
View file @
273771ff
/*
* Copyright 2012-20
19
Max Kellermann <max.kellermann@gmail.com>
* Copyright 2012-20
20
Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
...
...
@@ -63,7 +63,7 @@ SocketAddress::GetLocalRaw() const noexcept
/* not applicable */
return
nullptr
;
const
auto
sun
=
(
const
struct
sockaddr_un
*
)
GetAddress
();
const
auto
*
sun
=
&
CastTo
<
struct
sockaddr_un
>
();
const
auto
start
=
(
const
char
*
)
sun
;
const
auto
path
=
sun
->
sun_path
;
const
size_t
header_size
=
path
-
start
;
...
...
@@ -159,10 +159,10 @@ SocketAddress::GetSteadyPart() const noexcept
#ifdef HAVE_TCP
case
AF_INET
:
return
::
GetSteadyPart
(
*
(
const
struct
sockaddr_in
*
)(
const
void
*
)
GetAddress
());
return
::
GetSteadyPart
(
CastTo
<
struct
sockaddr_in
>
());
case
AF_INET6
:
return
::
GetSteadyPart
(
*
(
const
struct
sockaddr_in6
*
)(
const
void
*
)
GetAddress
());
return
::
GetSteadyPart
(
CastTo
<
struct
sockaddr_in6
>
());
#endif
default
:
...
...
src/net/SocketAddress.hxx
View file @
273771ff
/*
* Copyright 2012-20
19
Max Kellermann <max.kellermann@gmail.com>
* Copyright 2012-20
20
Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
...
...
@@ -82,6 +82,19 @@ public:
return
address
;
}
/**
* Cast the "sockaddr" pointer to a different address type,
* e.g. "sockaddr_in". This is only legal after checking
* !IsNull() and GetFamily().
*/
template
<
typename
T
>
constexpr
const
T
&
CastTo
()
const
noexcept
{
/* cast through void to work around the bogus
alignment warning */
const
void
*
q
=
reinterpret_cast
<
const
void
*>
(
address
);
return
*
reinterpret_cast
<
const
T
*>
(
q
);
}
constexpr
size_type
GetSize
()
const
noexcept
{
return
size
;
}
...
...
src/net/StaticSocketAddress.hxx
View file @
273771ff
...
...
@@ -68,6 +68,19 @@ public:
return
(
const
struct
sockaddr
*
)(
const
void
*
)
&
address
;
}
/**
* Cast the "sockaddr" pointer to a different address type,
* e.g. "sockaddr_in". This is only legal after checking
* GetFamily().
*/
template
<
typename
T
>
constexpr
const
T
&
CastTo
()
const
noexcept
{
/* cast through void to work around the bogus
alignment warning */
const
void
*
q
=
reinterpret_cast
<
const
void
*>
(
&
address
);
return
*
reinterpret_cast
<
const
T
*>
(
q
);
}
constexpr
size_type
GetCapacity
()
const
noexcept
{
return
sizeof
(
address
);
}
...
...
src/net/ToString.cxx
View file @
273771ff
/*
* Copyright 2011-20
19
Max Kellermann <max.kellermann@gmail.com>
* Copyright 2011-20
20
Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
...
...
@@ -85,7 +85,7 @@ ToString(SocketAddress address) noexcept
#ifdef HAVE_UN
if
(
address
.
GetFamily
()
==
AF_LOCAL
)
/* return path of local socket */
return
LocalAddressToString
(
*
(
const
sockaddr_un
*
)
address
.
GetAddress
(),
return
LocalAddressToString
(
address
.
CastTo
<
struct
sockaddr_un
>
(),
address
.
GetSize
());
#endif
...
...
src/net/ToString.hxx
View file @
273771ff
/*
* Copyright 2011-20
19
Max Kellermann <max.kellermann@gmail.com>
* Copyright 2011-20
20
Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
...
...
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