Programed by Terry
Reffered cpu's source code in bbs.gznet.edu.cn socket ban
send arp request, get arp reply
get the mac address of the hosts in a subnet
gcc arp.c -o arp1
interface:default is eth0
Note : run it need root right or root has permitted it.
Example:
[root@46# work]# ./arp1 202.114.67.254
request mac 00:e0:4c:dd:79:e1, request IP 202.114.67.211
replied mac 00:e0:34:f0:18:08, replied IP 202.114.67.254
-----------------------------------------------------------------------------
Total Time Delay : 2314 us
[root@46# work]# ./arp1 202.114.67.254
request mac 00:e0:4c:dd:79:e1, request IP 202.114.67.211
--User Break!--
Total Time Delay : 3294147 us
*/
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h> /* struct in_addr : /usr/include/netinet/in.h */
#include <sys/types.h>
#include <sys/socket.h> /* /usr/src/linux/include/linux/socket.h */
#include <sys/ioctl.h>
#include <net/if.h> /* use the ifreq */
#include <net/if_arp.h> /* use the arphdr */
#include <net/ethernet.h> /* use the ethernethdr */
#include <signal.h>
#include <netinet/ip.h>
#include <sys/time.h> /* use the timeval */
u_char ether_dhost[ETH_ALEN]; /* destination eth addr */
u_char ether_shost[ETH_ALEN]; /* source ether addr */
u_short ether_type; /* packet type ID field */
/* arp or rarp header from /net/if_arp.h */
u_short ar_hrd; /* Format of hardware address. */
u_short ar_pro; /* Format of protocol address. */
u_char ar_hln; /* Length of hardware address. */
u_char ar_pln; /* Length of protocol address. */
u_short ar_op; /* ARP opcode (command). */
u_char ar_sha[ETH_ALEN]; /* Sender hardware address. */
u_char ar_sip[4]; /* Sender IP address. */
u_char ar_tha[ETH_ALEN]; /* Target hardware address. */
u_char ar_tip[4]; /* Target IP address. */
u_char padding[18]; /* padding */
};
struct in_addr myself, dstaddr; /* defined in /usr/include/netinet/in.h */
/* uint32_t s_addr;Internet address */
int fd_arp; /* socket fd for receiving packets */
struct ifreq ifr; /* ifreq structure : */
/* defined in /usr/include/net/if.h */
long send_time,recv_time; /* time that send & receive arp packet */
long time_now() /* return time passed by */
/* since 1970.1.1 00:00:00, */
/* in 1/1000000 second */
{
struct timeval now;
long lPassed;
gettimeofday(&now, 0);
lPassed = now.tv_sec * 1000000 + now.tv_usec; /* now.tv_sec in second */
/* now.tv_usec in 1/1000000 second */
return lPassed;
}
void stat() /* signal process : quit */
{
printf( "\n--User Break!--\nTotal Time Delay : %ld us\n",time_now()-send_time);
exit(-1);
}
main(int argc, char *argv[])
{
char device[32]; /* ethernet device name */
struct sockaddr from, to;
int fromlen;
struct sockaddr_in * sin_ptr;
u_char * ptr;
int n;
u_char buf[PACKET_SIZE]; /* buffer for send and recv */
struct arp_hdr * arp = (struct arp_hdr *)buf;
if (argc < 2) {
printf("usage: %s ip [interface]\n", argv[0]);
exit(0);
}
if (argc >= 3) {
strncpy(device, argv[2], 32); /* interface name */
device[31] = '\0'; /* make device a string */
} else
strcpy(device, DEF_INTERFACE);
dstaddr.s_addr = inet_addr(argv[1]); /* convert IP xx.xx.. to binary */