-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacwatcher.c
336 lines (299 loc) · 8.71 KB
/
macwatcher.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <linux/errqueue.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#define PACKET_SIZE 64 // packet size of each echo packet
#define SENDER_LIMIT 12 // max try logic to send ping
#define free_defer(value) \
do { \
result = (value); \
goto defer; \
} while (0)
typedef struct {
int sock;
struct sockaddr_in to;
struct sockaddr_in from;
char* interface_name;
} Echo;
#define DEFAULT_HOST "google.com"
int sender_count;
typedef struct {
struct icmphdr hdr;
char msg[PACKET_SIZE - sizeof(struct icmphdr)];
} Ping_Packet;
void usage()
{
printf("Net Watcher\n");
printf(" Usage: netwatcher <INTERFACE_NAME> OPTIONS \n");
printf(" Options:\n");
printf(" <REMOTE_HOST>: remove host of your choice ");
printf(" default one is google.com");
printf(" <PING_TIMEOUT>: ping timeout in secs");
}
char* shift_args(int* argc, char*** argv)
{
// assert(*argc > 0);
char* result = **argv;
*argc -= 1;
*argv += 1;
return result;
}
u_char* get_mac_linux(int sock, const char* ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
int ret = ioctl(sock, SIOCGIFHWADDR, &ifr);
if (ret < 0) {
perror("could not get mac address");
close(sock);
exit(1);
}
unsigned char* mac = (unsigned char*)ifr.ifr_ifru.ifru_hwaddr.sa_data;
return mac;
}
int set_mac_linux(int sockfd, const char* interface_name, u_char mac[])
{
struct ifreq ifr;
// TURN DOWN INTERFACE;
strncpy(ifr.ifr_name, interface_name, IFNAMSIZ);
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
perror("could not turn down");
close(sockfd);
return -1;
}
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
perror("could not set flags down");
close(sockfd);
return -1;
}
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(ifr.ifr_hwaddr.sa_data, mac, 6);
if (ioctl(sockfd, SIOCSIFHWADDR, &ifr) < 0) {
perror("could not set new mac");
close(sockfd);
return -1;
}
printf("MAC ADDRESS SET DONE\n");
// TURN UP INTERFACE AGAIN
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
perror("could not back up");
close(sockfd);
return -1;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
perror("could not set up flags");
close(sockfd);
return -1;
}
close(sockfd);
return 0;
}
typedef struct {
pthread_t thread_id;
int thread_num;
} Thread_Info;
// RFC 1071
uint16_t checksum(uint16_t* addr, int len)
{
int count = len;
register uint32_t sum = 0;
uint16_t answer = 0;
while (count > 1) {
sum += *(addr++);
count -= 2;
}
if (count > 0) {
sum += *(uint8_t*)addr;
}
while (sum >> 16) {
sum = (sum & 0xffff) + (sum >> 16);
}
answer = ~sum;
return (answer);
}
int ping_timeout;
void* main_loop(void* icmp_raw)
{
Echo echo = *(Echo*)icmp_raw;
char packet[PACKET_SIZE];
memset(&packet, 0, PACKET_SIZE);
Ping_Packet* ping_pkt = (Ping_Packet*)packet;
ping_pkt->hdr.type = ICMP_ECHO;
ping_pkt->hdr.code = 0;
ping_pkt->hdr.un.echo.sequence = htons(1);
ping_pkt->hdr.un.echo.id = htons(getpid());
ping_pkt->hdr.checksum = checksum((uint16_t*)&packet, PACKET_SIZE);
srand(time(NULL));
u_char mac[] = { 0x00, 0x16, 0x3E, 0x33, 0x44, 0x66 };
for (int i = 2; i < 6; ++i) {
mac[i] = rand() % 256;
}
bool flag = 1;
while (flag) {
sleep(ping_timeout);
for (int i = 0; i < 4; ++i) {
if (sendto(echo.sock,
packet,
PACKET_SIZE,
0,
(struct sockaddr*)&echo.to,
sizeof(echo.to))
< 0) {
perror("sendto:");
}
}
// Type
// 8 for echo message;
// 0 for echo reply message.
Ping_Packet* ping_pkt = (Ping_Packet*)packet;
if (ping_pkt->hdr.type == 8) {
printf("SENDING ICMP MSG\n");
sender_count += 1;
}
if (sender_count > SENDER_LIMIT) {
int udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/// DESTINATION UNREACHABLE
/// that's maybed caused by no doing mac randomization
/// so let's randomize the mac then
if (get_mac_linux(udp_sock, echo.interface_name) == NULL) {
printf("COULD NOT GET MAC ADDRESS");
}
if (set_mac_linux(udp_sock, echo.interface_name, mac) < 0) {
printf("COULD NOT SET MAC ADDRESS");
}
sender_count = 0;
close(udp_sock);
}
}
}
int main(int argc, char** argv)
{
if (argc < 2) {
usage();
exit(1);
}
const char* program = shift_args(&argc, &argv);
char* interface_name = shift_args(&argc, &argv);
const char* optional_host = shift_args(&argc, &argv);
char* timeout = shift_args(&argc, &argv);
optional_host = (!optional_host) ? DEFAULT_HOST : optional_host;
ping_timeout = (!timeout) ? 300 : atoi(timeout);
printf("RUNNING %s\n", program);
Echo echo = { 0 };
echo.interface_name = interface_name;
int result = 0;
#ifdef __linux__
echo.sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (echo.sock < 0) {
perror("socket:");
free_defer(1);
}
int ttl_val = 64;
if (setsockopt(echo.sock, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) < 0) {
printf("Settting socket options to TLL failed\n");
free_defer(1);
}
struct hostent* hp = gethostbyname(optional_host);
if (hp == NULL) {
perror("gethostbyname:");
free_defer(1);
}
memset(&echo.to, 0, sizeof(echo.to));
echo.to.sin_family = AF_INET;
memcpy(&echo.to.sin_addr.s_addr, hp->h_addr_list[0], 4);
char buffer[PACKET_SIZE];
socklen_t from_len = sizeof(echo.from);
Thread_Info t_info = { 0 };
int rc = pthread_create(&t_info.thread_id, NULL, main_loop, (void*)&echo);
if (rc) {
printf("Could not start new thread\n");
free_defer(1);
}
// #define EAGAIN 11 /* Try again */
// #define ENOMEM 12 /* Out of memory */
while (1) {
// TODO: what if its an hotspot conneciton, hotspot connections just are
// Message Type 3 (the Most Important one)
// Type
// 3
// Code
// 0 = net unreachable;
// 1 = host unreachable;
// 2 = protocol unreachable;
// 3 = port unreachable;
// 4 = fragmentation needed and DF set;
// 5 = source route failed.
if (recvfrom(echo.sock,
buffer,
sizeof(buffer),
0,
(struct sockaddr*)&echo.from,
&from_len)
< 0) {
printf("recvfrom errno %d\n", errno);
if (errno == EAGAIN || errno == EINTR)
printf("got an error try again!");
continue;
}
Ping_Packet* ping_pkg = (Ping_Packet*)buffer;
if (ping_pkg->hdr.type == 3) {
switch (ping_pkg->hdr.code) {
case 0:
printf("Net Unreachable\n");
break;
case 1:
printf("Host Unreachable\n");
break;
case 2:
printf("Protocol Unreachable\n");
break;
case 3:
printf("Port Unreachable\n");
break;
case 5:
printf("Source Route Failed\n");
break;
default:
printf("Destination Unreacable");
break;
}
}
if (ping_pkg->hdr.type == 0) {
printf("ICMP MSG RECEIVED\n");
sender_count = 0;
}
}
if (pthread_join(t_info.thread_id, NULL) < 0) {
printf("could not join thread\n");
free_defer(1);
}
#endif /* ifdef __linux__*/
#ifdef __APPLE__
assert(0 && "NOT IMPLEMENTED YET ON MACOS");
#endif /* ifdef _APPLE__ */
#ifdef __WIN32
assert(0 && "NOT IMPLEMENTED YET ON WINDOWS");
#endif /* ifdef __WIN32__*/
close(echo.sock);
return 0;
defer:
close(echo.sock);
return result;
}