views:

78

answers:

3

Hello to everyone!!

I am trying to print the MAC address by using ether_ntoa. When i try to do

printf("MAC (src): %s\n",ether_ntoa((struct ether_addr *)&eheader->ether_shost));

I get a segmentation fault, so I have come up with two different approaches:

This is the snippet code nº1:

struct ether_header *eheader;
char *p;
...
p = ether_ntoa(struct ether_addr *) &eheader->ether_shost);
printf("-MAC (src): %s\n",p);

and the warning I obtain is:

assignment makes pointer from integer without a cast

so I do the cast and ...

This is the snippet code nº2:

struct ether_header *eheader;
char *p;
...
p = (char *) ether_ntoa((struct ether_addr *) &eheader->ether_shost);
printf("-MAC (src): %s\n",p);

and the warning I obtain is:

cast to pointer from integer of different size

If you take a look at the man page, ether_ntoa is defined this way, returning a char *:

extern char *ether_ntoa (__const struct ether_addr *__addr) __THROW;

so I don't know what I am doing wrong. The problem is "not" the warning, is the segmentation fault it comes after, when I try to print it.

I am getting this error under openSUSE (in ubuntu I don't need to do this *p trick) so if there is an openSUSE expert here I will appreciate her help.

Thank you very much!

A: 

That sure sounds like a word-size mismatch between the headers/library and the compiler. Do you have some strange mix of 64-bit and 32-bit environment in that Suse machine?

Amardeep
+1  A: 

This warning:

assignment makes pointer from integer without a cast

...is not an invitation to add a cast. It's telling you about a more serious problem; in this case, that the compiler thinks ether_ntoa() returns an int.

This happens when you use a function without a declaration in scope; for a library function like this, it means that you haven't included the right header. For ether_ntoa(), you need #include <netinet/ether.h>.

caf
A: 

It was a library problem. I don't know how I didn't check that (maybe because it was a file provided by the teacher with all the libraries needed ¬¬). Doing the include:

#include <netinet/ether.h>

was the solution, so I consider the question answered.

Thank you very much to both of you.

david