Re: How to determine if IPv6 is installed (using VC++ 6.0)

From: Alun Jones [MSFT] (alunj_at_online.microsoft.com)
Date: 01/06/05

  • Next message: Steve Alpert: "Re: SSH Sample?"
    Date: Thu, 6 Jan 2005 13:25:36 -0800
    
    

    "Eugene Gershnik" <gershnik@hotmail.com> wrote in message
    news:%23$3vLy28EHA.3596@TK2MSFTNGP12.phx.gbl...
    > Alun Jones [MSFT] wrote:
    >> Usually, though, the best way of determining if a resource is
    >> available is to try and use it, and handle the failure response. You'd
    >> hate to come up with some grandiose scheme to tell if IPv6 is
    >> installed, and find that your app puts up a dialog box saying "IPv6
    >> is not installed", when other apps are happily using it, because they
    >> don't bother to check that way, and simply open and use IPv6 sockets!
    >
    > That depends. If an application was only tested on a few specific
    > configurations (and most application are) which can be detected in a
    > certain way than it is better to try to detect them and at least warn the
    > user. A better message would be "App X cannot detect a supported version
    > of IPv6. Do you want to try to connect anyway? WARNING: using an
    > unsupported version of IPv6 may or may not work yada, yada"
    > You win two psychological victories this way. First if something doesn't
    > work the user would at least have some clue and hate your app a little
    > less. Second if he contacts support he may remeber to mention this message
    > which would simplify your life immensly.

    It's obviously a matter of personal taste. I prefer to tell people when
    problems actually occur in a manner that I can detect, rather than suggest
    to them that problems might happen.

    In this case, the absence of an IPv6 protocol listing in WSAEnumProtocols
    should actually cause your app to say "there is no IPv6 protocol installed",
    and refuse to do any IPv6 connectivity - either there is IPv6, in which case
    WSAEnumProtocols will list it, and socket(AF_INET6...) will work; or, there
    is no IPv6, in which case, WSAEnumProtocols won't list it, and
    socket(AF_INET6...) will fail. There is no valid configuration where one
    works and the other does not!

    My point was that, in general, when you want to produce an error if a
    resource is unavailable, you should ask to be given that resource when you
    need it, and handle the errors that come back, not try to guess ahead of
    time whether the resource is present, and then find that in between your
    asking if it's present, and the time that you ask for the resource to be
    given to you, it's been given to someone else.

    The classic example is the perennial question "is there a function to tell
    me whether port X is bound to a socket?" The answer is "yes, bind, which
    will fail if that port is already bound". The programmer is usually looking
    to see if port X is free, and if it is, to bind a socket to that port.
    Doing that outside of bind, i.e. non-atomically, leads to reliability
    problems, when another application might swoop in and grab the socket in the
    middle. This is especially bad when it leads to security vulnerabilities -
    for instance, in FTP, the FTP server and client negotiate data connections
    by one of them sending the other the address and port number to connect to.
    I've seen programmers try to figure out how to ask the OS for the next
    available port, send that number to the peer, and then bind to it; it's
    trivial for an attacker on that same machine to get in the middle of that
    exchange, and bind a socket to the port number requested.

    Instead, you always bind to port 0, and call getsockname() to see what port
    you got, so that you can use it.

    In this case, when you want to see if you will be able to create an IPv6
    socket, it's far less code to ask for an IPv6 socket and handle the error
    than to enumerate the protocol stack.

    Alun.
    ~~~~

    -- 
    Software Design Engineer, Internet Information Server (FTP)
    This posting is provided "AS IS" with no warranties, and confers no rights. 
    

  • Next message: Steve Alpert: "Re: SSH Sample?"

    Relevant Pages