ARP的C代码
Linux下的Socket写的C程序,用于模拟ARP包
下载地址:https://files.cnblogs.com/istrong/arp.rar
1 #include <netdb.h> 2 #include <sys/socket.h> 3 #include <sys/types.h> 4 #include <stdio.h> 5 #include <errno.h> 6 #include <sys/ioctl.h> 7 #include <net/if.h> 8 #include <signal.h> 9 #include <netinet/ip.h> 10 #include <netinet/in.h> 11 #include <string.h> 12 #include <arpa/inet.h> 13 #include <netinet/ip_icmp.h> 14 #include <linux/if_ether.h> 15 #define ETH_HW_ADDR_LEN 6 16 #define IP_ADDR_LEN 4 17 #define ARP_FRAME_TYPE 0x0806 18 #define ETHER_HW_TYPE 1 19 #define IP_PROTO_TYPE 0x0800 20 #define OP_ARP_REQUEST 2 21 #define OP_ARP_QUEST 1 22 #define DEFAULT_DEVICE "eth0" 23 24 25 char usage[] = 26 {"send_arp: sends out custom ARP packet. ferrysnow@gmail.com \nusage : send_arp src_ip_addr src_hw_addr targ_ip_addr tar_hw_addr number"}; 27 28 struct arp_packet 29 { 30 u_char targ_hw_addr[ETH_HW_ADDR_LEN]; 31 u_char src_hw_addr[ETH_HW_ADDR_LEN]; 32 u_short frame_type; 33 u_short hw_type; 34 u_short prot_type; 35 u_char hw_addr_size; 36 u_char prot_addr_size; 37 u_short op; 38 u_char sndr_hw_addr[ETH_HW_ADDR_LEN]; 39 u_char sndr_ip_addr[IP_ADDR_LEN]; 40 u_char rcpt_hw_addr[ETH_HW_ADDR_LEN]; 41 u_char rcpt_ip_addr[IP_ADDR_LEN]; 42 u_char padding[18]; 43 }; 44 45 void die(char*); 46 void get_ip_addr(struct in_addr*, char*); 47 void get_hw_addr(char*, char*); 48 49 int main(int argc, char* argv[]) 50 { 51 struct in_addr src_in_addr, targ_in_addr; 52 struct arp_packet pkt; 53 struct sockaddr sa; 54 int sock; 55 int j, number; 56 if (argc != 6) 57 { 58 die(usage); 59 } 60 sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_RARP)); 61 if (sock < 0) 62 { 63 printf("socket error"); 64 exit(1); 65 } 66 67 number = atoi(argv[5]); 68 pkt.frame_type = htons(ARP_FRAME_TYPE); 69 pkt.hw_type = htons(ETHER_HW_TYPE); 70 pkt.prot_type = htons(IP_PROTO_TYPE); 71 pkt.hw_addr_size = ETH_HW_ADDR_LEN; 72 pkt.prot_addr_size = IP_ADDR_LEN; 73 pkt.op = htons(OP_ARP_QUEST); 74 get_hw_addr(pkt.targ_hw_addr, argv[4]); 75 get_hw_addr(pkt.rcpt_hw_addr, argv[4]); 76 get_hw_addr(pkt.src_hw_addr, argv[2]); 77 get_hw_addr(pkt.sndr_hw_addr, argv[2]); 78 get_ip_addr(&src_in_addr, argv[1]); 79 get_ip_addr(&targ_in_addr, argv[3]); 80 memcpy(pkt.sndr_ip_addr, &src_in_addr, IP_ADDR_LEN); 81 memcpy(pkt.rcpt_ip_addr, &targ_in_addr, IP_ADDR_LEN); 82 bzero(pkt.padding, 18); 83 strcpy(sa.sa_data, DEFAULT_DEVICE); 84 for (j = 0; j < number; j++) 85 { 86 if (sendto(sock, &pkt, sizeof(pkt), 0, &sa, sizeof(sa)) < 0) 87 { 88 perror("sendto"); 89 exit(1); 90 } 91 } 92 exit(0); 93 } 94 95 96 void die(char* str) 97 { 98 fprintf(stderr, "%s\n", str); 99 exit(1); 100 } 101 102 103 void get_ip_addr(struct in_addr* in_addr, char* str) 104 { 105 struct hostent* hostp; 106 in_addr->s_addr = inet_addr(str); 107 if (in_addr->s_addr == -1) 108 { 109 if ((hostp = gethostbyname(str))) 110 { 111 bcopy(hostp->h_addr, in_addr, hostp->h_length); 112 } 113 else 114 { 115 fprintf(stderr, "send_arp: unknown host %s\n", str); 116 exit(1); 117 } 118 } 119 } 120 121 122 void get_hw_addr(char* buf, char* str) 123 { 124 int i; 125 char c, val; 126 for (i = 0; i < ETH_HW_ADDR_LEN; i++) 127 { 128 if (!(c = tolower(*str++))) 129 { 130 die("Invalid hardware address"); 131 } 132 if (isdigit(c)) 133 { 134 val = c - '0'; 135 } 136 else if (c >= 'a' && c <= 'f') 137 { 138 val = c - 'a' + 10; 139 } 140 else 141 { 142 die("Invalid hardware address"); 143 } 144 *buf = val << 4; 145 if (!(c = tolower(*str++))) 146 { 147 die("Invalid hardware address"); 148 } 149 if (isdigit(c)) 150 { 151 val = c - '0'; 152 } 153 else if (c >= 'a' && c <= 'f') 154 { 155 val = c - 'a' + 10; 156 } 157 else 158 { 159 die("Invalid hardware address"); 160 } 161 *buf++ |= val; 162 if (*str == ':') 163 { 164 str++; 165 } 166 } 167 }