LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2011, 05:35 PM   #1
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Question Segmentation fault using memcpy() and ether_aton() in combination on a 64bit machine


Hi there!

Right now i am toying around with Linux packet socket on my Linux Mint 10 32bit machine. All went well, until i tried to compile some of my results on my laptop, which is running openSuSE 11.3 64bit version. On this machine i get some warning, and the execution of the compiled program causes a segmentation fault.

The warning is:
cast to pointer from integer of different size

I made some shorted version of my code, which still causes the segmentation fault and warning:

Code:
#include <stdio.h>
#include <net/ethernet.h>	// ether_header
#include <net/if_arp.h>		// arphdr
#include <string.h>	//memset()
#include <stdlib.h>


int main(int argc, char *argv[])
{
	struct ether_header *EthHdr;	
	
	struct arphdr *ArpHdr;	// This struct is the protocol independent part of the arp header

	struct arp_ext_header{		// Ethernet dependent part of arp
		unsigned char S_MAC[ETH_ALEN];	/* Sender hardware address.  */
    		unsigned char S_IP[4];		/* Sender IP address.  */
    		unsigned char D_MAC[ETH_ALEN];	/* Target hardware address.  */
    		unsigned char D_IP[4];		/* Target IP address.  */
	} *ArpExtHdr;
	//  __attribute__ ((__packed__)); 	// still in doubt if neccessary, but on a 32 bit
						// architecture it executed well without
	int packetsize;
	packetsize = sizeof(struct ether_header) + sizeof(struct arphdr) + sizeof(struct arp_ext_header);

	// Get some memory for the Ethernet Frame
	unsigned char packet[packetsize];
	// and initialize with zero
	memset(packet,0,packetsize);

	// Initialize header pointers
	EthHdr = (struct ether_header *)packet;
	ArpHdr = (struct arphdr *)(packet + sizeof(struct ether_header));
	ArpExtHdr = (struct arp_ext_header *)(packet + sizeof(struct ether_header) + sizeof(struct arphdr));

	// define MAC-Addresses 
	char SMAC[18] = "BB:BB:BB:BB:BB:BB";
	char DMAC[18] = "AA:AA:AA:AA:AA:AA";

	// define IP-Addresses
	char SIP[18] = "12.12.111.111";
	char DIP[18] = "222.222.13.13";

	// This line is going to cause the seg fault
	memcpy(EthHdr->ether_shost,(const void *)ether_aton(SMAC),ETHER_ADDR_LEN);
	printf("This is only printed, if there was no seg fault\n");

printf("Exit_Success\n");
return 0;
}
Right now, i think i am getting in trouble, because ether_aton() returns an integer, while memcopy expects a const void pointer. Up till now i tried a lot to fix this matter, but i am still stuck, which might be because i am still not to comfortable in C.

Thanks to all for having a look at my problem...

mfg, Heraton
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-10-2011, 07:23 AM   #2
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
Hello,

compile it with -Wall and you'll see a warning about implicit declaration.
You forgot to include <netinet/ether.h>

Antegallya
 
3 members found this post helpful.
Old 04-10-2011, 08:02 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Antegallya answered your problem. But the warning you got about that cast also really answered the problem. If the warning wasn't clear enough, I'm not sure Antegallya's answer will be clear enough. So I'll expand it a little:

You need to add the following line near the top of your source code
Code:
#include <netinet/ether.h>
That is all you need, but if you want to understand why:
Quote:
Originally Posted by Heraton View Post
on my Linux Mint 10 32bit machine. All went well,
Because on x86 architecture int and pointer are the same size and are returned from functions in the same register.

Quote:
openSuSE 11.3 64bit version. On this machine i get some warning, and the execution of the compiled program causes a segmentation fault.
On x86_64 architecture int and pointer are different sizes. On some more obscure architecture, int and pointer might be returned from functions in different registers. So the code that works on x86 is generally wrong and will fail on several other architectures, not just on x86_64.

Quote:
i think i am getting in trouble, because ether_aton() returns an integer, while memcopy expects a const void pointer.
Almost correct. ether_anon returns a pointer, but the lack of a declaration makes the compiler think it returns an int. So the top half of the returned pointer is discarded by the compiled code. The cast to (void*) takes only the bottom half of the original pointer and uses it (sign extended) as a whole pointer.
 
2 members found this post helpful.
Old 04-10-2011, 09:04 AM   #4
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Original Poster
Rep: Reputation: 3
Lightbulb Thanks a lot, you made my day :-)

Quote:
Originally Posted by antegallya View Post
compile it with -Wall and you'll see a warning about implicit declaration.
Antegallya
I'm mortified. How could i dare to forget good old -Wall -.- I'm almost sure, this warning and google would have led me to a solution sooner or later...


Quote:
Originally Posted by johnsfine View Post
Almost correct. ether_anon returns a pointer, but the lack of a declaration makes the compiler think it returns an int. So the top half of the returned pointer is discarded by the compiled code. The cast to (void*) takes only the bottom half of the original pointer and uses it (sign extended) as a whole pointer.
This explanation and the part about registers was quite enlightening to me. Now that i understand what was wrong with my code, another question puzzles my mind:

Is the location of the precompiled ether_aton on the linkers search path? Otherwise it shouldn't have been able to link the proper function into my application. If so, why was it on the path?


Thanks for your help, i really appreciate it.

regards, Heraton
 
Old 05-24-2011, 12:57 AM   #5
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Rep: Reputation: Disabled
solution

Hi, could you pls share what was the fix you made to memcpy to get rid of the SEGV. Currently I am experiencing the same SEGV on my X84_64 platform.
 
Old 05-24-2011, 04:05 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by akshay_satish View Post
Hi, could you pls share what was the fix you made to memcpy to get rid of the SEGV. Currently I am experiencing the same SEGV on my X84_64 platform.
Please open a new thread -- and post your code.
 
0 members found this post helpful.
Old 05-24-2011, 05:12 AM   #7
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...54#post4365254
I have posted it in the above link with code and some information. Could you pls take a look and help me
Thank you.

---------- Post added 05-24-11 at 03:43 PM ----------

http://www.linuxquestions.org/questi...54#post4365254
I have posted it in the above link with code and some information. Could you pls take a look and help me
Thank you.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird SIGALRM segmentation fault on 64bit linux... NoStressHQ Programming 20 04-26-2010 06:33 AM
[SOLVED] segmentation fault during memcpy() Aquarius_Girl Programming 10 02-20-2010 04:41 AM
segmentation fault in memcpy George2 Programming 6 08-25-2007 11:33 PM
Seemingly random combination of statements causes segmentation fault (C++) ErrorBound Programming 3 07-28-2006 05:47 AM
Unreal 2004 64bit Segmentation fault phoenix49 Fedora 3 05-13-2006 12:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration