Kea  1.9.9-git
sockaddr_util.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2020 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 
20 namespace isc {
21 namespace util {
22 namespace io {
23 namespace internal {
24 
25 inline socklen_t
26 getSALength(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 
39 template <typename SAType>
40 const struct sockaddr*
41 convertSockAddr(const SAType* sa) {
42  const void* p = sa;
43  return (static_cast<const struct sockaddr*>(p));
44 }
45 
46 template <typename SAType>
47 const SAType*
48 convertSockAddr(const struct sockaddr* sa) {
49  const void* p = sa;
50  return (static_cast<const SAType*>(p));
51 }
52 
53 template <typename SAType>
54 struct sockaddr*
55 convertSockAddr(SAType* sa) {
56  void* p = sa;
57  return (static_cast<struct sockaddr*>(p));
58 }
59 
60 template <typename SAType>
61 SAType*
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
73 
74 // Local Variables:
75 // mode: c++
76 // End:
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition: isc_assert.h:18
socklen_t getSALength(const struct sockaddr &sa)
Definition: sockaddr_util.h:26
Defines the logger used by the top-level component of kea-dhcp-ddns.
const struct sockaddr * convertSockAddr(const SAType *sa)
Definition: sockaddr_util.h:41