1. // the program attempts to bind to IPV6-mapped IPV4 address
  2. // in a tight loop. If the address is not configured on the machine
  3. // running the program crashes Windows Server 2008 R2 (if program is 32-bit)
  4.  
  5. #include <winsock2.h>
  6. #include <WS2tcpip.h>
  7. #include <windows.h>
  8. #include <stdio.h>
  9.  
  10. #define IPV6_V6ONLY 27
  11.  
  12. void MyWsaStartup()
  13. {
  14. WORD wVersionRequested;
  15. WSADATA wsaData;
  16. int err;
  17.  
  18. wVersionRequested = MAKEWORD(2, 2);
  19.  
  20. err = WSAStartup(wVersionRequested, &wsaData);
  21. if (err != 0) {
  22. printf("WSAStartup failed with error: %d\n", err);
  23. exit(-1);
  24. }
  25. }
  26.  
  27. void main()
  28. {
  29. MyWsaStartup();
  30. bool bindSuccess = false;
  31.  
  32. while(!bindSuccess)
  33. {
  34. SOCKET sock = WSASocket(AF_INET6,
  35. SOCK_DGRAM,
  36. IPPROTO_UDP,
  37. NULL,
  38. 0,
  39. WSA_FLAG_OVERLAPPED);
  40. if(sock == INVALID_SOCKET)
  41. {
  42. printf("WSASocket failed\n");
  43. exit(-1);
  44. }
  45.  
  46. DWORD val = 0;
  47. if (setsockopt(sock,
  48. IPPROTO_IPV6,
  49. IPV6_V6ONLY,
  50. (const char*)&val,
  51. sizeof(val)) != 0)
  52. {
  53. printf("setsockopt failed\n");
  54. closesocket(sock);
  55. exit(-1);
  56. }
  57.  
  58. sockaddr_in6 sockAddr;
  59. memset(&sockAddr, 0, sizeof(sockAddr));
  60. sockAddr.sin6_family = AF_INET6;
  61. sockAddr.sin6_port = htons(5060);
  62.  
  63. // set address to IPV6-mapped 169.13.13.13 (not configured on the local machine)
  64. // that is [::FFFF:169.13.13.13]
  65. sockAddr.sin6_addr.u.Byte[15] = 13;
  66. sockAddr.sin6_addr.u.Byte[14] = 13;
  67. sockAddr.sin6_addr.u.Byte[13] = 13;
  68. sockAddr.sin6_addr.u.Byte[12] = 169;
  69. sockAddr.sin6_addr.u.Byte[11] = 0xFF;
  70. sockAddr.sin6_addr.u.Byte[10] = 0xFF;
  71.  
  72. int size = 28; // 28 is sizeof(sockaddr_in6)
  73.  
  74. int nRet = bind(sock, (sockaddr*)&sockAddr, size);
  75. if(nRet == SOCKET_ERROR)
  76. {
  77. closesocket(sock);
  78. Sleep(100);
  79. }
  80. else
  81. {
  82. bindSuccess = true;
  83. printf("bind succeeded\n");
  84. closesocket(sock);
  85. }
  86. }
  87. }