-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.c
More file actions
61 lines (49 loc) · 1.32 KB
/
block.c
File metadata and controls
61 lines (49 loc) · 1.32 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <assert.h>
int main(int argc, char *argv[]) {
size_t n;
int sock;
struct sockaddr_in servaddr;
unsigned char block_addr[5];
int ret;
int off;
int block_rate;
if (argc != 3) {
printf("Usage: provide an <IP address< <block rate>\n");
exit(0);
}
block_rate = atoi(argv[2]);
if (block_rate < 0 || block_rate > 100) {
printf("block rate must be between 0 and 100\n");
exit(0);
}
block_addr[4] = (unsigned char)block_rate;
ret = sscanf(argv[1], "%hhu.%hhu.%hhu.%hhu%n",
&block_addr[0], &block_addr[1], &block_addr[2], &block_addr[3], &off);
if (ret != 4 || argv[1][off] != '\0') {
printf("couldn't understand addr %s\n", argv[1]);
exit(0);
}
printf("Blocking: %d.%d.%d.%d\n",
(int)block_addr[0], (int)block_addr[1], (int)block_addr[2], (int)block_addr[3]);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket creation failed");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(100);
servaddr.sin_addr.s_addr = inet_addr("100.100.100.100");
n = sendto(sock, block_addr, 5, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
if (n != 5) {
perror("send error");
}
close(sock);
return 0;
}