The Windows documentation for WSAStringToAddress states:
INT WSAAPI WSAStringToAddress(
_In_ LPTSTR AddressString,
_In_ INT AddressFamily,
_In_opt_ LPWSAPROTOCOL_INFO lpProtocolInfo,
_Out_ LPSOCKADDR lpAddress,
_Inout_ LPINT lpAddressLength
);
AddressString
an _In_
parameter, and not an _Inout_
parameter. Its not clear to me why the API takes a non-const pointer, and its causing a compile failure because I have a const char*
.
My first question is, why does WSAStringToAddress
take a non-const pointer?
My second question is, is it safe to cast the const-ness away? Will WSAStringToAddress
modify the char*
argument?
Here's more of the back story... I'm trying to use WSAStringToAddress
in an inet_addr
replacement due to deprecated warnings under contemporary versions of Visual Studio.
Here's the same problem detailed in a question with an answer provided by Petar Korponaić. Korponaić experienced the same problem. Its the reason for the extra copy:
int inet_pton(int af, const char *src, void *dst)
{
struct sockaddr_storage ss;
int size = sizeof(ss);
char src_copy[INET6_ADDRSTRLEN+1];
ZeroMemory(&ss, sizeof(ss));
/* stupid non-const API */
strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
src_copy[INET6_ADDRSTRLEN] = 0;
if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
...
}