[H-GEN] socket options under linux

Gregory McGarry g.mcgarry at qut.edu.au
Sun Dec 14 17:28:30 EST 1997


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)
> 
> The compiler says that I should declare what SO_USELOOPBACK is, at
> which point, I hang my head in shame, for I know not what to do about
> it.  Can anyone give me a useful[1] suggestion about how to alter this
> line to keep the compiler happy, but make it do something sensible?
> 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.

I suspect it really isn't necessary.  It is to improve network efficiency
if the server is on localhost.  I don't have a linux machine to verify
this:

                do {
                        int     option;
 
                        Socket = socket(SOCK_FAMILY, SOCK_STREAM, 0);
                        if (Socket < 0)
                                err(1, "socket");
                        option = 1;
#ifndef LINUX
                        if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK,
                            &option, sizeof option) < 0)
                                warn("setsockopt loopback");
#endif
                        errno = 0;
                        if (connect(Socket, (struct sockaddr *) &Daemon,
                            DAEMON_SIZE) < 0) {
                                if (errno != ECONNREFUSED) {
                                        warn("connect");
                                        leave(1, "connect");
                                }
                        }
                        else
                                break;
                        sleep(1);
                } while (close(Socket) == 0);

I really don't think it is such a great example of learning to program
sockets.  I have an FAQ at home, which explains sockets programming
very well.  It should be available somewhere on the net.  It is many
years old nowadays.  All unix network programming books will talk about
sockets (Stevens, etc).

If you want to play with something simple with sockets, then pull ftpd
to bits.  It's much easier to understand.  Not as much fun though.

> [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. 
>       What*ever*.  This is pretty crufty code.  :)

>From the README:

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.

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



More information about the General mailing list