net/IPv[46]Address: add Cast(const sockaddr_in&)

This commit is contained in:
Max Kellermann 2020-11-30 21:33:10 +01:00 committed by Max Kellermann
parent ac4975cd7a
commit 34a070f5a6
4 changed files with 22 additions and 4 deletions

View File

@ -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

View File

@ -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
@ -157,6 +157,15 @@ public:
~uint16_t(0));
}
/**
* 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().

View File

@ -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

View File

@ -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
@ -126,6 +126,15 @@ public:
~uint32_t(0));
}
/**
* 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().