![]() |
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 |
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 ;) |
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 |
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 . |
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. |
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? |
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 |
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 . |
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? |
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.