LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem with htons() (https://www.linuxquestions.org/questions/programming-9/problem-with-htons-184778/)

abdullahgee 05-23-2004 07:59 AM

problem with htons()
 
i compile using
# gcc -c testing.c
its ok
when i isseu the command
# insmod testing.o
the error appears "unresolved symbol htons"
***********************************
/*************C O D E **************/

#define ETHERNET_DEVICE_NAME "eth0"
#define EAPOL_ETHERNET_TYPE 0x888E

struct net_device* g_poEthernetDevice;
struct packet_type g_oProtocolHandler;

int init_module (void)
{
int nReturnValue = 0;

if((g_poEthernetDevice = dev_get_by_name(ETHERNET_DEVICE_NAME)))
{
g_poEthernetDevice->flags |= IFF_PROMISC;
g_oProtocolHandler.type = htons(EAPOL_ETHERNET_TYPE); // <----- Problem ------
g_oProtocolHandler.dev = g_poEthernetDevice;
g_oProtocolHandler.func = My_EthernetRxPacket;
dev_add_pack(&g_oProtocolHandler);
printk("Ethernet Device allocated\n");
}
else
printk("Ethernet Device allocation ERROR\n");
return(nReturnValue);
}

void cleanup_module (void)
{
dev_remove_pack(&g_oProtocolHandler);
printk("Unregister Success \n");
}

int My_EthernetRxPacket(struct sk_buff* r_poSocketBuffer,struct net_device* r_poNetworkDevice,struct packet_type* r_poPacketType)
{
//here i receive the EAPOL packet
return(0);
}
/****************** E N D *******************/

jlliagre 05-23-2004 08:07 AM

You need to include a file where the htons macro is defined, netinet/in.h for example.

abdullahgee 05-23-2004 08:35 AM

including that file results in a huge amount of errors
actually there is no error till
gcc -c testing.c
with out including that file

itsme86 05-23-2004 08:36 AM

Yeah, what jlliagre said. I always try to compile with the -Wall option. Then gcc would throw up a warning about it being implicitly defined.

jlliagre 05-23-2004 08:21 PM

Your code sample is incomplete, you do not show the include directives you use.

paulsm4 05-23-2004 10:12 PM

Jlliagre is correct:

1. htons() is a macro, not a function.

The fact that you get the runtime error "unresolved symbol htons" means that your compiler incorrectly treated it like a function. Which in turn means that your compile (despite the lack of warnings) is broken.

2. The solution to problem 1) is to include the correct header (e.g. "#include "netinet/ih.h>").

3. That fact that you're getting lots of errors when you try to do so means that something ELSE is terribly wrong.

In all likelihood, you're probably missing *OTHER* headers as well.

4. itsme86, too, is absolutely correct: "-Wall" is your friend.

The more warnings you get, the more potential problems you're discovering at precisely the time when it's easiest to fix them.

Good luck .. PSM

aluser 05-23-2004 10:48 PM

It looks like the header you want is linux/byteorder/generic.h

I don't think netinet/in.h exists in the kernel tree so yeah, including it would be a really bad idea.

Since you're building a kernel module you probably need to either build using the kernel's build system (2.6) or #define some things, I think __KERNEL__ and __MODULE__ for 2.4.

paulsm4 05-24-2004 01:05 AM

You'd #include netinet/in.h if you're compiling a normal sockets program.

On the other hand, you'd #include linux/byteorder/generic.h if you're writing a kernel module (as you appear to be doing).

In either case, the basic problem is that "htons()" is a macro (not a function), and you're failing to #include the right header at compile time.

One other tip:
you can use the special bulletin board tags "
Code:

" and "
" to delimit your source code on linuxquestions.org - that way, you won't lose the formatting.

'Hope that helps .. PSM

paulsm4 05-24-2004 01:10 AM

Whoops:

That was the vB code stuff in your "Post Reply" page: " [code]" " [\code]"

abdullahgee 05-25-2004 12:54 PM

i have included linux\byteorder\generic.h
now the error is not appearing,
another problem faced by me. when i use i.e a= htons(0x888e) the value returned by the htons() is suppose 32764 ,now removing htons()
i.e a= 0x888e
the output remains 32764

jlliagre 05-25-2004 04:25 PM

on what architecure are you compiling, intel or sth else ?

itsme86 05-25-2004 05:26 PM

If the endianness of the computer your compiling on is the same as the network byte order than htons() doesn't do anything.

Network byte order is simply a means to insure compatibility between computers with different endianness on a network.

aluser 05-25-2004 06:11 PM

Unfortunately, the answer he's getting (32764) is wrong for either arch:

Code:

$ perl -le 'print hex "0x888e"; print hex "0x8e88"'
34958
36488

Not sure what's up with that...


All times are GMT -5. The time now is 12:19 AM.