LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i use rhel 7 i got segment fault core dump (https://www.linuxquestions.org/questions/linux-newbie-8/i-use-rhel-7-i-got-segment-fault-core-dump-4175610120/)

vicky chauhan 07-18-2017 02:08 PM

i use rhel 7 i got segment fault core dump
 
i got problem on rhel 7 segment faul core dumped when we using memcpy we got warnig cast int

suicidaleggroll 07-18-2017 02:12 PM

Without seeing your code or the compiler warning it will be impossible for anybody to help. Chances are you dumped into an array that was too small or not allocated yet.

AwesomeMachine 07-18-2017 02:13 PM

Hi vicky chauhan,

Welcome to LQ!

Segmentation fault occurs when memory is accessed incorrectly. You can try using "strace" to see what's happening. But given the low detail of your post, I can't really offer much "help".

vicky chauhan 07-18-2017 02:21 PM

Quote:

Originally Posted by suicidaleggroll (Post 5736717)
Without seeing your code or the compiler warning it will be impossible for anybody to help. Chances are you dumped into an array that was too small or not allocated yet.



i send my code help me

vicky chauhan 07-18-2017 02:23 PM

Quote:

Originally Posted by vicky chauhan (Post 5736720)
i send my code help me

unsigned char* CreateEthernetHeader(char *src_mac,char *dst_mac,int protocol)
{
struct ethhdr *ethernet_header;

ethernet_header=(struct ethhdr*)malloc(sizeof(struct ethhdr));

memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
memcpy(ethernet_header->h_dest,(void *)ether_aton(dst_mac),6);
ethernet_header->h_proto=htons(protocol);

return (unsigned char*)ethernet_header;

}

vicky chauhan 07-18-2017 02:27 PM

cast to pointer from integer of different size [-Wint-to-pointer-cast]
memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
this is compiler warning

vicky chauhan 07-18-2017 02:30 PM

Quote:

Originally Posted by vicky chauhan (Post 5736723)
cast to pointer from integer of different size [-Wint-to-pointer-cast]
memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
this is compiler warning




reply me sir i m waiting

vicky chauhan 07-18-2017 02:41 PM

Quote:

Originally Posted by suicidaleggroll (Post 5736717)
Without seeing your code or the compiler warning it will be impossible for anybody to help. Chances are you dumped into an array that was too small or not allocated yet.




unsigned char* CreateEthernetHeader(char *src_mac,char *dst_mac,int protocol)
{
struct ethhdr *ethernet_header;

ethernet_header=(struct ethhdr*)malloc(sizeof(struct ethhdr));

memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
memcpy(ethernet_header->h_dest,(void *)ether_aton(dst_mac),6);
ethernet_header->h_proto=htons(protocol);

return (unsigned char*)ethernet_header;

}

TB0ne 07-18-2017 02:54 PM

You need to read the "Question Guidelines" and "How to ask a smart question" links in my posting signature, along with the LQ Rules. We volunteer our time here, so telling us that "i m waiting" is plain rude. We answer when we can, if we can/want to. Secondly, you have posted one very small piece of your code, with no context at all...what do you think we'll be able to tell you??? And when posting code, you need to use CODE tags.

Post relevant details and we can try to help. Otherwise, since you're using RHEL 7...have you contacted RHEL support for help in analyzing a core dump?

vicky chauhan 07-18-2017 03:00 PM

#include<string.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<features.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<net/ethernet.h>

#define SRC_ETHER_ADDR "aa:aa:aa:aa:aa:aa"
#define DST_ETHER_ADDR "bb:bb:bb:bb:bb:bb"


int CreateRawSocket(int protocol_to_sniff)
{
int rawsock;
if((rawsock=socket(PF_PACKET,SOCK_RAW,htons(protocol_to_sniff)))==-1)
{
perror("Error creating raw socket:");
exit(-1);
}

return rawsock;

}

int BindRawSocketToInterface(char *device,int rawsock,int protocol)
{
struct sockaddr_ll sll;
struct ifreq ifr;

bzero(&sll,sizeof(sll));
bzero(&ifr,sizeof(ifr));
strncpy((char*)ifr.ifr_name,device,IFNAMSIZ);
if((ioctl(rawsock,SIOCGIFINDEX,&ifr))==-1)
{
perror("Error getting index:");
exit(-1);
}

sll.sll_family=AF_PACKET;
sll.sll_ifindex=ifr.ifr_ifindex;
sll.sll_protocol=htons(protocol);

if((bind(rawsock,(struct sockaddr*)&sll,sizeof(sll)))==-1)
{
perror("Error binding interface:");
exit(-1);
}

return 1;
}
int SendRawPacket(int rawsock,unsigned char *pkt,int pkt_len)
{

int sent=0;
if((sent=write(rawsock,pkt,pkt_len))!=pkt_len)
{
return 0;
}
return 1;
}

unsigned char* CreateEthernetHeader(char *src_mac,char *dst_mac,int protocol)
{
struct ethhdr *ethernet_header;

ethernet_header=(struct ethhdr*)malloc(sizeof(struct ethhdr));

memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
memcpy(ethernet_header->h_dest,(void *)ether_aton(dst_mac),6);
ethernet_header->h_proto=htons(protocol);

return (unsigned char*)ethernet_header;

}

main(int argc,char **argv)
{
int raw ;
unsigned char *packet;
int ethhdr_len;

raw=CreateRawSocket(ETH_P_ALL);
BindRawSocketToInterface(argv[1],raw,ETH_P_ALL);

packet=CreateEthernetHeader(SRC_ETHER_ADDR,DST_ETHER_ADDR,ETHERTYPE_IP);
ethhdr_len=sizeof(struct ethhdr);

if(!SendRawPacket(raw,packet,ethhdr_len))
{
perror("Error sending packet");
}

else
printf("packet sent successfully\n");
free(packet);
close(raw);
return 0;

}

vicky chauhan 07-18-2017 03:03 PM

compile error



warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
^

TB0ne 07-18-2017 03:17 PM

Quote:

Originally Posted by vicky chauhan (Post 5736748)
compile error

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
memcpy(ethernet_header->h_source,(void *)ether_aton(src_mac),6);
^

Again: YOU NEED TO USE CODE TAGS....go back and edit your previous post. If you would like help from the community, you need to at least read and acknowledge things that you get told. Also, that is not the entire output from your compile attempt; again, you need to post details. Did you read the message you got, and at least try to look it up?? The compiler returns a warning because you are converting an int to void.

Again: use CODE tags, provide details, and don't tell volunteers to hurry up and help you.

vicky chauhan 07-18-2017 03:24 PM

Quote:

Originally Posted by TB0ne (Post 5736758)
Again: YOU NEED TO USE CODE TAGS....go back and edit your previous post. If you would like help from the community, you need to at least read and acknowledge things that you get told. Also, that is not the entire output from your compile attempt; again, you need to post details. Did you read the message you got, and at least try to look it up?? The compiler returns a warning because you are converting an int to void.

Again: use CODE tags, provide details, and don't tell volunteers to hurry up and help you.


we remove void

rtmistler 07-19-2017 06:35 AM

As far as that warning goes, h_addr is an array of unsigned char of size 6.

manual page for ether_aton(3) quotes that this function returns a pointer to a structure type ether_addr. struct ether_addr contains the element named, ether_addr_octet which is an array of unsigned characters.

You should fix the warning.

You should check that the address returned by the ether_aton() function is not NULL, because it can be and copying to or from a NULL address will cause a crash.

If you're getting a core dump you can analyze it using gdb. I would recommend you compile your source with the -ggdb switch. It seems as if you already are obtaining a core dump, therefore you can use gdb to analyze this core and see the exact line where the problem is.

Note that the problem may not be where your warning is located at. That warning may have nothing to do with this crash. The warning should be resolved anyways, however it may not be your problem.

See my blog entry about how to analyze core files if you do not understand how to use gdb to analyze this problem.

Do also heed the advice of others about how to ask questions properly, and also review the comments by others clearly before repeating your same questions.

vivek singh chauhan1 07-21-2017 01:46 PM

my program compile safe of packet sniff

half program run safe and then...


segment fault(core dumped) stop the program


All times are GMT -5. The time now is 07:33 AM.