LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-23-2004, 07:59 AM   #1
abdullahgee
Member
 
Registered: Apr 2004
Posts: 30

Rep: Reputation: 15
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 *******************/
 
Old 05-23-2004, 08:07 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You need to include a file where the htons macro is defined, netinet/in.h for example.
 
Old 05-23-2004, 08:35 AM   #3
abdullahgee
Member
 
Registered: Apr 2004
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 05-23-2004, 08:36 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 05-23-2004, 08:21 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Your code sample is incomplete, you do not show the include directives you use.
 
Old 05-23-2004, 10:12 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 05-23-2004, 10:48 PM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 05-24-2004, 01:05 AM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 05-24-2004, 01:10 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Whoops:

That was the vB code stuff in your "Post Reply" page: " [code]" " [\code]"
 
Old 05-25-2004, 12:54 PM   #10
abdullahgee
Member
 
Registered: Apr 2004
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 05-25-2004, 04:25 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
on what architecure are you compiling, intel or sth else ?
 
Old 05-25-2004, 05:26 PM   #12
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 05-25-2004, 06:11 PM   #13
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
unresolved symbol htons mintong Linux - Software 4 08-03-2005 09:53 PM
does each header fields in packet sent on network uses htons? b123coder Programming 3 04-06-2005 08:36 AM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

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

All times are GMT -5. The time now is 10:42 AM.

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