This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
142 lines (114 loc) · 2.94 KB
/
client.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
/*
This file is part of DNSsnarf. DNSsnarf 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, version 2.
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.
Copyright Cyso
*/
#include <pcap.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <unistd.h>
#include "types.h"
#include "dns.h"
#include "helper.h"
#include "shm.h"
uint64_t *qcounter;
uint64_t *acounter;
#define MODE_QUERY 1
#define MODE_ANSWER 2
#define MODE_TOTAL 3
void dump_counters() {
int i;
fprintf(stderr, "\x1B[2J");
fprintf(stderr, "QUERY STATS:\n");
for(i = 0; i < 255; i++) {
if (qcounter[i] != 0)
fprintf(stderr, "%5s: [%04X] = 0x%016llx\n", dns_record_type_name[i], i, qcounter[i]);
}
fprintf(stderr, "\nANSWER STATS:\n");
for(i = 0; i < 255; i++) {
if (qcounter[i] != 0)
fprintf(stderr, "%5s: [%04X] = 0x%016llx\n", dns_record_type_name[i], i, qcounter[i]);
}
}
void usage(char *prog) {
fprintf(
stderr,
"Usage : %s <options>\n\n"
"Options : -r RECORDTYPE\n"
" -q Count of incoming packets\n"
" -a Count of outgoing packets\n"
" -t Combined count\n\n", prog
);
}
int main(int argc, char *argv[]) {
int c, mode = 0, shmid, i, rec_idx=-1;
char *shm;
char *record_type = NULL;
uint64_t total = 0;
while((c = getopt(argc, argv, "qatr:")) != -1) {
switch(c) {
case 'r':
record_type = optarg;
break;
case 'q':
mode |= MODE_QUERY;
break;
case 'a':
mode |= MODE_ANSWER;
break;
case 't':
mode |= MODE_TOTAL;
break;
}
}
if (record_type == NULL || mode == 0) {
usage(argv[0]);
exit(-1);
}
// check recordtype validity
for(i = 0; i < 255; i++) {
if (dns_record_type_name[i] == NULL)
continue;
if (strcmp(dns_record_type_name[i], record_type) == 0) {
rec_idx = i;
break;
}
}
if (rec_idx == -1) {
fprintf(stderr, "Invalid recordtype: '%s'\n", argv[1]);
exit(-1);
}
// grab shm handle and all that
if ((shmid = shmget(SHM_KEY, SHM_SIZE, 0666)) < 0) {
perror("shmget");
exit(-1);
}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
// initialize counter pointers within shm object
qcounter = (uint64_t*)shm;
acounter = (qcounter + 256);
if (mode & MODE_QUERY)
total += qcounter[rec_idx];
if (mode & MODE_ANSWER)
total += acounter[rec_idx];
printf("%llx\n", total);
return 0;
}