Networking Forums

Networking Forums > Computer Networking > Linux Networking > socket(,,protocol): when htons?

Reply
Thread Tools Display Modes

socket(,,protocol): when htons?

 
 
Sam Steingold
Guest
Posts: n/a

 
      10-11-2005, 06:55 PM
when do I need to call htons on the 3rd argument to socket()?
it appears that 0 is the most common argument,
but I often see examples like
socket(,,htons(ETH_P_ALL));
and
socket(,,IPPROTO_TCP);
so, what is the rule?
what CPP #define's can be passed to socket() as the protocol?
which ones need htons()?

the next question is, of course,
how portable the answer to the previous question is?
(i.e., what are the proper protocol argument to socket() on *BSD &c)

thanks!

--
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.jihadwatch.org/> <http://www.dhimmi.com/>
<http://www.openvotingconsortium.org/> <http://ffii.org/>
Every day above ground is a good day.
 
Reply With Quote
 
 
 
 
Phil Frisbie, Jr.
Guest
Posts: n/a

 
      10-11-2005, 07:51 PM
Sam Steingold wrote:

> when do I need to call htons on the 3rd argument to socket()?


Never. That third argument is almost never needed, and since they are predefined
in the OS specific headers the values should already be in the proper format.

> it appears that 0 is the most common argument,
> but I often see examples like
> socket(,,htons(ETH_P_ALL));
> and
> socket(,,IPPROTO_TCP);
> so, what is the rule?
> what CPP #define's can be passed to socket() as the protocol?
> which ones need htons()?
>
> the next question is, of course,
> how portable the answer to the previous question is?
> (i.e., what are the proper protocol argument to socket() on *BSD &c)


If you are only using the TCP/IP protocol and need full portability then only
use '0' for the third argument.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com
 
Reply With Quote
 
Sam Steingold
Guest
Posts: n/a

 
      10-11-2005, 08:52 PM
> * Phil Frisbie, Jr. <(E-Mail Removed)> [2005-10-11 19:51:46 +0000]:
>
> Sam Steingold wrote:
>
>> when do I need to call htons on the 3rd argument to socket()?

>
> Never. That third argument is almost never needed, and since they are
> predefined in the OS specific headers the values should already be in
> the proper format.


then why does googling for htons(ETH_P_ALL) returns 847 pages?
mostly in this context:
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

>> it appears that 0 is the most common argument,
>> but I often see examples like
>> socket(,,htons(ETH_P_ALL));
>> and
>> socket(,,IPPROTO_TCP);
>> so, what is the rule?
>> what CPP #define's can be passed to socket() as the protocol?
>> which ones need htons()?
>> the next question is, of course,
>> how portable the answer to the previous question is?
>> (i.e., what are the proper protocol argument to socket() on *BSD &c)

>
> If you are only using the TCP/IP protocol and need full portability
> then only use '0' for the third argument.


what if I need UDP and need full portability?

thanks for your help!

--
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://ffii.org/> <http://www.openvotingconsortium.org/>
<http://pmw.org.il/> <http://www.jihadwatch.org/> <http://www.memri.org/>
MS Windows: error: the operation completed successfully.
 
Reply With Quote
 
Phil Frisbie, Jr.
Guest
Posts: n/a

 
      10-12-2005, 04:21 PM
Sam Steingold wrote:

>>* Phil Frisbie, Jr. <(E-Mail Removed)> [2005-10-11 19:51:46 +0000]:
>>
>>Sam Steingold wrote:
>>
>>
>>>when do I need to call htons on the 3rd argument to socket()?

>>
>>Never. That third argument is almost never needed, and since they are
>>predefined in the OS specific headers the values should already be in
>>the proper format.

>
> then why does googling for htons(ETH_P_ALL) returns 847 pages?
> mostly in this context:
> socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));


OK, raw sockets is another story, and it it is not really a protocol.

>>>it appears that 0 is the most common argument,
>>>but I often see examples like
>>> socket(,,htons(ETH_P_ALL));
>>>and
>>> socket(,,IPPROTO_TCP);
>>>so, what is the rule?
>>>what CPP #define's can be passed to socket() as the protocol?
>>>which ones need htons()?
>>>the next question is, of course,
>>>how portable the answer to the previous question is?
>>>(i.e., what are the proper protocol argument to socket() on *BSD &c)

>>
>>If you are only using the TCP/IP protocol and need full portability
>>then only use '0' for the third argument.

>
> what if I need UDP and need full portability?


TCP/IP includes both TCP and UDP sockets. Just use '0'.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com
 
Reply With Quote
 
Sam Steingold
Guest
Posts: n/a

 
      10-12-2005, 04:27 PM
> * Phil Frisbie, Jr. <(E-Mail Removed)> [2005-10-12 16:21:19 +0000]:
> Sam Steingold wrote:
>>>* Phil Frisbie, Jr. <(E-Mail Removed)> [2005-10-11 19:51:46 +0000]:
>>>Sam Steingold wrote:
>>>
>>>>when do I need to call htons on the 3rd argument to socket()?
>>>
>>>Never. That third argument is almost never needed, and since they are
>>>predefined in the OS specific headers the values should already be in
>>>the proper format.

>> then why does googling for htons(ETH_P_ALL) returns 847 pages?
>> mostly in this context:
>> socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

>
> OK, raw sockets is another story, and it it is not really a protocol.


call it whatever you like :-)
when does the 3rd argument go through htons?
when the second argument is SOCK_RAW?

>>>>it appears that 0 is the most common argument,
>>>>but I often see examples like
>>>> socket(,,htons(ETH_P_ALL));
>>>>and
>>>> socket(,,IPPROTO_TCP);
>>>>so, what is the rule?
>>>>what CPP #define's can be passed to socket() as the protocol?
>>>>which ones need htons()?
>>>>the next question is, of course,
>>>>how portable the answer to the previous question is?
>>>>(i.e., what are the proper protocol argument to socket() on *BSD &c)
>>>
>>>If you are only using the TCP/IP protocol and need full portability
>>>then only use '0' for the third argument.

>> what if I need UDP and need full portability?

>
> TCP/IP includes both TCP and UDP sockets. Just use '0'.


this works (to get ICMP packets):
socket(AF_INET,SOCK_RAW,1)
this does not:
socket(AF_INET,SOCK_RAW,0)

Thanks.

--
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.iris.org.il> <http://www.memri.org/> <http://ffii.org/>
<http://www.honestreporting.com> <http://www.camera.org>
War doesn't determine who's right, just who's left.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux socket programming and HTTP Protocol Problem PGHULME Linux Networking 1 08-21-2006 09:35 PM
how to use IPX protocol in socket? minsu.kim@gmail.com Linux Networking 0 06-13-2005 11:59 AM
Protocol Chart - Learn how to use a Protocol Analyzer news.comcast.giganews.com Windows Networking 0 08-21-2004 04:44 PM
Help needed with "Can't start server : UNIX Socket : Address family not supported by protocol" Oliver Linux Networking 1 02-13-2004 01:21 PM
TCP/IP protocol missing from ME's protocol install options Hayse Windows Networking 1 07-21-2003 10:48 PM



1 2 3 4 5 6 7 8 9 10 11