I need help reading my arp replies. When writing requests to the socket,
I cant get replies related to what I sent..Ideas
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.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 DEFAULT_DEVICE "em1"
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 error(char *);
void get_ip_addr(struct in_addr*,char*);
void get_hw_addr(char*,char*);
int main(int argc,char** argv){
if(argc != 2)
error("usage: ./a.out <IP Address>");
struct in_addr src_in_addr,targ_in_addr;
struct arp_packet pkt;
struct sockaddr sa;
int sockfd = 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(1);
int i;
for(i = 0; i < 6; i++){
pkt.targ_hw_addr[i] = 0xff;
pkt.rcpt_hw_addr[i] = 0xff;
}
pkt.src_hw_addr[0] = 0xc8;
pkt.src_hw_addr[1] = 0x60;
pkt.src_hw_addr[2] = 0x00;
pkt.src_hw_addr[3] = 0xa4;
pkt.src_hw_addr[4] = 0xac;
pkt.src_hw_addr[5] = 0x8a;
pkt.sndr_hw_addr[0] = 0xc8;
pkt.sndr_hw_addr[1] = 0x60;
pkt.sndr_hw_addr[2] = 0x00;
pkt.sndr_hw_addr[3] = 0xa4;
pkt.sndr_hw_addr[4] = 0xac;
pkt.sndr_hw_addr[5] = 0x8a;
// get parrallel host resoultion using thread
src_in_addr.s_addr = inet_addr("192.168.1.99");
//in_addr->s_addr=inet_addr(str);
//get_ip_addr(&src_in_addr,argv[1]);
get_ip_addr(&targ_in_addr,argv[1]);
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);
if(sendto(sockfd,&pkt,sizeof(pkt),0,&sa,sizeof(sa)) < 0){
perror("sendto");
exit(1);
}
char buff[50];
if(setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,(const char *)&buff,sizeof(int)) < 0)
error("set");
int n;
while((n = read(sockfd,buff,50)) > 0){
buff[n] = '\0';
printf("%s",buff);
}
return 0;
}//main
void error(char *msg){
printf("%s\n",msg);
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\n",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++))) error("Invalid hardware address");
if(isdigit(c)) val = c-'0';
else if(c >= 'a' && c <= 'f') val = c-'a'+10;
else error("Invalid hardware address");
*buf = val << 4;
if( !(c = tolower(*str++))) error("Invalid hardware address");
if(isdigit(c)) val = c-'0';
else if(c >= 'a' && c <= 'f') val = c-'a'+10;
else error("Invalid hardware address");
*buf++ |= val;
if(*str == ':')str++;
}
}