diff options
author | Max Kellermann <mk@cm4all.com> | 2020-11-30 21:33:10 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-11-30 21:49:02 +0100 |
commit | 34a070f5a6c22c3d5d6cbf32dcb66c07d90132bb (patch) | |
tree | 2921333b324dfd97dc3cdc51958dd3252a965c7b /src/net | |
parent | ac4975cd7a681c896359157be8d4ae4aceca981d (diff) |
net/IPv[46]Address: add Cast(const sockaddr_in&)
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/IPv4Address.cxx | 2 | ||||
-rw-r--r-- | src/net/IPv4Address.hxx | 11 | ||||
-rw-r--r-- | src/net/IPv6Address.cxx | 2 | ||||
-rw-r--r-- | src/net/IPv6Address.hxx | 11 |
4 files changed, 22 insertions, 4 deletions
diff --git a/src/net/IPv4Address.cxx b/src/net/IPv4Address.cxx index c7f2ec521..eeb4f553e 100644 --- a/src/net/IPv4Address.cxx +++ b/src/net/IPv4Address.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2012-2020 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 diff --git a/src/net/IPv4Address.hxx b/src/net/IPv4Address.hxx index 6137c120e..b55204397 100644 --- a/src/net/IPv4Address.hxx +++ b/src/net/IPv4Address.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2012-2020 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 @@ -158,6 +158,15 @@ public: } /** + * Cast a #sockaddr_in6 reference to an IPv6Address reference. + */ + static constexpr const IPv4Address &Cast(const struct sockaddr_in &src) noexcept { + /* this reinterpret_cast works because this class is + just a wrapper for struct sockaddr_in6 */ + return *(const IPv4Address *)(const void *)&src; + } + + /** * Return a downcasted reference to the address. This call is * only legal after verifying SocketAddress::GetFamily(). */ diff --git a/src/net/IPv6Address.cxx b/src/net/IPv6Address.cxx index de8b44c5b..1071c1a77 100644 --- a/src/net/IPv6Address.cxx +++ b/src/net/IPv6Address.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2012-2020 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 diff --git a/src/net/IPv6Address.hxx b/src/net/IPv6Address.hxx index 52606024f..3ef6f7319 100644 --- a/src/net/IPv6Address.hxx +++ b/src/net/IPv6Address.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2012-2020 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 @@ -127,6 +127,15 @@ public: } /** + * Cast a #sockaddr_in6 reference to an IPv6Address reference. + */ + static constexpr const IPv6Address &Cast(const struct sockaddr_in6 &src) noexcept { + /* this reinterpret_cast works because this class is + just a wrapper for struct sockaddr_in6 */ + return *(const IPv6Address *)(const void *)&src; + } + + /** * Return a downcasted reference to the address. This call is * only legal after verifying SocketAddress::GetFamily(). */ |