#include "my_send_arp.h"
int send_arp(char *src_ip, char *src_hw, char *tg_ip, char *tg_hw, u_short op)
{
struct in_addr src_in_addr, targ_in_addr;
struct arp_packet pkt;
struct sockaddr sa;
int sock;
sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_RARP));
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);
get_hw_addr(pkt.targ_hw_addr, tg_hw);
get_hw_addr(pkt.rcpt_hw_addr, tg_hw);
get_hw_addr(pkt.src_hw_addr, src_hw);
get_hw_addr(pkt.sndr_hw_addr, src_hw);
get_ip_addr(&src_in_addr, src_ip);
get_ip_addr(&targ_in_addr, tg_ip);
memcpy(pkt.sndr_ip_addr, &src_in_addr, IP_ADDR_LEN);
memcpy(pkt.rcpt_ip_addr, &targ_in_addr, IP_ADDR_LEN);
memset(pkt.padding, 0, 18);
strcpy(sa.sa_data, DEFAULT_DEVICE);
sendto(sock,&pkt,sizeof(pkt),MSG_DONTWAIT,&sa,sizeof(sa));
close(sock);
}
void get_ip_addr (struct in_addr *in_addr, char *str)
{
struct hostent *hostp;
in_addr->s_addr = inet_addr(str);
}
void get_hw_addr (char *buf, char *str)
{
int i;
char c, val;
for(i = 0; i < ETH_HW_ADDR_LEN; i++) {
c = tolower(*str++);
if (isdigit(c))
val = c - '0';
else
val = c-'a'+10;
*buf = val << 4;
c = tolower(*str++);
if (isdigit(c))
val = c - '0';
else
val = c-'a'+10;
*buf++ |= val;
if (*str == ':')
str++;
}
}
|