Click to See Complete Forum and Search --> : Bind to *any* IPv6 address?


Orum
July 16th, 2005, 11:04 AM
I'm trying to bind an IPv6 socket to any IPv6 address, but I'm running into problems. For instance, here's how I do it in IPv4

SOCKADDR_IN SockAddr4;
SockAddr4.sin_family = AF_INET;
SockAddr4.sin_addr.s_addr = INADDR_ANY;
SockAddr4.sin_port = htons(port);

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(Socket == INVALID_SOCKET) {
MessageBox(hWnd, TEXT("Invalid socket!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}

if(bind(Socket, (SOCKADDR*)&SockAddr4, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
MessageBox(hWnd, TEXT("IPv4 socket bind error!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}

So, when I bind to an IPv6 socket, I did it very similarly....

const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;

SOCKADDR_IN6 SockAddr6;
SockAddr6.sin6_family = AF_INET6;
SockAddr6.sin6_addr = in6addr_any;
SockAddr6.sin6_port = htons(port);

Socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);

if(Socket == INVALID_SOCKET) {
MessageBox(hWnd, TEXT("Invalid socket!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}

if(bind(Socket, (SOCKADDR*)&SockAddr6, sizeof(SOCKADDR_IN6)) == SOCKET_ERROR) {
MessageBox(hWnd, TEXT("IPv6 socket bind error!"), PROGRAM_NAME, MB_ICONERROR);
return true;
}

Now for some reason, the IPv4 binding works just fine, but the IPv6 binding is always giving me WSAEADDRNOTAVAIL when I check WSAGetLastError(). I have IPv6 installed, and the sample IPv6 server code @ MSDN works just fine (and yes, I remembered to shut it down before attempting to test my program). However, I noticed that in their server code they get a list of addresses that the local computer has and then binds to them, but I just want to bind to the IPv6 equivalent of IPv4's IPADDR_ANY (IPv6 address ::).

Is there anything I'm doing wrong in my IPv6 code that's causing me to get WSAEADDRNOTAVAIL every time I try to bind to IPv6 address ::?

MikeAThon
July 16th, 2005, 12:26 PM
If you're using the same port number for IPv6 right after a run using IPv4, are you sure that the TIME_WAIT state has expired. A socket can stay in this state for quite a long time, usually 4 minutes:

"NOTE: It is normal to have a socket in the TIME_WAIT state for a long period of time. The time is specified in RFC793 as twice the Maximum Segment Lifetime (MSL). MSL is specified to be 2 minutes. So, a socket could be in a TIME_WAIT state for as long as 4 minutes. Some systems implement different values (less than 2 minutes) for the MSL." See http://support.microsoft.com/default.aspx?scid=kb;en-us;137984

Mike

Orum
July 16th, 2005, 12:42 PM
Well, this is just for having two sockets listen before accepting or closing any other sockets, so TIME_WAIT shouldn't be an issue. I tried just binding to the IPv6 address and *not* the IPv4 address, but it still gives the same error, WSAEADDRNOTAVAIL.

Orum
July 16th, 2005, 03:59 PM
Well, I found the problem......apparently I forgot to zero the sin6_flowinfo and sin6_scope_id members (IPv4 doesn't have them).