我正在用C语言编写简单的ARP请求包,出现了错误,找不出来,大虾帮忙看下
每次到pack函数填充的时候都会出现段错误,百思不得其解,想问问哪的问题,谢谢#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.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 DEST_IP "192.168.0.1"
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 pack(char *);
struct arp_packet arp_pack;
int sockfd;
int main(int argc, char *argv[])
{
struct sockaddr *sockaddr;
if(argc != 2)
{
printf("useage:%s target_IP\n",argv[1]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_PACKET, htons(ETH_P_RARP));
if(sockfd < 0)
{
perror("socket");
exit(1);
}
pack(argv[2]);
if(sendto(sockfd, &arp_pack, sizeof(arp_pack), 0, sockaddr, sizeof(*sockaddr)) < 0)
{
perror("sendto");
exit(1);
}
close(sockfd);
exit(0);
}
void pack(char * p)
{
struct in_addr src_in_addr, targ_in_addr;
struct ifreq ifr_hw;
unsigned long HW = 0xffffff;
arp_pack.frame_type = htons(0x0806);
arp_pack.hw_type = htons(1);
arp_pack.prot_type = htons(0x0800);
arp_pack.hw_addr_size = ETH_HW_ADDR_LEN;
arp_pack.prot_addr_size = IP_ADDR_LEN;
arp_pack.op = htons(1);
bzero(arp_pack.padding, 18);
strcpy(ifr_hw.ifr_name, "eth0");
if(ioctl(sockfd, SIOCGIFHWADDR, &ifr_hw) < 0)
{
perror("ioctl hw");
exit(1);
}
memcpy(arp_pack.targ_hw_addr,&(HW) , 6);
memcpy(arp_pack.rcpt_hw_addr,&(HW) , 6);
memcpy(arp_pack.src_hw_addr, ifr_hw.ifr_addr.sa_data, 6);
memcpy(arp_pack.sndr_hw_addr, ifr_hw.ifr_addr.sa_data, 6);
src_in_addr.s_addr = inet_addr(DEST_IP);
targ_in_addr.s_addr = inet_addr(p);
memcpy(arp_pack.sndr_ip_addr, &src_in_addr, IP_ADDR_LEN);
memcpy(arp_pack.rcpt_ip_addr, &targ_in_addr, IP_ADDR_LEN);
}