linux原始套接字-arp請求與接收
阿新 • • 發佈:2019-02-18
1 /** 2 * @file arp_request.c 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <unistd.h> 9 #include <sys/ioctl.h> 10 #include <sys/socket.h> 11 #include <arpa/inet.h> 12 #include <netinet/in.h> 13 #include <netinet/if_ether.h> 14#include <net/ethernet.h> 15 #include <net/if_arp.h> 16 #include <net/if.h> 17 #include <netpacket/packet.h> 18 19 /* 乙太網幀首部長度 */ 20 #define ETHER_HEADER_LEN sizeof(struct ether_header) 21 /* 整個arp結構長度 */ 22 #define ETHER_ARP_LEN sizeof(struct ether_arp) 23 /* 乙太網 + 整個arp結構長度*/ 24 #define ETHER_ARP_PACKET_LEN ETHER_HEADER_LEN + ETHER_ARP_LEN 25 /* IP地址長度 */ 26 #define IP_ADDR_LEN 4 27 /* 廣播地址 */ 28 #define BROADCAST_ADDR {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} 29 30 void err_exit(const char *err_msg) 31 { 32 perror(err_msg); 33 exit(1); 34 } 35 36 /* 填充arp包 */ 37struct ether_arp *fill_arp_packet(const unsigned char *src_mac_addr, const char *src_ip, const char *dst_ip) 38 { 39 struct ether_arp *arp_packet; 40 struct in_addr src_in_addr, dst_in_addr; 41 unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; 42 43 /* IP地址轉換 */ 44 inet_pton(AF_INET, src_ip, &src_in_addr); 45 inet_pton(AF_INET, dst_ip, &dst_in_addr); 46 47 /* 整個arp包 */ 48 arp_packet = (struct ether_arp *)malloc(ETHER_ARP_LEN); 49 arp_packet->arp_hrd = htons(ARPHRD_ETHER); 50 arp_packet->arp_pro = htons(ETHERTYPE_IP); 51 arp_packet->arp_hln = ETH_ALEN; 52 arp_packet->arp_pln = IP_ADDR_LEN; 53 arp_packet->arp_op = htons(ARPOP_REQUEST); 54 memcpy(arp_packet->arp_sha, src_mac_addr, ETH_ALEN); 55 memcpy(arp_packet->arp_tha, dst_mac_addr, ETH_ALEN); 56 memcpy(arp_packet->arp_spa, &src_in_addr, IP_ADDR_LEN); 57 memcpy(arp_packet->arp_tpa, &dst_in_addr, IP_ADDR_LEN); 58 59 return arp_packet; 60 } 61 62 /* arp請求 */ 63 void arp_request(const char *if_name, const char *dst_ip) 64 { 65 struct sockaddr_ll saddr_ll; 66 struct ether_header *eth_header; 67 struct ether_arp *arp_packet; 68 struct ifreq ifr; 69 char buf[ETHER_ARP_PACKET_LEN]; 70 unsigned char src_mac_addr[ETH_ALEN]; 71 unsigned char dst_mac_addr[ETH_ALEN] = BROADCAST_ADDR; 72 char *src_ip; 73 int sock_raw_fd, ret_len, i; 74 75 if ((sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) 76 err_exit("socket()"); 77 78 bzero(&saddr_ll, sizeof(struct sockaddr_ll)); 79 bzero(&ifr, sizeof(struct ifreq)); 80 /* 網絡卡介面名 */ 81 memcpy(ifr.ifr_name, if_name, strlen(if_name)); 82 83 /* 獲取網絡卡介面索引 */ 84 if (ioctl(sock_raw_fd, SIOCGIFINDEX, &ifr) == -1) 85 err_exit("ioctl() get ifindex"); 86 saddr_ll.sll_ifindex = ifr.ifr_ifindex; 87 saddr_ll.sll_family = PF_PACKET; 88 89 /* 獲取網絡卡介面IP */ 90 if (ioctl(sock_raw_fd, SIOCGIFADDR, &ifr) == -1) 91 err_exit("ioctl() get ip"); 92 src_ip = inet_ntoa(((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr); 93 printf("local ip:%s\n", src_ip); 94 95 /* 獲取網絡卡介面MAC地址 */ 96 if (ioctl(sock_raw_fd, SIOCGIFHWADDR, &ifr)) 97 err_exit("ioctl() get mac"); 98 memcpy(src_mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); 99 printf("local mac"); 100 for (i = 0; i < ETH_ALEN; i++) 101 printf(":%02x", src_mac_addr[i]); 102 printf("\n"); 103 104 bzero(buf, ETHER_ARP_PACKET_LEN); 105 /* 填充以太首部 */ 106 eth_header = (struct ether_header *)buf; 107 memcpy(eth_header->ether_shost, src_mac_addr, ETH_ALEN); 108 memcpy(eth_header->ether_dhost, dst_mac_addr, ETH_ALEN); 109 eth_header->ether_type = htons(ETHERTYPE_ARP); 110 /* arp包 */ 111 arp_packet = fill_arp_packet(src_mac_addr, src_ip, dst_ip); 112 memcpy(buf + ETHER_HEADER_LEN, arp_packet, ETHER_ARP_LEN); 113 114 /* 傳送請求 */ 115 ret_len = sendto(sock_raw_fd, buf, ETHER_ARP_PACKET_LEN, 0, (struct sockaddr *)&saddr_ll, sizeof(struct sockaddr_ll)); 116 if ( ret_len > 0) 117 printf("sendto() ok!!!\n"); 118 119 close(sock_raw_fd); 120 } 121 122 int main(int argc, const char *argv[]) 123 { 124 if (argc != 3) 125 { 126 printf("usage:%s device_name dst_ip\n", argv[0]); 127 exit(1); 128 } 129 130 arp_request(argv[1], argv[2]); 131 132 return 0; 133 }