Kea: sockaddr_util.h Source File

| Kea 3.2.0-git

sockaddr_util.h
Go to the documentation of this file.
1// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef SOCKADDR_UTIL_H
8#define SOCKADDR_UTIL_H 1
9
10#include <sys/types.h>
11#include <sys/socket.h>
12#include <netinet/in.h>
13
14#include <exceptions/isc_assert.h>
15
16// These definitions in this file are for the convenience of internal
17// implementation and test code, and are not intended to be used publicly.
18// The namespace "internal" indicates the intent.
19
20namespace isc {
21namespace util {
22namespace io {
23namespace internal {
24
25inline socklen_t
26getSALength(const struct sockaddr& sa) {
27 if (sa.sa_family == AF_INET) {
28 return (sizeof(struct sockaddr_in));
29 } else {
30 isc_throw_assert(sa.sa_family == AF_INET6);
31 return (sizeof(struct sockaddr_in6));
32 }
33}
34
35// Lower level C-APIs require conversion between various variants of
36// sockaddr's, which is not friendly with C++. The following templates
37// are a shortcut of common workaround conversion in such cases.
38
39template <typename SAType>
40const struct sockaddr*
41convertSockAddr(const SAType* sa) {
42 const void* p = sa;
43 return (static_cast<const struct sockaddr*>(p));
44}
45
46template <typename SAType>
47const SAType*
48 convertSockAddr(const struct sockaddr* sa) {
49 const void* p = sa;
50 return (static_cast<const SAType*>(p));
51}
52
53template <typename SAType>
54struct sockaddr*
55 convertSockAddr(SAType* sa) {
56 void* p = sa;
57 return (static_cast<struct sockaddr*>(p));
58}
59
60template <typename SAType>
61SAType*
62 convertSockAddr(struct sockaddr* sa) {
63 void* p = sa;
64 return (static_cast<SAType*>(p));
65}
66
67}
68}
69}
70}
71
72#endif // SOCKADDR_UTIL_H
isc_assert.h
isc_throw_assert
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition isc_assert.h:18
isc::util::io::internal
Definition pktinfo_utilities.h:20
isc::util::io::internal::convertSockAddr
const struct sockaddr * convertSockAddr(const SAType *sa)
Definition sockaddr_util.h:41
isc::util::io::internal::getSALength
socklen_t getSALength(const struct sockaddr &sa)
Definition sockaddr_util.h:26
isc::util::io
Definition fd.cc:16
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18