Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
byteorder.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
20 */
21
22#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <arpa/inet.h> // for ntohs() and friends
26#include <iosfwd>
27#include <utility>
28#endif
29
30#include <seastar/core/unaligned.hh>
31#include <seastar/util/modules.hh>
32
33namespace seastar {
34
35inline uint64_t ntohq(uint64_t v) {
36#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37 // big endian, nothing to do
38 return v;
39#else
40 // little endian, reverse bytes
41 return __builtin_bswap64(v);
42#endif
43}
44inline uint64_t htonq(uint64_t v) {
45 // htonq and ntohq have identical implementations
46 return ntohq(v);
47}
48
49namespace net {
50
51inline void ntoh() {}
52inline void hton() {}
53
54inline uint8_t ntoh(uint8_t x) { return x; }
55inline uint8_t hton(uint8_t x) { return x; }
56inline uint16_t ntoh(uint16_t x) { return ntohs(x); }
57inline uint16_t hton(uint16_t x) { return htons(x); }
58inline uint32_t ntoh(uint32_t x) { return ntohl(x); }
59inline uint32_t hton(uint32_t x) { return htonl(x); }
60inline uint64_t ntoh(uint64_t x) { return ntohq(x); }
61inline uint64_t hton(uint64_t x) { return htonq(x); }
62
63inline int8_t ntoh(int8_t x) { return x; }
64inline int8_t hton(int8_t x) { return x; }
65inline int16_t ntoh(int16_t x) { return ntohs(x); }
66inline int16_t hton(int16_t x) { return htons(x); }
67inline int32_t ntoh(int32_t x) { return ntohl(x); }
68inline int32_t hton(int32_t x) { return htonl(x); }
69inline int64_t ntoh(int64_t x) { return ntohq(x); }
70inline int64_t hton(int64_t x) { return htonq(x); }
71
72// Deprecated alias net::packed<> for unaligned<> from unaligned.hh.
73// TODO: get rid of this alias.
74template <typename T> using packed = unaligned<T>;
75
76template <typename T>
77inline T ntoh(const packed<T>& x) {
78 T v = x;
79 return ntoh(v);
80}
81
82template <typename T>
83inline T hton(const packed<T>& x) {
84 T v = x;
85 return hton(v);
86}
87
88template <typename T>
89inline std::ostream& operator<<(std::ostream& os, const packed<T>& v) {
90 auto x = v.raw;
91 return os << x;
92}
93
94inline
95void ntoh_inplace() {}
96inline
97void hton_inplace() {};
98
99template <typename First, typename... Rest>
100inline
101void ntoh_inplace(First& first, Rest&... rest) {
102 first = ntoh(first);
103 ntoh_inplace(std::forward<Rest&>(rest)...);
104}
105
106template <typename First, typename... Rest>
107inline
108void hton_inplace(First& first, Rest&... rest) {
109 first = hton(first);
110 hton_inplace(std::forward<Rest&>(rest)...);
111}
112
113template <class T>
114inline
115T ntoh(const T& x) {
116 T tmp = x;
117 tmp.adjust_endianness([] (auto&&... what) { ntoh_inplace(std::forward<decltype(what)&>(what)...); });
118 return tmp;
119}
120
121template <class T>
122inline
123T hton(const T& x) {
124 T tmp = x;
125 tmp.adjust_endianness([] (auto&&... what) { hton_inplace(std::forward<decltype(what)&>(what)...); });
126 return tmp;
127}
128
129}
130
131}
Seastar API namespace.
Definition: abort_on_ebadf.hh:26