Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Approach to sin_zero (http://www.velocityreviews.com/forums/t953180-approach-to-sin_zero.html)

woodbrian77@gmail.com 10-09-2012 01:58 AM

Approach to sin_zero
 
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

Melzzzzz 10-09-2012 02:53 AM

Re: Approach to sin_zero
 
On Mon, 8 Oct 2012 18:58:53 -0700 (PDT)
woodbrian77@gmail.com 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 ;)



Ian Collins 10-09-2012 03:20 AM

Re: Approach to sin_zero
 
On 10/09/12 14:58, woodbrian77@gmail.com 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

Jorgen Grahn 10-09-2012 03:21 PM

Re: Approach to sin_zero
 
On Tue, 2012-10-09, woodbrian77@gmail.com 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 .

woodbrian77@gmail.com 10-09-2012 03:31 PM

Re: Approach to sin_zero
 
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.


woodbrian77@gmail.com 10-09-2012 03:52 PM

Re: Approach to sin_zero
 
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?

Victor Bazarov 10-09-2012 06:44 PM

Re: Approach to sin_zero
 
On 10/9/2012 11:52 AM, woodbrian77@gmail.com 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

Jorgen Grahn 10-09-2012 07:56 PM

Re: Approach to sin_zero
 
On Tue, 2012-10-09, Victor Bazarov wrote:
> On 10/9/2012 11:52 AM, woodbrian77@gmail.com 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 .

woodbrian77@gmail.com 10-10-2012 01:47 AM

Re: Approach to sin_zero
 
Here's a version that uses getaddrinfo:

sock_type cmw::udp_server (uint16_t port)
{
::std::ostringstream 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?


woodbrian77@gmail.com 10-10-2012 02:06 AM

Re: Approach to sin_zero
 
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.


All times are GMT. The time now is 01:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57