"Go" BusyBox analysis--arp set the MAC address in the ARP cache table
Transferred from: http://blog.chinaunix.net/uid-26009923-id-5098083.html
1. Modify the MAC address of an IP in the ARP cache table
[email protected]:/work/test/tcpip/busy/arp$ cat arp.c #include "utils.h" #include <net/if_arp.h> #include <linux/sockios.h>//usage:arp 192.168.4.111 78:6a:89:18:31:0c#define eth_alen 6int inet_resolve (const char *name , struct sockaddr_in *s_in) {struct hostent *hp; /* Grmpf.-FVK */s_in->sin_family = af_inet; S_in->sin_port = 0; /* See if it ' s a dotted quad. */if (Inet_aton (name, &s_in->sin_addr)) {return 0; } HP = gethostbyname (name); if (HP = = NULL) {return-1; } memcpy (&s_in->sin_addr, hp->h_addr_list[0], sizeof (struct in_addr)); return 0;} /* Convert Ethernet address from ' xx[:]xx[:]xx[:]xx[:]xx[:]xx ' to sockaddr. * Return nonzero On error. */int in_ether (const char *BUFP, struct sockaddr *sap) {char *ptr; int I, J; unsigned char Val; unsigned char c; Dbmsg (); sap->sa_family = Arphrd_ether; PTR = (char *) sap->sa_data; i = Eth_alen; Goto first; do {/* We might get a semicolon here */if (*BUFP = = ': ') bufp++; first:j = val = 0; do {c = *BUFP; if (((unsigned char) (C-' 0 ')) <= 9) {c-= ' 0 '; } else if ((unsigned char) ((c|0x20)-' a ') <= 5) {c = (unsigned char) ((c|0x20)-' a ') + 10; } else {if (J && (c = = ': ' | | c = = ') ')//* One-digit byte: __:x:__ */ Break return-1; } ++BUFP; Val <<= 4; Val + = C; J ^= 1; } while (j); *ptr++ = val; } while (i.); /* Error If we aren ' t at end of String */return *BUFP;} int main (int argc, char *argv[]) {struct arpreq req; struct SOCKADDR sa; int ret; if (argc! = 3) {dbmsg ("Usage:./arp"); Exit (0); } int sockfd = socket (af_inet, SOCK_DGRAM, 0); if (SOCKFD < 0) return-1; memset (&req, 0, sizeof (req)); A. Put host IP to Arp_p Inet_resolve (argv[1], (struct sockaddr_in*) &sa); memcpy (&REQ.ARP_PA, &sa, sizeof (struct sockaddr)); B. Put host MAC to Arp_ha In_ether (argv[2], &req.arp_ha); C. Set flag Req.arp_flags = Atf_perm | atf_com; D. Invoke IOCTL Dbmsg ("Next Invoke IOCTL"); RET = IOCTL (SOCKFD, Siocsarp, &req); if (Ret < 0) {dbmsg ("error%s", Strerror (errno)); Exit (-1); } return exit_success;}
3. Execution results
[Email protected]:/work/test/tcpip/busy/arp$ Cat/proc/net/arp | grep "192.168.4.111"//view 192.168.4.111 MAC address in ARP cache 192.168.4.111 0x1 0x2 78:6a:89:18:31:0b * eth2-End is 0B[EMAIL&N bsp;protected]:/work/test/tcpip/busy/arp$ ping 192.168.4.111//Ping 192.168.4.111 (192.168.4.111) 56 (84) Can Ping Bytes of data.64 bytes from 192.168.4.111:icmp_seq=1 ttl=64 time=93.9 ms[email protected]:/work/test/tcpip/busy/ arp$ sudo./arp 192.168.4.111 78:6a:89:18:31:0c//Modify the MAC address of 192.168.4.111 in the ARP cache arp.c:in_ether[38]: arp.c:main[101]: Next invoke ioctl[email protected]:/work/test/tcpip/busy/arp$ ping 192.168.4.111//ping again found no ping 192.168.4.111 (192.168.4.111) bytes of data. From 192.168.4.62:icmp_seq=1 Redirect Network (New nexthop:192.168.4.62) from 192.168.4.62:icmp_seq=2 Redirect Network ( New nexthop:192.168.4.62) from 192.168.4.62:icmp_seq=3 Redirect Network (new nexthop:192.168.4.62) [email protected]:/work/test/tcpip/busy/arp$ Cat/proc/net/arp | grep "192.168.4.111 "//View the MAC address of 192.168.4.111 in the ARP cache 192.168.4.111 0x1 0x6 78:6a:89:18:31:0c * eth2-the end of 0b is changed to 0c
"Go" BusyBox analysis--arp set the MAC address in the ARP cache table