[H-GEN] socket options under linux

Martin Pool mbp at pharos.com.au
Sun Dec 14 19:50:04 EST 1997


On Mon, 15 Dec 1997, Gregory McGarry wrote:
> Jason Parker wrote:
> > The code in question uses the result of this call for an if statement,
> > which the compiler barfs at:
> > 
> > setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)

More context would be nice.  I'm assuming that's what Gregory quoted
below.

> > Sockets are not my strong point (indeed, that's the whole point of the
> > project---to learn more about them by working with them, but I need a
> > working executable first...) so any help, pointers to FAQ's, books,
> > and the like will be appreciated.

"Network programming in Java 1.1", Addison Wesley :-)

> I suspect it really isn't necessary.  It is to improve network efficiency
> if the server is on localhost. 

If I remember correctly, then Linux will do this (effectively route
through lo rather than eth0) automatically, but I'm not sure. 

> #ifndef LINUX
>                         if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK,
>                             &option, sizeof option) < 0)
>                                 warn("setsockopt loopback");
> #endif

Yuck.  Well, I probably would have done the same thing in this case, but
as a general rule it'd be better to condition on the feature you're after,
not on the OS.  Doing the latter will only cause trouble when linux 4.3.23
introduces support for SO_USELOOPBACK, or when you discover that RiscOS
and SYNIX don't have it either.  It's just like using hardcoded constants.

The right way(tm) to do it is to use autoconfig, which just lets you right 

   #ifdef HAVE_SO_USELOOPBACK
   ...

and it takes care of determining whether you HAVE_ or not.  But that kind
of requires a more substantial porting effort than just patching the bits
that break.  OTOH, for the effort of a single port, you effectively port
to every machine.  autoconf is very cool.

> > [2] : Now that I look at it, the use of "sizeof" looks a bit dodgy. 
> >       But I didn't write it.  :)  And I think it *is* actually valid. 

Why do you say that?  It seems straightforward to me.  The last two
parameters are a variable-length option block, and the length of that
block.

> P.S.  The authors of the game want to emphasize that this version of hunt
> was started over eight years ago, and the programming style exhibited here
> in no way reflects the current programming practices of the authors.

:-)

--
Martin Pool, Pharos



----------------------- HUMBUG General List --------------------------------
echo "unsubscribe general" | mail majordomo at humbug.org.au # To Unsubscribe



More information about the General mailing list