title: Using the Hyper-V sockets between Windows host and Linux guest
body: <p>I want to write simple application that communicates between the Hyper-V host and its virtual machine using Hyper-V sockets (netcat over vsock). In the Internet there are a few documents describing how to do it: <a href="https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/make-integration-service" rel="nofollow noreferrer">Make your own integration services</a>, <a href="https://w4mhi.wordpress.com/2016/08/29/what-about-hyper-v-socket-communication/" rel="nofollow noreferrer">Practical Hyper-V socket communication</a>. However, any of them helps me to achieve my goal.</p>
  
  <p>First of all, I've made sure that the connection using Hyper-V sockets is possible. On the guest Linux I loaded <code>hv_sock</code> module and run <a href="https://github.com/stefanha/nc-vsock" rel="nofollow noreferrer">nc-vsock</a> application which is able to listen on vsocks:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ sudo modprobe hv_sock
  $ nc-vsock -l 1234
  </code></pre>
  
  <p>On Windows in PowerShell I ran hvc, which utilises Hyper-V sockets and is able to emulate netcat:</p>
  
  <pre class="lang-sh prettyprint-override"><code>hvc nc -t vsock little-helper 1234
  </code></pre>
  
  <p>and it works. I can see data sent from server to client and vice versa.</p>
  
  <p>Then I wrote a simple application basing on <a href="https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/make-integration-service" rel="nofollow noreferrer">1</a> and <a href="https://w4mhi.wordpress.com/2016/08/29/what-about-hyper-v-socket-communication/" rel="nofollow noreferrer">2</a> with slight changes.
  I registered my application with the Hyper-V Host's registry as said in <a href="https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/make-integration-service" rel="nofollow noreferrer">1</a> and I ran my application. The connection was not established and the <code>connect</code> function returned error 10049.</p>
  
  <p>I've tried to run my application as a administrator and manipulate GUIDs in the source code and on the Hyper-V Host's registry as well. However, nothing helps and application always reports error 10049.</p>
  
  <p>In my opinion in the document are some ambiguity. E.g. it's said that the service id shall be a random GUID. But later on there is a note that the first four octets translate to port in AF_VSOCK address family, and the specific GUID is presented for this purpose.</p>
  
  <p>Question is rather simple: what I did wrong or misunderstood. Is it possible to write netcat utilising vsock between Windows and Linux?</p>
  
  <p>Full code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;hvsocket.h&gt;
  
  #include &lt;combaseapi.h&gt;
  
  int main()
  {
      struct __declspec(uuid("00000000-185c-4e04-985a-4c2eee3e03cc")) VSockTemplate {};
      struct __declspec(uuid("2a9fa68e-4add-45cb-85c8-de97fc66d388")) ServerVsockTemplate {};
  
      //----------------------
      // Initialize Winsock
      WSADATA wsaData;
      int iResult = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
      if (iResult != NO_ERROR) {
          wprintf(L"WSAStartup function failed with error: %d\n", iResult);
          return 1;
      }
      //----------------------
      // Create a SOCKET for connecting to server
      SOCKET ConnectSocket;
      ConnectSocket = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
      if (ConnectSocket == INVALID_SOCKET) {
          wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
          WSACleanup();
          return 1;
      }
      //----------------------
      // The sockaddr_in structure specifies the address family,
      // IP address, and port of the server to be connected to.
      SOCKADDR_HV clientService;
      clientService.Family = AF_HYPERV;
      clientService.VmId = __uuidof(ServerVsockTemplate);
      clientService.ServiceId = __uuidof(VSockTemplate);
      clientService.ServiceId.Data1 = 1234;
  
      //----------------------
      // Connect to server.
      iResult = connect(ConnectSocket, (SOCKADDR*)&amp;clientService, sizeof(clientService));
      if (iResult == SOCKET_ERROR) {
          wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
          iResult = closesocket(ConnectSocket);
          if (iResult == SOCKET_ERROR)
              wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
          WSACleanup();
          return 1;
      }
  
      wprintf(L"Connected to server.\n");
  
      iResult = closesocket(ConnectSocket);
      if (iResult == SOCKET_ERROR) {
          wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
          WSACleanup();
          return 1;
      }
  
      WSACleanup();
      return 0;
  }
  </code></pre>