当前位置:文档之家› ARP协议的C语言实现源代码

ARP协议的C语言实现源代码

target = *(argv+1); if (inet_aton(target, &dst) != 1) {
struct hostent *hp; hp = gethostbyname2(target, AF_INET); printf("\ntarget = %s \n", target ); if (!hp) {
printf("Interface \"%s\" is down\n", if_dev); exit(2); } if (ifr.ifr_flags&(IFF_NOARP|IFF_LOOPBACK)) {
printf("Interface \"%s\" is not ARPable\n", if_dev); exit(2); } return ifindex; } // check_device() int socket_init() {
fprintf(stderr, "arping: unknown iface %s\n", if_dev); exit(2); } ifindex = ifr.ifr_ifindex; if (ioctl(ss, SIOCGIFFLAGS, (char*)&ifr)) { perror("ioctl(SIOC); } if (!(ifr.ifr_flags&IFF_UP)) {
perror("bind"); exit(2); } int alen = sizeof(me); if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) { perror("getsockname"); exit(2); } if (me.sll_halen == 0) { printf("Interface \"%s\" is not ARPable (no ll address)\n", device); exit(2); } he = me; memset(he.sll_addr, -1, he.sll_halen); // set dmac addr FF:FF:FF:FF:FF:FF return s; } int create_pkt(unsigned char * buf, struct in_addr src, struct in_addr dst, struct sockaddr_ll * FROM, struct sockaddr_ll * TO) { struct arphdr *ah = (struct arphdr*) buf; unsigned char *p = (unsigned char *)(ah+1); ah->ar_hrd = htons(FROM->sll_hatype); if (ah->ar_hrd == htons(ARPHRD_FDDI)) ah->ar_hrd = htons(ARPHRD_ETHER); ah->ar_pro = htons(ETH_P_IP); ah->ar_hln = FROM->sll_halen; ah->ar_pln = 4; ah->ar_op = htons(ARPOP_REQUEST); memcpy(p, &FROM->sll_addr, ah->ar_hln); p+=FROM->sll_halen; memcpy(p, &src, 4); p+=4; memcpy(p, &TO->sll_addr, ah->ar_hln); p+=ah->ar_hln; memcpy(p, &dst, 4); p+=4;
#include <netdb.h> #include <unistd.h> #include <ctype.h> #include <errno.h> #include <signal.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <getopt.h> #include <sys/uio.h> #include <sys/file.h> #include <sys/time.h> #include <sys/signal.h> #include <sys/ioctl.h> #include <sys/param.h> #include <sys/socket.h> #include <linux/sockios.h> #include <linux/if.h> #include <linux/if_arp.h> #include <linux/if_packet.h> #include <linux/types.h> #include <netinet/in.h> #include <arpa/inet.h> #define src_addr "192.168.0.239" #define device "eth0" #define fill_buf "aaaaaaaaaaaa" int socket_id; char *target = src_addr; int send_count = 0; int recv_count = 0; struct in_addr src, dst; struct sockaddr_ll me, he; struct timeval send_time, recv_time; struct in_addr get_src_ip(char * devices) { struct sockaddr_in saddr; int sock_id = socket(AF_INET, SOCK_DGRAM, 0); if (sock_id < 0) {
from.sll_addr[2], from.sll_addr[3], from.sll_addr[4], from.sll_addr[5]); printf(" %ld.%ld ms\n", (long int)msecs, (long int)usecs); fflush(stdout); } void finish() { printf("\nSent %d ARP probe packet(s) \n", send_count); printf("Received %d response(s)", recv_count); printf("\n\n");
perror("socket"); exit(2); } if (devices) {
if (setsockopt(sock_id, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1)
perror("WARNING: interface is ignored"); } int alen = sizeof(saddr); memset(&saddr, 0, sizeof(saddr)); saddr.sin_port = htons(0x1000); saddr.sin_family = AF_INET; if (connect(sock_id, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
memcpy(p, fill_buf, strlen(fill_buf) ); p+=12; return (p-buf); } void send_pkt() { unsigned char send_buf[256]; int pkt_size = create_pkt(send_buf, src, dst, &me, &he); gettimeofday(&send_time, NULL); int cc = sendto(socket_id, send_buf, pkt_size, 0, (struct sockaddr*)&he, sizeof(he)); if( cc == pkt_size )
fflush(stdout); exit(!recv_count); } //////////////////////////////////////////////////////////////// int main(int argc, char **argv) { uid_t uid = getuid(); setuid(uid); if( *(argv+1) != NULL )
return 0; if (ah->ar_pro != htons(ETH_P_IP) || ah->ar_pln != 4 || ah->ar_hln != me.sll_halen )
return 0; memcpy(&src_ip, p+ah->ar_hln, 4); memcpy(&dst_ip, p+ah->ar_hln+4+ah->ar_hln, 4); if (src_ip.s_addr != dst.s_addr || src.s_addr != dst_ip.s_addr )
int s, s_errno; s = socket(PF_PACKET, SOCK_DGRAM, 0); s_errno = errno; me.sll_family = AF_PACKET; me.sll_ifindex = check_device(device, s); me.sll_protocol = htons(ETH_P_ARP); if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
相关主题