util.h 3.57 KB
Newer Older
1
/* horst - Highly Optimized Radio Scanning Tool
2
 *
3
 * Copyright (C) 2005-2016 Bruno Randolf (br1@einfach.org)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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_

23
#include <stdio.h>
Bruno Randolf's avatar
Bruno Randolf committed
24 25 26 27

#ifdef _ALLBSD_SOURCE
#include <machine/endian.h>
#elif __linux__
28
#include <endian.h>
Bruno Randolf's avatar
Bruno Randolf committed
29 30 31 32
#endif

#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
Bruno Randolf's avatar
Bruno Randolf committed
33 34 35 36 37 38
#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)
Bruno Randolf's avatar
Bruno Randolf committed
39
#else
40
#include <byteswap.h>
Bruno Randolf's avatar
Bruno Randolf committed
41
#endif
42

43

44
#if BYTE_ORDER == LITTLE_ENDIAN
45 46

#if !defined(le64toh)
47
	#define le64toh(x) (x)
48 49
#endif
#if !defined(le32toh)
50
	#define le32toh(x) (x)
51 52
#endif
#if !defined(le16toh)
53
	#define le16toh(x) (x)
54 55
#endif
#if !defined(htole64)
56
	#define htole64(x) (x)
57 58
#endif
#if !defined(htole32)
59
	#define htole32(x) (x)
60 61
#endif
#if !defined(htole16)
62
	#define htole16(x) (x)
63 64
#endif

65
#else
66
#if !defined(le64toh)
67
	#define le64toh(x) bswap_64(x)
68 69
#endif
#if !defined(le32toh)
70
	#define le32toh(x) bswap_32(x)
71 72
#endif
#if !defined(le16toh)
73
	#define le16toh(x) bswap_16(x)
74 75
#endif
#if !defined(htole64)
76
	#define htole64(x) bswap_64(x)
77 78
#endif
#if !defined(htole32)
79
	#define htole32(x) bswap_32(x)
80 81
#endif
#if !defined(htole16)
82 83
	#define htole16(x) bswap_16(x)
#endif
84
#endif
85

Bruno Randolf's avatar
Bruno Randolf committed
86
#define MAC_NOT_EMPTY(_mac) (_mac[0] || _mac[1] || _mac[2] || _mac[3] || _mac[4] || _mac[5])
Bruno Randolf's avatar
Bruno Randolf committed
87

88
#define MAC_EMPTY(_mac) (!_mac[0] && !_mac[1] && !_mac[2] && !_mac[3] && !_mac[4] && !_mac[5])
Bruno Randolf's avatar
Bruno Randolf committed
89

Bruno Randolf's avatar
Bruno Randolf committed
90 91
#define BIT(nr) (1 << (nr))

92
#define TOGGLE_BIT(_x, _m) (_x) ^= (_m)
Bruno Randolf's avatar
Bruno Randolf committed
93

Bruno Randolf's avatar
Bruno Randolf committed
94 95 96 97
/**
 * TOGGLE_BITSET() - toggle set of bits as a whole
 * @_x: an integer variable
 * @_s: an integer variable interpreted as a bitset
Bruno Randolf's avatar
Bruno Randolf committed
98
 * @_t: type (e.g. uint16_t)
Bruno Randolf's avatar
Bruno Randolf committed
99 100 101 102
 * 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 {	\
Bruno Randolf's avatar
Bruno Randolf committed
103
		if ((_x) & (_s))		\
Bruno Randolf's avatar
Bruno Randolf committed
104
		(_x) &= (_type)~(_s);		\
Bruno Randolf's avatar
Bruno Randolf committed
105 106 107
		else				\
			(_x) |= (_s);		\
		} while(0)
Bruno Randolf's avatar
Bruno Randolf committed
108

109
#define max(_x, _y) ((_x) > (_y) ? (_x) : (_y))
Bruno Randolf's avatar
Bruno Randolf committed
110

111
#define min(_x, _y) ((_x) < (_y) ? (_x) : (_y))
112

113 114
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

Bruno Randolf's avatar
Bruno Randolf committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
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);
}

Bruno Randolf's avatar
Bruno Randolf committed
135 136 137 138 139
static inline __attribute__((const))
int is_power_of_2(unsigned long n)
{
	return (n != 0 && ((n & (n - 1)) == 0));
}
140

141
#endif