forked from br101/horst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
141 lines (121 loc) · 3.57 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2005-2015 Bruno Randolf (br1@einfach.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _UTIL_H_
#define _UTIL_H_
#include <stdio.h>
#ifdef _ALLBSD_SOURCE
#include <machine/endian.h>
#elif __linux__
#include <endian.h>
#endif
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define le64toh(x) OSSwapLittleToHostInt64(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#else
#include <byteswap.h>
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
#if !defined(le64toh)
#define le64toh(x) (x)
#endif
#if !defined(le32toh)
#define le32toh(x) (x)
#endif
#if !defined(le16toh)
#define le16toh(x) (x)
#endif
#if !defined(htole64)
#define htole64(x) (x)
#endif
#if !defined(htole32)
#define htole32(x) (x)
#endif
#if !defined(htole16)
#define htole16(x) (x)
#endif
#else
#if !defined(le64toh)
#define le64toh(x) bswap_64(x)
#endif
#if !defined(le32toh)
#define le32toh(x) bswap_32(x)
#endif
#if !defined(le16toh)
#define le16toh(x) bswap_16(x)
#endif
#if !defined(htole64)
#define htole64(x) bswap_64(x)
#endif
#if !defined(htole32)
#define htole32(x) bswap_32(x)
#endif
#if !defined(htole16)
#define htole16(x) bswap_16(x)
#endif
#endif
#define MAC_NOT_EMPTY(_mac) (_mac[0] || _mac[1] || _mac[2] || _mac[3] || _mac[4] || _mac[5])
#define MAC_EMPTY(_mac) (!_mac[0] && !_mac[1] && !_mac[2] && !_mac[3] && !_mac[4] && !_mac[5])
#define BIT(nr) (1 << (nr))
#define TOGGLE_BIT(_x, _m) (_x) ^= (_m)
/**
* TOGGLE_BITSET() - toggle set of bits as a whole
* @_x: an integer variable
* @_s: an integer variable interpreted as a bitset
* @_t: type (e.g. uint16_t)
* If any of the bits are set, all bits will be unset. Otherwise, if
* none of the bits are set, all bits will be set.
*/
#define TOGGLE_BITSET(_x, _s, _type) do { \
if ((_x) & (_s)) \
(_x) &= (_type)~(_s); \
else \
(_x) |= (_s); \
} while(0)
#define max(_x, _y) ((_x) > (_y) ? (_x) : (_y))
#define min(_x, _y) ((_x) < (_y) ? (_x) : (_y))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
void dump_packet(const unsigned char* buf, int len);
const char* ether_sprintf(const unsigned char *mac);
const char* ether_sprintf_short(const unsigned char *mac);
const char* ip_sprintf(const unsigned int ip);
const char* ip_sprintf_short(const unsigned int ip);
void convert_string_to_mac(const char* string, unsigned char* mac);
int normalize(float val, int max_val, int max);
const char* kilo_mega_ize(unsigned int val);
int ilog2(int x);
static inline int normalize_db(int val, int max)
{
if (val <= 30)
return 0;
else if (val >= 100)
return max;
else
return normalize(val - 30, 70, max);
}
static inline __attribute__((const))
int is_power_of_2(unsigned long n)
{
return (n != 0 && ((n & (n - 1)) == 0));
}
#endif