Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a dll in C++ which allows me to perform Winsock functions from PowerBuilder code.

For IPv4 it works great but I am getting an error in IPv6. I am starting with the UDP functions because they are simpler. The following error occurs on the sendto:

The requested address is not valid in its context

In the below code, ud.family will either be set to AF_INET or AF_NET6.

What I have tried:

C++
// method of_sendto (Unicode String)
PBXRESULT winsock::fn_sendto_u(PBCallInfo * ci)
{
   PBXRESULT pbxr = PBX_OK;
   SOCKET SendSocket = INVALID_SOCKET;
   sockaddr_in SendAddr4;
   sockaddr_in6 SendAddr6;
   USHORT port;
   int iResult = 0;
   LPCTSTR lpHostName = NULL;
   LPCTSTR lpMessage = NULL;
   int nBytes = 0, nLeft = 0, idx = 0;

   // get arguments
   pbstring arg0 = ci->pArgs->GetAt(0)->GetString();
   lpHostName = ud.session->GetString(arg0);
   port = ci->pArgs->GetAt(1)->GetUint();
   pbstring arg2 = ci->pArgs->GetAt(2)->GetString();
   lpMessage = ud.session->GetString(arg2);
   nBytes = (int) _tcslen(lpMessage) * sizeof(TCHAR);

   // create a socket
   SendSocket = socket(ud.family, SOCK_DGRAM, IPPROTO_UDP);
   if (SendSocket == INVALID_SOCKET) {
      // set the return value
      ci->returnValue->SetBool(FALSE);
   	return pbxr;
   }

   switch(ud.family) {
      case AF_INET:
         SendAddr4.sin_family = AF_INET;
         SendAddr4.sin_port = htons(port);
         InetPtonW(AF_INET, lpHostName, &SendAddr4.sin_addr);
         break;
      case AF_INET6:
         SendAddr6.sin6_family = AF_INET6;
         SendAddr6.sin6_port = htons(port);
         InetPtonW(AF_INET6, lpHostName, &SendAddr6.sin6_addr);
         break;
   }

   nLeft = nBytes;
   while (nLeft > 0) {
      // send the message
      switch(ud.family) {
         case AF_INET:
            iResult = sendto(SendSocket, (char*) lpMessage + idx, (int) nLeft, 0, 
                                 (SOCKADDR *) &SendAddr4, sizeof(SendAddr4));
            break;
         case AF_INET6:
            iResult = sendto(SendSocket, (char*) lpMessage + idx, (int) nLeft, 0, 
                                 (SOCKADDR *) &SendAddr6, sizeof(SendAddr6));
            break;
      }
      if (iResult == SOCKET_ERROR) {
         // save error and close socket
         iResult = WSAGetLastError();
         closesocket(SendSocket);
         WSASetLastError(iResult);

         // set the return value
         ci->returnValue->SetBool(FALSE);
         return pbxr;
      }
      nLeft -= iResult;
      idx += iResult;
   }

   // close the socket
   closesocket(SendSocket);

   // set the return value
   ci->returnValue->SetBool(TRUE);

   return pbxr;
}
Posted
Comments
Jochen Arndt 2-Oct-17 3:54am    
Are you passing a valid IPv6 address?
Check the InetPton return value to verify this.
Roland M Smith 2-Oct-17 6:52am    
I added code to check the return from InetPton and it works. I am passing a valid version 6 IP address. The error is coming from sendto.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900