-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp.c
executable file
·74 lines (59 loc) · 1.77 KB
/
arp.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
#include "arp.h"
//int main() {
// arp_set("192.168.2.64","00:00:00:64:64:64");
// arp_del("192.168.2.64");
// exit(0);
//}
int arp_set(char* ip, char* mac) {
int sockfd;
struct arpreq req;
struct ether_addr* mac_addr;
struct hostent* hp;
//get enough memory for struct
memset((char*) &req,0,sizeof(req));
//fill in ip address
hp = gethostbyname(ip);
req.arp_pa.sa_family=AF_INET;
memcpy(&(req.arp_pa.sa_data[2]),hp->h_addr, hp->h_length);
//fill in mac address
req.arp_ha.sa_family=ARPHRD_ETHER;
mac_addr = ether_aton((const char*)mac);
memcpy(&(req.arp_ha.sa_data[0]),mac_addr->ether_addr_octet,ETHER_ADDR_LEN);
//set needed flags (commit and add permanently)
req.arp_flags = ATF_PERM | ATF_COM;
//set correct device
strcpy(req.arp_dev, ""); //alternatively: "eth0"
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/* Call the kernel. */
if (ioctl(sockfd, SIOCSARP, &req) < 0) {
perror("SIOCSARP");
close(sockfd);
return (0);
}
close(sockfd);
return (1);
}
int arp_del(char* ip) {
int sockfd;
struct arpreq req;
struct hostent* hp;
//get enough memory for struct
memset((char*) &req,0,sizeof(req));
//fill in ip address
hp = gethostbyname(ip);
req.arp_pa.sa_family=AF_INET;
memcpy(&(req.arp_pa.sa_data[2]),hp->h_addr, hp->h_length);
//set needed flags (commit and add permanently)
req.arp_flags = ATF_COM;
//set correct device
strcpy(req.arp_dev, ""); //alternatively: "eth0"
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/* Call the kernel. */
if (ioctl(sockfd, SIOCDARP, &req) < 0) {
perror("SIOCDARP");
close(sockfd);
return (0);
}
close(sockfd);
return (1);
}