Branch data Line data Source code
1 : : // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #ifndef __SOCKADDR_UTIL_H_
16 : : #define __SOCKADDR_UTIL_H_ 1
17 : :
18 : : #include <sys/socket.h>
19 : : #include <netinet/in.h>
20 : :
21 : : #include <cassert>
22 : :
23 : : // These definitions in this file are for the convenience of internal
24 : : // implementation and test code, and are not intended to be used publicly.
25 : : // The namespace "internal" indicates the intent.
26 : :
27 : : namespace isc {
28 : : namespace util {
29 : : namespace io {
30 : : namespace internal {
31 : :
32 : : inline socklen_t
33 : 152 : getSALength(const struct sockaddr& sa) {
34 [ + + ][ # # ]: 152 : if (sa.sa_family == AF_INET) {
35 : : return (sizeof(struct sockaddr_in));
36 : : } else {
37 [ - + ][ # # ]: 152 : assert(sa.sa_family == AF_INET6);
38 : : return (sizeof(struct sockaddr_in6));
39 : : }
40 : : }
41 : :
42 : : // Lower level C-APIs require conversion between various variants of
43 : : // sockaddr's, which is not friendly with C++. The following templates
44 : : // are a shortcut of common workaround conversion in such cases.
45 : :
46 : : template <typename SAType>
47 : : const struct sockaddr*
48 : : convertSockAddr(const SAType* sa) {
49 : 21 : const void* p = sa;
50 : : return (static_cast<const struct sockaddr*>(p));
51 : : }
52 : :
53 : : template <typename SAType>
54 : : const SAType*
55 : : convertSockAddr(const struct sockaddr* sa) {
56 : : const void* p = sa;
57 : : return (static_cast<const SAType*>(p));
58 : : }
59 : :
60 : : template <typename SAType>
61 : : struct sockaddr*
62 : : convertSockAddr(SAType* sa) {
63 : 159 : void* p = sa;
64 : : return (static_cast<struct sockaddr*>(p));
65 : : }
66 : :
67 : : template <typename SAType>
68 : : SAType*
69 : : convertSockAddr(struct sockaddr* sa) {
70 : : void* p = sa;
71 : : return (static_cast<SAType*>(p));
72 : : }
73 : :
74 : : }
75 : : }
76 : : }
77 : : }
78 : :
79 : : #endif // __SOCKADDR_UTIL_H_
80 : :
81 : : // Local Variables:
82 : : // mode: c++
83 : : // End:
|