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 *******************/ |
You need to include a file where the htons macro is defined, netinet/in.h for example.
|
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 |
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.
|
Your code sample is incomplete, you do not show the include directives you use.
|
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 |
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. |
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 " 'Hope that helps .. PSM |
Whoops:
That was the vB code stuff in your "Post Reply" page: " [code]" " [\code]" |
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 |
on what architecure are you compiling, intel or sth else ?
|
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. |
Unfortunately, the answer he's getting (32764) is wrong for either arch:
Code:
$ perl -le 'print hex "0x888e"; print hex "0x8e88"' |
All times are GMT -5. The time now is 12:19 AM. |