/* Custom InetPton implementation */

#include <Winsock2.h>
#include <ws2tcpip.h>

int __stdcall
InetPton(int Family,
         const char *pszAddrString,
         void *pAddrBuf)
{
    IN_ADDR *addr = pAddrBuf;
    unsigned long ulTmp;
    
    if (Family != AF_INET) {
        // Oops! We don't support that addr family
        WSASetLastError(WSAEAFNOSUPPORT);
        return -1;
    }

    if (!pAddrBuf || !pszAddrString ||
        pszAddrString[0] == '\0') {
		WSASetLastError(WSAEFAULT);
		return -1;
    }

	// Technically we could just put the return val directly
	// into addr->S_un.S_addr, but I don't like writing bad
	// data unless absolutely necessary. So store it in a temp
	// variable, first.
    ulTmp = inet_addr(pszAddrString);
    if (ulTmp == INADDR_NONE) {
		// Not an IP address
		WSASetLastError(WSAEFAULT);
		return -1;
    }

	// S_un.S_addr is an unsigned long. Easy!
    addr->S_un.S_addr = ulTmp;
    return 0;
}
