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
| #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <linux/if_ether.h> #include <netinet/if_ether.h> #include <linux/if_packet.h> #include <net/if.h> #include <string.h> #include <net/if_arp.h> #include <arpa/inet.h> #include <unistd.h>
int main() { int socket_fd = socket(AF_PACKET, SOCK_RAW, 0); if (socket_fd < 0) { perror("create socket failed"); exit(1); }
struct sockaddr_ll local; local.sll_family = AF_PACKET; local.sll_ifindex = (int) if_nametoindex("eth0"); socklen_t local_len = sizeof local; if (bind(socket_fd, (struct sockaddr *) &local, local_len) < 0) { perror("bind dev failed"); exit(1); } getsockname(socket_fd, (struct sockaddr *) &local, &local_len);
unsigned char buf[256]; struct ethhdr *eh = (struct ethhdr *) buf; struct ether_arp *arp = (struct ether_arp *) (eh + 1); size_t size = (unsigned char *) (struct ether_arp *) (arp + 1) - buf;
memset(eh->h_dest, 255, ETH_ALEN); memcpy(eh->h_source, local.sll_addr, ETH_ALEN); eh->h_proto = htons(ETH_P_ARP);
arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); arp->ea_hdr.ar_pro = htons(ETH_P_IP); arp->ea_hdr.ar_hln = ETH_ALEN; arp->ea_hdr.ar_pln = 4; arp->ea_hdr.ar_op = htons(ARPOP_REQUEST);
memcpy(arp->arp_sha, local.sll_addr, ETH_ALEN); struct in_addr src_ip; inet_aton("172.22.211.129", &src_ip); memcpy(arp->arp_spa, &src_ip.s_addr, 4);
memset(arp->arp_tha, 255, ETH_ALEN); struct in_addr dst_ip; inet_aton("172.22.208.1", &dst_ip); memcpy(arp->arp_tpa, &dst_ip.s_addr, 4);
while (1) { if (send(socket_fd, buf, size, 0) < 0) { perror("send arp failed"); exit(1); } printf("send arp success\n"); sleep(1); } }
|