switch source package format to 3.0 quilt
[debian/gnuradio] / gruel / src / include / gruel / inet.h.in
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008, 2009 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef INCLUDED_INET_H
20 #define INCLUDED_INET_H
21
22 #include <stdint.h>
23
24 #if 1 /* missing htonll or ntohll */
25 #if @GR_ARCH_BIGENDIAN@  /* GR_ARCH_BIGENDIAN */
26 // Nothing to do...
27 static inline uint64_t htonll(uint64_t x){ return x;}
28 static inline uint64_t ntohll(uint64_t x){ return x;}
29 #else
30 #if @GR_HAVE_BYTESWAP@  /* GR_HAVE_BYTESWAP */
31 #include <byteswap.h>
32 #else
33
34 static inline uint64_t
35 bswap_64(uint64_t x)
36 {
37   return ((x & 0x00000000000000ffull) << 56) | ((x & 0x000000000000ff00ull) << 40) |
38          ((x & 0x0000000000ff0000ull) << 24) | ((x & 0x00000000ff000000ull) <<  8) |
39          ((x & 0x000000ff00000000ull) >>  8) | ((x & 0x0000ff0000000000ull) >> 24) |
40          ((x & 0x00ff000000000000ull) >> 40) | ((x & 0xff00000000000000ull) >> 56);
41 }
42
43 #endif /* GR_HAVE_BYTESWAP */
44
45 static inline uint64_t htonll(uint64_t x){ return bswap_64(x);}
46 static inline uint64_t ntohll(uint64_t x){ return bswap_64(x);}
47
48 #endif /* GR_ARCH_BIGENDIAN */
49 #endif /* missing htonll or ntohll */ 
50
51 #if @GR_HAVE_ARPA_INET@  /* GR_HAVE_ARPA_INET */
52 #include <arpa/inet.h>
53 #elif @GR_HAVE_NETINET_IN@  /* GR_HAVE_NETINET_IN */
54 #include <netinet/in.h>
55 #else
56 #include <stdint.h>
57
58 #if @GR_ARCH_BIGENDIAN@  /* GR_ARCH_BIGENDIAN */
59 // Nothing to do...
60 static inline uint32_t htonl(uint32_t x){ return x; }
61 static inline uint16_t htons(uint16_t x){ return x; }
62 static inline uint32_t ntohl(uint32_t x){ return x; }
63 static inline uint16_t ntohs(uint16_t x){ return x; }
64 #else
65 #if @GR_HAVE_BYTESWAP@  /* GR_HAVE_BYTESWAP */
66 #include <byteswap.h>
67 #else
68 static inline uint16_t
69 bswap_16 (uint16_t x)
70 {
71   return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
72 }
73
74 static inline uint32_t
75 bswap_32 (uint32_t x)
76 {
77   return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) \
78         | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
79 }
80 #endif /* GR_HAVE_BYTESWAP */
81
82 static inline uint32_t htonl(uint32_t x){ return bswap_32(x); }
83 static inline uint16_t htons(uint16_t x){ return bswap_16(x); }
84 static inline uint32_t ntohl(uint32_t x){ return bswap_32(x); }
85 static inline uint16_t ntohs(uint16_t x){ return bswap_16(x); }
86 #endif /* GR_ARCH_BIGENDIAN */
87
88 #endif /* !(GR_HAVE_NETINET_IN || GR_HAVE_ARPA_INET) */
89
90 static inline uint8_t  ntohx(uint8_t  x){ return x;        }
91 static inline uint16_t ntohx(uint16_t x){ return ntohs(x); }
92 static inline uint32_t ntohx(uint32_t x){ return ntohl(x); }
93 static inline uint64_t ntohx(uint64_t x){ return ntohll(x);}
94 static inline uint8_t  htonx(uint8_t  x){ return x;        }
95 static inline uint16_t htonx(uint16_t x){ return htons(x); }
96 static inline uint32_t htonx(uint32_t x){ return htonl(x); }
97 static inline uint64_t htonx(uint64_t x){ return htonll(x);}
98
99 #endif /* INCLUDED_INET_H */