forked from br101/horst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
152 lines (132 loc) · 3.03 KB
/
util.c
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
142
143
144
145
146
147
148
149
150
151
152
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2005-2014 Bruno Randolf ([email protected])
*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include "util.h"
int
normalize(float oval, int max_val, int max) {
int val;
val= (oval / max_val) * max;
if (val > max) /* cap if still bigger */
val = max;
if (val == 0 && oval > 0)
val = 1;
if (val < 0)
val = 0;
return val;
}
#if DO_DEBUG
void
dump_packet(const unsigned char* buf, int len)
{
int i;
for (i = 0; i < len; i++) {
if ((i % 2) == 0) {
printf(" ");
}
if ((i % 16) == 0) {
printf("\n");
}
printf("%02x", buf[i]);
}
printf("\n");
}
#else
void
dump_packet(__attribute__((unused)) const unsigned char* buf,
__attribute__((unused)) int len)
{
}
#endif
const char*
ether_sprintf(const unsigned char *mac)
{
static char etherbuf[18];
snprintf(etherbuf, sizeof(etherbuf), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return etherbuf;
}
const char*
ether_sprintf_short(const unsigned char *mac)
{
static char etherbuf[5];
snprintf(etherbuf, sizeof(etherbuf), "%02x%02x",
mac[4], mac[5]);
return etherbuf;
}
const char*
ip_sprintf(const unsigned int ip)
{
static char ipbuf[18];
unsigned char* cip = (unsigned char*)&ip;
snprintf(ipbuf, sizeof(ipbuf), "%d.%d.%d.%d",
cip[0], cip[1], cip[2], cip[3]);
return ipbuf;
}
const char*
ip_sprintf_short(const unsigned int ip)
{
static char ipbuf[5];
unsigned char* cip = (unsigned char*)&ip;
snprintf(ipbuf, sizeof(ipbuf), ".%d", cip[3]);
return ipbuf;
}
void
convert_string_to_mac(const char* string, unsigned char* mac)
{
int c;
for(c = 0; c < 6 && string; c++) {
int x = 0;
if (string)
sscanf(string, "%x", &x);
mac[c] = x;
string = strchr(string, ':');
if (string)
string++;
}
}
const char*
kilo_mega_ize(unsigned int val) {
static char buf[20];
char c = 0;
int rest;
if (val >= 1024) { /* kilo */
rest = (val & 1023) / 102.4; /* only one digit */
val = val >> 10;
c = 'k';
}
if (val >= 1024) { /* mega */
rest = (val & 1023) / 102.4; /* only one digit */
val = val >> 10;
c = 'M';
}
if (c)
snprintf(buf, sizeof(buf), "%d.%d%c", val, rest, c);
else
snprintf(buf, sizeof(buf), "%d", val);
return buf;
}
/* simple ilog2 implementation */
int
ilog2(int x) {
int n;
for (n = 0; !(x & 1); n++)
x = x >> 1;
return n;
}