我在嵌入式板子上跑C语言程序。报错segmentation fault。
查了资料说和内存有关,
没办法gdb,就用printf定位出错位置。
程序时这样的:
主程序
。。。
printf("send start\r\n");
fd=sendOtherFrame(ethPacket,interfaceB,pkthdr->len);
printf("send start\r\n");
。。。
子函数:
int sendOtherFrame(union ETHDataPacket ipDataPacket,char *interface,int len)
{
int i, datalen,frame_length, sd, bytes;
// char *interface="enp4s0";;
uint8_t data[IP_MAXPACKET];
uint8_t src_mac[6];
uint8_t dst_mac[6];;
uint8_t ether_frame[IP_MAXPACKET];
struct sockaddr_ll device;
struct ifreq ifr;
printf("\r\nFunction sendOtherFrame(): 1\r\r");
// Find interface index from interface name and store index in
// struct sockaddr_ll device, which will be used as an argument of sendto().
memset (&device, 0, sizeof (device));
if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
perror ("if_nametoindex() failed to obtain interface index ");close (sd);
exit (EXIT_FAILURE);
}
printf("\r\nFunction sendOtherFrame(): 2\r\r");
// printf ("Index for interface %s is %i\n", interface, device.sll_ifindex);
// 2, 设置源MAC地址
for(i=0;i<6;i++){
src_mac[i]=ipDataPacket.ethPacket.ethHeader.ether_shost[i];
}
// 1, 设置目标MAC地址
for(i=0;i<6;i++){
dst_mac[i]=ipDataPacket.ethPacket.ethHeader.ether_dhost[i];
}
printf("\r\nFunction sendOtherFrame(): 3\r\r");
// Fill out sockaddr_ll.
device.sll_family = AF_PACKET;
memcpy (device.sll_addr, src_mac, 6);
device.sll_halen = htons (6);
printf("\r\nFunction sendOtherFrame(): 4\r\r");
#define IPHEADER ipDataPacket.ethPacket.ethData.ipPacket.ipHeader
#define IPPACKET ipDataPacket.ethPacket.ethData.ipPacket
#define ETHDATA ipDataPacket.ethPacket.ethData
datalen = len-ETH_HEADER_LEN_BYTE; //len of ip frame : ipheader+data
//Frame data
for(i=0;i<(len-ETH_HEADER_LEN_BYTE);i++){
data[i]=ETHDATA.data[i];
}
// Fill out ethernet frame header.
frame_length = 6 + 6 + 2 + datalen;
// Destination and Source MAC addresses
memcpy (ether_frame, dst_mac, 6);
memcpy (ether_frame + 6, src_mac, 6);
printf("\r\nFunction sendOtherFrame(): 5\r\r");
// 3, 这里有点迷???-这个看得明白,但是实际显示不是这么回事,这是咋回事呢?原因是类型不一样
// int -- uint16
ether_frame[12] = ipDataPacket.ethPacket.ethHeader.ether_type % 256;
ether_frame[13] = ipDataPacket.ethPacket.ethHeader.ether_type / 256;
// ether_frame[13] = ipDataPacket.ethPacket.ethHeader.ether_type / 256 ;
// printf("%02d,",ether_frame[12]);
// printf("%02d,\r\n",ether_frame[13]);
printf("\r\nFunction sendOtherFrame(): 6\r\r");
// data
memcpy (ether_frame + 14 , data, datalen);
printf("\r\nFunction sendOtherFrame(): 7\r\r");
// Submit request for a raw socket descriptor.
if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {//创建正真发送的socket
perror ("socket() failed ");close (sd);
exit (EXIT_FAILURE);
}
printf("\r\nFunction sendOtherFrame(): 8\r\r");
// Send ethernet frame to socket.
if ((bytes = sendto (sd, ether_frame, frame_length, 0, (struct sockaddr *) &device, sizeof (device))) <= 0) {
perror ("sendto() failed");close (sd);
exit (EXIT_FAILURE);
}
printf("\r\nFunction sendOtherFrame(): 9\r\r");
// printf ("send num=%d,read num=%d\n",frame_length,bytes);
// Close socket descriptor.
close (sd);
printf("\r\nFunction sendOtherFrame(): 10\r\r");
return (EXIT_SUCCESS);
}
运行结果
有知道怎么回事吗?给点建议吧