-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniffer.c
216 lines (190 loc) · 6.38 KB
/
sniffer.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <dbus/dbus.h>
#include "packet_snifferd.h"
#include "statistic.h"
static void show_usage();
static void start_daemon();
static void stop_daemon();
static void show_ip_count(char *);
static void select_interface(char *);
static void print_statistics(char *);
static void reset_statistics();
static bool init_dbus_connection(DBusError *, DBusConnection **,
dbus_uint32_t *, bool check_bus, char *);
/* Cli controlling daemon that sniffs packets */
int main(int argc, char **argv){
if (argc < 2 || strcmp(argv[1], "--help") == 0){
show_usage();
return -1;
}
if (strcmp(argv[1], "start") == 0){
if (argc != 2){
show_usage();
return -1;
}
start_daemon();
} else if (strcmp(argv[1], "stop") == 0){
if (argc != 2){
show_usage();
return -1;
}
stop_daemon();
} else if (strcmp(argv[1], "show") == 0){
if (argc != 4 || strcmp(argv[3], "count") != 0){
show_usage();
return -1;
}
show_ip_count(argv[2]);
} else if (strcmp(argv[1], "select") == 0){
if (argc != 4 || strcmp(argv[2], "iface") != 0){
show_usage();
return -1;
}
select_interface(argv[3]);
} else if (strcmp(argv[1], "stat") == 0){
char *if_name = NULL;
if (argc > 3){
show_usage();
return -1;
}
if (argc == 3)
if_name = argv[2];
print_statistics(if_name);
} else if (strcmp(argv[1], "reset") == 0){
if (argc != 2){
show_usage();
return -1;
}
reset_statistics();
} else {
show_usage();
return -1;
}
return 0;
}
/* Echoes usage information */
static void show_usage(){
printf ("Packet sniffer cli\n");
printf ("Usage:\n");
printf ("\t> sniffer COMMAND [IFACE]...\n");
printf ("\tstart - starts sniffing daemon\n");
printf ("\tstop - stops sniffing daemon\n");
printf ("\tshow [ip] count - prints number of packets coming from ip\n");
printf ("\tselect iface [iface] - directs daemon so sniff packets from interface\n");
printf ("\treset - deletes all statistics\n");
printf ("\tstat [iface] - show statistics of inerface, of all if omitted\n");
printf ("\t--help - output this description message\n");
}
/* Starts sniffer daemon */
static void start_daemon(){
printf ("Starting daemon\n");
// Create dbus structs
dbus_uint32_t id = 0;
DBusError error;
DBusConnection *connection = NULL;
// initialize dbus constructs
if (!init_dbus_connection(&error, &connection, &id, false, NULL)){
printf("Can send message on the bus\n");
return;
}
// Point to the trigger
dbus_bus_request_name(connection, DBUS_ACTIVATOR_DEST, 0, &error);
// Create dummy message
DBusMessage *message;
message = dbus_message_new_method_call(DBUS_ACTIVATOR_DEST, // rest args are dummy
MAIN_OBJECT, CHANGE_IFACE_INTERFACE, CHANGE_IFACE_METHOD);
// 'knock on the door' effectively starting daemon process
dbus_connection_send (connection, message, &id);
printf ("Done\n");
}
/* Stops sniffer daemon */
static void stop_daemon(){
printf ("Stopping daemon\n");
// declare dbus structs
dbus_uint32_t id = 0;
DBusError error;
DBusConnection *connection = NULL;
// initialize dbus constructs
if (!init_dbus_connection(&error, &connection, &id, true, SNIFFERD_DEST)){
fprintf(stderr, "Can send message on the bus\n");
return;
}
// Construct message
DBusMessage *stop_message;
stop_message = dbus_message_new_method_call(SNIFFERD_DEST, MAIN_OBJECT,
STOP_INTERFACE, STOP_METHOD);
// Send message
dbus_connection_send (connection, stop_message, &id);
printf ("Done\n");
}
/* Prints num of hits for given ip address */
static void show_ip_count(char *ip){
connect_to_db();
printf ("Showing count for %s\n", ip);
print_ip_count(ip);
}
static bool init_dbus_connection(DBusError *error, DBusConnection **connection,
dbus_uint32_t *id, bool check_bus,
char *well_known_name){
dbus_error_init (error);
// connect to the bus
*connection = dbus_bus_get (DBUS_BUS_SYSTEM, error);
if (dbus_error_is_set (error)){
fprintf (stderr, "fatal: Error acquiring system bus %s\n", error->message);
dbus_error_free (error);
return false;
}
// see if daemon is running
if (check_bus && !dbus_bus_name_has_owner(*connection, well_known_name, error)){
fprintf (stderr, "fatal: Daemon not running!! %s\n", error->message);
dbus_error_free (error);
return false;
}
return true;
}
/* Directs daemon to sniff on given interface */
static void select_interface(char *iface){
printf ("Making daemon listen to %s\n", iface);
// Create dbus structs
dbus_uint32_t id = 0;
DBusError error;
DBusConnection *connection = NULL;
// initialize dbus constructs
if (!init_dbus_connection(&error, &connection, &id, true, SNIFFERD_DEST))
return;
// create message
DBusMessage *message;
message = dbus_message_new_method_call(SNIFFERD_DEST, MAIN_OBJECT,
CHANGE_IFACE_INTERFACE, CHANGE_IFACE_METHOD);
if (message == NULL){
fprintf (stderr, "fatal: Can't initiate method call\n");
dbus_error_free (&error);
return;
}
// Add new iface name as argument to message
dbus_message_append_args (message, DBUS_TYPE_STRING, &iface,
DBUS_TYPE_INVALID);
// Send message
if (!dbus_connection_send (connection, message, &id)){
fprintf (stderr, "fatal: Memory overflow");
dbus_error_free (&error);
return;
}
dbus_message_unref (message);
dbus_error_free (&error);
}
static void print_statistics(char *iface){
connect_to_db();
if (iface == NULL)
printf ("Showing statistics for all interfaces\n");
else
printf ("Showing statistics for iface %s\n", iface);
print_all_statistics(iface);
}
static void reset_statistics(){
connect_to_db();
reset_db();
}