Implement ARP packet sending in C language in linux

Source: Internet
Author: User

ARP packets can be sent at will. The source code is as follows:
# Include <netdb. h>
# Include <sys/socket. h>
# Include <sys/types. h>
# Include <stdio. h>
# Include <errno. h>
# Include <sys/ioctl. h>
# Include <net/if. h>
# Include <signal. h>
# Include <netinet/ip. h>
# Include <netinet/in. h>
# Include <string. h>
# Include <arpa/inet. h>
# Include <netinet/ip_icmp.h>
# Include <linux/if_ether.h>
# Define ETH_HW_ADDR_LEN 6
# Define IP_ADDR_LEN 4
# Define ARP_FRAME_TYPE 0x0806
# Define ETHER_HW_TYPE 1
# Define IP_PROTO_TYPE 0x0800
# Define OP_ARP_REQUEST 2
# Define OP_ARP_QUEST 1
# Define DEFAULT_DEVICE "eth0"

Char usage [] =
{"Send_arp: sends out custom ARP packet. ferrysnow@gmail.com usage: send_arp src_ip_addr src_hw_addr targ_ip_addr tar_hw_addr number "};

Struct arp_packet
{
U_char targ_hw_addr [ETH_HW_ADDR_LEN];
U_char src_hw_addr [ETH_HW_ADDR_LEN];
U_short frame_type;
U_short hw_type;
U_short prot_type;
U_char hw_addr_size;
U_char prot_addr_size;
U_short op;
U_char sndr_hw_addr [ETH_HW_ADDR_LEN];
U_char sndr_ip_addr [IP_ADDR_LEN];
U_char rcpt_hw_addr [ETH_HW_ADDR_LEN];
U_char rcpt_ip_addr [IP_ADDR_LEN];
U_char padding [18];
};

Void die (char *);
Void get_ip_addr (struct in_addr *, char *);
Void get_hw_addr (char *, char *);

Int main (int argc, char * argv [])
{
Struct in_addr src_in_addr, targ_in_addr;
Struct arp_packet pkt;
Struct sockaddr sa;
Int sock;
Int j, number;
If (argc! = 6)
{
Die (usage );
}
Sock = socket (AF_INET, SOCK_PACKET, htons (ETH_P_RARP ));
If (sock <0)
{
Perror ("socket ");
Exit (1 );
}
Number = atoi (argv [5]);
Pkt. frame_type = htons (ARP_FRAME_TYPE );
Pkt. hw_type = htons (ETHER_HW_TYPE );
Pkt. prot_type = htons (IP_PROTO_TYPE );
Pkt. hw_addr_size = ETH_HW_ADDR_LEN;
Pkt. prot_addr_size = IP_ADDR_LEN;
Pkt. op = htons (OP_ARP_QUEST );
Get_hw_addr(pkt.tar g_hw_addr, argv [4]);
Get_hw_addr (pkt. rcpt_hw_addr, argv [4]);
Get_hw_addr (pkt. src_hw_addr, argv [2]);
Get_hw_addr (pkt. sndr_hw_addr, argv [2]);
Get_ip_addr (& src_in_addr, argv [1]);
Get_ip_addr (& targ_in_addr, argv [3]);
Memcpy (pkt. sndr_ip_addr, & src_in_addr, IP_ADDR_LEN );
Memcpy (pkt. rcpt_ip_addr, & targ_in_addr, IP_ADDR_LEN );
Bzero (pkt. padding, 18 );
Strcpy (sa. sa_data, DEFAULT_DEVICE );
For (j = 0; j <number; j ++)
{
If (sendto (sock, & pkt, sizeof (pkt), 0, & sa, sizeof (sa) <0)
{
Perror ("sendto ");
Exit (1 );
}
}
Exit (0 );
}


Void die (char * str)
{
Fprintf (stderr, "% s", str );
Exit (1 );
}


Void get_ip_addr (struct in_addr * in_addr, char * str)
{
Struct hostent * hostp;
In_addr-> s_addr = inet_addr (str );
If (in_addr-> s_addr =-1)
{
If (hostp = gethostbyname (str )))
{
Bcopy (hostp-> h_addr, in_addr, hostp-> h_length );
}
Else
{
Fprintf (stderr, "send_arp: unknown host % s", str );
Exit (1 );
}
}
}


Void get_hw_addr (char * buf, char * str)
{
Int I;
Char c, val;
For (I = 0; I <ETH_HW_ADDR_LEN; I ++)
{
If (! (C = tolower (* str ++ )))
{
Die ("Invalid hardware address ");
}
If (isdigit (c ))
{
Val = c-0;
}
Else if (c> = a & c <= f)
{
Val = c-a + 10;
}
Else
{
Die ("Invalid hardware address ");
}
* Buf = val <4;
If (! (C = tolower (* str ++ )))
{
Die ("Invalid hardware address ");
}
If (isdigit (c ))
{
Val = c-0;
}
Else if (c> = a & c <= f)
{
Val = c-a + 10;
}
Else
{
Die ("Invalid hardware address ");
}
* Buf ++ | = val;
If (* str = :)
{
Str ++;
}
}
}
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.