EMANE  1.2.1
net.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2010 - DRS CenGen, LLC, Columbia, Maryland
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of DRS CenGen, LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef EMANENET_HEADER_
34 #define EMANENET_HEADER_
35 
36 #include <cstdlib>
37 #include <cstdio>
38 
39 namespace EMANE
40 {
41 #ifdef HTONL
42 #undef HTONL
43 #endif
44 #ifdef HTONS
45 #undef HTONS
46 #endif
47 #ifdef NTOHL
48 #undef NTOHL
49 #endif
50 #ifdef NTOHS
51 #undef NTOHS
52 #endif
53 #ifdef NTOHLL
54 #undef NTOHLL
55 #endif
56 #ifdef HTONLL
57 #undef HTONLL
58 #endif
59 
60 #define SWAP_16(x) \
61  ((((x) & 0XFF00) >> 8)) | \
62  (((x) & 0X00FF) << 8)
63 
64 #define SWAP_32(x) \
65  ((((x) & 0XFF000000UL) >> 24) | \
66  (((x) & 0X00FF0000UL) >> 8) | \
67  (((x) & 0X0000FF00UL) << 8) | \
68  (((x) & 0X000000FFUL) << 24))
69 
70 #define SWAP_64(x) \
71  ((((x) & 0XFF00000000000000ULL) >> 56) | \
72  (((x) & 0X00FF000000000000ULL) >> 40) | \
73  (((x) & 0X0000FF0000000000ULL) >> 24) | \
74  (((x) & 0X000000FF00000000ULL) >> 8) | \
75  (((x) & 0X00000000FF000000ULL) << 8) | \
76  (((x) & 0X0000000000FF0000ULL) << 24) | \
77  (((x) & 0X000000000000FF00ULL) << 40) | \
78  (((x) & 0X00000000000000FFULL) << 56))
79 
80 
81  constexpr std::uint64_t HTONLL(std::uint64_t x)
82  {
83 #if __BYTE_ORDER == __BIG_ENDIAN
84  return x;
85 #elif __BYTE_ORDER == __LITTLE_ENDIAN
86  return SWAP_64(x);
87 #else
88 # error "need to specify endian type"
89 #endif
90  }
91 
92  constexpr std::uint64_t NTOHLL(std::uint64_t x)
93  {
94 #if __BYTE_ORDER == __BIG_ENDIAN
95  return x;
96 #elif __BYTE_ORDER == __LITTLE_ENDIAN
97  return SWAP_64(x);
98 #else
99 # error "need to specify endian type"
100 #endif
101  }
102 
103  constexpr std::uint32_t HTONL(std::uint32_t x)
104  {
105 #if __BYTE_ORDER == __BIG_ENDIAN
106  return x;
107 #elif __BYTE_ORDER == __LITTLE_ENDIAN
108  return SWAP_32(x);
109 #else
110 # error "need to specify endian type"
111 #endif
112  }
113 
114  constexpr std::uint32_t NTOHL(std::uint32_t x)
115  {
116 #if __BYTE_ORDER == __BIG_ENDIAN
117  return x;
118 #elif __BYTE_ORDER == __LITTLE_ENDIAN
119  return SWAP_32(x);
120 #else
121 # error "need to specify endian type"
122 #endif
123  }
124 
125  constexpr std::uint16_t HTONS(std::uint16_t x)
126  {
127 #if __BYTE_ORDER == __BIG_ENDIAN
128  return x;
129 #elif __BYTE_ORDER == __LITTLE_ENDIAN
130  return SWAP_16(x);
131 #else
132 # error "need to specify endian type"
133 #endif
134  }
135 
136  constexpr std::uint16_t NTOHS(std::uint16_t x)
137  {
138 #if __BYTE_ORDER == __BIG_ENDIAN
139  return x;
140 #elif __BYTE_ORDER == __LITTLE_ENDIAN
141  return SWAP_16(x);
142 #else
143 # error "need to specify endian type"
144 #endif
145  }
146 }
147 
148 #endif //EMANE_NET_HEADER_
#define SWAP_64(x)
Definition: net.h:70
constexpr std::uint32_t NTOHL(std::uint32_t x)
Definition: net.h:114
constexpr std::uint64_t HTONLL(std::uint64_t x)
Definition: net.h:81
constexpr std::uint64_t NTOHLL(std::uint64_t x)
Definition: net.h:92
#define SWAP_32(x)
Definition: net.h:64
constexpr std::uint16_t HTONS(std::uint16_t x)
Definition: net.h:125
#define SWAP_16(x)
Definition: net.h:60
constexpr std::uint16_t NTOHS(std::uint16_t x)
Definition: net.h:136
constexpr std::uint32_t HTONL(std::uint32_t x)
Definition: net.h:103
Definition: agent.h:43