Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Approach to sin_zero

Reply
Thread Tools

Approach to sin_zero

 
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      10-09-2012
Shalom

Is the memset below needed? Stevens says sin_zero is
not used. Others on the newsgroups say clearing the field
is needed on a few platforms. I've left the memset in just
to be on the safe side.

sock_type cmw::udp_server (uint32_t port)
{
sock_type sd = getSocket(SOCK_DGRAM);
sockaddr_in si_me;
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
::std::memset(&si_me.sin_zero, 0, sizeof(si_me.sin_zero));
if (::bind(sd, (sockaddr*) &si_me, sizeof(si_me))==-1) {
throw failure("udp Bind errno: ") << GetError();
}
return sd;
}

Thanks in advance.

Ebenezer Enterprises -- making programming fun again.
http://webEbenezer.net
 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      10-09-2012
On Mon, 8 Oct 2012 18:58:53 -0700 (PDT)
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Shalom
>
> Is the memset below needed? Stevens says sin_zero is
> not used. Others on the newsgroups say clearing the field
> is needed on a few platforms. I've left the memset in just
> to be on the safe side.
>
> sock_type cmw::udp_server (uint32_t port)
> {
> sock_type sd = getSocket(SOCK_DGRAM);
> sockaddr_in si_me;
> si_me.sin_family = AF_INET;
> si_me.sin_port = htons(port);
> si_me.sin_addr.s_addr = htonl(INADDR_ANY);
> ::std::memset(&si_me.sin_zero, 0, sizeof(si_me.sin_zero));
> if (::bind(sd, (sockaddr*) &si_me, sizeof(si_me))==-1) {
> throw failure("udp Bind errno: ") << GetError();
> }
> return sd;
> }
>


Quick google search shows that on mac OS X it is needed.
Perhaps on Free BSD, too.

Anyway, I always initialize whole struct to zero prior
filling fields


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-09-2012
On 10/09/12 14:58, (E-Mail Removed) wrote:
> Shalom
>
> Is the memset below needed? Stevens says sin_zero is
> not used. Others on the newsgroups say clearing the field
> is needed on a few platforms. I've left the memset in just
> to be on the safe side.


While somewhat OT, a couple of points:

> sock_type cmw::udp_server (uint32_t port)
> {
> sock_type sd = getSocket(SOCK_DGRAM);
> sockaddr_in si_me;


Why not just write

sockaddr_in si_me = {0};

and leave it at that?

> si_me.sin_family = AF_INET;
> si_me.sin_port = htons(port);


What happens if post is too big for a uint16_t? In other words, why is
the parameter type a uint32_t?

> si_me.sin_addr.s_addr = htonl(INADDR_ANY);
> ::std::memset(&si_me.sin_zero, 0, sizeof(si_me.sin_zero));
> if (::bind(sd, (sockaddr*)&si_me, sizeof(si_me))==-1) {
> throw failure("udp Bind errno: ")<< GetError();
> }
> return sd;
> }


--
Ian Collins
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-09-2012
On Tue, 2012-10-09, (E-Mail Removed) wrote:
> Shalom
>
> Is the memset below needed? Stevens says sin_zero is
> not used. Others on the newsgroups say clearing the field
> is needed on a few platforms. I've left the memset in just
> to be on the safe side.
>
> sock_type cmw::udp_server (uint32_t port)
> {
> sock_type sd = getSocket(SOCK_DGRAM);
> sockaddr_in si_me;
> si_me.sin_family = AF_INET;
> si_me.sin_port = htons(port);
> si_me.sin_addr.s_addr = htonl(INADDR_ANY);
> ::std::memset(&si_me.sin_zero, 0, sizeof(si_me.sin_zero));
> if (::bind(sd, (sockaddr*) &si_me, sizeof(si_me))==-1) {
> throw failure("udp Bind errno: ") << GetError();
> }
> return sd;
> }


Is there a reason you don't use POSIX getaddrinfo(3)? It handles those
things for you, takes care of IPv4/IPv6, and is described in Stevens'
books.

Followup set.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      10-09-2012
On Monday, October 8, 2012 10:20:33 PM UTC-5, Ian Collins wrote:
>
> What happens if post is too big for a uint16_t? In other words, why is
>
> the parameter type a uint32_t?
>
>


That's probably laziness and I'll check on
changing it.

 
Reply With Quote
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      10-09-2012
On Tuesday, October 9, 2012 10:21:33 AM UTC-5, Jorgen Grahn wrote:
>
> Is there a reason you don't use POSIX getaddrinfo(3)? It handles those
>
> things for you, takes care of IPv4/IPv6, and is described in Stevens'
>
> books.
>
>


I'm using getaddrinfo elsewhere, but it hadn't
occurred to me to use it here.

>
> Followup set.
>
>


What does that mean?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-09-2012
On 10/9/2012 11:52 AM, (E-Mail Removed) wrote:
> On Tuesday, October 9, 2012 10:21:33 AM UTC-5, Jorgen Grahn wrote:
>> [..]
>> Followup set.
>>
>>

>
> What does that mean?


That means that if you used a real newsreader, the reply would
automatically be addressed to another newsgroup, in this case
'comp.protocols.tcp-ip', which Jorgen probably recommends for this
discussion (instead of c.l.c++). Of course the newsgroup for the reply
can always be overridden, but it requires conscious effort (IOW using
one's brain)...

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-09-2012
On Tue, 2012-10-09, Victor Bazarov wrote:
> On 10/9/2012 11:52 AM, (E-Mail Removed) wrote:
>> On Tuesday, October 9, 2012 10:21:33 AM UTC-5, Jorgen Grahn wrote:
>>> [..]
>>> Followup set.
>>>
>>>

>>
>> What does that mean?

>
> That means that if you used a real newsreader, the reply would
> automatically be addressed to another newsgroup,


Yes, and I'm surprised that Google ignores it. I knew they were a pain
on Usenet, but not that they were *that* arrogant.

But I should have written "Followup-to set to comp.protocols.tcp-ip"
for clarity.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      10-10-2012
Here's a version that uses getaddrinfo:

sock_type cmw::udp_server (uint16_t port)
{
::std:stringstream out;
out << port;

addrinfo* res;
addrinfo hints;
::std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;

int err;
if ((err = ::getaddrinfo(nullptr
, out.str().c_str()
, &hints
, &res
)) != 0) {
throw failure("udp_server getaddrinfo: ") << gai_strerror(err);
}

sock_type sd = getSocket(SOCK_DGRAM);
if (::bind(sd, res->ai_addr, res->ai_addrlen)==-1) {
throw failure("udp_server bind: ") << GetError();
}
return sd;
}

The unfortunate thing is the change adds over 2,000 bytes
to an executable. It isn't a big executable and the
percentage increase is close to 3%. That strikes me as
excessive. Are there any other alternatives?

 
Reply With Quote
 
woodbrian77@gmail.com
Guest
Posts: n/a
 
      10-10-2012
On Tuesday, October 9, 2012 9:47:21 PM UTC-4, (unknown) wrote:

>
> Are there any other alternatives?


Does it work to change

si_me.sin_family = AF_INET;

to

si_me.sin_family = AF_UNSPEC;

in the function I originally posted?

It looks like there are people doing that out there,
but I couldn't find anything that says it is valid.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Core Exams - Best approach to take? =?Utf-8?B?R2F2aW4=?= MCSE 1 02-02-2006 03:29 PM
Bitplane approach to FIR filter architecture Wiener, Norbert VHDL 4 11-18-2004 02:31 AM
advice needed - what approach for sql access KavvY ASP .Net 1 07-17-2004 08:17 AM
what is a better approach to synthezise synchronous reset on FPGA? valentin tihomirov VHDL 9 04-15-2004 01:41 PM
is smart to use frames approach les ASP .Net 1 07-31-2003 03:49 PM



Advertisments