-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntc_tools.c
112 lines (99 loc) · 2.25 KB
/
ntc_tools.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
#include <ntc.h>
#include <ntc_tools.h>
bool primality_test(int p) {
int rest;
rest = mod(p,6);
if (rest==1 || rest==5)
return true;
return false;
}
int mod(int p, int m) {
do
p = p - m;
while (p>m);
return p;
}
void quit(char *s) {
printf("\n%s\n",s);
exit(EXIT_FAILURE);
}
/*
Select 'num' bytes from the packet 'p' from position 'poz'.
If size of packet is too small and 'poz' exceeds packet size then returns -1
*/
int get_from_packet(packet *p, u_int8_t poz, u_int8_t num) {
int number;
u_int8_t l, i;
number = 0;
if (p->size < (poz+num))
number = -1;
else {
l = 8 * (num-1);
for (i=poz; i<(poz+num); i++) {
number = number + (0xFFFFFFFF & (p->buff[i]<<l));
l = l - 8;
}
}
return number;
}
/*
Detects the traffic direction.
Returns 0 (incoming) or 1 (outgoing).
*/
u_int8_t detect_direction(u_int32_t IPs, u_int32_t IPd, u_int32_t ip_if) {
u_int8_t d;
if ((IPd != ip_if) && (IPs != ip_if))
d = 0;
else {
if (IPd == ip_if)
d = 0;
else
d = 1;
}
return d;
}
int compare_keys(u_int32_t current_srcIP, u_int32_t current_dstIP, u_int32_t new_srcIP, u_int32_t new_dstIP) {
if ((new_srcIP == current_srcIP) && (new_dstIP == current_dstIP)) return 0;
if (new_srcIP > current_srcIP) return 1;
if (new_srcIP < current_srcIP) return -1;
if (new_dstIP > current_dstIP) return 1;
if (new_dstIP < current_dstIP) return -1;
return 0;
}
int ht_count(hashtable *ht) {
int count;
count = 0;
while (ht) {
count++;
ht = ht->next;
}
return count;
}
void int_date_to_str(u_int32_t ymd, char *str) {
int p;
u_int16_t y;
u_int8_t d,i;
char c, r, m[4];
y = ymd >> 16;
for (i=0; i<5; i++) {
d = ymd & 0x000000FF;
c = (d / 10) + '0';
r = (d % 10) + '0';
str[i] = c; i++;
str[i] = r; i++;
str[i] = '.';
ymd = ymd >> 8;
}
for (p = 0; p < 4; p++) {
r = y % 10;
y = y / 10;
m[p] = r + '0';
}
for (p=p-1; p>=0; p--,i++)
str[i] = m[p];
str[i] = '\0';
}