I replaced the call to 'gethostbyname' to 'getaddrinfo'.
The problem is that getaddrinfo doesn't seem to do what gethostbyname does.
When I call gethostbyname with the a NULL parameter, I get back a list of
all IP V4 address on my machine.
Example:
192.168.1.101
192.168.127.1
192.168.236.1
However, whenever I call getaddrinfo with the parms below (and I tried other
parms), I only seem to get a loopback address.
Hints.ai_family = PF_INET6;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
rc = getaddrinfo( NULL, szListenPort6, &Hints, &AddrInfo);
I want to listen on specific addresses for my service and I don't seem to be
able to enumerate addresses for IP V6 to do this. I used
GetAdaptersAddresses to get the real addresses but never seem to be able to
connect using those addresses.
All help is greatly appreciated.
Pierre
> The problem is that getaddrinfo doesn't seem to do what gethostbyname
> does.
Yes, it does. It does more than that actually.
> When I call gethostbyname with the a NULL parameter, I get back a list of
> all IP V4 address on my machine.
It is not legal to pass NULL as the parameter to gethostbyname() if we
follow the BSD convention. It will crash on non-Windows systems. That is a
Windows-specific extension.
For getaddrinfo(), passing NULL as a hostname *is* legal, and specified. It
means you want to get wildcards addresses, that is :: for IPv6 and/or
0.0.0.0 for IPv4.
/* Anyway, reading MSDN: */
struct hostent *ent = gethostbyname (NULL);
/* should be very much the same as: */
char buf[NI_MAXHOST];
struct hostent *ent
gethostname (buf, sizeof (buf));
ent = gethostbyname (buf);
/* though I admit I haven't tried, which is OK on any system. */
Of course, you probably do not care for portability outside Windows, so this
is not really a problem.
> However, whenever I call getaddrinfo with the parms below (and I tried
> other parms), I only seem to get a loopback address.
> Hints.ai_family = PF_INET6;
> Hints.ai_socktype = SOCK_STREAM;
> Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
Err. Are you sure? You should get the loopback (::1) address if you do NOT
set the AI_PASSIVE flag. Otherwise you should really get the wildcard (::)
address.
> rc = getaddrinfo( NULL, szListenPort6, &Hints, &AddrInfo);
> I want to listen on specific addresses for my service and I don't seem to
> be able to enumerate addresses for IP V6 to do this.
I don't think getaddrinfo() can do that, whether on Windows or not. When
setting the hostname to NULL, you will really get the wildcard address, not
a list of all addresses assigned to the system. This is what is specified
by RFC2553.
Anyway, it is *not* appropriate to bind to all network interface's specific
addresses when using IPv6. Interfaces are namely expected to have several
and/or often-changing IPv6 addresses (your system probably has so-called
"provisionnal", "transient" or "temporary" IPv6 addresses by the way). As
such, you should *really* bind a single socket to the wildcard address than
try to bind to all available addresses, if only as they are likely to
change while your program is running.
I'd suggest you bind to the wildcard address, and use the (Windows-specific)
SO_EXCLUSIVEADDRUSE if you don't want other applications to take over your
TCP port. By the way, that could make your life fairly easier for IPv4 as
well: only one listening socket is needed instead of as many as network
interfaces.
If you don't want to receive traffic from some interfaces, e.g. loopback,
they are various other way you can: use Winsock socket filtering capability
(SO_CONDITIONAL_ACCEPT), or check the socket address returned from accept()
for example. The first solution is more secure (the port does not appear to
respond to the outside world if the address is not allowed in), the later
is more portable.
"Avoiding Server Hijacking" in chapter "Socket Security" from "Writing
Secure Code 2nd edition" from Microsoft Press might be a good read if you
can find the book. Otherwise:
http://msdn.microsoft.com/library/en-us/winsock/winsock/using_so_exclusiveaddruse.asp
should be good.
--
Rémi Denis-Courmont
http://www.simphalempin.com/home/
Thanks for the note. I discovered that when you install IP V6, it also
installs a "IP V6 Internet Connection Firewall" as a Service. But you can't
seem to configure this bugger from anywhere.
So, I temporarily stopped the service and now I can use the V6 addresses for
all the interfaces.
Regards,
Pierre
"Remi Denis-Courmont" <remi-usene...@simutrans.fr.st> wrote in
message news:uv15RQA...@tk2msftngp13.phx.gbl...
> Remi,
>
> Thanks for the note. I discovered that when you install IP V6, it also
> installs a "IP V6 Internet Connection Firewall" as a Service. But you
> can't seem to configure this bugger from anywhere.
>
> So, I temporarily stopped the service and now I can use the V6 addresses
> for all the interfaces.
While the following page deals with Teredo, the section concerning the IPv6
firewall should be applicable to any case, so it might be interesting:
http://people.via.ecp.fr/~laug/2004/04/teredo-howto.html
by Laurent Granger
Specifically:
http://www.microsoft.com/technet/itsolutions/network/security/ipv6fw/hcfgv601.mspx
Stewart.
http://research.microsoft.com/collaboration/university/
http://www.microsoft.com/ipv6
"Remi Denis-Courmont" <remi-usene...@simutrans.fr.st> wrote in
message news:%23$AFeERZE...@TK2MSFTNGP09.phx.gbl...