![]() |
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
|
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.
|
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". |
Quote:
i send my code help me |
Quote:
{ 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; } |
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 |
Quote:
reply me sir i m waiting |
Quote:
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; } |
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? |
#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; } |
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); ^ |
Quote:
Again: use CODE tags, provide details, and don't tell volunteers to hurry up and help you. |
Quote:
we remove void |
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. |
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. |